Top Description Inners Fields Constructors Methods
org.antlr.tool

public Class Grammar

extends Object
Class Inheritance
Imports
org.antlr.Tool, org.antlr.analysis.DFA, .DFAState, .LL1Analyzer, .LL1DFA, .Label, .LookaheadSet, .NFA, .NFAConversionThread, .NFAState, .NFAToDFAConverter, .SemanticContext, .Transition, org.antlr.codegen.CodeGenerator, .Target, org.antlr.grammar.v3.ANTLRLexer, .ANTLRParser, .ANTLRTreePrinter, .ActionAnalysis, .DefineGrammarItemsWalker, .TreeToNFAConverter, org.antlr.misc.Barrier, .IntSet, .IntervalSet, .MultiMap, .OrderedHashSet, .Utils, org.antlr.runtime.ANTLRReaderStream, .ANTLRStringStream, .CommonToken, .CommonTokenStream, .RecognitionException, .Token, org.antlr.runtime.tree.CommonTreeNodeStream, org.stringtemplate.v4.ST, .STGroup, .STGroupString, java.io.BufferedReader, .File, .FileNotFoundException, .FileReader, .IOException, .PrintStream, .Reader, .StreamTokenizer, .StringReader, java.util.ArrayList, .Arrays, .BitSet, .Collection, .HashMap, .HashSet, .Iterator, .LinkedHashMap, .List, .Map, .Set, .Vector

Represents a grammar in memory.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
public class

Field Summary

Modifier and TypeField and Description
private Map<String, Map<String, Object>>
actions

Map a scope to a map of name:action pairs.

protected boolean
public static String[]
ANTLRLiteralCharValueEscape

Given a char, we need to be able to show as an ANTLR literal.

public static int[]
ANTLRLiteralEscapedCharValue

When converting ANTLR char and string literals, here is the value set of escape chars.

public static final String
public boolean
atLeastOneBacktrackOption

At least one backtrack=true in rule or decision or grammar.

public boolean
atLeastOneRuleMemoizes

At least one rule has memoize=true

public static final String
public Set<GrammarAST>
blocksWithSemPreds

Track decisions with syn preds specified for reporting.

public Set<GrammarAST>
blocksWithSynPreds

Track decisions with syn preds specified for reporting.

protected boolean
builtFromString

We need a way to detect when a lexer grammar is autogenerated from another grammar or we are just sending in a string representing a grammar.

public static final int
protected IntSet
charVocabulary

Todo

hook this to the charVocabulary option

public static final int
public CompositeGrammar
composite

If this grammar is part of a larger composite grammar via delegate statement, then this points at the composite.

public CompositeGrammarTree
compositeTreeNode

A pointer back into grammar tree.

protected int
decisionCount

Be able to assign a number to every decision in grammar; decisions in 1..n

public Set<DFA>
decisionsWhoseDFAsUsesSemPreds

Track decisions that actually use the syn preds in the DFA.

public Set<DFA>
decisionsWhoseDFAsUsesSynPreds

Track decisions that actually use the syn preds in the DFA.

public static final Map<String, String>
defaultBlockOptions

What are the default options for a subrule?

public static final Map<String, String>
public static final Map<String, String>
public static final String
protected Set<Rule>
delegatedRuleReferences

The list of all rules referenced in this grammar, not defined here, and defined in a delegate grammar.

public long
DFACreationWallClockTimeInMS

How long in ms did it take to build DFAs for this grammar?

public static final Set<String>
protected boolean
externalAnalysisAbort

An external tool requests that DFA analysis abort prematurely.

protected NFAFactory
protected String
fileName

What file name holds this grammar?

public static final String
protected CodeGenerator
generator

If non-null, this is the code generator we will use to generate recognizers in the target language.

protected int
global_k

Is there a global fixed lookahead set for this grammar?

public static final String
protected GrammarAST
grammarTree

An AST that records entire input grammar with all rules.

public static final String[]
public static final String[]
public static final String
public boolean
implicitLexer

Was this created from a COMBINED grammar?

protected Vector<Grammar.Decision>
indexToDecision

Each subrule/rule is a decision point and we must track them so we can go back later and build DFA predictors for them.

public static final int
public static final int
public String
label

If this is a delegate of another grammar, this is the label used as an instance var by that grammar to point at this grammar.

public static String[]
protected Set<Rule>
leftRecursiveRules

A list of all rules that are in any left-recursive cycle.

public static final Set<String>
public static final Set<String>
public static final Set<String>
public static final Set<String>
legalTokenOptions

Legal options for terminal refs like ID<node=MyVarNode>

public static final Set<String>
public static final int
public static final String
LEXER_GRAMMAR_FILE_EXTENSION

used for generating lexer temp files

protected ST
protected String
lexerGrammarTemplate

