1 / 12

Recursive Descent Parsing

Recursive Descent Parsing. Module 08.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez. Recursive Descent Parsing Manually coded parser PT information built into the code Suitable for LL(1) grammars. topics. Grammars (So FAR). Our “model” PL grammar.

dbrewer
Télécharger la présentation

Recursive Descent 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. Recursive Descent Parsing Module 08.1COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez

  2. Recursive Descent Parsing Manually coded parser PT information built into the code Suitable for LL(1) grammars topics

  3. Grammars (So FAR) Our “model” PL grammar. Modified, LL(1) grammar. S →begin SL end {begin} → id := E; {id} SL → SL S {begin,id} →S {begin,id} E → E+T {(,id} → T {(,id} T → P*T {(,id} → P {(,id} P → (E) {(} → id {id} S → begin SL end {begin} → id := E ; {id} SL → S Z {begin,id} Z →S Z {begin,id} → {end} E → T Y {(,id} Y → + T Y {+} → {;,)} T → P X {(,id} X → * T {*} → {;,+,)} P → (E) {(} → id {id}

  4. Parse table Can parse using this table. Tedious (Both parsing and PT construction)

  5. Top-down parsing strategy, for LL(1) grammars. One procedure per nonterminal. Stack contents embedded in recursive call sequence. Each procedure “commits” to one production, based on the next input symbol, and the select sets. Good technique for hand-written parsers. Recursive Descent Parsing

  6. proc S; caseNext_Tokenof T_begin : Read(T_begin); SL(); Read(T_end); T_id : Read(T_id); Read(T_:=); E(); Read(T_;); otherwise Error; end; end; Recursive descent parser “Next_Token” is the upcoming token. Assume it is initialized. “Read (T_X)” verifies that the upcoming token is X, and consumes it.

  7. proc SL; S(); Z(); end; proc Z; case Next Token of T_begin, T_id: S();Z(); T_end: ; otherwise Error; end; end; Recursive descent parser SL: Technically, should insist Next Token ∊{ T_begin, T_id}, but S will do that anyway. Checking earlier or later ? Aid error recovery.

  8. Recursive descent parser proc E; T(); Y(); end; proc Y; if Next Token = T_+ then Read(T_+); T(); Y(); end; E: Technically, should insist Next Token ∊{ T_id, T_( }, but T will do that anyway. Y: Could have used a case statement

  9. Recursive descent parser proc T; P(); X(); end; proc X; if Next Token = T_* then Read(T_*); T(); end; T: Could have checked for T_( and T_id.. X: Could have used a case statement.

  10. proc P; case Next Token of T_(: Read(T_(); E(); Read(T_)); T_id: Read(T_id); otherwise Error; end; end; Recursive descent parser

  11. Tracing the parser • Not so easy (as table-driven) • In table-driven parser, stack keeps all future (predicted) symbols. Stack (towards the end): • Next item is obvious. • In RD parser, stack keeps recursive calling sequence.

  12. Recursive Descent Parsing Manually coded parser. PT information built into the code. Suitable for LL(1) grammars. summary

More Related