Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.

Similar presentations


Presentation on theme: "Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1."— Presentation transcript:

1 Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1

2 Outline Introduction to Parsing Context-free Grammars Pare Trees Ambiguity in Grammars 2

3 Parse Tree A parse tree pictorially shows how the start symbol of a grammar derives a string in the language. If nonterminal A has a production A  XYZ, then a parse tree may have an interior node labeled A with three children labeled X,Y, and Z, from left to right. 3

4 Parse Tree Pictorially we can show how the start symbol of a grammar derives a given string 4 A  XYZ

5 Parsing Is the problem of taking a string of terminals and figuring out how to derive it from the start symbol of the grammar, If it cannot be derived from the start symbol of the grammar, then reporting syntax errors within the string. 5

6 Derivations and parse trees We can describe a derivation using a graphical representation called parse tree: ◦ the root is labeled with the start symbol, S ◦ each internal node is labeled with a non-terminal ◦ the children of an internal node A are the right- hand side of a production A  ◦ each leaf is labeled with a terminal A parse tree has a unique leftmost and a unique rightmost derivation (however, we cannot tell which one was used by looking at the tree)

7 Parse Trees 7

8 8

9 9

10 10 Parse Trees

11 11 Derivation Tree Parse Trees

12 12 yields Derivation Tree Parse Trees

13 Parse Tree Example Two Consider the following grammar list  list + digit | list – digit | digit digit  0|1|2|3|4|5|6|7|8|9 Derive 9-5+2 from these rules. ◦ list  list + digit  list - digit + digit  digit – digit + digit  9 - digit + digit  9 – 5 + digit  9 – 5 + 2 13

14 Parse Tree Example Two The derivation of 9-5+2 can be represented by this parse tree. 14 The process of finding a parse tree for a given string of terminals is called parsing that string

15 Parsing Parse Trees At each step, we choose a non-terminal to replace. ◦ This choice can lead to different derivations. Two strategies are especially interesting: 1.Leftmost derivation: replace the leftmost non- terminal at each step 2.Rightmost derivation: replace the rightmost non-terminal at each step 15© Oscar Nierstrasz

16 16 Sometimes the derivation order does not matter. Same derivation tree Leftmost derivation: Rightmost derivation: Parse Trees

17 Outline Introduction to Parsing Context-free Grammars Derivation Trees Ambiguity in Grammars 17

18 Ambiguity in Grammar Consider the following grammar E  E + E | E *E | (E) | a Derive the string a+a*a from the grammar. E  E +E  a +E  a + E*E  a+a*E  a+a*a 18

19 Ambiguity Left-most derivation from the grammar gives this parse tree. E  E + E  a + E  a + E * E  a + a * E  a + a * a 19

20 Ambiguity Another left-most derivation from the grammar gives this parse tree. E  E * E  E + E * E  a + E * E  a + a * E  a + a * a 20

21 Ambiguity E  E + E | E *E | (E) | a for the string a + a * a has two derivation trees. 21

22 Ambiguity The grammar E  E + E | E *E | (E) | a is ambiguous. The string a+a*a has two parse trees. The string a+a*a has two left-most derivations. 22 1 2

23 Ambiguity 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, we find a terminal string that is the yield of more than one parse tree. 23

24 Ambiguity Since a string with more than one parse tree usually has more than one meaning. We need to design unambiguous grammars for compiling applications. Use ambiguous grammars with additional rules to resolve the ambiguities. 24

25 Ambiguity: Example One Consider the grammar: S  AB B  b A  aA | c Show that the grammar is ambiguous. 25

26 Ambiguity: Example One Consider the grammar: S  AB B  b A  aA | c The grammar has a number of derivations: ◦ Arbitrary order:  S  AB  aAB  aAb  aaAb  aacb ◦ Leftmost derivation:  S  AB  aAB  aaAB  aacB  aacb ◦ Rightmost derivation:  S  AB  Ab  aAb  aaAb  aacb A B S a A a A c b 26

27 Ambiguity: Example Two Consider the following grammar string  string + string| string– string| string string  0|1|2|3|4|5|6|7|8|9 Show that the grammar is ambiguous. 27

28 Ambiguity: Example Two Consider the following grammar string  string + string| string– string| string string  0|1|2|3|4|5|6|7|8|9 Show that the grammar is ambiguous. The expression 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). 28

