Download Final Report --

Transcript
6.3. DESCRIPTION OF IMPLEMENTATION
primary_expr
99
::= number | ’(’ expression ’)’
| ’[’ function ’]’
The rule says that a primary_expr is either a nonterminal number symbol,
the terminal symbol ’(’ followed by a nonterminal expression symbol followed
by the terminal ’)’ symbol, or the terminal symbol ’[’ followed by a function
nonterminal symbol (i.e. a function from the function library), followed by the
terminal symbol ’]’.
The function in Listing 6.3 takes in a list of tokens and the indices startIdx
and endIdx which gives the range in the token list the function shall parse.
The function first checks the token at the start to determine whether this is
an instance of the first, second or third case3 . Next, it calls the appropriate
routines for parsing nonterminal symbols.
For instance, when the parser discovers the symbol ’(’, it knows that the symbol in the token list at index endIdx must be ’)’. If this is not the case, it
reports an error. Also, the symbol in between the parentheses terminals must
be the nonterminal symbol expression. For parsing this symbol, it just calls
the routine expression which parses and evaluates an expression symbol. Note that this follows our strategy: Terminal symbols are parsed directly,
and we have one method for each nonterminal symbol in the grammar.
This design allows the grammar to easily be extended with new operators, as
we shall see in Section 6.4.3.
3 This
is why the parser is called predictive: While parsing, it predicts what types of symbols
comes next. It is a recursive descent parser because it starts with the start symbol of the grammar
and repeatedly parses the nonterminal symbols until there are only terminal symbols left