jmathlib.core.interpreter
Class Parser

java.lang.Object
  extended by jmathlib.core.interpreter.RootObject
      extended by jmathlib.core.interpreter.Parser
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, ErrorCodes, TokenConstants

public class Parser
extends RootObject
implements TokenConstants, ErrorCodes

The parser uses the Lexer to produce a list of tokens which it builds into an abstract syntax tree.

See Also:
Serialized Form

Field Summary
private  Token currentToken
          the token which is currently processed
 java.lang.String evaluationLockWords
           
private  LexicalAnalyser lex
          The lexical analyser which separates an expression into tokens
private  Token peekNextToken
          the token which will be parsed next
private  boolean peekNextTokenBool
          indicator wether the next token to be parsed was already requested from the lexical analyser
 
Fields inherited from interface jmathlib.core.constants.TokenConstants
ADDSUB_PRIORITY, AND_PRIORITY, ANDAND_PRIORITY, ASSIGN_PRIORITY, BRACKET_PRIORITY, COMPARE_PRIORITY, CONCAT, MATRIX, MULDIV_PRIORITY, OR_PRIORITY, OROR_PRIORITY, PARAMETER, POWER_PRIORITY, RELATION_PRIORITY, SINGLE, ttDelimiter, ttNotSet, ttOperand, ttOperator
 
Fields inherited from interface jmathlib.core.constants.ErrorCodes
ERR_BRACKET_OPEN, ERR_BRACKET_ORDER, ERR_FUNCTION_NOT_FOUND, ERR_FUNCTION_NOT_IMPLEMENTED, ERR_INSUFFICIENT_PARAMETERS, ERR_INVALID_PARAMETER, ERR_LVALUE_REQUIRED, ERR_MATRIX_SINGULAR, ERR_NOT_SQUARE_MATRIX, ERR_OPNOTSUPPORTED, ERR_TOO_MANY_PARAMETERS, ERR_USER_ERROR, ERR_VARIABLE_NOTDEFINED, OK, OK_FUNCTION_PROCESS
 
Constructor Summary
Parser()
          default constructor - creates the Parser object with an empty string
 
Method Summary
 Token getNextToken()
          get next token from the lexical analyser
private  Token getNextToken(int type)
          get next token from the lexical analyser
 java.lang.String getScannedLineOfCode()
          return the currently scanned line of code to identify the possible errors.
private  boolean isDisplayResultToken(Token token)
          return true if an expression is terminated by something else ", EOF"
private  boolean isExpectedDelimiter(Token token, char c)
          return true if the token is a delimiter and is the specified char, e.g. ,;(){}
private  boolean isExpectedDelimiter(Token token, java.lang.String c)
           
private  OperandToken parseArithExpression(int deliTyp)
          parse the expression contained in exp, this method is also called recursivly to find the parameters of functions and matrices (e.g. something like 2+3+(2*4) )
private  OperandToken parseAssignmentOperator(Token currentToken, java.util.Stack operandStack)
          parse <=> (e.g. a=3 or a=2+3+4 or [a,b]=step(x) )
private  OperandToken parseBinaryOperator(Token nextToken, java.util.Stack operandStack)
          parse (e.g. 3+4, 3*4, 3/4, 1+2*3+4*5)
private  OperandToken parseCase()
          parsing of "case" commands
private  OperandToken parseCellArray()
          this method parses cell arrays (e.g. a={1, "barfoo", rand(3)} )
