O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required