Parsing the Token Array
Now that your lexer is complete, you can turn an input string into an array of Tokens, each of which is either a .number or a .plus.
The next step is to write a parser whose job is to evaluate a series of tokens delivered to it from the lexer.
For example, feeding [.number(5), .plus, .number(3)] through your parser should give you the answer 8.
The algorithm to parse this sequence of tokens is more restrictive than the algorithm you used for lexing, because the order in which the tokens appear is very important.
The rules are:
-
The first token must be a number.
-
After parsing a number, either the parser must be at the end of input, or the next token must be .plus.
-
After parsing a .plus, the next token must ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access