For merged lexer/parsers, we must construct a separate lexer spec.

public List<String>
lexerRuleNamesInCombined

The ANTLRParser tracks lexer rules when reading combined grammars so we can build the Tokens rule.

pack-priv Map<String, DFA>
lineColumnToLookaheadDFAMap

For ANTLRWorks, we want to be able to map a line:col to a specific decision DFA so it can display DFA.

public LL1Analyzer
public String
name

What name did the user provide for this grammar?

public NameSpaceChecker
protected LinkedHashMap<String, Rule>
nameToRuleMap

Map a rule to it's Rule object

protected LinkedHashMap<String, GrammarAST>
nameToSynpredASTMap

When we read in a grammar, we track the list of syntactic predicates and build faux rules for them later.

public NFA
nfa

The NFA that represents the grammar with edges labelled with tokens or epsilon.

public int
public int
public int
protected Map<String, Object>
options

A list of options specified at the grammar level such as language=Java.

public Set<String>
overriddenRules

If this rule is a delegate, some rules might be overridden; don't want to gen code for them.

public static final int
public List<String>
precRuleInitCodeBlocks

Each left-recursive precedence rule must define precedence array for binary operators like: static int[] e_prec = new int[tokenNames.length]; static { e_prec[75] = 1; } Track and we push into parser later; this is computed early when we look for prec rules.

public static final int
public static final int
protected Set<GrammarAST>
ruleRefs

The unique set of all rule references in any rule; set of tree node objects so two refs to same rule can exist but at different line/position.

pack-priv GrammarSanity
sanity

Factored out the sanity checking code; delegate to it.

protected Set<GrammarAST>
protected Map<String, AttributeScope>
scopes

Track the scopes defined outside of rules and the scopes associated with all rules (even if empty).

public Set<Integer>
public Set<Integer>
public static final String
public static final String
public Set<String>
synPredNamesUsedInDFA

Track names of preds so we can avoid generating preds that aren't used Computed during NFA to DFA conversion.

pack-priv Target
target

Useful for asking questions about target during analysis

public static final int
public static final int
public CommonTokenStream
tokenBuffer

This is the buffer of *all* tokens found in the grammar file including whitespace tokens etc...

protected Set<Token>
tokenIDRefs

The unique set of all token ID references in any rule

public Tool
public static final int
public int
type

What type of grammar is this: lexer, parser, tree walker

public static MultiMap<Integer, Integer>
validDelegations

Set of valid imports.

public static final int
public static final int

Constructor Summary

AccessConstructor and Description
public
Grammar(Tool tool, String fileName, CompositeGrammar composite)

Create a grammar from file name.

public
Grammar()

Useful for when you are sure that you are not part of a composite already.

public
Grammar(Tool tool)

public
Grammar(String grammarString)

Used for testing; only useful on noncomposite grammars.

public
Grammar(Tool tool, String grammarString)

Used for testing and Interp/RandomPhrase.

Method Summary

Modifier and TypeMethod and Description
public GrammarAST
addArtificialMatchTokensRule(GrammarAST grammarAST, List<String> ruleNames, List<String> delegateNames, boolean filterMode)

Parse a rule we add artificially that is a list of the other lexer rules like this: "Tokens : ID | INT | SEMI ;" nextToken() will invoke this to set the current token.

protected void
addDelegateGrammar(Grammar delegateGrammar)

add new delegate to composite tree

public void
addRule(GrammarAST grammarTree, GrammarAST t)

public void
public boolean
public void
altReferencesRule(String enclosingRuleName, GrammarAST refScopeAST, GrammarAST refAST, int outerAltNum)

Track a rule reference within an outermost alt of a rule.

public void
altReferencesTokenID(String ruleName, GrammarAST refAST, int outerAltNum)

Track a token reference within an outermost alt of a rule.

public int
public boolean
public void
public boolean
public List<? extends Collection<? extends Rule>>
public void
checkAllRulesForUselessLabels()

Remove all labels on rule refs whose target rules have no return value.

public void
checkNameSpaceAndActions()

ANALYZE ACTIONS, LOOKING FOR LABEL AND ATTR REFS, sanity check

public void
checkRuleReference(GrammarAST scopeAST, GrammarAST refAST, GrammarAST argsAST, String currentRuleName)

public IntSet
complement(IntSet set)

For lexer grammars, return everything in unicode not in set.

public IntSet
complement(int atom)

public String
computeTokenNameFromLiteral(int tokenType, String literal)

given a token type and the text of the literal, come up with a decent token type label.

protected Grammar.Decision
createDecision(int decision)

public DFA
public DFA
createLookaheadDFA(int decision, boolean wackTempStructures)

public void
createLookaheadDFAs()

For each decision in this grammar, compute a single DFA using the NFA states associated with the decision.

public void
createLookaheadDFAs(boolean wackTempStructures)

