L4 Formal Grammers
L4 Formal Grammers
Compiler Design
Dr. Sadu Chiranjeevi
Assistant Professor
Department of Computer Science and Engineering
schiranjeevi@rguktn.ac.in
1
Syntax Analysis
• Check syntax and construct abstract syntax tree
if
== = ;
b 0 a b
• Grammar
list → list + digit
| list – digit
| digit
digit → 0 | 1 | … | 9
7
Syntax analyzers
• Testing for membership whether w belongs
to L(G) is just a “yes” or “no” answer
• However the syntax analyzer
– Must generate the parse tree
– Handle errors gracefully if string is not in the
language
• Form of the grammar is important
– Many grammars generate the same language
– Tools are sensitive to the grammar
8
What syntax analysis cannot do!
• To check whether variables are of types on
which operations are allowed
list + digit
list - digit 2
digit 5
9
Ambiguity
• A Grammar can have more than one
parse tree for a string
• Consider grammar
list list+ list
| list – list
|0|1|…|9
if e1 e1 if expr stmt s2
if e2
s1
else s2 e2 s1
stmt
if expr stmt
e2 s1 s2 17
Resolving dangling else problem
• General rule: match each else with the closest
previous unmatched if. The grammar can be
rewritten as
stmt matched-stmt
| unmatched-stmt
matched-stmt if expr matched-stmt
else matched-stmt
| others
unmatched-stmt if expr stmt
| if expr matched-stmt
else unmatched-stmt 18
Associativity
• If an operand has operator on both the
sides, the side on which operator takes this
operand is the associativity of that
operator
• In a+b+c b is taken by left +
• +, -, *, / are left associative
• ^, = are right associative
• Grammar to generate strings with right
associative operators
right letter = right | letter
letter a| b |…| z
Precedence
• String a+5*2 has two possible
interpretations because of two
different parse trees corresponding to
(a+5)*2 and a+(5*2)
• Precedence determines the correct
interpretation.
• Next, an example of how precedence
rules are encoded in a grammar
Precedence/Associativity in the
Grammar for Arithmetic Expressions
Ambiguous • Unambiguous,
with precedence
EE+E and associativity
| E*E rules honored
| (E) EE+T|T
| num | id TT*F|F
F ( E ) | num
3+2+5 | id
3+2*5
Parsing
• Process of determination whether a string
can be generated by a grammar
• Parsing falls in two categories:
– Top-down parsing:
Construction of the parse tree starts at the root
(from the start symbol) and proceeds towards
leaves (token or terminals)
– Bottom-up parsing:
Construction of the parse tree starts from the
leaf nodes (tokens or terminals of the grammar)
and proceeds towards root (start symbol)