Class Lexer

java.lang.Object
  extended byLexer

public class Lexer
extends java.lang.Object

This class is aimed at lexical analysis of a string representing an expression that may contain unsigned integers, variable names, operators, parentheses and whitespaces.


Field Summary
static int TT_END
          A token type that identifies the end of input string.
static int TT_LEFT_PAR
          A token type that identifies the left parenthesis.
static int TT_NUMBER
          A token type that identifies a number.
static int TT_OPERATOR
          A token type that identifies an operator ('+', '-' or '*').
static int TT_RIGHT_PAR
          A token type that identifies the right parenthesis.
static int TT_UNKNOWN
          Unknown token.
static int TT_VAR
          A token type that identifies a variable.
 
Constructor Summary
Lexer(java.lang.String str)
          Creates a lexer.
 
Method Summary
 int currentPosition()
          Returns the curent position in the string scanned.
 int nextToken()
          Returns the type of the next token.
 int number()
          Returns the integer representing a number if the last call of nextToken() returned TT_NUMBER.
 char operator()
          Returns the character representing an operator if the last call of nextToken() returned TT_OPERATOR.
 java.lang.String var()
          Returns the string representing a variable if the last call of nextToken() returned TT_VAR.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TT_END

public static final int TT_END
A token type that identifies the end of input string.

See Also:
Constant Field Values

TT_UNKNOWN

public static final int TT_UNKNOWN
Unknown token.

See Also:
Constant Field Values

TT_NUMBER

public static final int TT_NUMBER
A token type that identifies a number.

See Also:
Constant Field Values

TT_VAR

public static final int TT_VAR
A token type that identifies a variable.

See Also:
Constant Field Values

TT_OPERATOR

public static final int TT_OPERATOR
A token type that identifies an operator ('+', '-' or '*').

See Also:
Constant Field Values

TT_LEFT_PAR

public static final int TT_LEFT_PAR
A token type that identifies the left parenthesis.

See Also:
Constant Field Values

TT_RIGHT_PAR

public static final int TT_RIGHT_PAR
A token type that identifies the right parenthesis.

See Also:
Constant Field Values
Constructor Detail

Lexer

public Lexer(java.lang.String str)
Creates a lexer.

Parameters:
str - a string representing an expression to be scanned
Throws:
java.lang.NullPointerException - if the parameter str is null
Method Detail

nextToken

public int nextToken()
Returns the type of the next token.


var

public java.lang.String var()
Returns the string representing a variable if the last call of nextToken() returned TT_VAR.

Throws:
java.lang.IllegalStateException - if the last call of nextToken() did not return TT_VAR
See Also:
nextToken()

number

public int number()
Returns the integer representing a number if the last call of nextToken() returned TT_NUMBER.

Throws:
java.lang.IllegalStateException - if the last call of nextToken() did not return TT_NUMBER
See Also:
nextToken()

operator

public char operator()
Returns the character representing an operator if the last call of nextToken() returned TT_OPERATOR.

Throws:
java.lang.IllegalStateException - if the last call of nextToken() did not return TT_OPERATOR
See Also:
nextToken()

currentPosition

public int currentPosition()
Returns the curent position in the string scanned.



Hosted by uCoz