Class AssemblyParser
This class constructs the Action/Goto table (and all the other trappings) of a LALR(1) parser and
provides a parse(String)
method to parse actual sentences.
This implementation is somewhat unconventional in that it permits ambiguous grammars. Instead of complaining, it produces the set of all possible parse trees. Of course, this comes at the cost of some efficiency.
See Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman, Compilers: Principles, Techniques, & Tools. Boston, MA: Pearson, 2007.
See Jackson, Stephen. LALR(1) Parsing. Halifax, Nova Scotia, Canada: Dalhousie University. <http://web.cs.dal.ca/~sjackson/lalr1.html>
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static class
A map key used to identify merges for Step 4 in Stephen Jackson's rantprotected static class
The map value keyed byAssemblyParser.MergeKey
-
Field Summary
Modifier and TypeFieldDescriptionprotected AssemblyParseActionGotoTable
protected static final DbgTimer
Change this toDbgTimer.ACTIVE
for verbose diagnosticsprotected static final boolean
protected AssemblyExtendedGrammar
protected final AssemblyFirstFollow
protected final AssemblyFirstFollow
protected final AssemblyGrammar
protected Map
<AssemblyParser.MergeKey, AssemblyParser.MergeValue> protected final ArrayList
<AssemblyParseState> protected final AssemblyParseTransitionTable
-
Constructor Summary
ConstructorDescriptionAssemblyParser
(AssemblyGrammar grammar) Construct a LALR(1) parser from the given grammar -
Method Summary
Modifier and TypeMethodDescriptionprotected int
addLR0State
(AssemblyParseState state) Add a newly-constructed LR0 state, and return it's assigned numberprotected void
protected void
protected void
protected AssemblyExtendedProduction
extend
(AssemblyProduction prod, int start) Extend a production, using the given LR0 start stateGet the grammar used to construct this parserParse the given sentenceparse
(String input, AssemblyNumericSymbols symbols) Parse the given sentence with the given defined symbolsvoid
For debuggingvoid
For debuggingvoid
For debuggingvoid
printGrammar
(PrintStream out) For debuggingvoid
For debuggingvoid
For debuggingvoid
printMergers
(PrintStream out) For debuggingvoid
For debuggingvoid
printStuff
(PrintStream out) For debugging
-
Field Details
-
grammar
-
ff
-
states
-
table
-
extendedGrammar
-
extff
-
mergers
-
actions
-
DBG
Change this toDbgTimer.ACTIVE
for verbose diagnostics -
DBG_DETAIL
protected static final boolean DBG_DETAIL- See Also:
-
-
Constructor Details
-
AssemblyParser
Construct a LALR(1) parser from the given grammar- Parameters:
grammar
- the grammar
-
-
Method Details
-
buildLR0Machine
protected void buildLR0Machine() -
addLR0State
Add a newly-constructed LR0 state, and return it's assigned numberIf the state already exists, this just returns its previously assigned number
- Parameters:
state
- the newly-constructed state- Returns:
- the assigned number
-
buildExtendedGrammar
protected void buildExtendedGrammar() -
extend
Extend a production, using the given LR0 start state- Parameters:
prod
- the production to extendstart
- the starting LR0 state- Returns:
- the extended production, if the start state is valid for it
-
buildActionGotoTable
protected void buildActionGotoTable() -
parse
Parse the given sentence- Parameters:
input
- the sentence to parse- Returns:
- all possible parse trees (and possible errors)
-
parse
Parse the given sentence with the given defined symbolsThe tokenizer for numeric terminals also accepts any key in
labels
. In such cases, the resulting token is assigned the value of the symbols.- Parameters:
input
- the sentence to parsersymbols
- the symbols- Returns:
- all possible parse results (trees and errors)
-
printGrammar
For debugging -
printLR0States
For debugging -
printLR0TransitionTable
For debugging -
printExtendedGrammar
For debugging -
printGeneralFF
For debugging -
printExtendedFF
For debugging -
printMergers
For debugging -
printParseTable
For debugging -
printStuff
For debugging -
getGrammar
Get the grammar used to construct this parser- Returns:
- the grammar
-