xrel.parser
Class SymTable

java.lang.Object
  |
  +--xrel.parser.SymTable
All Implemented Interfaces:
SpecialNames, SymElementKind, XRelParserTreeConstants

public class SymTable
extends java.lang.Object
implements XRelParserTreeConstants, SymElementKind, SpecialNames

The symbol table contains the types, the patterns, the variables and the expressions inside the program.

Author:
Fabrizio Bisi

Field Summary
protected  java.util.HashMap automata
          Contains the automata inside the program.
protected  java.util.Vector ErrorList
           
protected  int ErrorNum
           
protected  java.util.Vector expressions
          Contains the expressions of the program.
 int MAX_ERROR_NUM
          Maximum number of signaled error, after this limit error checking stops.
 int originalExpressions
          number of program expressions
 int originalPatterns
          number of program patterns (variable handled at hand from the extern)
protected  java.util.Vector patterns
          Contains the patterns of the program.
protected  java.lang.String switchId
          the name of the typeswitch identifier, if any
protected  java.util.Vector types
          Contains the types of the program.
protected  java.util.Vector variables
          Contains the variables bound inside the patterns of the program.
protected  int WarningNum
           
 
Fields inherited from interface xrel.parser.XRelParserTreeConstants
JJTAS, JJTCASE, JJTCOMMA, JJTEMPTY, JJTEXPORTTP, JJTIDENTIFIER, JJTIMPORTST, jjtNodeName, JJTOPERATOR, JJTPAR, JJTPROGRAM, JJTSTATEMENTLIST, JJTSTRING, JJTSTRINGLITERAL, JJTSWITCHCASE, JJTTAG, JJTTYPEDECL, JJTVOID
 
Fields inherited from interface xrel.parser.SymElementKind
EXPRESSION, LABEL, PATTERN, S_EXPRESSIONS, S_LABELS, S_PATTERNS, S_TYPES, S_VARIABLES, TYPE, UNDEFINED, VARIABLE
 
Fields inherited from interface xrel.parser.SpecialNames
AnyTagName, AnyTypeName, AutomatonPrefix, EmptyPatternName, ExportTypeName, ExpressionPrefix, PatternPrefix, SpecialPrefix, StringName, StringPatternName, StringTypeName, StrLiteralPrefix, StrLiteralSuffix, StrUnquotedPrefix, StrUnquotedSuffix
 
Constructor Summary
SymTable()
          Initializes the symbol table.
 
Method Summary
 void add_parsing_error(SimpleNode n, java.lang.String msg)
           
 void add_parsing_error(Token first, Token last, java.lang.String msg)
           
 void add_syntax_error(SimpleNode n, java.lang.String msg)
           
 void add_syntax_error(Token first, Token last, java.lang.String msg)
           
 void add_type_error(SimpleNode n, java.lang.String msg)
           
 void add_type_error(Token first, Token last, java.lang.String msg)
           
 void add_warning(SimpleNode n, java.lang.String msg)
           
 void add_warning(Token first, Token last, java.lang.String msg)
           
 void addAutomaton(TA A)
           
protected  void addClause(Node clause)
           
 void checkErrors(SimpleNode rt)
           
 void detachPatterns()
          Detaches the symbol table elements from the original syntax tree.
 void dump(java.io.PrintStream out, int kinds, boolean debug, boolean internalFormat)
          Prints a list of the content of the symbol table to a stream.
 void dumpTokens(java.io.PrintStream out, int kinds, boolean debug)
           
 boolean foundErrors()
           
 boolean foundWarnings()
           
 SimpleNode getClause(int i)
           
 TADifference getDiffAutomaton(TA A1, TA A2, int BuildPhases, boolean debug)
           
 SimpleNode getImportSt()
           
 TAProduct getProductAutomaton(TA A1, TA A2, int BuildPhases, boolean force, boolean debug)
           
 TASimple getSimpleAutomaton(java.lang.String nm, SymElement el, int BuildPhases, boolean debug)
          Returns an automaton with a given name.
 SymElement getTypeSwitchVar()
          Returns the variable element associated to the typeswitch identifier.
 SimpleNode getTypeSwNode()
           
 TAUnion getUnionAutomaton(java.util.HashSet alNames, int BuildPhases, boolean debug)
           
 java.util.Vector getVector(int k)
          Gets the vector of kind k.
 void mergeVariables()
          Join variables that have the same name and the same scope.
 void put_expressions(int scope, SimpleNode nodeValue)
          Puts expressions of a code block into the symbol table.
 void put_pat(int scope, java.lang.String name, SimpleNode nodeValue)
          Puts a pattern into the symbol table.
 SymElement put_type(int scope, java.lang.String name, SimpleNode nodeValue)
          Puts a type into the symbol table.
 SymElement put_var(int scope, java.lang.String name, SimpleNode nodeValue)
          Puts a variable into the symbol table.
 TA search_automaton(java.lang.String nm)
           
 SymElement search(int k, int scope, java.lang.String name)
          Searches an identifier with name "name", scope "scope" and of kind k (type or pattern or variable or expression).
 SymElement search(int kinds, java.lang.String name)
          Searches the last identifier with name "name" inserted in the symbol table. This function explicitly searches first variables (if specified) then patterns (if specified) then everything else. N.B.: to get the last one I search it from the bottom
 SymElement searchPattern(java.lang.String pStr, int scope)
           
