Compilers Design
Course
Dr. Ramadan Babers
Compilers Design – Lec6
Faculty of Science Helwan University
1st Term – 2017/2018
2
Chapter 2
A Simple Syntax-Directed
Translator
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.
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
2.4.2 Predictive Parsing
5
Example:
term xy + ; term () {
x();
y();
match(+);
match(;);
}
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
2.4.5 Left Recursion
7
nonterminal terminal or nonterminal
anything
2.4.5 Left Recursion
8
A
αA
A
β
α
Rewriting new nonterminal
2.4.5 Left Recursion
9
A
αA
A
β
α
A
R
e
α
β
R
α R
2.5.2 Adapting the Translation Scheme
10
In general
2.5.2 Adapting the Translation Scheme
11
2.5.2 Adapting the Translation Scheme
12
9-5+2 95-2+
2.5.3 Procedures for the Nonterminals
13
2.5.4 Simplifying the Translator
14
Tail recursion
2.5.5 The Complete Program
15
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.
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.
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.

Compiler lec 6_1