public AttributeScope
createParameterScope(String ruleName, Token argAction)

public AttributeScope
createReturnScope(String ruleName, Token retAction)

public AttributeScope
createRuleScope(String ruleName, Token scopeAction)

public void
createRuleStartAndStopNFAStates()

Define all the rule begin/end NFAStates to solve forward reference issues.

protected void
public AttributeScope
defineGlobalScope(String name, Token scopeAction)

public void
protected void
defineLabel(Rule r, Token label, GrammarAST element, int type)

Define a label defined in a rule r; check the validity then ask the Rule object to actually define it.

public void
defineLexerRuleForAliasedStringLiteral(String tokenID, String literal, int tokenType)

If someone does PLUS='+' in the parser, must make sure we get "PLUS : '+' ;" in lexer not "T73 : '+';"

public void
defineLexerRuleForStringLiteral(String literal, int tokenType)

public void
public void
defineNamedAction(GrammarAST ampersandAST, String scope, GrammarAST nameAST, GrammarAST actionAST)

Given @scope::name {action} define it for this grammar.

public void
defineRule(Token ruleToken, String modifier, Map<String, Object> options, GrammarAST tree, GrammarAST argActionAST, int numAlts)

Define a new rule.

public void
defineRuleListLabel(String ruleName, Token label, GrammarAST element)

public void
defineRuleRefLabel(String ruleName, Token label, GrammarAST ruleRef)

public String
defineSyntacticPredicate(GrammarAST blockAST, String currentRuleName)

Define a new predicate and get back its name for use in building a semantic predicate reference to the syn pred.

public void
defineToken(String text, int tokenType)

Define a token at a particular token type value.

public void
defineTokenListLabel(String ruleName, Token label, GrammarAST element)

public void
defineTokenRefLabel(String ruleName, Token label, GrammarAST tokenRef)

public void
defineWildcardTreeLabel(String ruleName, Token label, GrammarAST tokenRef)

public void
protected void
examineAllExecutableActions()

Before generating code, we examine all actions that can have $x.y and $y stuff in them because some code generation depends on Rule.referencedPredefinedRuleAttributes.

public void
externallyAbortNFAToDFAConversion()

Terminate DFA creation (grammar analysis).

protected GrammarAST
public LookaheadSet
public boolean
generateMethodForRule(String ruleName)

Should codegen.g gen rule for ruleName?

public Map<String, Map<String, Object>>
public IntSet
getAllCharValues()

If there is a char vocabulary, use it; else return min to max char as defined by the target.

public Set<? extends Rule>
getAllImportedRules()

Get set of all rules imported from all delegate grammars even if indirectly delegated.

public static String
getANTLRCharLiteralForChar(int c)

Return a string representing the escaped char for code c.

protected List<? extends GrammarAST>
getArtificialRulesForSyntacticPredicates(LinkedHashMap<String, GrammarAST> nameToSynpredASTMap)

for any syntactic predicates, we need to define rules for them; they will get defined automatically like any other rule.

public boolean
getAutoBacktrackMode(int decision)

public Object
public static int
getCharValueFromGrammarCharLiteral(String literal)

Given a literal like (the 3 char sequence with single quotes) 'a', return the int value of 'a'.

public CodeGenerator
protected Grammar.Decision
getDecision(int decision)

public GrammarAST
getDecisionBlockAST(int decision)

public NFAState
public List<NFAState>
public List<Grammar.Decision>
public String
getDefaultActionScope(int grammarType)

Given a grammar type, what should be the default action scope?

public Set<Rule>
public Set<? extends Rule>
getDelegatedRules()

Get the set of Rules that need to have manual delegations like "void rule() { importedGrammar.rule(); }" If this grammar is master, get list of all rule definitions from all delegate grammars.

public List<String>
public List<Grammar>
getDelegates()

Get list of all delegates from all grammars directly or indirectly imported into this grammar.

public Grammar
getDelegator()

Who's my direct parent grammar?

public List<Grammar>
getDelegators()

Get list of all delegators.

public List<Grammar>
public String
public AttributeScope
getGlobalScope(String name)

Get a global scope

public Map<String, AttributeScope>
public boolean
public int
public GrammarAST
public String
public boolean
public String
public List<Grammar>
getIndirectDelegates()

Get delegates below direct delegates

public Set<String>
getLabels(Set<GrammarAST> rewriteElements, int labelType)

Given a set of all rewrite elements on right of ->, filter for label types such as Grammar.TOKEN_LABEL, Grammar.TOKEN_LIST_LABEL, ...

public Set<Rule>
getLeftRecursiveRules()

Return a list of left-recursive rules; no analysis can be done successfully on these.

public String
getLexerGrammar()

If the grammar is a combined grammar, return the text of the implicit lexer grammar.

