140 likes | 261 Vues
This resource delves into ambiguous grammars, outlining how they generate strings with multiple parse trees. It discusses the significance of disambiguating rules and grammar rewriting to resolve ambiguities, particularly in arithmetic expressions. By explaining top-down parsing approaches such as LL(1) and highlighting the roles of precedence and associativity, the text provides insights essential for effective syntax analysis in programming language design. Additionally, it covers techniques to eliminate left recursion and left factoring, ensuring the clarity and efficiency of parsing processes.
E N D
COMP313A Programming Languages Syntax Analysis (2)
More on ambiguous grammars • Predictive parsing • Nonrecursive Predictive Parsing
Parsing token sequence: id + id * id E E + E | E * E | ( E ) | - E | id
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
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
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’
Associativity • 45-10-5 ?30 or 40 Subtraction is left associative, left to right (=30) • E E addop E | TermDoes not tell us how to split up 45-10-5 • E E addop Term | TermForces left associativity via left recursion • Precedence & associativity remove ambiguity of arithmetic expressions • Which is what our maths teachers took years telling us!
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
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
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
Two grammar problems • Eliminating left recursion A -> Aa | bA -> bA’ A’ -> aA’ | eExample E -> E + T | T T -> T * F | F F -> (E) | id The general case A -> Aa1 | Aa2 | …| Aam | b1 | b2 | …| bn
Two grammar problems • Eliminating left recursion involving derivations of two or more steps S -> Aa | b A -> Ac | Sd | e A -> Ac | Aad | bd | e
Two grammar problems… • Left factoring Stmt -> if Exp then Stmt else Stmt | if Expr then Stmt A -> ab1 | ab2 A -> aA’ A’ -> b1 | b2
exercises Eliminate left recursion from the following grammars. • S->(L) | aL->L,S | S • Bexpr ->Bexpr or Bterm | BtermBterm -> Bterm and Bfactor | BfactorBfactor -> not Bfactor | (Bexpr) | true | false