1 / 10

Parsers

Parsers. Grammars can be used to describe a set of strings (Language) that have certain properties. Moreover, the productions in a grammar can be used to “generate” strings in the specified language.

rworrell
Télécharger la présentation

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. Parsers • Grammars can be used to describe a set of strings (Language) that have certain properties. Moreover, the productions in a grammar can be used to “generate” strings in the specified language. • Given a particular string a parser can be used to determine whether or not the string is in the language generated by a specified grammar. A parser can produce a parse tree which provides a derivation for the string. • Concepts • Parse tree • Ambiguous grammars • Recursive descent parsers

  2. Parse Tree • A derivation is conveniently stored in a tree, where internal nodes correspond to syntactic categories and the children of a node correspond to the element of the rhs in the rule that was applied

  3. Grammars • Grammar 1 • <number>  <digit> <number> • <digit>  0|1|2|3|4|5|6|7|8|9 • Grammar 2 • <expr>  <expr> + <expr> • <expr>  <number> • Grammar 3 • <Balanced>  (<Balanced>) <Balanced> • <Balanced>  

  4. Example Parse Tree <number> / \ <digit> <number> | / \ 1 <digit> <number> | | 2 <digit> | 3

  5. Ambiguous Grammars <expr> <expr> / | \ / | \ <expr>+<expr> <expr>+<expr> / | \ / | \ <expr>+<expr> <expr>+<expr>

  6. More Grammars • Grammar 3 • <Balanced>  (<Balanced>) • <Balanced>  <Balanced> <Balanced> • <Balanced>   • Grammar 4 • <Balanced>  (<Balanced>) <Balanced> • <Balanced>  

  7. Recursive Descent Parser • For some grammars it is simple to write a parser that recognizes strings in the language generated by the grammar. • Sometimes it is possible to determine which production to apply based on the next character in the input string. This may not always be possible, but when it is, it is simple to construct a parser.

  8. Problem 1 • Using the grammar • <Balanced>  <Balanced><Balanced> • <Balanced>  (<Balanced>) • <Balanced>  • Show two different parse trees for the input • ()()()

  9. Problem 2 • Show the sequence of calls made by the program in Fig. 11.27 on the inputs • (()()) • Draw the resulting parse tree • What happens with the input • ())(

  10. Problem 3 • Consider the following grammar • <Number>  <Digit><Number>| • <Digit>  0|1|2|3|4|5|6|7|8|9 • Design a recursive-descent parser for this grammar; that is, write a pair of functions, one for <Number> and the other for <Digit>

More Related