FileDocCategorySizeDatePackage
Grammar.javaAPI DocGlassfish v2 API9577Wed Aug 30 15:34:06 BST 2006persistence.antlr

Grammar

public abstract class Grammar extends Object
A Grammar holds a set of rules (which are stored in a symbol table). Most of the time a grammar needs a code generator and an LLkAnalyzer too.

Fields Summary
protected Tool
antlrTool
protected CodeGenerator
generator
protected LLkGrammarAnalyzer
theLLkAnalyzer
protected Hashtable
symbols
protected boolean
buildAST
protected boolean
analyzerDebug
protected boolean
interactive
protected String
superClass
protected TokenManager
tokenManager
The token manager associated with the grammar, if any. // The token manager is responsible for maintaining the set of valid tokens, and // is conceptually shared between the lexer and parser. This may be either a // LexerGrammar or a ImportVocabTokenManager.
protected String
exportVocab
The name of the export vocabulary...used to generate the output token types interchange file.
protected String
importVocab
The name of the import vocabulary. "Initial conditions"
protected Hashtable
options
protected Vector
rules
protected Token
preambleAction
protected String
className
protected String
fileName
protected Token
classMemberAction
protected boolean
hasSyntacticPredicate
protected boolean
hasUserErrorHandling
protected int
maxk
protected boolean
traceRules
protected boolean
debuggingOutput
protected boolean
defaultErrorHandler
protected String
comment
Constructors Summary
public Grammar(String className_, Tool tool_, String superClass)

 // javadoc comment

           
        className = className_;
        antlrTool = tool_;
        symbols = new Hashtable();
        options = new Hashtable();
        rules = new Vector(100);
        this.superClass = superClass;
    
Methods Summary
public voiddefine(persistence.antlr.RuleSymbol rs)
Define a rule

        rules.appendElement(rs);
        // add the symbol to the rules hash table
        symbols.put(rs.getId(), rs);
    
public abstract voidgenerate()
Top-level call to generate the code for this grammar

protected java.lang.StringgetClassName()

        return className;
    
public booleangetDefaultErrorHandler()

        return defaultErrorHandler;
    
public java.lang.StringgetFilename()

        return fileName;
    
public intgetIntegerOption(java.lang.String key)
Get an integer option. Given the name of the option find its associated integer value. If the associated value is not an integer or is not in the table, then throw an exception of type NumberFormatException.

param
key The name of the option
return
The value associated with the key.

        Token t = (Token)options.get(key);
        if (t == null || t.getType() != ANTLRTokenTypes.INT) {
            throw new NumberFormatException();
        }
        else {
            return Integer.parseInt(t.getText());
        }
    
public persistence.antlr.TokengetOption(java.lang.String key)
Get an option. Given the name of the option find its associated value.

param
key The name of the option
return
The value associated with the key, or null if the key has not been set.

        return (Token)options.get(key);
    
protected abstract java.lang.StringgetSuperClass()

public persistence.antlr.GrammarSymbolgetSymbol(java.lang.String s)

        return (GrammarSymbol)symbols.get(s);
    
public java.util.EnumerationgetSymbols()

        return symbols.elements();
    
public booleanhasOption(java.lang.String key)
Check the existence of an option in the table

param
key The name of the option
return
true if the option is in the table

        return options.containsKey(key);
    
public booleanisDefined(java.lang.String s)
Is a rule symbol defined? (not used for tokens)

        return symbols.containsKey(s);
    
public abstract voidprocessArguments(java.lang.String[] args)
Process command line arguments. Implemented in subclasses

public voidsetCodeGenerator(persistence.antlr.CodeGenerator gen)

        generator = gen;
    
public voidsetFilename(java.lang.String s)

        fileName = s;
    
public voidsetGrammarAnalyzer(persistence.antlr.LLkGrammarAnalyzer a)

        theLLkAnalyzer = a;
    
public booleansetOption(java.lang.String key, persistence.antlr.Token value)
Set a generic option. This associates a generic option key with a Token value. No validation is performed by this method, although users of the value (code generation and/or analysis) may require certain formats. The value is stored as a token so that the location of an error can be reported.

param
key The name of the option.
param
value The value to associate with the key.
return
true if the option was a valid generic grammar option, false o/w

        options.put(key, value);
        String s = value.getText();
        int i;
        if (key.equals("k")) {
            try {
                maxk = getIntegerOption("k");
				if ( maxk<=0 ) {
					antlrTool.error("option 'k' must be greater than 0 (was " +
									value.getText() + ")",
									getFilename(),
									value.getLine(),
									value.getColumn());
					maxk = 1;
				}
            }
            catch (NumberFormatException e) {
                antlrTool.error("option 'k' must be an integer (was " + value.getText() + ")", getFilename(), value.getLine(), value.getColumn());
            }
            return true;
        }
        if (key.equals("codeGenMakeSwitchThreshold")) {
            try {
                i = getIntegerOption("codeGenMakeSwitchThreshold");
            }
            catch (NumberFormatException e) {
                antlrTool.error("option 'codeGenMakeSwitchThreshold' must be an integer", getFilename(), value.getLine(), value.getColumn());
            }
            return true;
        }
        if (key.equals("codeGenBitsetTestThreshold")) {
            try {
                i = getIntegerOption("codeGenBitsetTestThreshold");
            }
            catch (NumberFormatException e) {
                antlrTool.error("option 'codeGenBitsetTestThreshold' must be an integer", getFilename(), value.getLine(), value.getColumn());
            }
            return true;
        }
        if (key.equals("defaultErrorHandler")) {
            if (s.equals("true")) {
                defaultErrorHandler = true;
            }
            else if (s.equals("false")) {
                defaultErrorHandler = false;
            }
            else {
                antlrTool.error("Value for defaultErrorHandler must be true or false", getFilename(), value.getLine(), value.getColumn());
            }
            return true;
        }
        if (key.equals("analyzerDebug")) {
            if (s.equals("true")) {
                analyzerDebug = true;
            }
            else if (s.equals("false")) {
                analyzerDebug = false;
            }
            else {
                antlrTool.error("option 'analyzerDebug' must be true or false", getFilename(), value.getLine(), value.getColumn());
            }
            return true;
        }
        if (key.equals("codeGenDebug")) {
            if (s.equals("true")) {
                analyzerDebug = true;
            }
            else if (s.equals("false")) {
                analyzerDebug = false;
            }
            else {
                antlrTool.error("option 'codeGenDebug' must be true or false", getFilename(), value.getLine(), value.getColumn());
            }
            return true;
        }
        if (key.equals("classHeaderSuffix")) {
            return true;
        }
        if (key.equals("classHeaderPrefix")) {
            return true;
        }
        if (key.equals("namespaceAntlr")) {
            return true;
        }
        if (key.equals("namespaceStd")) {
            return true;
        }
        if (key.equals("genHashLines")) {
            return true;
        }
        if (key.equals("noConstructors")) {
            return true;
        }
        return false;
    
public voidsetTokenManager(persistence.antlr.TokenManager tokenManager_)

        tokenManager = tokenManager_;
    
public java.lang.StringtoString()
Print out the grammar without actions

        StringBuffer buf = new StringBuffer(20000);
        Enumeration ids = rules.elements();
        while (ids.hasMoreElements()) {
            RuleSymbol rs = (RuleSymbol)ids.nextElement();
            if (!rs.id.equals("mnextToken")) {
                buf.append(rs.getBlock().toString());
                buf.append("\n\n");
            }
        }
        return buf.toString();