Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 COMP313A Programming Languages Syntax Analysis (2)

Similar presentations


Presentation on theme: "1 COMP313A Programming Languages Syntax Analysis (2)"— Presentation transcript:

1 1 COMP313A Programming Languages Syntax Analysis (2)

2 2 More on ambiguous grammars Predictive parsing Nonrecursive Predictive Parsing

3 3 Parsing token sequence: id + id * id E  E + E | E * E | ( E ) | - E | id

4 4 Ambiguous Grammars A grammar that generates a string with 2 distinct parse trees is called an ambiguous grammar –2+3*4 = 2 + (3*4) = 14 –2+3*4 = (2+3) * 4 = 20 Our experience of maths says interpretation 1 is correct but the grammar does not express this: –E  E + E | E * E | ( E ) | - E | id

5 5 Removing Ambiguity Two methods 1. Disambiguating Rules –+ve leaves grammar unchanged –-ve grammar is not sole source of syntactic knowledge 2. Rewrite the Grammar Using knowledge of the meaning that we want to use later in the translation into object code to guide grammar alteration

6 6 Precedence E  E addop E | Term Addop  + | - Term  Term * Term | Factor Factor  ( exp ) | number | id Operators of equal precedence are grouped together at the same ‘level’ of the grammar  ’precedence cascade’

7 7 Associativity 45-10-5 ? 30 or 40 Subtraction is left associative, left to right (=30) E  E addop E | Term Does not tell us how to split up 45-10-5 E  E addop Term | Term Forces left associativity via left recursion Precedence & associativity remove ambiguity of arithmetic expressions –Which is what our maths teachers took years telling us!

8 8 Ambiguous grammars Statement -> If-statement | other If-statement -> if (Exp) Statement | if (Exp) Statement else Statement Exp -> 0 | 1 Parse if (0) if (1) other else other

9 9 Removing ambiguity Statement -> Matched-stmt | Unmatched-stmt Matched-stmt -> if (Exp) Matched-stmt else Matched-stmt | other Unmatched-stmt ->if (Exp) Statement | if (Exp) Matched-stmt else Unmatched-stmt

10 10 Predictive Parsing Top down parsing LL(1) parsing Table driven predictive parsing versus recursive descent parsing No backtracking E -> E + T | T T -> T * F | F F -> (E) | id

11 11 Two grammar problems Eliminating left recursion A -> A  |  A ->  A’ A’ ->  A’ |  Example E -> E + T | T T -> T * F | F F -> (E) | id The general case A -> A  1 | A  2 | …| A  m |  1 |  2 | …|  n

12 12 Two grammar problems Eliminating left recursion involving derivations of two or more steps S -> Aa | b A -> Ac | Sd |  A -> Ac | Aad | bd | 

13 13 Two grammar problems… Left factoring Stmt -> if Exp then Stmt else Stmt | if Expr then Stmt A ->  1 |  2 A ->  A’ A’ ->  1 |  2

14 14 exercises Eliminate left recursion from the following grammars. a)S->(L) | a L->L,S | S b)Bexpr ->Bexpr or Bterm | Bterm Bterm -> Bterm and Bfactor | Bfactor Bfactor -> not Bfactor | (Bexpr) | true | false


Download ppt "1 COMP313A Programming Languages Syntax Analysis (2)"

Similar presentations


Ads by Google