Compiler Construction Week 6
Compiler Construction Week 6
Zulfiqar Ali
UIT University
Week 6 - Contents
• Derivations in CFG
• Ambiguity in Derivations
• Associativity in Derivations
• Precedence issue in Derivations
Derivations in CFG
• A CFG list->list+list | list x list | digit OR
– list->list+list
– List->list x list
– List->digit
• CFG has (V T P S), here V=list, T=+ x digit, P is
above given rules and S is any string derive from
the rules.
Derivations in CFG
• A grammar derives strings by beginning with
the start symbol and repeatedly replacing a
nonterminal by the body of a production for
that nonterminal. The terminal strings that
can b e derived from the start symbol form
the language defined by the grammar.
Example
List
For Example: We have a grammar
list->list+list | list x list | digit
list
list
+
We want to derive a string digit x digit + digit
digit
List
List
X
digit
digit
Ambiguity in Derivations
• We have to b e careful in talking about the structure of a
string according to a grammar.
• A grammar can have more than one parse tree generating a
given string of terminals. Such a grammar is said to be
ambiguous.
• To show that a grammar is ambiguous, all we need to do is
find a terminal string that is the yield of more than one
parse tree.
• Since a string with more than one parse tree usually has
more than one meaning, we need to design unambiguous
grammars for compiling applications, or to use ambiguous
grammars with additional rules to resolve the ambiguities.
Ambiguity Examples
• Tree in previous slide shows that an expression
like 9-5+2 has more than one parse tree with this
grammar.
• The two trees for 9-5+2 correspond to the two
ways of parenthesizing the expression: (9-5)+2
and 9-(5+2).
• This second parenthesization gives the expression
the unexpected value 2 rather than the
customary value 6.
Ambiguity Examples
• Suppose we used a single nonterminal string
and did not distinguish between digits and
lists. Following is a rule/grammar.
Associativity of Operators
• By convention, 9+5+2 is equivalent to (9+5)+2 and 9-5-2 is
equivalent to (9-5)-2.
• When an operand like 5 has operators to its left and right,
conventions are needed for deciding which operator applies to that
operand.
• We say that the operator + associates to the left, because an
operand with plus signs on both sides of it belongs to the operator
to its left.
• In most programming languages the four arithmetic operators,
addition, subtraction, multiplication, and division are left-
associative.
Associativity of Operators
• Some common operators such as exponentiation are right-
associative.
• As another example, the assignment operator = in C and its
descendants is right-associative; that is, the expression a=b=c
is treated in the same way as the expression a=(b=c).
• Strings like a=b=c with a right-associative operator are
generated by the following grammar.
Associativity of Operators
Precedence of Operators
• Consider the expression 9+5*2. There are two possible
interpretations of this expression: (9+5)*2 or 9+(5*2).
• The associativity rules for + and * apply to occurrences
of the same operator, so they do not resolve this
ambiguity.
• Rules defining the relative precedence of operators are
needed when more than one kind of operator is
present.
• We say that * has higher precedence than + if * takes
its operands before + does.
Precedence of Operators
• In ordinary arithmetic, multiplication and
division have higher precedence than addition
and subtraction.
• Therefore, 5 is taken by * in b oth 9+5*2 and
9*5+2; i.e., the expressions are equivalent t
9+(5*2) and (9*5)+2, respectively.
Precedence of Operators
Exercises
• Determine whether the following grammars
are ambiguous or not
– S-> (S)| SS | E using the string ()()().
– S->SbS | a using the string ababa
– S->aB|ab, A->AB|a, B->Abb|b using string ab
Associativity solution in CFGs
• Associativity issue can be resolved by
restricting the Grammar.
• Example: Consider a grammar
– E->E+E, E->E-E, E->digit
E->E+E, E->E-E, E->digit
Incorrect
Parse Tree
Precedence solutions in CFGs
• Precedence issue can be resolved by restricting the
grammar. Let restring the first rule 1. E->E+E.
• 1. E->E+E as we know that + is left associative and has
less precedence than x.so + should expand first.
• To restrict it to left associative we to stop it grow from
right, we will update the rule to1. E->E+T.
• The 2nd rule will be 2. T->TxTbut x is also left, so make it
left recursive, update it to 2. T->TxF.
• E->E+T , T->TxF
Precedence solutions in CFGs
• Remember from the Grammar E->E+E | ExE | id E is also directly
giving id, so will update our first rule to 1. E->E+T | T.
• Third rule will be F->id
• we should be able write id directly from E, so will update rule 2
2. T->TxF | F
• so our final all production rules are
1. E->E+T | T
2. T=TxF | F
3. F-> id
Precedence solutions in CFGs
• Lets drive the string id+idxid or n+n x n using
the rules
1. E->E+T | T
2. T=TxF | F
3. F-> id
Reference
• Compilers: Principles, Techniques, and Tools, A. V.
Aho, R. Sethi and J. D. Ullman, Addison-Wesley, 2nd
ed., 2006.
– Chapter - 2
THANK YOU