protected  void setImportSt(Node impSt)
           
 void setSwitchId(java.lang.String sId)
          Set the typeswitch variable name.
protected  void setTypeSwNode(Node tswNode)
           
 void showBoundVariables(java.io.PrintStream out, boolean uniqueName, boolean internalFormat)
          Shows the variables bound in the pattern matching and theirs values.
 void showErrors(java.io.PrintStream out, SimpleNode rt)
           
 void showThings(java.io.PrintStream out)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

types

protected java.util.Vector types
Contains the types of the program. They're those defined in the typedef declarations together with the export type.

patterns

protected java.util.Vector patterns
Contains the patterns of the program. The first pattern of the vector is that of the import statement, while the following are the patterns of the clauses of the typeswitch statement.

variables

protected java.util.Vector variables
Contains the variables bound inside the patterns of the program.

expressions

protected java.util.Vector expressions
Contains the expressions of the program. Expressions are the actual arguments of the printf calls. This vector contains an ASTStatementList node for each clause of the typeswitch construct, so that expressions of the same clause are grouped together.

automata

protected java.util.HashMap automata
Contains the automata inside the program. Inside this vector you can find an automaton for each pattern, an automaton for each local variable for which there is the need to compute the inferred type and other kinds of automata.

originalPatterns

public int originalPatterns
number of program patterns (variable handled at hand from the extern)

originalExpressions

public int originalExpressions
number of program expressions

switchId

protected java.lang.String switchId
the name of the typeswitch identifier, if any

ErrorNum

protected int ErrorNum

WarningNum

protected int WarningNum

ErrorList

protected java.util.Vector ErrorList

MAX_ERROR_NUM

public final int MAX_ERROR_NUM
Maximum number of signaled error, after this limit error checking stops.
Constructor Detail

SymTable

public SymTable()
Initializes the symbol table. You don't need to call this constructor as there is one only symbol table and it's defined by the parser.
Method Detail

showThings

public void showThings(java.io.PrintStream out)

addClause

protected void addClause(Node clause)

getClause

public SimpleNode getClause(int i)

setTypeSwNode

protected void setTypeSwNode(Node tswNode)

getTypeSwNode

public SimpleNode getTypeSwNode()

setImportSt

protected void setImportSt(Node impSt)

getImportSt

public SimpleNode getImportSt()

getVector

public java.util.Vector getVector(int k)
Gets the vector of kind k.
Parameters:
k - the kind of the vector required (TYPE, PATTERN, VARIABLE or EXPRESSION)

getTypeSwitchVar

public SymElement getTypeSwitchVar()
Returns the variable element associated to the typeswitch identifier.
Returns:
the element associated to the typeswitch identifier

setSwitchId

public void setSwitchId(java.lang.String sId)
Set the typeswitch variable name. This function is used only by the parser.
Parameters:
sId -  

detachPatterns

public void detachPatterns()
Detaches the symbol table elements from the original syntax tree.

put_type

public SymElement put_type(int scope,
                           java.lang.String name,
                           SimpleNode nodeValue)
Puts a type into the symbol table.
Parameters:
scope - the scope of the element (i.e. its identifier)
name - the identifier of the element
nodeValue - the root node associated to the expression that defines the type

put_var

public SymElement put_var(int scope,
                          java.lang.String name,
                          SimpleNode nodeValue)
Puts a variable into the symbol table.
Parameters:
scope - the scope of the element (i.e. its identifier)
name - the name of the element
nodeId - the node of the identifier
nodeValue - the subtree associated to the element
Returns:
the new symbol element or null in case of error

