Package ghidra.app.plugin.assembler.sleigh.parse

# Class AssemblyParser

java.lang.Object
ghidra.app.plugin.assembler.sleigh.parse.AssemblyParser

public class AssemblyParser extends Object
A 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 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

  Nested Classes
  Modifier and Type
  Class
  Description
  `protected static class`
  `AssemblyParser.MergeKey`
  A map key used to identify merges for Step 4 in Stephen Jackson's rant
  `protected static class`
  `AssemblyParser.MergeValue`
  The map value keyed by `AssemblyParser.MergeKey`

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected AssemblyParseActionGotoTable`
  `actions`
  `protected static final DbgTimer`
  `DBG`
  Change this to `DbgTimer.ACTIVE` for verbose diagnostics
  `protected static final boolean`
  `DBG_DETAIL`
  `protected AssemblyExtendedGrammar`
  `extendedGrammar`
  `protected final AssemblyFirstFollow`
  `extff`
  `protected final AssemblyFirstFollow`
  `ff`
  `protected final AssemblyGrammar`
  `grammar`
  `protected Map<AssemblyParser.MergeKey,AssemblyParser.MergeValue>`
  `mergers`
  `protected final ArrayList<AssemblyParseState>`
  `states`
  `protected final AssemblyParseTransitionTable`
  `table`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `AssemblyParser(AssemblyGrammar grammar)`
  Construct a LALR(1) parser from the given grammar

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected int`
  `addLR0State(AssemblyParseState state)`
  Add a newly-constructed LR0 state, and return it's assigned number
  `protected void`
  `buildActionGotoTable()`
  `protected void`
  `buildExtendedGrammar()`
  `protected void`
  `buildLR0Machine()`
  `protected AssemblyExtendedProduction`
  `extend(AssemblyProduction prod, int start)`
  Extend a production, using the given LR0 start state
  `AssemblyGrammar`
  `getGrammar()`
  Get the grammar used to construct this parser
  `Iterable<AssemblyParseResult>`
  `parse(String input)`
  Parse the given sentence
  `Collection<AssemblyParseResult>`
  `parse(String input, AssemblyNumericSymbols symbols)`
  Parse the given sentence with the given defined symbols
  `void`
  `printExtendedFF(PrintStream out)`
  For debugging
  `void`
  `printExtendedGrammar(PrintStream out)`
  For debugging
  `void`
  `printGeneralFF(PrintStream out)`
  For debugging
  `void`
  `printGrammar(PrintStream out)`
  For debugging
  `void`
  `printLR0States(PrintStream out)`
  For debugging
  `void`
  `printLR0TransitionTable(PrintStream out)`
  For debugging
  `void`
  `printMergers(PrintStream out)`
  For debugging
  `void`
  `printParseTable(PrintStream out)`
  For debugging
  `void`
  `printStuff(PrintStream out)`
  For debugging

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### grammar

    protected final AssemblyGrammar grammar

  - ### ff

    protected final AssemblyFirstFollow ff

  - ### states

    protected final ArrayList<AssemblyParseState> states

  - ### table

    protected final AssemblyParseTransitionTable table

  - ### extendedGrammar

    protected AssemblyExtendedGrammar extendedGrammar

  - ### extff

    protected final AssemblyFirstFollow extff

  - ### mergers

    protected Map<AssemblyParser.MergeKey,AssemblyParser.MergeValue> mergers

  - ### actions

    protected AssemblyParseActionGotoTable actions

  - ### DBG

    protected static final DbgTimer DBG
    Change this to `DbgTimer.ACTIVE` for verbose diagnostics

  - ### DBG_DETAIL

    protected static final boolean DBG_DETAIL
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### AssemblyParser

    public AssemblyParser(AssemblyGrammar grammar)
    Construct a LALR(1) parser from the given grammar
    Parameters:
      `grammar` - the grammar

- ## Method Details

  - ### buildLR0Machine

    protected void buildLR0Machine()

  - ### addLR0State

    protected int addLR0State(AssemblyParseState state)
    Add a newly-constructed LR0 state, and return it's assigned number

    If 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

    protected AssemblyExtendedProduction extend(AssemblyProduction prod, int start)
    Extend a production, using the given LR0 start state
    Parameters:
      `prod` - the production to extend
      `start` - the starting LR0 state
    Returns:
      the extended production, if the start state is valid for it

  - ### buildActionGotoTable

    protected void buildActionGotoTable()

  - ### parse

    public Iterable<AssemblyParseResult> parse(String input)
    Parse the given sentence
    Parameters:
      `input` - the sentence to parse
    Returns:
      all possible parse trees (and possible errors)

  - ### parse

    public Collection<AssemblyParseResult> parse(String input, AssemblyNumericSymbols symbols)
    Parse the given sentence with the given defined symbols

    The 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 parser
      `symbols` - the symbols
    Returns:
      all possible parse results (trees and errors)

  - ### printGrammar

    public void printGrammar(PrintStream out)
    For debugging

  - ### printLR0States

    public void printLR0States(PrintStream out)
    For debugging

  - ### printLR0TransitionTable

    public void printLR0TransitionTable(PrintStream out)
    For debugging

  - ### printExtendedGrammar

    public void printExtendedGrammar(PrintStream out)
    For debugging

  - ### printGeneralFF

    public void printGeneralFF(PrintStream out)
    For debugging

  - ### printExtendedFF

    public void printExtendedFF(PrintStream out)
    For debugging

  - ### printMergers

    public void printMergers(PrintStream out)
    For debugging

  - ### printParseTable

    public void printParseTable(PrintStream out)
    For debugging

  - ### printStuff

    public void printStuff(PrintStream out)
    For debugging

  - ### getGrammar

    public AssemblyGrammar getGrammar()
    Get the grammar used to construct this parser
    Returns:
      the grammar