COMP313A Programming Languages - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

COMP313A Programming Languages

Description:

Our experience of maths says interpretation 1 is correct but the grammar does not express this: ... Two grammar problems. Eliminating left recursion involving ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 15
Provided by: margaretj4
Category:

less

Transcript and Presenter's Notes

Title: COMP313A Programming Languages


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
  • 234 2 (34) 14
  • 234 (23) 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 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
Ambiguous grammars
  • Statement -gt If-statement other
  • If-statement -gt if (Exp) Statement
  • if (Exp) Statement
    else Statement
  • Exp -gt 0 1
  • Parse if (0) if (1) other else other

9
Removing ambiguity
  • Statement -gt Matched-stmt Unmatched-stmt
  • Matched-stmt -gt if (Exp) Matched-stmt else
    Matched-stmt other
  • Unmatched-stmt -gtif (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 -gt E T T T -gt T F F F -gt (E) id
11
Two grammar problems
  • Eliminating left recursion
  • A -gt Aa b A -gt bA
  • A -gt aA eExample E -gt E T T
  • T -gt T F F
  • F -gt (E) id
  • The general case
  • A -gt Aa1 Aa2 Aam b1 b2 bn

12
Two grammar problems
  • Eliminating left recursion involving derivations
    of two or more steps S -gt Aa b
  • A -gt Ac Sd e
  • A -gt Ac Aad bd e

13
Two grammar problems
  • Left factoring
  • Stmt -gt if Exp then Stmt else Stmt if Expr
    then Stmt
  • A -gt ab1 ab2
  • A -gt aA
  • A -gt b1 b2

14
exercises
  • Eliminate left recursion from the following
    grammars.
  • S-gt(L) aL-gtL,S S
  • Bexpr -gtBexpr or Bterm BtermBterm -gt Bterm and
    Bfactor BfactorBfactor -gt not Bfactor
    (Bexpr) true false
Write a Comment
User Comments (0)
About PowerShow.com