29 Ambiguity: Example Two Two parse trees for 9-5+2 29

30 Ambiguity: Example Three Show that the following grammar is ambiguous, by producing one string in the language that has two di ff erent parse trees: S → aS | aSbS |  Show that the grammar is ambiguous. 30

31 Ambiguity: Example Three 31

32 Ambiguity: Example Four Consider the grammar ◦ E  E * E | E + E | ( E ) | id ◦ Build a parse tree for: id * id + id * id ◦ Show that the grammar is ambiguous. 32

33 Ambiguity: Example Four Consider the grammar ◦ E  E * E | E + E | ( E ) | id E E+E E*EE*E id E E*E E+E E*E E E*E E*E E+E 33

34 Ambiguity Exercises Is this grammar ambiguous or not? a)S  +SS | -SS | a b) S  S(S)S | ε c)S  a | S + S | SS | S* | (S) 34

35 Why care about ambiguity? Consider the string a+a*a. Take a = 3 35

36 Why care about ambiguity? The string becomes 3+3*3 36 33 3 33 3

37 37 Why care about ambiguity?

38 38 Why care about ambiguity? The correct result should be 3+3*3 = 12

39 Ambiguity in Grammar Ambiguity implies multiple parse trees Ambiguity is bad for programming languages. ◦ Can make parsing more difficult ◦ Can impact the semantics of the language  Different parse trees can have different semantic meanings, yield different execution results. Try to Eliminate ambiguity ◦ Sometimes, rewrite the grammar can eliminate ambiguity  But it may add additional semantics in the language 39

40 Ambiguity Rewrite grammar to eliminate ambiguity ◦ Many ways to rewrite the grammar  The new grammar should accept the same language ◦ For each input string, there may be multiple parse trees ◦ Each has a different semantic meaning ◦ Which one do we want? Rewrite grammar should be based on the desired semantics. There is no general algorithm to rewrite ambiguous grammars. 40

41 Rewrite Ambiguous Grammar Try to use a single recursive nonterminal in each rule ◦ When the left symbol appears more than once on the right side, ◦ Use additional symbols to substitute them and allow only one  Force to only allow one expansion 41

42 Rewrite Ambiguous Grammar Example grammar The grammar is ambiguous Change to a new unambiguous grammar 42

43 Rewrite Ambiguous Grammar For the string A new derivation for it is: 43

44 Rewrite Ambiguous Grammar A new derivation for it is: Unique derivation tree for the string a+a*a 44

45 Rewrite Ambiguous Grammar The grammar is unambiguous. 45

46 Associativity of Operators How will you evaluate this? 9-5-2 Will ‘5’ go with the ‘-’ on the left or the one on the right? If it goes with the one on the left: (9-5)-2 we say that the operator ‘-’ is left associative. If it goes with the one on the right: 9-(5- 2) we say that the operator ‘-’ is right- associative. 46

47 Associativity of Operators How to express associativity in production rules? term-> term – digit digit -> 0|1|2|3|4|5|6|7|8|9 term->digit-term digit -> 0|1|2|3|4|5|6|7|8|9 47 Left associative (9-5)-2 Right associative 9-(5-2)

48 Precedence of Operators Associativity applies to occurrence of the same operator What if operators are different? How will you evaluate: 9-5*2 48

49 Precedence of Operators For the expression 9+5*2, there are two possible interpretations of it: ◦ (9+5) *2 ◦ 9+ (5*2) The associativity rules for + and * apply to occurrences of the same operator, so they do not resolve this ambiguity. 49

50 Precedence of Operators Therefore, * has higher precedence than + if * takes its operands before + does. In ordinary arithmetic, multiplication and division have higher precedence than addition and subtraction. So, 5 is taken by * in both 9+5*2 and 9*5+2. The expressions are equivalent to ◦ 9+ (5*2) ◦ (9*5) +2 50

51 Conclusion Ambiguous grammars can be a significant problem in practice, because we rely on the parse tree to capture the basic structure of a program. To avoid the problems of ambiguity, we can try to: ◦ Rewrite grammar ◦ Use “disambiguating rules” when we implement parser for grammar. Most tools allow precedence and associativity declarations to disambiguate grammars 51


Download ppt "Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1."

Similar presentations


Ads by Google