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 the tokens appear in 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 be a number.

The setup ...

Get Swift Programming: The Big Nerd Ranch Guide, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.