Top Banner
Pushdown Automata • Consists of – Pushdown stack (can have terminals and nonterminals) – Finite state automaton control • Can do one of three actions (based on state and input): – Shift: Move current input from input onto stack – Reduce: • If symbols on top of stack match right hand side of some grammar production NT Pop symbols () off of the stack Push left hand side nonterminal (NT) onto stack – Accept the input string
58

Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Jan 15, 2016

Download

Documents

Savana Woodhams
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Pushdown Automata• Consists of

– Pushdown stack (can have terminals and nonterminals)

– Finite state automaton control

• Can do one of three actions (based on state and input):– Shift: Move current input from input onto stack

– Reduce:• If symbols on top of stack match right hand side of some grammar production NT

• Pop symbols () off of the stack• Push left hand side nonterminal (NT) onto stack

– Accept the input string

Page 2: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift-Reduce Parser Example

* ( + num )numnum

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Stack

Tokenized Input String 5*(2+3)

Page 3: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift-Reduce Parser Example

* ( + num )numnum

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Grammar Rules

Page 4: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

SHIFT

Shift-Reduce Parser Example

* ( + num )numnum

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Page 5: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

SHIFT

Shift-Reduce Parser Example

* ( + num )num

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Page 6: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift-Reduce Parser Example

* ( + num )num

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

REDUCE

Page 7: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift-Reduce Parser Example

* ( + num )num

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

REDUCE

Expr

Page 8: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift-Reduce Parser Example

* ( + num )num

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Expr

SHIFT

Page 9: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift-Reduce Parser Example

( + num )num

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Expr

SHIFT

*

Page 10: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift-Reduce Parser Example

( + num )num

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Expr

Op

REDUCE

*

Page 11: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift-Reduce Parser Example

( + num )num

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Expr

Op

*SHIFT

Page 12: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift-Reduce Parser Example

+ num )num

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Expr

Op

*SHIFT

(

Page 13: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift-Reduce Parser Example

+ num )num

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Expr

Op

*SHIFT

(

Page 14: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift-Reduce Parser Example

+ num )

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Expr

Op

*SHIFT

(

num

Page 15: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift-Reduce Parser Example

+ num )

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Expr

Op

*SHIFT

(

num

REDUCE

Expr

Page 16: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift-Reduce Parser Example

+ num )

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Expr

Op

*SHIFT

(

num

Expr

Page 17: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift-Reduce Parser Example

num )

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Expr

Op

*SHIFT

(

num

Expr

+

Page 18: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift-Reduce Parser Example

num )

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Expr

Op

*SHIFT

(

num

Expr

Op

REDUCE

+

Page 19: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift-Reduce Parser Example

num )

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Expr

Op

*SHIFT

(

num

Expr

Op

+

Page 20: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift-Reduce Parser Example

)

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Expr

Op

*SHIFT

(

num

Expr

num

Op

+

Page 21: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift-Reduce Parser Example

)

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Expr

Op

*SHIFT

(

num

Expr

Op

+

Expr

REDUCE

num

Page 22: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift-Reduce Parser Example

)

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Expr

Op

*SHIFT

(

num +

REDUCE

num

Expr

Expr

Op

ExprExpr

Page 23: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift-Reduce Parser Example

)

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Expr

Op

*SHIFT

