Class ShuntingYard
java.lang.Object
com.singularsys.jep.configurableparser.ShuntingYard
- All Implemented Interfaces:
GrammarParser
- Direct Known Subclasses:
LineNumberingShuntingYard
An operator precedence parser based on the shunting yard algorithm.
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static enum
Return result of following methods.static class
Factory creating new ShuntingYard instances. -
Field Summary
Modifier and TypeFieldDescriptionprotected static final boolean
protected static final Operator
protected Lookahead2Iterator<Token>
protected final Jep
protected final List<GrammarMatcher>
protected final NodeFactory
protected static final Operator
-
Constructor Summary
ConstructorDescriptionShuntingYard
(Jep jep, List<GrammarMatcher> gm) Normal constructor, iterator set inparse(Iterator)
-
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
compareOps
(Operator op1, Operator op2) Compare operators based on their precedence and associativity.protected void
protected void
Match prefixSuffix() optionally followed by a binary or ternary operator or an implicit multiplication.Main entry point, construct tree from sequence of tokens.Callback function used by GrammarMatchers where the matcher expects a simple number, variable, or function call with optional, prefix or suffix operator.Callback function used by GrammarMatchersprotected void
popOp()
Pops an operator off the Operator stack, and creates a new node.protected void
prefix()
Matches identifies, numbers, prefix operators and plugged in grammar matchers.protected void
A prefix() optionally followed by suffix operators.protected ShuntingYard.PrefixRes
Attempt to match a prefix optionally followed by suffix operators.protected ShuntingYard.PrefixRes
Matches identifies, numbers, prefix operators and plugged in grammar matchers.protected void
The pushOp function is worth some explanation Say 1+2*3 is parsed.void
Set the iterator used by theGrammarParser.parseSubExpression()
-
Field Details
-
DUMP
protected static final boolean DUMP- See Also:
-
ops
-
nodes
-
it
-
matchers
-
jep
-
sentinel
-
implicitMul
-
nf
-
-
Constructor Details
-
ShuntingYard
Normal constructor, iterator set inparse(Iterator)
- Parameters:
jep
- Jep instancegm
- list of grammar rules
-
-
Method Details
-
parse
Main entry point, construct tree from sequence of tokens.- Specified by:
parse
in interfaceGrammarParser
- Parameters:
input
- stream of tokens- Returns:
- constructed tree
- Throws:
ParseException
- if the input cannot be parsed
-
parseSubExpression
Callback function used by GrammarMatchers- Specified by:
parseSubExpression
in interfaceGrammarParser
- Returns:
- node tree
- Throws:
ParseException
- if the input cannot be parsed
-
expression
Match prefixSuffix() optionally followed by a binary or ternary operator or an implicit multiplication. Consumes tokens and pushes operators found onto to the stack.- Throws:
ParseException
-
parsePrefixSuffix
Callback function used by GrammarMatchers where the matcher expects a simple number, variable, or function call with optional, prefix or suffix operator.- Specified by:
parsePrefixSuffix
in interfaceGrammarParser
- Returns:
- node tree or null if parsing failed to match
- Throws:
ParseException
- if the input cannot be parsed
-
prefixSuffix
A prefix() optionally followed by suffix operators.- Throws:
ParseException
-
prefixSuffixUnchecked
Attempt to match a prefix optionally followed by suffix operators.- Returns:
- code indicating state of parsing
- Throws:
ParseException
- if node construction fails- Since:
- 3.5
-
prefixUnchecked
Matches identifies, numbers, prefix operators and plugged in grammar matchers.- Returns:
- code indicating state of parsing
- Throws:
ParseException
- if node construction fails- Since:
- 3.5
-
prefix
Matches identifies, numbers, prefix operators and plugged in grammar matchers. Wrapper function around theprefixUnchecked()
- Throws:
ParseException
- if input fails to match or error construction node tree
-
pushOp
The pushOp function is worth some explanation Say 1+2*3 is parsed. First + is pushed onto the stack, then * is pushed. For 1*2+3. * is pushed, when + is encountered * has tighter precedence so it and the top two elements from the node stack are popped, the result computed and pushed on the node stack. Special cases -1+2 [uminus,+],[1] -> [+],[-1] -> [+],[-1,2] -> [],[(-1)+2] -1^2 [uminus,^],[1] -> [uminus,^],[1] -> [uminus,^],[1,2] -> [uminus],[1^2] -> [],[-(1^2)] 1^-2 [^],[1] -> [^,uminus],[1] ->[^,uminus],[1,2] -> [^],[1,(-2)] ->[],[1^(-2)]- Parameters:
op
-tok
- Token operator came from- Throws:
ParseException
-
compareOps
Compare operators based on their precedence and associativity.- Parameters:
op1
-op2
-- Returns:
- true if op1 has a lower precedence than op2, or equal precedence and a left assoc op, etc.
-
popOp
Pops an operator off the Operator stack, and creates a new node. The children of the node are popped off the node stack and the result is pushed onto the node stack.- Throws:
ParseException
-
dumpState
-
getIterator
-
setIterator
Description copied from interface:GrammarParser
Set the iterator used by theGrammarParser.parseSubExpression()
- Specified by:
setIterator
in interfaceGrammarParser
- Parameters:
it
- the iterator
-