Search for tokens: the expression sum=a+b contains 5 tokens - sum, =, a, +, and b.
Tokens identification algorithm:
repeat until the end of the line: skip over blanks in the source string if a letter is seen then save it and all following characters, up to a blank or end of line else if a digit or "-" or "+" is seen then save it and all following characters, up to a blank or end of line else if a special symbol (like =) is seen then save that symbol else something wrong is happened - send an error message send what has been saved to token storage
After the tokens have been identified, they are encoded by some internal code.
The translator scant the list of tokens and tries to make is a sense. It constructs so-called parse-tree.
Examples:
A parse tree for x*(2+y) | A parse tree for sum=a+b |
---|---|
2 y \ / x + \ / * |
a b \ / sum + \ / = |
Here for each operation we have a box and one or two dependent tokens, that are the arguments for this operation.
The translator uses the parse trees and the following algorithm to generate code (for sum=a+b):
STO 6
ADD 5
LOD 4to put it in the accumulator.
LOD 4 ;place a into ACC ADD 5 ;ACC=a+b STO 6 ;save the result
00000100 00000100 00000000 00000101 00000101 00000110