(

num

Expr

Op

+

Expr

num

Expr

Page 24: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift-Reduce Parser Example

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Expr

Op

*SHIFT

(

num

Expr

Op

+

Expr

num

Expr

)

Page 25: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

REDUCE

Shift-Reduce Parser Example

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *

Expr

Op

*

(

num

Expr

Op

+

Expr

num

Expr

)Expr

Page 26: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

REDUCE

Shift-Reduce Parser Example

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *Expr

Op

*

(

num

Expr

Op

+

Expr

num

Expr

)Expr

Expr

Page 27: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

ACCEPT!

Shift-Reduce Parser Example

num

Expr Expr Op Expr Expr (Expr)Expr - Expr

Expr numOp +Op -Op *Expr

Op

*

(

num

Expr

Op

+

Expr

num

Expr

)Expr

Expr

Page 28: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

*

num:5

num:2

+

num:3

Input String

5 * (2 + 3)

Page 29: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Basic Idea

• Goal: reconstruct parse tree for input string

• Read input from left to right• Build tree in a bottom-up fashion

• Use stack to hold pending sequences of terminals and nonterminals

Page 30: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Potential Conflicts

• Reduce/Reduce Conflict– Top of the stack may match RHS of multiple productions

– Which production to use in the reduction?

• Shift/Reduce Conflict– Stack may match RHS of production– But that may not be the right match– May need to shift an input and later find a different reduction

Page 31: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Conflicts

Expr Expr Op ExprExpr (Expr)

Expr - ExprExpr numOp +Op - Op *

•Original GrammarExpr Expr Op ExprExpr Expr - ExprExpr (Expr)

Expr Expr - Expr numOp +Op - Op *

•New Grammar

Page 32: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Conflicts

- numnum

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

Page 33: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Conflicts

- numnum

SHIFT

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

Page 34: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Conflicts

- num

SHIFT

num

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

Page 35: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Conflicts

- num

SHIFT

Expr

REDUCE

num

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

Page 36: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Conflicts

- num

SHIFT

Expr

num

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

Page 37: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Conflicts

num

SHIFT

Expr

num

-

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

Page 38: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift/Reduce/Reduce Conflict

num

Expr

num

-

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

Options:ShiftReduceReduce

Page 39: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift/Reduce/Reduce Conflict

num

Expr

num

-

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

What Happens if Choose

Reduce

REDUCE

Page 40: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift/Reduce/Reduce Conflict

num

Expr

num -

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

What Happens if Choose

Reduce

SHIFT

Expr

Page 41: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift/Reduce/Reduce Conflict

Expr

num -

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

What Happens if Choose

Reduce

SHIFT

Expr

num

Page 42: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift/Reduce/Reduce Conflict

Expr

num -

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

What Happens if Choose

Reduce

REDUCE

Expr

Expr

num

Page 43: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift/Reduce/Reduce Conflict

Expr

num -

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

What Happens if Choose

Reduce

FAILS!

Expr

Expr

num

Page 44: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift/Reduce/Reduce Conflict

num

Expr

num

-

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

Both of These Actions Work

ReduceShift

Page 45: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift/Reduce/Reduce Conflict

num

Expr

num

-

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

What Happens if Choose

Reduce

Page 46: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift/Reduce/Reduce Conflict

num

Expr

num

Op

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

What Happens if Choose

Reduce

-

REDUCE

Page 47: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift/Reduce/Reduce Conflict

Expr

num

Op

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

What Happens if Choose

Reduce

-

SHIFT

num

Page 48: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift/Reduce/Reduce Conflict

Expr

num

Op

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

What Happens if Choose

Reduce

-

REDUCE

Expr

num

Page 49: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift/Reduce/Reduce Conflict

Expr

num

Op

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

What Happens if Choose

Reduce

-

REDUCE

Expr

num

Expr

Page 50: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift/Reduce/Reduce Conflict

Expr

num

Op

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

What Happens if Choose

Reduce

-

ACCEPT

Expr

num

Expr

Page 51: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

num

Expr

num

-

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

SHIFT

Conflicts

What Happens if Choose

Shift

Page 52: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Expr

num

-

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

SHIFT

num

Conflicts

What Happens if Choose

Shift

Page 53: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Expr

num

-

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

REDUCE

Expr

Conflicts

num

What Happens if Choose

Shift

Page 54: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Expr

num -

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

REDUCE

Expr

Conflicts

num

Expr

What Happens if Choose

Shift

Page 55: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Expr

num -

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

ACCEPT

Expr

Conflicts

num

Expr

What Happens if Choose

Shift

Page 56: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift/Reduce/Reduce Conflict

num

Expr

num

-

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

This Shift/Reduce Conflict Reflects Ambiguity in Grammar

Page 57: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift/Reduce/Reduce Conflict

num

Expr

num

-

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

This Shift/Reduce Conflict Reflects Ambiguity in Grammar

Eliminate by HackingGrammar

Page 58: Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.

Shift/Reduce/Reduce Conflict

num

Expr

num

-

Expr Expr Op ExprExpr Expr - Expr

Expr (Expr)Expr Expr -

Expr numOp +Op -Op *

This Shift/Reduce Conflict Can Be Eliminated By Lookahead of One Symbol

Parser Generator Should Handle It