Class AssemblyParser
- java.lang.Object
-
- ghidra.app.plugin.assembler.sleigh.parse.AssemblyParser
-
public class AssemblyParser extends java.lang.ObjectA class to encapsulate LALR(1) parsing for a given grammar This class constructs the Action/Goto table (and all the other trappings) of a LALR(1) parser and provides aparse(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. Bostom, 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
Nested Classes Change Modifier and Type Class Description protected static classAssemblyParser.MergeKeyA map key used to identify merges for Step 4 in Stephen Jackson's rantprotected static classAssemblyParser.MergeValueThe map value keyed byAssemblyParser.MergeKey
-
Field Summary
Fields Change Modifier and Type Field Description NEW protected AssemblyParseActionGotoTableactionsNEW protected static DbgTimerdbgNEW protected static booleandbg_detailstatic java.util.Map<java.lang.String,java.lang.Long>EMPTY_LABELSA convenience to specify no labels inparse(String, Map)NEW protected AssemblyExtendedGrammarextendedGrammarNEW protected AssemblyFirstFollowextffNEW protected AssemblyFirstFollowffNEW protected AssemblyGrammargrammarNEW protected java.util.Map<AssemblyParser.MergeKey,AssemblyParser.MergeValue>mergersNEW protected java.util.ArrayList<AssemblyParseState>statesNEW protected AssemblyParseTransitionTabletable
-
Constructor Summary
Constructors Change Constructor Description AssemblyParser(AssemblyGrammar grammar)Construct a LALR(1) parser from the given grammar
-
Method Summary
All Methods Instance Methods Concrete Methods Change Modifier and Type Method Description NEW protected intaddLR0State(AssemblyParseState state)Add a newly-constructed LR0 state, and return it's assigned numberNEW protected voidbuildActionGotoTable()NEW protected voidbuildExtendedGrammar()NEW protected voidbuildLR0Machine()NEW protected AssemblyExtendedProductionextend(AssemblyProduction prod, int start)Extend a production, using the given LR0 start stateAssemblyGrammargetGrammar()Get the grammar used to construct this parserjava.lang.Iterable<AssemblyParseResult>parse(java.lang.String input)Parse the given sentencejava.util.Collection<AssemblyParseResult>parse(java.lang.String input, java.util.Map<java.lang.String,java.lang.Long> labels)Parse the given sentence with the given defined labelsvoidprintExtendedFF(java.io.PrintStream out)For debuggingvoidprintExtendedGrammar(java.io.PrintStream out)For debuggingvoidprintGeneralFF(java.io.PrintStream out)For debuggingvoidprintGrammar(java.io.PrintStream out)For debuggingvoidprintLR0States(java.io.PrintStream out)For debuggingvoidprintLR0TransitionTable(java.io.PrintStream out)For debuggingvoidprintMergers(java.io.PrintStream out)For debuggingvoidprintParseTable(java.io.PrintStream out)For debuggingvoidprintStuff(java.io.PrintStream out)For debugging
-
-
-
Field Detail
-
grammar NEW
protected final AssemblyGrammar grammar
-
ff NEW
protected final AssemblyFirstFollow ff
-
states NEW
protected final java.util.ArrayList<AssemblyParseState> states
-
table NEW
protected final AssemblyParseTransitionTable table
-
extendedGrammar NEW
protected AssemblyExtendedGrammar extendedGrammar
-
extff NEW
protected final AssemblyFirstFollow extff
-
mergers NEW
protected java.util.Map<AssemblyParser.MergeKey,AssemblyParser.MergeValue> mergers
-
actions NEW
protected AssemblyParseActionGotoTable actions
-
EMPTY_LABELS
public static final java.util.Map<java.lang.String,java.lang.Long> EMPTY_LABELS
A convenience to specify no labels inparse(String, Map)
-
dbg NEW
protected static final DbgTimer dbg
Constructor Detail
-
AssemblyParser
public AssemblyParser(AssemblyGrammar grammar)
Construct a LALR(1) parser from the given grammar- Parameters:
grammar- the grammar
-
-
Method Detail
-
addLR0State NEW
protected int addLR0State(AssemblyParseState state)
Add a newly-constructed LR0 state, and return it's assigned number- Parameters:
state- the newly-constructed state- Returns:
- the assigned number If the state already exists, this just returns its previously assigned number
-
extend NEW
protected AssemblyExtendedProduction extend(AssemblyProduction prod, int start)
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
-
parse
public java.lang.Iterable<AssemblyParseResult> parse(java.lang.String input)
Parse the given sentence- Parameters:
input- the sentence to parse- Returns:
- all possible parse trees (and possible errors)
-
parse
public java.util.Collection<AssemblyParseResult> parse(java.lang.String input, java.util.Map<java.lang.String,java.lang.Long> labels)
Parse the given sentence with the given defined labels- Parameters:
input- the sentence to parserlabels- a map of label to number substitutions- Returns:
- all possible parse results (trees and errors)
The tokenizer for numeric terminals also accepts any key in
labels.In such cases, the resulting token is assigned the value of the label.
-
printGrammar
public void printGrammar(java.io.PrintStream out)
For debugging
-
printLR0States
public void printLR0States(java.io.PrintStream out)
For debugging
-
printLR0TransitionTable
public void printLR0TransitionTable(java.io.PrintStream out)
For debugging
-
printExtendedGrammar
public void printExtendedGrammar(java.io.PrintStream out)
For debugging
-
printGeneralFF
public void printGeneralFF(java.io.PrintStream out)
For debugging
-
printExtendedFF
public void printExtendedFF(java.io.PrintStream out)
For debugging
-
printMergers
public void printMergers(java.io.PrintStream out)
For debugging
-
printParseTable
public void printParseTable(java.io.PrintStream out)
For debugging
-
printStuff
public void printStuff(java.io.PrintStream out)
For debugging
-
getGrammar
public AssemblyGrammar getGrammar()
Get the grammar used to construct this parser- Returns:
- the grammar
-
-