fri.patterns.interpreter.parsergenerator.parsertables
Class AbstractParserTables

java.lang.Object
  |
  +--fri.patterns.interpreter.parsergenerator.parsertables.AbstractParserTables
All Implemented Interfaces:
ParserTables, java.io.Serializable
Direct Known Subclasses:
SLRParserTables, SyntaxBuilderParserTables

public abstract class AbstractParserTables
extends java.lang.Object
implements ParserTables, java.io.Serializable

Base class for all parser tables. Default implementations.

Source generation method: toSourceFile(). Needed by CompiledTables, used for buffering compiled table classes at runtime.

Dump methods to print human readable table contents. Dumping does not work on buffered table objects (needs scratch build by constructor).

Author:
(c) 2000, 2001, 2002 Fritz Ritzberger
See Also:
Serialized Form

Field Summary
static int CELLWIDTH
          Helper for the cell width of printed tables.
protected  java.util.List gotoTable
          The GOTO-table with all possible follow states.
protected  java.util.List nonterminals
          Non-terminals, with START-symbol.
protected  java.util.List parseTable
          The PARSE-ACTION table with SHIFT and REDUCE entries.
protected  java.util.List symbols
          Non-terminals and terminals, without EPSILON, with START-symbol: column header of GOTO-table.
protected  Syntax syntax
          The raw syntax to apply on parsing.
protected  java.util.List terminals
          Terminals, with EPSILON: column header of PARSE-ACTION table.
protected  java.util.List terminalsWithoutEpsilon
          Terminals, without EPSILON: tokens for the Lexer.
 
Fields inherited from interface fri.patterns.interpreter.parsergenerator.ParserTables
ACCEPT, ERROR, SHIFT
 
Constructor Summary
protected AbstractParserTables()
          Serializable constructor.
 
Method Summary
static AbstractParserTables construct(java.lang.Class parserType, Syntax syntax)
          Factory to build precompiled parser tables, or construct them from scratch.
 void dump(java.io.PrintStream out)
          Implements ParserTables: prints rules, FIRST/FOLLOW sets, syntax nodes (states), GOTO-table, PARSE-ACTION-table.
protected  void dumpGoto(java.io.PrintStream out)
           
protected  void dumpParseAction(java.io.PrintStream out)
           
protected  java.lang.String dumpRule(Rule rule, int i)
           
 void dumpSyntax(java.io.PrintStream out)
           
protected  void dumpTable(java.lang.String title, java.util.List head, java.util.List table, java.io.PrintStream out)
           
 void dumpTables(java.io.PrintStream out)
           
 java.util.Map getExpected(java.lang.Integer state)
          Implements ParserTables: Returns a Map of the expected tokens for current state, contained in keySet iterator.
 java.lang.Integer getGotoState(java.lang.Integer currentState, java.lang.String symbol)
          Implements ParserTables: returns the next state from GOTO-table.
 java.lang.Integer getParseAction(java.lang.Integer currentState, java.lang.String terminal)
          Implements ParserTables: returns the next action from PARSE-ACTION-table.
 Syntax getSyntax()
          Implements ParserTables: returns Syntax.
 java.util.List getTerminals()
          Implements ParserTables: returns String List of terminals, without EPSILON.
 void report(java.io.PrintStream out)
          Reports count of rules, ternminals and nonterminals.
 java.lang.String toSourceFile(java.lang.String parserClassName)
          Generates a compilable source file "parserClassName.java".
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

syntax

protected Syntax syntax
The raw syntax to apply on parsing.


gotoTable

protected java.util.List gotoTable
The GOTO-table with all possible follow states. As much lines as states and columns as terminals and nonterminals are present.


parseTable

protected java.util.List parseTable
The PARSE-ACTION table with SHIFT and REDUCE entries. As much lines as states and as much columns as terminals are present.


symbols

protected transient java.util.List symbols
Non-terminals and terminals, without EPSILON, with START-symbol: column header of GOTO-table.


terminals

protected transient java.util.List terminals
Terminals, with EPSILON: column header of PARSE-ACTION table.


terminalsWithoutEpsilon

protected java.util.List terminalsWithoutEpsilon
Terminals, without EPSILON: tokens for the Lexer.


nonterminals

protected transient java.util.List nonterminals
Non-terminals, with START-symbol.


CELLWIDTH

public static transient int CELLWIDTH
Helper for the cell width of printed tables. Can be set manually if automatic result does not fit.

Constructor Detail

AbstractParserTables

protected AbstractParserTables()
Serializable constructor.

Method Detail

getGotoState

public java.lang.Integer getGotoState(java.lang.Integer currentState,
                                      java.lang.String symbol)
Implements ParserTables: returns the next state from GOTO-table.

Specified by:
getGotoState in interface ParserTables
Parameters:
currentState - curent state
symbol - current terminal or nonterminal
Returns:
new state

getParseAction

public java.lang.Integer getParseAction(java.lang.Integer currentState,
                                        java.lang.String terminal)
Implements ParserTables: returns the next action from PARSE-ACTION-table.

Specified by:
getParseAction in interface ParserTables
Parameters:
currentState - curent state
terminal - recently received terminal
Returns:
new action

getTerminals

public java.util.List getTerminals()
Implements ParserTables: returns String List of terminals, without EPSILON.

Specified by:
getTerminals in interface ParserTables

getSyntax

public Syntax getSyntax()
Implements ParserTables: returns Syntax.

Specified by:
getSyntax in interface ParserTables

getExpected

public java.util.Map getExpected(java.lang.Integer state)
Implements ParserTables: Returns a Map of the expected tokens for current state, contained in keySet iterator.

Specified by:
getExpected in interface ParserTables

construct

public static AbstractParserTables construct(java.lang.Class parserType,
                                             Syntax syntax)
                                      throws java.lang.Exception
Factory to build precompiled parser tables, or construct them from scratch. Used by CompiledTables and SerializedTables.

Parameters:
parserType - LALRParserTables, SLRParserTables, LRParserTables class.
syntax - can be null for precompiled tables, else the syntax to build.
java.lang.Exception

dump

public void dump(java.io.PrintStream out)
Implements ParserTables: prints rules, FIRST/FOLLOW sets, syntax nodes (states), GOTO-table, PARSE-ACTION-table.

Specified by:
dump in interface ParserTables

dumpTables

public void dumpTables(java.io.PrintStream out)

dumpSyntax

public void dumpSyntax(java.io.PrintStream out)

dumpRule

protected java.lang.String dumpRule(Rule rule,
                                    int i)

dumpGoto

protected void dumpGoto(java.io.PrintStream out)

dumpParseAction

protected void dumpParseAction(java.io.PrintStream out)

dumpTable

protected void dumpTable(java.lang.String title,
                         java.util.List head,
                         java.util.List table,
                         java.io.PrintStream out)

report

public void report(java.io.PrintStream out)
Reports count of rules, ternminals and nonterminals.


toSourceFile

public java.lang.String toSourceFile(java.lang.String parserClassName)
                              throws java.io.IOException
Generates a compilable source file "parserClassName.java". If the class name contains package path, the file will be generated within the corresponding directory, else within working directory.

Parameters:
parserClassName - name of class to generate: "Java" will generate "Java.java" in current directory, "my.pkg.PT" will generate "my/pkg/PT.java" in current directory.
Returns:
fileName if File could be written, else null.
java.io.IOException