parsing: derivations, ambiguity, precedence, associativity

29
Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh) 1 Parsing: Derivations, Ambiguity, Precedence, Associativity Lecture 8

Upload: others

Post on 04-May-2022

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

1

Parsing: Derivations, Ambiguity, Precedence, Associativity

Lecture 8

Page 2: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

2

Topics covered so far

•  Regular languages and Finite automaton

•  Parser overview

•  Context-free grammars (CFG�s)

•  Derivations

•  First, follow, predict sets

Page 3: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

3

Outline of Today’s Lecture

•  Ambiguity in context-free grammars

•  Left and right derivations

•  Associativity

•  Precedence

•  Bottom-up Parsing

Page 4: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

4

CFGs (Cont.)

•  A CFG consists of –  A set of terminals T –  A set of non-terminals N –  A start symbol S (a non-terminal) –  A set of productions

{ }1 2

where and n

i

X YY YX N Y T N ε

∈ ∈ ∪ ∪

L

Page 5: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

5

Examples of CFGs (cont.)

Simple arithmetic expressions:

( )

E E E| E + E| E| id

→ ∗

Page 6: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

6

Arithmetic Example

Simple arithmetic expressions: Some elements of the language:

E E+E | E E | (E) | id→ ∗

id id + id(id) id id(id) id id (id)

∗ ∗

Page 7: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

7

Derivation Example

•  Grammar

•  String

E E+E | E E | (E) | id→ ∗

id id + id∗

Page 8: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

8

Derivation Example (Cont.)

EE+EE E+Eid E + Eid id + Eid id + id

→ ∗

→ ∗

→ ∗

→ ∗

E

E

E E

E +

id *

id id

Page 9: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

9

Derivation in Detail (1)

E

E

Page 10: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

10

Derivation in Detail (2)

EE+E→

E

E E +

Page 11: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

11

Derivation in Detail (3)

E E

EE+EE +→ ∗

E

E

E E

E +

*

Page 12: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

12

Derivation in Detail (4)

EE+EE E+Eid E + E→ ∗

→ ∗

E

E

E E

E +

*

id

Page 13: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

13

Derivation in Detail (5)

EE+EE E+Eid E + id id +

EE→ ∗

→ ∗

→ ∗

E

E

E E

E +

*

id id

Page 14: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

14

Derivation in Detail (6)

EE+EE E+Eid E + Eid id + Eid id + id

→ ∗

→ ∗

→ ∗

E

E

E E

E +

id *

id id

Page 15: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

15

Notes on Derivations

•  A parse tree has –  Terminals at the leaves –  Non-terminals at the interior nodes

•  An in-order traversal of the leaves is the original input

•  The parse tree shows the association of operations, the input string does not

Page 16: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

16

Left-most and Right-most Derivations

•  The example is a left-most derivation –  At each step, replace the

left-most non-terminal

•  There is an equivalent notion of a right-most derivation

EE+EE+idE E + idE id + idid id + id

→ ∗

→ ∗

→ ∗

Page 17: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

17

Ambiguity

•  Grammar

•  String

E E+E | E E | (E) | id→ ∗

id id + id∗

Page 18: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

18

Ambiguity (Cont.)

This string has two parse trees

E

E

E E

E *

id +

id id

E

E

E E

E +

id *

id id

Page 19: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

19

Ambiguity (Cont.)

•  A grammar is ambiguous if it has more than one parse tree for some string –  Equivalently, there is more than one right-most or

left-most derivation for some string

•  Ambiguity is BAD –  Leaves meaning of some programs ill-defined

Page 20: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

20

Dealing with Ambiguity

•  There are several ways to handle ambiguity

•  Most direct method is to rewrite grammar unambiguously

•  Enforces precedence of * over +

' '

'

E E E | EE id E | id | (E) E | (E)

→ +

" "→ ∗ ∗

Page 21: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

21

Ambiguity in Arithmetic Expressions

•  Recall the grammar E → E + E | E * E | ( E ) | int •  The string int * int + int has two parse trees:

E

E

E E

E *

int +

int int

E

E

E E

E +

int *

int int

Page 22: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

22

Ambiguity: The Dangling Else

•  Consider the grammar E → if E then E | if E then E else E | OTHER

•  This grammar is also ambiguous

Page 23: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

23

The Dangling Else: Example

•  The expression if E1 then if E2 then E3 else E4 has two parse trees

if

E1 if

E2 E3 E4

if

E1 if

E2 E3

E4

•  Typically we want the second form

Page 24: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

24

The Dangling Else: A Fix

•  else matches the closest unmatched then •  We can describe this in the grammar

E → MIF /* all then are matched */ | UIF /* some then is unmatched */ MIF → if E then MIF else MIF | OTHER UIF → if E then E | if E then MIF else UIF

•  Describes the same set of strings (Associativity)

Page 25: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

25

The Dangling Else: Example Revisited

•  The expression if E1 then if E2 then E3 else E4

if

E1 if

E2 E3 E4

•  A valid parse tree (for a UIF)

if

E1 if

E2 E3

E4

•  Not valid because the then expression is not a MIF

Page 26: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

26

Ambiguity

•  No general techniques for handling ambiguity

•  Impossible to convert automatically an ambiguous grammar to an unambiguous one

•  Used with care, ambiguity can simplify the grammar –  Sometimes allows more natural definitions –  We need disambiguation mechanisms

Page 27: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

27

Precedence and Associativity Declarations

•  Instead of rewriting the grammar –  Use the more natural (ambiguous) grammar –  Along with disambiguating declarations

•  Most grammar generators allow precedence and associativity declarations to disambiguate grammars

•  Examples …

Page 28: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

28

Associativity Declarations

•  Consider the grammar E → E + E | int •  Ambiguous: two parse trees of int + int + int

E

E

E E

E +

int +

int int

E

E

E E

E +

int +

int int

•  Left associativity declaration: %left +

Page 29: Parsing: Derivations, Ambiguity, Precedence, Associativity

Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

29

Precedence Declarations

•  Consider the grammar E → E + E | E * E | int –  And the string int + int * int

E

E

E E

E +

int *

int int

E

E

E E

E *

int +

int int •  Precedence declarations: %left + %left *