1 / 7

Recursive Descent Parsers

Recursive Descent Parsers. Lecture 6 Mon, Jan 31, 2005. Recursive Descent Parser. Each nonterminal in the grammar is implemented as a function. Begin with the start symbol S of the grammar by calling the function S() .

dorit
Download Presentation

Recursive Descent Parsers

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 Parsers Lecture 6 Mon, Jan 31, 2005

  2. Recursive Descent Parser • Each nonterminal in the grammar is implemented as a function. • Begin with the start symbol S of the grammar by calling the function S(). • Based on the first token received, apply the appropriate grammar rule for S. • Continue in this manner until S is “satisfied.”

  3. Recursive Descent Parsers • The first Pascal compiler used a recursive descent parser. • Recursive descent parsers have the benefit of being very simple. • However, • Error-recovery is difficult. • They are not able to handle as large a set of grammars as other parsing methods.

  4. Error recovery • When a syntax error occurs, in order for the compiler to recover, it usually has to discard the last few tokens, move to the end of the line, and resume. • In a recursive descent parser, discarding tokens involves returning from several nested function calls. • In a table-driven parser, discarding tokens requires simply clearing part of the stack.

  5. Other Disadvantages • The grammars that can be parsed by this method are called LL grammars. • We will see that LL grammars form a proper subset of the LR grammars (grammars that can be parsed by LR algorithms). • Most modern compilers use LR algorithms. • LR(0), LR(1), or LALR. • CUP uses LALR.

  6. Example: Recursive Descent • Write a parser for the following grammar. SifCthenS ; | whileCdoS ; | id = num | id++ Cid == num | id != num • S represents a statement and C represents a condition. • Example: IfWhileParser

  7. Example: Recursive Descent • Modify the previous example by adding the production S' SS' |  • S' represents a sequence of statements. • Modify the previous example by adding the productions S  doSwhileC ; C  id < num

More Related