Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCE 531 Compiler Construction

Similar presentations


Presentation on theme: "CSCE 531 Compiler Construction"— Presentation transcript:

1 CSCE 531 Compiler Construction
Lecture 19 CFL  PDA Topics Given a Context Free language construct a PDA Readings: November 2, 2008

2 New: Homework Last Time: Simple4.y revisited
Instantaneous Descriptors and traces Moves properties: more input, more on stack Language Accepted by a PDA: by final states and by empty stack New: From Grammars to PDA Section 6.3 Homework

3 Homework from Last time
Given the grammar simple4.y (and simple4.l) Modify the semantic actions so that it produces a trace of productions involved in the parse. If you look at the sequence do you notice anything? 6.2.1b) Strings of 0’s and 1’s such that no prefix has more 1’s than 0’s 6.2.5b,c

4 Equivalences CFL PDA
Last time we showed acceptance by final state was equivalent to acceptance by empty stack. CFL Grammar PDA by Empty Stack PDA by final state

5 Review Leftmost Derivations
E  T + E | T T  F * T | F F  id | num | ( E ) Parse x*(y+7.2) Actually id*(id+num) E lm T lm F * T lm id * T lm id * F

6 Top-Down Parsing Algorithm
Initialize stack by pushing Start Symbol tok = next-token() top = top-of-stack while (the Stack is non-empty) if tok = top then pop(); tok = next-token(); else if top is non-terminal A Guess production A  α1 α2 … αn then pop A and push α1 α2 … αn with α1 on top else the parse fails end

7 Top-Down Parsing with a Stack
E lm T lm F * T lm id * T lm id * F lm id * ( E ) lm id * ( T + E ) lm id * ( F + E ) lm id * ( id + E ) lm id * ( id + T ) lm id * ( id + F ) lm id * (id+num) Input: id * ( id + num ) Stack on Side

8 Parsing == Sequence of Moves
E lm T lm F * T lm id * T lm id * F lm id * ( E ) lm id * ( T + E ) lm id * ( F + E ) lm id * ( id + E ) lm id * ( id + T ) lm id * ( id + F ) lm id * (id+num) Input: id * ( id + num ) (q, id * ( id + num ), E) ├ (q, id * ( id + num ), T)

9 Main Idea behind Grammar  PDA
Construct PDA that simulates leftmost derivations Left sentential forms Every left sentential form is of the form xAα where x is a string of terminals A is the leftmost non-terminal (variable) α is a string of grammar symbols (terminals and non-terminals) Aα is called the tail; note tail = ε  no non-terminals The constructed PDA has single state q

10 A State of the Simulation
Parse of the string w = xy Consume some input say x, leaving y the rest of the input Enter some state q ID (q, y, Aα) Among the “A” productions suppose we guess to use A β, where β = x1…xk then (q, y, Aα) ├ (q, y, βα) = (q, y, x1…xkα) Match tokens from x1…xk until the first variable Y occurs

11 Formal PDA Construction from CFL
Let G = (V, T, Q, S) P = ({q}, T, (V U T), δ, q, S) where δ is defined by: δ(q, ε, A) = { (q, β) | A β is a production of G} δ(q, a, a) = { (q, ε) }

12 Example 1

13 Construction PDA  Grammar
Who cares? Other than it exists

14 Deterministic PDAs

15 Improving the Grammar

16 Example 2

17 When will our parsing algorithm be deterministic?
First(α ) = { x in T such that α * xβ } Then if A α and A β are two productions we are choosing from and the current input character is a which is in First(α) but not in First(β ) Then we choose the production A α. So if First(α) ∩ First(β ) = ϕ then we can always deterministically choose There is a type of grammar that is LL(1) for which this works well. A grammar with no ε-productions is LL(1) if it satisfies the disjointness property above.

18 Homework 6.3.1* 6.3.2 Convert the grammar 6.3.5b S  aAA
A  aS | bS | a To a PDA that accepts the same language by empty stack 6.3.5b

19

20

21

22

23

24

25

26

27

28


Download ppt "CSCE 531 Compiler Construction"

Similar presentations


Ads by Google