Recursive descent parsing - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Recursive descent parsing

Description:

Pops the top three things from the stack, assembles them into a tree, and pushes ... I wrote a number of helper methods for the Parser and for the ParserTest classes ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 12
Provided by: davidlm3
Category:
Tags: descent | of | parsing | pops | recursive | the | top

less

Transcript and Presenter's Notes

Title: Recursive descent parsing


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
  • ltif statementgt if ltconditiongt ltblockgt
  • 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 ltconditiongt, which parses a
    command and puts it on the stack
  • Stack now contains if ltconditiongt (top is
    on right)
  • Calls the parser for ltblockgt, which parses a
    block and puts it on the stack
  • Stack now contains if ltconditiongt ltblockgt
  • 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
    ltcommandgt
  • ltcommandgt ltmovegt ltexpressiongt lteolgt
    "penup" lteolgt "pendown" lteolgt
    "color" ltcolornamegt lteolgt
    ...
  • In my code, I broke that into multiple methods
  • ltcommandgt ltmove commandgt ltpenup
    commandgt ltpendown commandgt
    ltcolor commandgt ...

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 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
Write a Comment
User Comments (0)
About PowerShow.com