150 likes | 371 Vues
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.
E N D
Recursive Descent Parsing Module 08.1COP4020 – 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. 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}
Parse table Can parse using this table. Tedious (Both parsing and PT construction)
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
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.
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.
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
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.
proc P; case Next Token of T_(: Read(T_(); E(); Read(T_)); T_id: Read(T_id); otherwise Error; end; end; Recursive descent parser
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.
Recursive Descent Parsing Manually coded parser. PT information built into the code. Suitable for LL(1) grammars. summary