1 / 11

Recursive descent parsing

Recursive descent parsing. The Stack. One easy way to do recursive descent parsing is to have each parse method take the tokens it needs, build a parse tree , and put the parse tree on a global stack Write a parse method for each nonterminal in the grammar

dionysius
Download Presentation

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

  2. The Stack • One easy way to do recursive descent parsing is to have each parse method take the tokens it needs, build a parse tree, and put the parse tree on a global stack • Write a parse method for each nonterminal in the grammar • Each parse method should get the tokens it needs, and only those tokens • Those tokens (usually) go on the stack • Each parse method may call other parse methods, and expect those methods to leave their results on the stack • Each (successful) parse method should leave one result on the stack

  3. Example: if statement • <if statement> ::= “if” <condition> <block> • The parse method for an if statement: • Calls the Tokenizer, which returns an “if” token • Makes the “if” into a binary tree, which it puts on the stack • Calls the parser for <condition>, which parses a command and puts it on the stack • Stack now contains: “if” <condition> (“top” is on right) • Calls the parser for <block>, which parses a block and puts it on the stack • Stack now contains: “if” <condition> <block> • Pops the top three things from the stack, assembles them into a tree, and pushes this tree onto the stack

  4. Sample Java code • public boolean ifCommand() { if (keyword("if")) { if (condition()) { if (block()) { makeTree(3, 2, 1); return true; } } error("Error in \"if\" statement"); } return false;}

  5. Fancier error messages • public boolean ifCommand() { if (keyword("if")) { if (condition()) { if (block()) { makeTree(3, 2, 1); return true; } error("Error in \"if\" block"); } error("Error in \"if\" condition"); } return false;}

  6. Alternative code • public boolean ifCommand() { if (keyword("if") && condition() && block()) { makeTree(3, 2, 1); return true; } return false;} • No room for an error condition in this code

  7. Alternative code with one message • public boolean ifCommand() { if (keyword("if")) { if (condition()) && (block()) { makeTree(3, 2, 1); return true; } error("Error in \"if\" statement"); } return false;}

  8. Parser methods • In the BNF, I have one long definition for <command> • <command> ::= <move> <expression> <eol> | "penup" <eol> | "pendown" <eol> | "color" <colorname> <eol> ... • In my code, I broke that into multiple methods • <command> ::= <move command> | <penup command> | <pendown command> | <color command> ...

  9. My command() method • public boolean command() { if (move()) return true; if (penup()) return true; if (pendown()) return true; ...

  10. My helper methods • I wrote a number of helper methods for the Parserand for the ParserTest classes • One very useful method is bt, in the ParserTest class • This method lets me build parse trees for use in assertEquals tests • Another is assertStackTop, which is just • private void assertStackTop(BinaryTree bt) { assertEquals(bt, parser.stack.peek());} • Example: • BinaryTree condition = bt("=", "2", "2");assertStackTop(bt("if", condition, "list"));

  11. The End

More Related