3. 2.4.2 Predictive Parsing
3
Recursive-descent parsing is a top-down method of syntax analysis in which
a set of recursive procedures is used to process the input. One procedure is
associated with each nonterminal of a grammar.
4. 2.4.2 Predictive Parsing
4
Now we need a predictive
parser for the following
code (page 62- Fig 2.16)
There are three functions:
1- optexpr
2- stmt
3- match
6. 2.4.5 Left Recursion
6
Example:
expr expr + term
term 0
1
2
expr() { expr();
match (“+”);
term (); }
term() { if (lookahead == ‘0’) match (“0”);
elseif (lookahead == ‘1’) match (“1”);
else error (); }
Left recursion problem
16. 2.6 Lexical Analysis
16
a basic lexical unit of a
language, consisting of one
word or several words,
considered as an abstract
unit, and applied to a family
of words related by form or
meaning.
17. 2.6.1 Removal of White Space and Comments
17
White space is:
1- blank,
2- a tab, or
3- a newline
Variable peek holds the next input character.
18. 2.6.2 Reading Ahead
18
A lexical analyzer may need to read ahead some characters
before it can decide on the token to be returned to the parser.
For example, a lexical analyzer for C or Java must read
ahead after it sees the character >.
If the next character is =, then > is part of the character
sequence >=, the lexeme for the token for the "greater than or
equal to" operator.
Otherwise > itself forms the "greater than" operator, and the
lexical analyzer has read one character too many.