Parser Hints - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Parser Hints

Description:

All boolean Recognizer methods should continue to return boolean values, but in ... way because, although it means trickier arithmetic in makeTree, it simplifies ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 13
Provided by: davidlm3
Category:
Tags: hints | parser | trickier

less

Transcript and Presenter's Notes

Title: Parser Hints


1
Parser Hints
2
The Stack
  • To turn a Recognizer into a Parser, we need
    the use of a Stack
  • All boolean Recognizer methods should continue to
    return boolean values, but in addition, if they
    succeed they should leave a Tree on the stack
  • The stack should be an instance variable of the
    Parser
  • Heres one easy way to do it
  • Create a new Parser(String s) for each String you
    want to use, and call some parsing method (term,
    command, ...) for that particular String

3
Recognizer helper methods
  • private boolean number() return
    nextTokenMatches(Token.NUMBER)
  • private boolean name() return
    nextTokenMatches(Token.NAME)
  • private boolean keyword(String expectedKeyword)
    return nextTokenMatches(Token.KEYWORD,
    expectedKeyword)
  • private boolean symbol(String expectedSymbol)
    return nextTokenMatches(Token.SYMBOL,
    expectedSymbol)

4
nextTokenMatches
  • private boolean nextTokenMatches(int type)
    Token t tokenizer.next() if (t.getType()
    type) return true else
    tokenizer.pushBack(t) return false
  • private boolean nextTokenMatches(int type, String
    value) Token t tokenizer.next() if
    (type t.getType() value.equals(t.getValue())
    ) return true else
    tokenizer.pushBack(t) return false

5
Revised nextTokenMatches
  • private boolean nextTokenMatches(int type)
    Token t tokenizer.next() if (t.getType()
    type) stack.push(new Tree(t))
    return true else tokenizer.pushBack(t
    ) return false
  • private boolean nextTokenMatches(int type, String
    value) Token t tokenizer.next() if
    (type t.getType() value.equals(t.getValue())
    ) stack.push(new Tree(t))
    return true else tokenizer.pushBack(t)
    return false

6
comparator
  • ltcomparatorgt "lt" "" "gt
  • public boolean comparator() if
    (symbol("lt")) return true if (symbol(""))
    return true if (symbol("gt")) return true
    return false
  • Whatever a comparator is found, one Tree node
    (with the Token representing that comparator as
    its value) is left on the stack
  • Since we recognized one thing, and we leave one
    thing on the stack, this method does not need to
    be changed in any way

7
while command
  • public boolean whileCommand() if
    (keyword("while")) if (condition())
    if (block())
    return true
    error("Error in \"while\" statement")
    return false

makeTree(3, 2, 1)
The makeTree method will take three things off
the stack and replace them with one thing
8
Counting
  • I like to count this way 1 2
    3 while condition block
  • This way is easier to implement 2
    1 0 while condition block
  • I prefer the first way because, although it means
    trickier arithmetic in makeTree, it simplifies
    counting everywhere else

9
Accessing the Stack
  • A stack, as a stack, has no methods for accessing
    the nth thing from the top, but...
  • class Stack extends Vector
  • ...and Vector has an elementAt(int index) operator
  • This represents a stack with five things in it
  • The e is at the top of the stack
  • This means that, with some annoying arithmetic,
    you can access any element of the stack you like,
    counting elementAt(vector.size() - 1) as the top

10
Recursion in BNF
  • lttermgt ltfactorgt lttermgt ltfactorgtis bad,
    because of the left recursion
  • Implemented in the obvious way, it would cause
    our program to go into an infinite recursion
  • lttermgt ltfactorgt ltfactorgt lttermgthas
    another problem
  • Implemented in the obvious way, it will build a
    tree of the wrong shape
  • lttermgt ltfactorgt ltfactorgt works well
  • Implemented in the obvious wayiteratively, not
    recursivelyit builds a tree of the correct shape

11
term()
  • public boolean term() if (!factor()) return
    false while (multiplyOperator())
    if (!factor()) error("No term after '' or
    '/'") makeTree(2, 3, 1)
    return true

12
The End
Write a Comment
User Comments (0)
About PowerShow.com