public Map<String, DFA>
public Object
public Rule
public DFA
getLookaheadDFA(int decision)

public List<Integer>
getLookaheadDFAColumnsForLineInFile(int line)

returns a list of column numbers for all decisions on a particular line so ANTLRWorks choose the decision depending on the location of the cursor (otherwise, ANTLRWorks has to give the *exact* location which is not easy from the user point of view).

public DFA
getLookaheadDFAFromPositionInFile(int line, int col)

Useful for ANTLRWorks to map position in file to the DFA for display

public int
getMaxCharValue()

What is the max char value possible for this grammar's target?

public int
getMaxTokenType()

How many token types have been allocated so far?

public int
getNewTokenType()

Return a new unique integer in the token type space

public NFAState
getNFAStateForAltOfDecision(NFAState decisionState, int alt)

Get the ith alternative (1..n) from a decision; return null when an invalid alt is requested.

public int
getNumberOfAltsForDecisionNFA(NFAState decisionState)

Decisions are linked together with transition(1).

public int
public int
public Object
public String
getRecognizerName()

Get the name of the generated recognizer; may or may not be same as grammar name.

public Rule
getRule(String ruleName)

public Rule
getRule(String scopeName, String ruleName)

public int
getRuleIndex(String scopeName, String ruleName)

public int
getRuleIndex(String ruleName)

public String
public String
getRuleName(int ruleIndex)

public Collection<Rule>
public NFAState
public NFAState
getRuleStartState(String scopeName, String ruleName)

public NFAState
public IntSet
getSetFromRule(TreeToNFAConverter nfabuilder, String ruleName)

Get the set equivalent (if any) of the indicated rule from this grammar.

public Set<String>
getStringLiterals()

Get the list of ANTLR String literals

public GrammarAST
public LinkedHashMap<String, GrammarAST>
public String
getTokenDisplayName(int ttype)

Given a token type, get a meaningful name for it such as the ID or string literal.

public Set<String>
getTokenDisplayNames()

Get a list of all token IDs and literals that have an associated token type.

public Set<String>
getTokenIDs()

Get the list of tokens that are IDs like BLOCK and LPAREN

public int
getTokenType(String tokenName)

public IntSet
getTokenTypes()

Return a set of all possible token or char types for this grammar

public Collection<Integer>
getTokenTypesWithoutID()

Return an ordered integer list of token types that have no corresponding token ID like INT or KEYWORD_BEGIN; for stuff like 'begin'.

public Tool
public static StringBuffer
getUnescapedStringFromGrammarStringLiteral(String literal)

ANTLR does not convert escape sequences during the parse phase because it could not know how to print String/char literals back out when printing grammars etc...

public int
getUserMaxLookahead(int decision)

public String
public String
grammarTreeToString(GrammarAST t, boolean showActions)

public void
importGrammar(GrammarAST grammarNameAST, String label)

Import the rules/tokens of a delegate grammar.

public int
importTokenVocabulary(Grammar importFromGr)

Pull your token definitions from an existing grammar in memory.

public int
importTokenVocabulary(GrammarAST tokenVocabOptionAST, String vocabName)

Load a vocab file <vocabName>.tokens and return max token type found.

public boolean
isAtomTokenType(int ttype)

public boolean
public boolean
isEmptyRule(GrammarAST block)

Rules like "a : ;" and "a : {...} ;" should not generate try/catch blocks for RecognitionException.

public boolean
isValidSet(TreeToNFAConverter nfabuilder, GrammarAST t)

Given set tree like ( SET A B ), check that A and B are both valid sets themselves, else we must tree like a BLOCK

public boolean
public LookaheadSet
protected List<IntervalSet>
public boolean
public boolean
public void
public void
public GrammarAST
public void
public void
referenceRuleLabelPredefinedAttribute(String ruleName)

To yield smaller, more readable code, track which rules have their predefined attributes accessed.

protected void
removeUselessLabels(Map<String, Grammar.LabelElementPair> ruleToElementLabelPairMap)

A label on a rule is useless if the rule has no return value, no tree or template output, and it is not referenced in an action.

public boolean
public void
public void
setDecisionBlockAST(int decision, GrammarAST blockAST)

public void
setDecisionNFA(int decision, NFAState state)

public void
setFileName(String fileName)

public void
setGrammarContent(String grammarString)

public void
public void
setLookaheadDFA(int decision, DFA lookaheadDFA)

Set the lookahead DFA for a particular decision.

public void
public String
setOption(String key, Object value, Token optionsStartToken)

Save the option key/value pair and process it; return the key or null if invalid option.

public void
setOptions(Map<String, Object> options, Token optionsStartToken)

public void
public void
public void
setTool(Tool tool)

public void
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public void
private void
public boolean
validImport(Grammar delegate)

Many imports are illegal such as lexer into a tree grammar

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait