Search in sources :

Example 1 with LeftRecursiveRuleTransformer

use of org.antlr.v4.analysis.LeftRecursiveRuleTransformer in project antlr4 by antlr.

the class SemanticPipeline method process.

public void process() {
    if (g.ast == null)
        return;
    // COLLECT RULE OBJECTS
    RuleCollector ruleCollector = new RuleCollector(g);
    ruleCollector.process(g.ast);
    // DO BASIC / EASY SEMANTIC CHECKS
    int prevErrors = g.tool.errMgr.getNumErrors();
    BasicSemanticChecks basics = new BasicSemanticChecks(g, ruleCollector);
    basics.process();
    if (g.tool.errMgr.getNumErrors() > prevErrors)
        return;
    // TRANSFORM LEFT-RECURSIVE RULES
    prevErrors = g.tool.errMgr.getNumErrors();
    LeftRecursiveRuleTransformer lrtrans = new LeftRecursiveRuleTransformer(g.ast, ruleCollector.rules.values(), g);
    lrtrans.translateLeftRecursiveRules();
    // don't continue if we got errors during left-recursion elimination
    if (g.tool.errMgr.getNumErrors() > prevErrors)
        return;
    // STORE RULES IN GRAMMAR
    for (Rule r : ruleCollector.rules.values()) {
        g.defineRule(r);
    }
    // COLLECT SYMBOLS: RULES, ACTIONS, TERMINALS, ...
    SymbolCollector collector = new SymbolCollector(g);
    collector.process(g.ast);
    // CHECK FOR SYMBOL COLLISIONS
    SymbolChecks symcheck = new SymbolChecks(g, collector);
    // side-effect: strip away redef'd rules.
    symcheck.process();
    for (GrammarAST a : collector.namedActions) {
        g.defineAction(a);
    }
    // LINK (outermost) ALT NODES WITH Alternatives
    for (Rule r : g.rules.values()) {
        for (int i = 1; i <= r.numberOfAlts; i++) {
            r.alt[i].ast.alt = r.alt[i];
        }
    }
    // ASSIGN TOKEN TYPES
    g.importTokensFromTokensFile();
    if (g.isLexer()) {
        assignLexerTokenTypes(g, collector.tokensDefs);
    } else {
        assignTokenTypes(g, collector.tokensDefs, collector.tokenIDRefs, collector.terminals);
    }
    symcheck.checkForModeConflicts(g);
    assignChannelTypes(g, collector.channelDefs);
    // CHECK RULE REFS NOW (that we've defined rules in grammar)
    symcheck.checkRuleArgs(g, collector.rulerefs);
    identifyStartRules(collector);
    symcheck.checkForQualifiedRuleIssues(g, collector.qualifiedRulerefs);
    // don't continue if we got symbol errors
    if (g.tool.getNumErrors() > 0)
        return;
    // CHECK ATTRIBUTE EXPRESSIONS FOR SEMANTIC VALIDITY
    AttributeChecks.checkAllAttributeExpressions(g);
    UseDefAnalyzer.trackTokenRuleRefsInActions(g);
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) LeftRecursiveRuleTransformer(org.antlr.v4.analysis.LeftRecursiveRuleTransformer) Rule(org.antlr.v4.tool.Rule)

Aggregations

LeftRecursiveRuleTransformer (org.antlr.v4.analysis.LeftRecursiveRuleTransformer)1 Rule (org.antlr.v4.tool.Rule)1 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)1