0% found this document useful (0 votes)
43 views34 pages

Compiler Construction Week 6

Uploaded by

A I M E N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views34 pages

Compiler Construction Week 6

Uploaded by

A I M E N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Compiler Constructions

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

• The bodies of the three productions with nonterminal list as


head equivalently can b e grouped:

• According to our conventions, the terminals of the grammar are the


symbols
• Suppose we want to derive 9-5+2 from the given
productions.
• Taking the first production List->list+digit we may drive
list->list+2
• Now taking second product list-digit we may drive
list-digit+2
• we will replace digital with 5, we will have list-5+2
• Now from left most list we drive digit then we have
digit-5+2
• After replacing digit with 9, we will have 9-5+2
Types of Derivations
• Left Most Derivation
• Right most derivation
• Parse tree
Left most derivation
• In the leftmost derivation, the input is
scanned and replaced with the production
rule from left to right. So in leftmost
derivation, we read the input string from left
to right.
Left most derivation
• Let derive the string Digit + Digit x Digit using left most
derivation using the given production.
– list->list+list | list x list | digit
• Let choose best rule first to derive above string
– List->list+list
– List->digit+list
– List->digit+list x list
– -list->digit+digit x list
– list>->digit + digit x digit
Right most derivation
• In rightmost derivation, the input is scanned
and replaced with the production rule from
right to left. So in rightmost derivation, we
read the input string from right to left.
Right Most Derivation
• Lets derive the same string digit + digit x digit
using right most with same rules.
• list->list+list | list x list | digit
– Lets choose the best rule first to derive above string
• List -> list + list
• List->list + list x list
• list -> list + list x digit
• List -> list + digit x digit
• List -> digit + digit x digit
Parse Tree Derivation
• Parse tree Derivation tree is a graphical representation
for the derivation of the given production rules for a
given CFG. It is the simple way to show how the
derivation can be done to obtain some string from a
given set of production rules.
• Parse tree follows the precedence of operators. The
deepest sub-tree traversed first. So, the operator in the
parent node has less precedence over the operator in
the sub-tree.
Parse Tree Derivation
• A parse tree contains the following properties:
– The root node is always a node indicating start
symbols.
– The derivation is read from left to right.
– The leaf node is always terminal nodes.
– The interior nodes are always the non-terminal
nodes.
Parse Tree Derivation

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

• For a string digit+digit-digit (9+4-5) we have a


parse trees.
E E
E + E E - E
9 E - E E + E 5
4 5 9 4
Lets restrict the grammar
• E->E+E, E->E-E, E->digit
• E->E+E Updated to E->E+digit to enable it for
left associative, because + is left assoc..
E
• E->E-E is updated to E->E-digit E - digit
• E->digit it will remain same E + digit 5
9 4
Precedence solutions in CFGs
• We have a grammar and a string id+idxid
• E->E+E
Correct
E->ExE
Parse Tree
E->id
– But there are other ways to illustrate parse trees, but that will
lead incorrect result.

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

You might also like