put_pat

public void put_pat(int scope,
                    java.lang.String name,
                    SimpleNode nodeValue)
Puts a pattern into the symbol table.
Parameters:
scope - the scope of the element (i.e. its identifier)
name - the name of the element
nodeValue - the subtree associated to the element

put_expressions

public void put_expressions(int scope,
                            SimpleNode nodeValue)
Puts expressions of a code block into the symbol table.
Parameters:
scope - the scope of the element (i.e. its identifier)
name - the name of the element
nodeValue - the ASTStatementList node

getSimpleAutomaton

public TASimple getSimpleAutomaton(java.lang.String nm,
                                   SymElement el,
                                   int BuildPhases,
                                   boolean debug)
Returns an automaton with a given name. If it doesn't exist create it.
Parameters:
nm - the name of the automaton
Returns:
the automaton with the given name

getProductAutomaton

public TAProduct getProductAutomaton(TA A1,
                                     TA A2,
                                     int BuildPhases,
                                     boolean force,
                                     boolean debug)

addAutomaton

public void addAutomaton(TA A)

getDiffAutomaton

public TADifference getDiffAutomaton(TA A1,
                                     TA A2,
                                     int BuildPhases,
                                     boolean debug)

getUnionAutomaton

public TAUnion getUnionAutomaton(java.util.HashSet alNames,
                                 int BuildPhases,
                                 boolean debug)

search_automaton

public TA search_automaton(java.lang.String nm)

mergeVariables

public void mergeVariables()

Join variables that have the same name and the same scope.

This is a little trick as I need to check syntactic errors and linearity constraints before to compute a type for the variable.

This function copies also the subtrees of the variables (it detaches the variable types from the tree).


search

public SymElement search(int k,
                         int scope,
                         java.lang.String name)
Searches an identifier with name "name", scope "scope" and of kind k (type or pattern or variable or expression).
Parameters:
k - TYPE or PATTERN or VARIABLE or EXPRESSION
scope - scoping number: 0 for types and the number of the clause for variables/patterns/expressions (0 if the variable is bound in the import statement or if the pattern is in the import statement)
name - the name of the identifier to search
Returns:
the searched element or null if not found

search

public SymElement search(int kinds,
                         java.lang.String name)

Searches the last identifier with name "name" inserted in the symbol table.

This function explicitly searches first variables (if specified) then patterns (if specified) then everything else.

N.B.: to get the last one I search it from the bottom

Parameters:
name - the identifier to search
Returns:
the searched element or null

searchPattern

public SymElement searchPattern(java.lang.String pStr,
                                int scope)

showBoundVariables

public void showBoundVariables(java.io.PrintStream out,
                               boolean uniqueName,
                               boolean internalFormat)
Shows the variables bound in the pattern matching and theirs values.
Parameters:
out - the print stream where to write out
uniqueName - if this flag is true variable names are prepended with the internal unique code
internalFormat - if this flag is true output is shown in an internal format, otherwise in an external (more readable) one.

dump

public void dump(java.io.PrintStream out,
                 int kinds,
                 boolean debug,
                 boolean internalFormat)
Prints a list of the content of the symbol table to a stream.
Parameters:
out - the stream where to direct the output

dumpTokens

public void dumpTokens(java.io.PrintStream out,
                       int kinds,
                       boolean debug)

add_parsing_error

public void add_parsing_error(SimpleNode n,
                              java.lang.String msg)

add_parsing_error

public void add_parsing_error(Token first,
                              Token last,
                              java.lang.String msg)

add_syntax_error

public void add_syntax_error(SimpleNode n,
                             java.lang.String msg)

add_syntax_error

public void add_syntax_error(Token first,
                             Token last,
                             java.lang.String msg)

add_type_error

public void add_type_error(SimpleNode n,
                           java.lang.String msg)

add_type_error

public void add_type_error(Token first,
                           Token last,
                           java.lang.String msg)

add_warning

public void add_warning(SimpleNode n,
                        java.lang.String msg)

add_warning

public void add_warning(Token first,
                        Token last,
                        java.lang.String msg)

checkErrors

public void checkErrors(SimpleNode rt)

showErrors

public void showErrors(java.io.PrintStream out,
                       SimpleNode rt)

foundErrors

public boolean foundErrors()

foundWarnings

public boolean foundWarnings()