recognizer
Class Recognizer

java.lang.Object
  extended by recognizer.Recognizer

public class Recognizer
extends java.lang.Object

This class consists of a number of methods that "recognize" strings composed of Tokens that follow the indicated grammar rules for each method.

Each method may have one of three outcomes:

Version:
February 15, 2010
Author:
David Matuszek

Constructor Summary
Recognizer(java.lang.String text)
          Constructs a Recognizer for the given string.
 
Method Summary
 Tokenizer getTokenizer()
          Returns the Tokenizer being used; needed for testing.
 boolean isAddOperator()
          Tries to recognize an <add_operator>.
 boolean isExpression()
          Tries to recognize an <expression>.
 boolean isFactor()
          Tries to recognize a <factor>.
 boolean isMultiplyOperator()
          Tries to recognize a <multiply_operator>.
(package private)  boolean isSymbol(java.lang.String expectedSymbol)
          Tests whether the next token is the expected symbol.
 boolean isTerm()
          Tries to recognize a <term>.
 boolean isVariable()
          Tries to recognize a <variable>.
 boolean nextTokenMatches(TokenType type)
          Tests whether the next token has the expected type.
 boolean nextTokenMatches(TokenType type, java.lang.String value)
          Tests whether the next token has the expected type and value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Recognizer

public Recognizer(java.lang.String text)
Constructs a Recognizer for the given string.

Parameters:
text - The string to be recognized.
Method Detail

getTokenizer

public Tokenizer getTokenizer()
Returns the Tokenizer being used; needed for testing.

Returns:
The current Tokenizer.

isExpression

public boolean isExpression()
Tries to recognize an <expression>.
<expression> ::= [ <add_operator> ] <term> { <add_operator> <expression> }
A SyntaxException will be thrown if an <add_operator> is present after the first <term> but not followed by a valid <expression>.

Returns:
true if an <expression> is recognized.

isTerm

public boolean isTerm()
Tries to recognize a <term>.
<term> ::= <factor> { <multiply_operator> <term>}
A SyntaxException will be thrown if the <multiply_operator> is present but not followed by a valid <term>.

Returns:
true if a term is recognized.

isFactor

public boolean isFactor()
Tries to recognize a <factor>.
<factor> ::= <name>
           | <number>
           | "x"
           | "y"
           | "(" <expression> ")"
A SyntaxException will be thrown if the opening parenthesis is present but not followed by a valid <expression> and a closing parenthesis.

Returns:
true if a factor is recognized.

isAddOperator

public boolean isAddOperator()
Tries to recognize an <add_operator>.
<add_operator> ::= "+" | "-"

Returns:
true if an <add_operator> is recognized.

isMultiplyOperator

public boolean isMultiplyOperator()
Tries to recognize a <multiply_operator>.
<multiply_operator> ::= "*" | "/"

Returns:
true if a <multiply_operator> is recognized.

isVariable

public boolean isVariable()
Tries to recognize a <variable>.
<variable> ::= <NAME>

Returns:
true if a <variable> is recognized.

isSymbol

boolean isSymbol(java.lang.String expectedSymbol)
Tests whether the next token is the expected symbol. If it is, the token is consumed, otherwise it is not.

Parameters:
expectedSymbol - The String value of the token we expect to encounter next.
Returns:
true if the next token is the expected symbol.

nextTokenMatches

public boolean nextTokenMatches(TokenType type)
Tests whether the next token has the expected type. If it does, the token is consumed, otherwise it is not. This method would normally be used only when the token's value is not relevant.

Parameters:
type - The expected type of the next token.
Returns:
true if the next token has the expected type.

nextTokenMatches

public boolean nextTokenMatches(TokenType type,
                                java.lang.String value)
Tests whether the next token has the expected type and value. If it does, the token is consumed, otherwise it is not. This method would normally be used when the token's value is important.

Parameters:
type - The expected type of the next token.
value - The expected value of the next token; must not be null.
Returns:
true if the next token has the expected type.