Professional Documents
Culture Documents
SYNTAX Analyzer
SYNTAX Analyzer
Context-Free Grammars
Top-down methods:
• Parse-trees built from root to leaves.
• Input to parser scanned from left to right one symbol at a time
The Parser
Bottom-up methods:
• Start from leaves and work their way up to the root.
• Input to parser scanned from left to right one symbol at a time
Dealing With Errors
If compiler had to process only correct programs, its
design and implementation would be simplified greatly!
Terminals Nonterminals
(token name)
Example:
Start Productions
Symbol
Derivations
Example:
This evaluates as x – ( 2 * y ) 2 y
*
Derivations and Parse Trees
Rightmost derivation
Rule Sentent
ialForm
— Expr
1 Expr O pE xpr
3 Expr O p<id,y> E
6 Expr* <id,y>
1 Expr O pE xpr* <id,y>
2 Expr O p<num ,2>* <id,y> E Op E
5 Expr–<num ,2>* <id,y>
3 <id,x>–<num ,2>*<id,y>
E Op E * y
This evaluates as ( x – 2 ) * y x – 2
Derivations and Precedence
These two derivations point out a problem with the grammar:
It has no idea of precedence, or implied order of evaluation
To add precedence
• Create a non-terminal for each level of precedence
• Isolate the corresponding part of the grammar
• Force the parser to recognize high precedence sub-expressions first
9 Expr –Term*<id,y>
T T * F
7 Expr –Factor*<id,y>
8 Expr –<num ,2> *<id,y> F F <id,y>
4 Term–<num ,2> *<id,y>
7 Factor –<num ,2> *<id,y> <id,x> <num,2>
9 <id,x> –<num, 2> *<id,y>
Its parse tree
The rightmost derivation
Rule Sen
tenti
al Form Rule Sen
tenti
al Form
— Expr — Expr
1 ExprOpExpr 1 ExprOpExpr
3 <id,x>Op Expr 1 ExprOpExprOpExpr
5 <id,x>–Expr 3 <id,x>Op Expr Op Expr
1 <id,x>–ExprOpExpr 5 <id,x>–ExprOpExpr
2 <id,x>–<num ,2>Op Expr 2 <id,x>–<num ,2>Op Expr
6 <id,x>–<num ,2>*Ex pr 6 <id,x>–<num ,2>*Ex pr
3 <id,x>–<num ,2>*<id,y> 3 <id,x>–<num ,2>*<id,y>
if S2 if
S1 S1 S2
Context-Free Grammar Vs
Regular Expressions
• Grammars are more powerful notations than
regular expressions
– Every construct that can be described by a
regular
expression can be described by a grammar, but not
vice-versa
Regular expression -> NFA then:
(a|b)*abb
Question Worth Asking
If grammars are much powerful than regular
expressions, why not using them in lexical
analysis too?
• Lexical rules are quite simple and do not
need notation as powerful as grammars
• Regular expressions are more concise and
easier to understand for tokens
• More efficient lexical analyzers can be
generated from regular expressions than
from grammars