1 / 9

Lecture 7 Push Down Automata

Lecture 7 Push Down Automata. Parsers based on Push-Down Automata (PDA) Abstract machine containing : states ,transitions , and a push – down stack The stack distinguishes it from a FSA and allow it to recognize CFG , not just REs Two type of parsers:

linh
Télécharger la présentation

Lecture 7 Push Down Automata

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 7Push Down Automata • Parsers based on Push-Down Automata (PDA) • Abstract machine containing : states ,transitions , and a push – down stack • The stack distinguishes it from a FSA and allow it to recognize CFG , not just REs • Two type of parsers: • Top –down parser builds parse tree from start non-terminal • Alt each stage , parser guesses which production to apply to extend the partially built tree towards leaves. • Bottom –up parser builds parse tree by starting at the terminal and working up the tree.

  2. Push Down Automata ,cont’d • FSA # Stack • Instead of only looking at the next token ,it can use the stack to choose nest state • Formally ,PDA=(K,A,H,D,q0,z0,F) • K is finite set of states • A is finite input alphabet (tokens) • H is finite stack alphabet • q0 E K is the start state • Z0 E H is initial symbol on stack • F is set of final states • D is transition function mapping (q ,a, z) - ((p1,h1).(p2,h2),….) New state New TOS State Input TOS

  3. PDA Example • Recognize balance parentheses • K = (1,2) , A = [( , ) , eof ] , H= { ( , start }, q0 = 1 , z0 = start , F = 2 input = eof , TOS = Start D = [ (1 , eof , start)] -> (2, ). pop (1 , ‘(‘ , λ )---> (1 , ‘(‘ ) push (1 , ‘)’ , ‘(‘)---> (1 , λ ) input = ‘(‘ push ‘ (‘ State Input TOS New State New TOS input = ‘)’ TOS = ‘(‘ pop D(1,eof,start) = (2, λ) D(1 , ‘(‘ , λ ) = (1 , ‘(‘) D(1 , ‘)’ , ‘(‘ ) = (1 , λ ) 2 1

  4. PDA programming • Common Idioms • D (q , λ , z ) = (p ,h) • When in state q with TOS z ,change to state p replace z with h without consuming input • D (q , a , z) = (p, λ ) • When in state q with TOS z and input a , read the input , pop the stack , and goto state p • A PDA accepts when moves into a final state • PDA can be non-deterministic • Accept if there exists a sequence of moves that load to a final state (with empty stack )

  5. Equivalence Theorem • For every CFG G, it is possible to build a NDPDA M such that μ(G) = μ(M) • PDA will perform a leftmost derivation • Each time it processes a non- terminal , PDA non deterministically chooses a production to apply • Stack holds partially derived string • If TOS is non-terminal X • Ignore input token • Pop stack and push a , where X  a is a production • If TOS is terminals • If current input token is c , then pop stack • Otherwise , reject input

  6. Parsing Example • Grammar for balanced parentheses : • S  () | (S) | SS • Input Stack Action ()(()) S pop. push SS ()(()) SS pop. push () ()(()) ()S pop. & read )(()) )S pop. & read (()) S pop. Push (S) (()) (S) pop. & read ()) S) pop. Push () ()) ()) pop & read )) )) pop & read ) ) pop & read

  7. Construction of PDRAM • M has single state q • A is set of terminals of grammars • H = A U N (non – terminals of G ) • Q0 = q and z0 = S (start symbol) • F = (q) • Construct a transition for each terminal of G • D (q , c , c)  (q , λ) • Construct a transition for each production X  a of G • D (q , λ , X)  (q , a) • M is non deterministic if anon terminal occurs on the LHS of more than one production • If deterministic the grammar is finite

  8. Example • S  () | (S) | SS • 1 D (q, ‘(‘ , ‘( )  (q, λ ) 2 D (q, ‘)’,’)’)  (q, λ ) 3 D(q, λ , S)  (q, ‘()’) 4 D (q, λ , S)  (q, ‘(S)’) 5 D (q, λ, S)  (q, ‘SS’) • Input can be rejected because of bad choice • Input Stack Action ()() S rule 4 ()() (S) rule 1 )() S) rule 3 )() ()) ??? • Input can be rejected because it is not in μ(G) • ) S ????

  9. Building Parsers • Unfortunately , NDPDA are more powerful than DPDA • Cannot mechanically transform NDPDA into a machine that can be efficiently simulated • However , another algorithm (Early ) can parse an arbitrary CFG in O(n2) • But, for certain classes of CFG, can build deterministic parsers that run in O(n) time

More Related