4th - Syntax Analysis
4th - Syntax Analysis
Syntax Analysis
Overview
02
The Role of the Parser
Syntax analysis is done by the parser.
3) Error productions
Augment the grammar with productions that generate the
erroneous constructs
Example: add a rule for
:= in C assignment statements
Report error but continue compile
Self correction + diagnostic messages
4) Global correction
Choosing minimal sequence of changes to obtain a globally least-
cost correction
Adding / deleting / replacing symbols
Costly - key issues
06
Context Free Grammars
10
Context Free Grammars
Derivations:
A step in a derivation is zero or one action that replaces a NT with the RHS of
a production rule.
Productions are treated as rewriting rules to generate a string
EXAMPLE: E -E (the means “derives” in one step) using the production rule: E
-E
Leftmost: Replace the leftmost non-terminal symbol (Input scanned and replaced with
the production rules from left to right)
E E A E id A E id * E id * id
Rightmost: Replace the leftmost non-terminal symbol (Input scanned and replaced
with the production rules from right to left)
E E A E E A id E * id id * id
Example 4.4: The string –(id+id) is a sentence of grammar because there is the
derivation
E -E -(E) -(E+E) -(id+E) -(id+id)
11
Context Free Grammars
Example 4.5:
Build a parse tree from the derivation (Leftmost derivations) -(id+id) or
E -E -(E) -(E+E) -(id+E) - (id+id)
12
Context Free Grammars
Consider the expression grammar:
E E+E | E*E | (E) | -E | id
Leftmost derivations of id + id * id
E
EE+E
E + E
E
E + E id + E E + E
id
E
id + E id + E * E E + E
id E * E
13
Context Free Grammars
Consider the expression grammar:
E E+E | E*E | (E) | -E | id
Rightmost derivations of id + id * id
E
EE+E
E * E
E
E + E E* id E * E
id
E
id + E E+E*id E * E
E E
+ id
14
Ambiguous Grammars
Example 4.6 :
The arithmetic expression grammar permits two distinct leftmost derivations
for the sentence id + id * id:
EE*E EE+E
E+E*E id + E
id + E * E id + E * E
id + id * E id + id * E
id + id * id id + id * id
E E
E * E E + E
E id E * E
+ E id
id id id id
15
Ambiguous Grammars
Ambiguity: A CFG is said to be ambiguous if for some strings there exist
more than one parse tree Or more than one leftmost derivation Or more than
one rightmost derivation
Example: id+id*id
E
E
E + E
E * E
id E * E
E + E id
id id id id
1+5*3
• The problem with ambiguity precedence of operator is violated.
id id id id
17
How to remove ambiguity
id id id id
Both are correct using parse tree, but operator associativity is violated here
18
How to remove ambiguity (with Associativity)
We are getting both associativity in the previous grammar because we are defining the
grammar without any order.
To achieve left associativity we have to grow the grammar in left direction only.
For example +, * are left associative operator, in this case we have to maintain left
associativity.
Consider the expression grammar:
E E E+E | E*E | id E E+id | E*id | id
E + id
E + id
id
19
How to remove ambiguity (with Associativity)
We are getting both associativity in the previous grammar because we are defining the
grammar without any order.
To achieve right associativity we have to grow the grammar in left direction only.
For example “ ”is right associative operator, in this case we have to maintain left
associativity.
Consider the expression grammar:
E Tree should
E E ^ E | id E F ^ E | F grow in
F ^ E F id right
direction
only.
id F ^ E
id F
id
for the sentence 2^3^2, Which is
20
How to remove ambiguity (With Precedence)
To maintain the order of precedence of operators, Heights precedence operator should be at
the least level.
E E+T | T
T T*F | F After maintaining the precedence order
F id
21
Left Recursion
Left recursion: A production of grammar is said to have left recursive if the leftmost variable
of its RMS is same as the variable of its LMS.
Example:
22
Left Recursion
Why to Left recursion: In recursive descent parsing, left recursive grammar cause an infinite
loop . How infinite loops?
Example:
The language generated by the grammar is
23
Elimination of Direct Left Recursion
The Grammar
24
Elimination of Direct Left Recursion
25
Elimination of indirect Left Recursion
Consider The following indirect left recursive Grammar
26
Left Factoring
Consider the following grammar
| Common prefixes in the grammar
One or more productions on the RHS are having something common in the prefixes. This is
called common prefix problem on non-deterministic grammar.
Left factoring:
Sometimes it is not clear which production rules to choose to expand a non terminal because
multiple productions begin with the same terminal or non-terminal. This type of grammar is
non-deterministic grammar or grammar containing left factoring.
How to remove left factoring:
Postponed the decision making.
|
|
27
Left Factoring
Class Work S
|aSaSb
|abb
|b
28
Types of Parser
Parser Shift-
Reduce
Top down parser Bottom up parser parser
L scan from
Top down Operator left -right
Top down
with full precedence LR Parser
without R Reverse
backtracking parser
backtracking of RMD
LL(1) The first L stands for scanning the input from left to right,
or the second L stands for producing a leftmost derivation,
Predictive and the 1 stands for using one input symbol of lookahead
parser at each step to make parsing action decision.
29