expr-tail -'+expr-tail -'-[end[+[-factor -[numbervariablefactor-tail -factor-tail -'*factor-tail -'/[+-end[*[/term -term -term -'(')[number[variable[(tokens()numvar-+/*setend this is fairly typical grammar for simple expression languageand it allows for arbitrary expression nesting (some example expressions appear at the end of the test parser module listing in example - strings to be parsed are either an expression or an assignment to variable name (setexpressions involve numbersvariablesand the operators +-*and because factor is nested in expr in the grammarand have higher precedence ( they bind tighterthan and expressions can be enclosed in parentheses to override precedenceand all operators are left associative-that isthey group on the left ( is treated the same as ( - )- tokens are just the most primitive components of the expression language each grammar rule listed earlier is followed in square brackets by list of tokens used to select it in recursive descent parsingwe determine the set of tokens that can possibly start rule' substringand we use that information to predict which rule will work ahead of time for rules that iterate (the -tail rules)we use the set of possibly following tokens to know when to stop typicallytokens are recognized by string processor ( scanner)and higher-level processor ( parseruses the token stream to predict and step through grammar rules and substrings the parser' code the system is structured as two modulesholding two classesthe scanner handles low-level character-by-character analysis the parser embeds scanner and handles higher-level grammar analysis the parser is also responsible for computing the expression' value and testing the system in this versionthe parser evaluates the expression while it is being parsed to use the systemwe create parser with an input string and call its parse method we can also call parse again later with new expression string there' deliberate division of labor here the scanner extracts tokens from the stringbut it knows nothing about the grammar the parser handles the grammarbut it is custom language parsers