private  OperandToken parseColonOperator(Token nextToken, java.util.Stack operandStack)
          parse :: (e.g. 2:3:8 -> [2,5,8]
private  OperandToken parseCommandList()
          parse a list of commands (e.g. a=2+3;if(a=2){b=3};c=sin(pi) )
private  OperandToken parseDefault()
          parse "default" or "otherwise" command of switch
private  OperandToken parseDotOperator(java.util.Stack operandStack, int deliTyp)
          parse expressions of the form value.function()
 OperandToken parseExpression(java.lang.String expression)
          Parse an expression and return an operand tree of the expression
private  OperandToken parseFor()
          this method parses for loops
private  OperandToken parseFunctionAndParameters(OperandToken nextToken, OperandToken firstParam)
          parse (,,....)
private  OperandToken parseIf()
          parse (){} else {}
private  OperandToken parseMatrix()
          this method parses matrices (e.g. a=[1,2,3;4,5,6] or a=[1 2 3])
 OperandToken parseRemainingExpression()
          parse remaining tokens of a m-function file or m-script file
private  OperandToken parseSingle(java.util.Stack operandStack, int deliTyp)
          parse the expression contained in exp, this method is also called recursivly to find the parameters of functions and matrices
private  OperandToken parseSwitch()
          parse switch()
private  OperandToken parseUnaryOperator(Token nextToken, java.util.Stack operandStack)
          parse (e.g. 3!
private  OperandToken parseWhile()
          parse while(something) { do something }
 Token peekNextToken()
          get next Token from lexical analyser, without moving forward
private  Token peekNextToken(int type)
          get next Token from lexical analyser, without moving forward
 void setExpression(java.lang.String expression)
          set expression of a m-function file or m-script file.
 
Methods inherited from class jmathlib.core.interpreter.RootObject
clone, toString
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

currentToken

private Token currentToken
the token which is currently processed


peekNextToken

private Token peekNextToken
the token which will be parsed next


peekNextTokenBool

private boolean peekNextTokenBool
indicator wether the next token to be parsed was already requested from the lexical analyser


lex

private LexicalAnalyser lex
The lexical analyser which separates an expression into tokens


evaluationLockWords

public java.lang.String evaluationLockWords
Constructor Detail

Parser

public Parser()
default constructor - creates the Parser object with an empty string

Method Detail

parseExpression

public OperandToken parseExpression(java.lang.String expression)
Parse an expression and return an operand tree of the expression

Parameters:
expression - = a vector of tokens
Returns:
the parsed expressions as a tree of operands

setExpression

public void setExpression(java.lang.String expression)
set expression of a m-function file or m-script file. This method is used for parsing m-scripts/functions.

Parameters:
expression - = code of a function as a string

parseRemainingExpression

public OperandToken parseRemainingExpression()
parse remaining tokens of a m-function file or m-script file

Returns:
the parsed expresssion

getScannedLineOfCode

public java.lang.String getScannedLineOfCode()
return the currently scanned line of code to identify the possible errors. (e.g. a=sin(3]+4 is an eroror and returns a=sin(3] to display to the user

Returns:
the currently scanned line of code

getNextToken

public Token getNextToken()
get next token from the lexical analyser

Returns:
the next token

getNextToken

private Token getNextToken(int type)
get next token from the lexical analyser

Parameters:
indicator - if lexical analyser should consider whitespaces or not
Returns:
the next token

peekNextToken

public Token peekNextToken()
get next Token from lexical analyser, without moving forward

Returns:
the next token

peekNextToken

private Token peekNextToken(int type)
get next Token from lexical analyser, without moving forward

Parameters:
indicator - if lexical analyser should consider whitespaces or not
Returns:
the next token

parseSingle

private OperandToken parseSingle(java.util.Stack operandStack,
                                 int deliTyp)
parse the expression contained in exp, this method is also called recursivly to find the parameters of functions and matrices

Parameters:
deliTyp - - shows whether parser is parsing expressions or a matrix
Returns:
the parsed operand/expression

parseArithExpression

private OperandToken parseArithExpression(int deliTyp)
parse the expression contained in exp, this method is also called recursivly to find the parameters of functions and matrices (e.g. something like 2+3+(2*4) )

Parameters:
deliTyp - indicates if an argument is part of a matrix (whitespaces are treated as delimiters in this case)
Returns:
expression (e.g. 3 or 2+3 or sin(cos(3)) )

parseCommandList

private OperandToken parseCommandList()
parse a list of commands (e.g. a=2+3;if(a=2){b=3};c=sin(pi) )

Returns:
OperantToken tree of operand tokens (e.g. a=2+3)

parseAssignmentOperator

private OperandToken parseAssignmentOperator(Token currentToken,
                                             java.util.Stack operandStack)
parse <=> (e.g. a=3 or a=2+3+4 or [a,b]=step(x) )


parseColonOperator

private OperandToken parseColonOperator(Token nextToken,
                                        java.util.Stack operandStack)
parse :: (e.g. 2:3:8 -> [2,5,8]


parseBinaryOperator

private OperandToken parseBinaryOperator(Token nextToken,
                                         java.util.Stack operandStack)
parse (e.g. 3+4, 3*4, 3/4, 1+2*3+4*5)


parseUnaryOperator

private OperandToken parseUnaryOperator(Token nextToken,
                                        java.util.Stack operandStack)
parse (e.g. 3! or !(a<3))


parseDotOperator

private OperandToken parseDotOperator(java.util.Stack operandStack,
                                      int deliTyp)
parse expressions of the form value.function()


parseFunctionAndParameters

private OperandToken parseFunctionAndParameters(OperandToken nextToken,
                                                OperandToken firstParam)
parse (,,....) (e.g. min(3,4) )


parseIf

private OperandToken parseIf()
parse (){} else {}


parseSwitch

private OperandToken parseSwitch()
parse switch()


parseCase

private OperandToken parseCase()
parsing of "case" commands

Returns:

parseDefault

private OperandToken parseDefault()
parse "default" or "otherwise" command of switch

Returns:

parseWhile

private OperandToken parseWhile()
parse while(something) { do something }


parseFor

private OperandToken parseFor()
this method parses for loops


parseMatrix

private OperandToken parseMatrix()
this method parses matrices (e.g. a=[1,2,3;4,5,6] or a=[1 2 3])


parseCellArray

private OperandToken parseCellArray()
this method parses cell arrays (e.g. a={1, "barfoo", rand(3)} )


isDisplayResultToken

private boolean isDisplayResultToken(Token token)
return true if an expression is terminated by something else ", EOF"


isExpectedDelimiter

private boolean isExpectedDelimiter(Token token,
                                    char c)
return true if the token is a delimiter and is the specified char, e.g. ,;(){}


isExpectedDelimiter

private boolean isExpectedDelimiter(Token token,
                                    java.lang.String c)

FOOTER

BOTTOM