CSCI 3136 Principles of Programming Languages Syntactic Analysis and Context-Free Grammars - 6 Summer 2013 Faculty of Computer Science Dalhousie University 1 / 74
Push-Down Automata: Formal Definition A push-down automaton (PDA) is a 6-tuple: ( Q, Σ , Γ , δ, q 0 , F ) • Q : a finite set of states • Σ : input alphabet (set of terminals) • Γ : stack alphabet • δ : a set of transition rules Q × (Σ ∪ { ǫ } ) × (Γ ∪ { ǫ } ) → Q × Γ ∗ currentState, inputSymbol, headOfStack → newState, pushSymbolsOnStack • q 0 : the start state • F : the set of accepting states 2 / 74
Graph Representation of PDA 3 / 74
Graph Representation of PDA • each state is a node 4 / 74
Graph Representation of PDA • each state is a node • for each transition δ ( q 1 , a, s → q 2 , α ) 5 / 74
Graph Representation of PDA • each state is a node • for each transition δ ( q 1 , a, s → q 2 , α ) • there is an edge from q 1 to q 2 with label ( a, s ) /α ( a, s ) /α q 1 q 2 6 / 74
Computation in a PDA 7 / 74
Computation in a PDA • start in state q 0 8 / 74
Computation in a PDA • start in state q 0 • if in state q 1 q 1 9 / 74
Computation in a PDA • start in state q 0 • if in state q 1 , a is the next input symbol Input: abbc . . . q 1 10 / 74
Computation in a PDA • start in state q 0 • if in state q 1 , a is the next input symbol, s is on the top of the stack Input: abbc . . . q 1 s p q q . . . Stack 11 / 74
Computation in a PDA • start in state q 0 • if in state q 1 , a is the next input symbol, s is on the top of the stack, and δ ( q 1 , a, s → q 2 , α ) is a transition ( a, s ) /α Input: abbc . . . q 1 q 2 s p q q . . . Stack 12 / 74
Computation in a PDA • start in state q 0 • if in state q 1 , a is the next input symbol, s is on the top of the stack, and δ ( q 1 , a, s → q 2 , α ) is a transition, then we can read a ( a, s ) /α Input: abbc . . . Input: bbc . . . q 1 q 2 s p q q . . . Stack 13 / 74
Computation in a PDA • start in state q 0 • if in state q 1 , a is the next input symbol, s is on the top of the stack, and δ ( q 1 , a, s → q 2 , α ) is a transition, then we can read a , pop s , push α (e.g., α = xy ) on stack, and change state to q 2 ( a, s ) /α Input: abbc . . . Input: bbc . . . q 1 q 2 s x p y q p q q . . . q Stack . . . Stack 14 / 74
Computation in a PDA • start in state q 0 • if in state q 1 , a is the next input symbol, s is on the top of the stack, and δ ( q 1 , a, s → q 2 , α ) is a transition, then we can read a , pop s , push α (e.g., α = xy ) on stack, and change state to q 2 ( a, s ) /α Before After Input: abbc . . . Input: bbc . . . q 1 q 2 s x p y q p q q . . . q Stack . . . Stack 15 / 74
How does a PDA Accept? Two modes of acceptance • Accept by empty stack: ◦ Consume all the input, and ◦ Have an empty stack at the end (Set of final states is irrelevant) • Accept by final state: ◦ Consume all the input, and ◦ Reach a final state 16 / 74
( ǫ, S ) /E $ Graph Representation of DPDA ( ǫ, E ) /TT ′ ( ǫ, T ) /FF ′ ( ǫ, T ′ ) /ǫ ( ǫ, F ′ ) /ǫ ( ǫ, F ) / ( E ) Rule R PREDICT( R ) ( ǫ, T ′ ) /ǫ S → E $ { n, ( } ( ǫ, F ′ ) /ǫ q ( q $ E → TT ′ { n, ( } ( ǫ, T ′ ) /AE ( ǫ, $) /ǫ T ′ → AE ($ , ǫ ) /ǫ q ) ( ǫ, F ′ ) /ǫ { + , −} . . . ( ǫ, A ) / + . . . T ′ → ǫ { $ , ) } . . . . . . q + T → FF ′ { n, ( } . . .. . . F ′ → MT ( ǫ, T ′ ) /AE {∗ , / } ( ǫ, F ′ ) /ǫ F ′ → ǫ q 0 . . . { + , − , $ , ) } . . . q − ( ǫ, A ) / − F → n { n } . . . . . . ( n, ǫ ) /ǫ F → ( E ) { ( } ( ǫ, n ) /ǫ . . . ( ǫ, F ′ ) /MT . . . q ∗ A → + { + } ( ǫ, M ) / ∗ A → − {−} M → ∗ {∗} ( ǫ, S ) /E $ q n q / ( ǫ, F ′ ) /MT ( ǫ, E ) /TT ′ ( ǫ, M ) // M → / { / } ( ǫ, T ) /FF ′ ( ǫ, F ) /n 17 / 74
( ǫ, S ) /E $ Parsing LL(1) Lang. using DPDA ( ǫ, E ) /TT ′ ( ǫ, T ) /FF ′ ( ǫ, T ′ ) /ǫ ( ǫ, F ′ ) /ǫ ( ǫ, F ) / ( E ) Input : ( ǫ, T ′ ) /ǫ ( ǫ, F ′ ) /ǫ q ( 2 + (4 − 1) ∗ 3$ q $ ( ǫ, T ′ ) /AE ( ǫ, $) /ǫ ($ , ǫ ) /ǫ q ) ( ǫ, F ′ ) /ǫ . . . ( ǫ, A ) / + . . . . . . . . . q + . . .. . . ( ǫ, T ′ ) /AE ( ǫ, F ′ ) /ǫ q 0 . . . . . . q − ( ǫ, A ) / + S . . . . . . ( n, ǫ ) /ǫ ( ǫ, n ) /ǫ Stack . . . ( ǫ, F ′ ) /MT . . . q ∗ ( ǫ, M ) / ∗ ( ǫ, S ) /E $ q / q n ( ǫ, F ′ ) /MT ( ǫ, E ) /TT ′ ( ǫ, M ) // ( ǫ, T ) /FF ′ ( ǫ, F ) /n 18 / 74
( ǫ, S ) /E $ Parsing LL(1) Lang. using DPDA ( ǫ, E ) /TT ′ ( ǫ, T ) /FF ′ ( ǫ, T ′ ) /ǫ ( ǫ, F ′ ) /ǫ ( ǫ, F ) / ( E ) Input : ( ǫ, T ′ ) /ǫ ( ǫ, F ′ ) /ǫ q ( 2 + (4 − 1) ∗ 3$ q $ ( ǫ, T ′ ) /AE ( ǫ, $) /ǫ ($ , ǫ ) /ǫ q ) ( ǫ, F ′ ) /ǫ . . . ( ǫ, A ) / + . . . . . . . . . q + . . .. . . ( ǫ, T ′ ) /AE ( ǫ, F ′ ) /ǫ q 0 . . . . . . q − ( ǫ, A ) / + S . . . . . . ( n, ǫ ) /ǫ ( ǫ, n ) /ǫ Stack . . . ( ǫ, F ′ ) /MT . . . q ∗ ( ǫ, M ) / ∗ ( ǫ, S ) /E $ q / q n ( ǫ, F ′ ) /MT ( ǫ, E ) /TT ′ ( ǫ, M ) // ( ǫ, T ) /FF ′ ( ǫ, F ) /n 19 / 74
( ǫ, S ) /E $ Parsing LL(1) Lang. using DPDA ( ǫ, E ) /TT ′ ( ǫ, T ) /FF ′ ( ǫ, T ′ ) /ǫ ( ǫ, F ′ ) /ǫ ( ǫ, F ) / ( E ) Input : ( ǫ, T ′ ) /ǫ ( ǫ, F ′ ) /ǫ q ( 2 + (4 − 1) ∗ 3$ q $ ( ǫ, T ′ ) /AE ( ǫ, $) /ǫ ($ , ǫ ) /ǫ q ) ( ǫ, F ′ ) /ǫ . . . ( ǫ, A ) / + . . . . . . . . . q + . . .. . . ( ǫ, T ′ ) /AE ( ǫ, F ′ ) /ǫ q 0 . . . . . . q − E ( ǫ, A ) / + $ . . . . . . ( n, ǫ ) /ǫ ( ǫ, n ) /ǫ Stack . . . ( ǫ, F ′ ) /MT . . . q ∗ ( ǫ, M ) / ∗ ( ǫ, S ) /E $ q / q n ( ǫ, F ′ ) /MT ( ǫ, E ) /TT ′ ( ǫ, M ) // ( ǫ, T ) /FF ′ ( ǫ, F ) /n 20 / 74
( ǫ, S ) /E $ Parsing LL(1) Lang. using DPDA ( ǫ, E ) /TT ′ ( ǫ, T ) /FF ′ ( ǫ, T ′ ) /ǫ ( ǫ, F ′ ) /ǫ ( ǫ, F ) / ( E ) Input : ( ǫ, T ′ ) /ǫ ( ǫ, F ′ ) /ǫ q ( 2 + (4 − 1) ∗ 3$ q $ ( ǫ, T ′ ) /AE ( ǫ, $) /ǫ ($ , ǫ ) /ǫ q ) ( ǫ, F ′ ) /ǫ . . . ( ǫ, A ) / + . . . . . . . . . q + . . .. . . ( ǫ, T ′ ) /AE T ( ǫ, F ′ ) /ǫ q 0 . . . . . . q − T ′ ( ǫ, A ) / + $ . . . . . . ( n, ǫ ) /ǫ ( ǫ, n ) /ǫ Stack . . . ( ǫ, F ′ ) /MT . . . q ∗ ( ǫ, M ) / ∗ ( ǫ, S ) /E $ q / q n ( ǫ, F ′ ) /MT ( ǫ, E ) /TT ′ ( ǫ, M ) // ( ǫ, T ) /FF ′ ( ǫ, F ) /n 21 / 74
( ǫ, S ) /E $ Parsing LL(1) Lang. using DPDA ( ǫ, E ) /TT ′ ( ǫ, T ) /FF ′ ( ǫ, T ′ ) /ǫ ( ǫ, F ′ ) /ǫ ( ǫ, F ) / ( E ) Input : ( ǫ, T ′ ) /ǫ ( ǫ, F ′ ) /ǫ q ( 2 + (4 − 1) ∗ 3$ q $ ( ǫ, T ′ ) /AE ( ǫ, $) /ǫ ($ , ǫ ) /ǫ q ) ( ǫ, F ′ ) /ǫ . . . ( ǫ, A ) / + . . . . . . . . . q + F . . .. . . ( ǫ, T ′ ) /AE F ′ ( ǫ, F ′ ) /ǫ q 0 . . . . . . q − T ′ ( ǫ, A ) / + $ . . . . . . ( n, ǫ ) /ǫ ( ǫ, n ) /ǫ Stack . . . ( ǫ, F ′ ) /MT . . . q ∗ ( ǫ, M ) / ∗ ( ǫ, S ) /E $ q / q n ( ǫ, F ′ ) /MT ( ǫ, E ) /TT ′ ( ǫ, M ) // ( ǫ, T ) /FF ′ ( ǫ, F ) /n 22 / 74
( ǫ, S ) /E $ Parsing LL(1) Lang. using DPDA ( ǫ, E ) /TT ′ ( ǫ, T ) /FF ′ ( ǫ, T ′ ) /ǫ ( ǫ, F ′ ) /ǫ ( ǫ, F ) / ( E ) Input : ( ǫ, T ′ ) /ǫ ( ǫ, F ′ ) /ǫ q ( 2 + (4 − 1) ∗ 3$ q $ ( ǫ, T ′ ) /AE ( ǫ, $) /ǫ ($ , ǫ ) /ǫ q ) ( ǫ, F ′ ) /ǫ . . . ( ǫ, A ) / + . . . . . . . . . q + n . . .. . . ( ǫ, T ′ ) /AE F ′ ( ǫ, F ′ ) /ǫ q 0 . . . . . . q − T ′ ( ǫ, A ) / + $ . . . . . . ( n, ǫ ) /ǫ ( ǫ, n ) /ǫ Stack . . . ( ǫ, F ′ ) /MT . . . q ∗ ( ǫ, M ) / ∗ ( ǫ, S ) /E $ q / q n ( ǫ, F ′ ) /MT ( ǫ, E ) /TT ′ ( ǫ, M ) // ( ǫ, T ) /FF ′ ( ǫ, F ) /n 23 / 74
( ǫ, S ) /E $ Parsing LL(1) Lang. using DPDA ( ǫ, E ) /TT ′ ( ǫ, T ) /FF ′ ( ǫ, T ′ ) /ǫ ( ǫ, F ′ ) /ǫ ( ǫ, F ) / ( E ) Input : ( ǫ, T ′ ) /ǫ ( ǫ, F ′ ) /ǫ q ( +(4 − 1) ∗ 3$ q $ ( ǫ, T ′ ) /AE ( ǫ, $) /ǫ ($ , ǫ ) /ǫ q ) ( ǫ, F ′ ) /ǫ . . . ( ǫ, A ) / + . . . . . . . . . q + . . .. . . ( ǫ, T ′ ) /AE F ′ ( ǫ, F ′ ) /ǫ q 0 . . . . . . q − T ′ ( ǫ, A ) / + $ . . . . . . ( n, ǫ ) /ǫ ( ǫ, n ) /ǫ Stack . . . ( ǫ, F ′ ) /MT . . . q ∗ ( ǫ, M ) / ∗ ( ǫ, S ) /E $ q / q n ( ǫ, F ′ ) /MT ( ǫ, E ) /TT ′ ( ǫ, M ) // ( ǫ, T ) /FF ′ ( ǫ, F ) /n 24 / 74
( ǫ, S ) /E $ Parsing LL(1) Lang. using DPDA ( ǫ, E ) /TT ′ ( ǫ, T ) /FF ′ ( ǫ, T ′ ) /ǫ ( ǫ, F ′ ) /ǫ ( ǫ, F ) / ( E ) Input : ( ǫ, T ′ ) /ǫ ( ǫ, F ′ ) /ǫ q ( +(4 − 1) ∗ 3$ q $ ( ǫ, T ′ ) /AE ( ǫ, $) /ǫ ($ , ǫ ) /ǫ q ) ( ǫ, F ′ ) /ǫ . . . ( ǫ, A ) / + . . . . . . . . . q + . . .. . . ( ǫ, T ′ ) /AE F ′ ( ǫ, F ′ ) /ǫ q 0 . . . . . . q − T ′ ( ǫ, A ) / + $ . . . . . . ( n, ǫ ) /ǫ ( ǫ, n ) /ǫ Stack . . . ( ǫ, F ′ ) /MT . . . q ∗ ( ǫ, M ) / ∗ ( ǫ, S ) /E $ q / q n ( ǫ, F ′ ) /MT ( ǫ, E ) /TT ′ ( ǫ, M ) // ( ǫ, T ) /FF ′ ( ǫ, F ) /n 25 / 74
Recommend
More recommend