1 / 14

COMP313A Programming Languages

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.

mona-ortiz
Download Presentation

COMP313A Programming Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP313A Programming Languages Syntax Analysis (2)

  2. More on ambiguous grammars • Predictive parsing • Nonrecursive Predictive Parsing

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

  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. 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. 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. 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!

  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. 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. 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. 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

  12. 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

  13. Two grammar problems… • Left factoring Stmt -> if Exp then Stmt else Stmt | if Expr then Stmt A -> ab1 | ab2 A -> aA’ A’ -> b1 | b2

  14. 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

More Related