Presentation is loading. Please wait.

Presentation is loading. Please wait.

26-Jun-15 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.

Similar presentations


Presentation on theme: "26-Jun-15 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."— Presentation transcript:

1 26-Jun-15 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: while statement ::= “while” The parse method for a does this: Calls the Tokenizer, which returns a “while” token Makes the “while” into a Tree, which it puts on the stack Calls the parser for, which parses a condition and puts a Tree representation of that condition on the stack Stack now contains: “while” (stack “top” is on the right), where stands for some created Tree Calls the parser for, which parses a block and puts a Tree representation of that block on the stack Stack now contains: “while” Pops the top three things from the stack, assembles them into a Tree representing a while statement, and pushes this Tree onto the stack

4 Sample Java code public boolean whileCommand() { if (keyword("while")) { if (condition()) { if (block()) { makeTree(3, 2, 1); // or some such return true; } } error("Error in \"while\" statement"); } return false; }

5 Fancier error messages public boolean whileCommand() { if (keyword("while")) { if (condition()) { if (block()) { makeTree(3, 2, 1); // or some such return true; } error("Error in \"while\" block"); } error("Error in \"while\" condition"); } return false; }

6 Alternative code public boolean whileCommand() { if (keyword("while") && condition() && block()) { makeTree(3, 2, 1); // or some such return true; } return false; } No room for an error condition in this code

7 Alternative code with one message public boolean whileCommand() { if (keyword("while")) { if (condition()) && (block()) { makeTree(3, 2, 1); // or some such return true; } error("Error in \"while\" statement"); } return false; }

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

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 Parser and for the ParserTest classes One very useful method is tree, in the ParserTest class tree just takes three Objects and builds a tree from them This method lets me build parse trees for use in assertEquals tests Another is assertStackTop, which is just private void assertStackTop(Tree bt) { assertEquals(bt, parser.stack.peek()); } Examples: Tree condition = tree("=", "2", "2"); assertStackTop(tree("if", condition, "list"));

11 The End


Download ppt "26-Jun-15 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."

Similar presentations


Ads by Google