1 / 14

Top-Down Parsing

Top-Down Parsing. CPSC 388 Ellen Walker Hiram College. From Last Week. Push Down Automata recognize CFL’s State machine augmented with stack Transition is c,P;S where c is character read, P is symbol popped, and S is symbol pushed. . More From Last Week.

arlais
Télécharger la présentation

Top-Down Parsing

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. Top-Down Parsing CPSC 388 Ellen Walker Hiram College

  2. From Last Week • Push Down Automata recognize CFL’s • State machine augmented with stack • Transition is c,P;S where c is character read, P is symbol popped, and S is symbol pushed.

  3. More From Last Week • Every CFL has a PDA that accepts it: • Non-terminals and terminals are pushed on stack • For every rule, pop the LHS, push the RHS • E.g. e,S;xSy (assume top is at right) • When an input character matches the top of the stack, eat the character and pop • E.g. a,a;e

  4. Every PDA has a CFL... • Idea: • A goal of a PDA is <current state, stack-top, future state> • Overall goal is <Init,e,Final> • For each transition p -- x,y;z --> q make a rule <p,y,r> -> x<q,z,r> • Also make rules <p,e,p> -> e • Result: Correct grammar , many rules!

  5. Why do we care? • We proved that every PDA has a corresponding CFL (by algorithm) • We proved that every CFL has a corresponding PDA (by earlier algorithm) • Therefore, PDA’s and CFL’s are equivalent in the class of languages they accept (Context Free Languages)

  6. Top Down Parsing • Begin with the start symbol S (top of tree) • Until the string has been read (parsed): • Choose a rule for the first non-terminal • Read characters for terminals and push symbols for non-terminals • If the stack is empty, or no rule can “eat” the current character, parse error!

  7. But how to choose the rule? • Backtracking: • Systematically try everything • When you get stuck, change the most recent choice (“backtrack”) • If all choices are tried, then syntax error • Prediction • Look at current and future characters to pick the right choice

  8. Recursive Descent Parsing • Every non-terminal is a function For each RHS of this symbol Call non-terminals in sequence, reading tokens as appropriate If the whole RHS worked, return

  9. Example: Rule to Function • exp -> factor + factor | factor Int exp(){ int res = factor(); if (!res) return 0; if (nexttoken()== ‘+’) res=factor(); return true; }

  10. Another Example • S-> aSa | bSb | e int S(){ if (nexttoken()==‘a’ && S() && nexttoken()==‘a’) return true if (nexttoken()==‘b’ && S() && nexttoken()==‘b’) return true; return true; }

  11. What we’re not showing • If a parse fails, many input characters may have been read on the way to the failed parse. These must be “unread” before the next try. • This involves a bit of bookkeeping, but value parameters help • Recursive Descent is best for simple languages without too many choices

  12. Another Problem • Consider: • S -> Sab | e int S(){ if ( S() && nexttoken()==‘a’ && nexttoken()==‘b’) return true return true; }

  13. Adding Prediction • No lookahead: case statement based on first character (e.g. “exp” in book) • Adding lookahead: consider two sets • All possible first characters of S • All possible characters that can follow S

  14. Using First & Follow Sets • Use an explicit stack as in the CFL to PDA algorithm. • If the current character is in First (S), then it’s OK to pop S and push its rule beginning with this character • If the current character is in Follow (S) then it’s OK to pop S without pushing (i.e. use S-> e rule)

More Related