Class AssemblyParseMachine
- All Implemented Interfaces:
Comparable<AssemblyParseMachine>
Instances of this class store a parse state. In order to work correctly, the class must be given a properly-constructed Action/Goto table.
This implementation is somewhat unconventional. First, instead of strictly tokenizing and then parsing, each terminal is given the opportunity to match a token in the input. If none match, it results in a syntax error (equivalent to the token type having an empty cell in the classical algorithm). If more than one match, the parser branches. Also, because a single cell may also contain multiple actions, the parser could branch again. Thus, if a sentence is ambiguous, this algorithm will identify all possible parse trees, including ones where the input is tokenized differently than in other trees.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected final Stringprotected intprotected Collection<AssemblyTerminal> protected Stringprotected final intprotected AssemblyParseTokenprotected final AssemblyParserprotected intprotected final AssemblyNumericSymbolsprotected final Stack<AssemblyParseTreeNode> -
Constructor Summary
ConstructorsConstructorDescriptionAssemblyParseMachine(AssemblyParser parser, String input, int pos, AssemblyParseToken lastTok, AssemblyNumericSymbols symbols) Construct a new parse state -
Method Summary
Modifier and TypeMethodDescriptionintprotected voidconsume(AssemblyTerminal t, AssemblyParseToken tok, Set<AssemblyParseMachine> results, Deque<AssemblyParseMachine> visited) Consume a given terminal (and corresponding token) and continue parsingcopy()Duplicate this machine stateprotected voiddoAction(AssemblyParseActionGotoTable.Action a, AssemblyParseToken tok, Set<AssemblyParseMachine> results, Deque<AssemblyParseMachine> visited) Perform a given action and continue parsing, exhausting all results after the actionbooleanexhaust()Parse (or continue parsing) all possible trees from this machine stateprotected voidexhaust(Set<AssemblyParseMachine> results, Deque<AssemblyParseMachine> visited) Parse (or continue parsing) all possible trees from this machine stateprotected static AssemblyParseMachinefindLoop(AssemblyParseMachine machine, Collection<AssemblyParseMachine> visited) Look for previous machine states having the same stack and positiongetTree()If in the accepted state, get the resulting parse tree for this machineinthashCode()toString()
-
Field Details
-
parser
-
output
-
stack
-
treeStack
-
buffer
-
pos
protected int pos -
lastTok
-
symbols
-
accepted
protected boolean accepted -
error
protected int error -
got
-
expected
-
id
protected final int id
-
-
Constructor Details
-
AssemblyParseMachine
public AssemblyParseMachine(AssemblyParser parser, String input, int pos, AssemblyParseToken lastTok, AssemblyNumericSymbols symbols) Construct a new parse state- Parameters:
parser- the parser driving this machineinput- the full input linepos- the position in the line identifying the next characters to parselastTok-symbols-
-
-
Method Details
-
hashCode
public int hashCode() -
equals
-
compareTo
- Specified by:
compareToin interfaceComparable<AssemblyParseMachine>
-
copy
Duplicate this machine stateThis is used extensively when branching
- Returns:
- the duplicate
-
doAction
protected void doAction(AssemblyParseActionGotoTable.Action a, AssemblyParseToken tok, Set<AssemblyParseMachine> results, Deque<AssemblyParseMachine> visited) Perform a given action and continue parsing, exhausting all results after the actionThe visited list prevents infinite loops or stack overflows resulting from consuming epsilon and going to the same state. Such loops may involve many states.
- Parameters:
a- the actiontok- the token given by the terminal (column) of the entry containing this actionresults- a place to store all the parsing results (each must be accept or error state)visited- a collection of machine states already visited
-
consume
protected void consume(AssemblyTerminal t, AssemblyParseToken tok, Set<AssemblyParseMachine> results, Deque<AssemblyParseMachine> visited) Consume a given terminal (and corresponding token) and continue parsing- Parameters:
t- the terminaltok- the corresponding tokenresults- a place to store all the parsing resultsvisited- a collection of machine states already visited
-
findLoop
protected static AssemblyParseMachine findLoop(AssemblyParseMachine machine, Collection<AssemblyParseMachine> visited) Look for previous machine states having the same stack and positionThis would imply we have gone in a loop without consuming anything. We need to prune.
- Parameters:
machine- the machine state to checkvisited- the stack of previous machine states- Returns:
- if there is a loop, the machine state proving it, null otherwise
-
toString
-
exhaust
Parse (or continue parsing) all possible trees from this machine state- Parameters:
results- a place to store all the parsing resultsvisited- a collection of machine states already visited
-
exhaust
Parse (or continue parsing) all possible trees from this machine state- Returns:
- the set of all possible trees and errors
-
getTree
If in the accepted state, get the resulting parse tree for this machine- Returns:
- the parse tree
-