Search in sources :

Example 1 with LeftFactoringRuleTransformer

use of org.antlr.v4.analysis.LeftFactoringRuleTransformer in project antlr4 by tunnelvisionlabs.

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);
    // CLONE RULE ASTs FOR CONTEXT REFERENCE
    for (Rule rule : ruleCollector.rules.values()) {
        List<RuleAST> list = g.contextASTs.get(rule.getBaseContext());
        if (list == null) {
            list = new ArrayList<RuleAST>();
            g.contextASTs.put(rule.getBaseContext(), list);
        }
        list.add((RuleAST) rule.ast.dupTree());
    }
    // 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;
    }
    // AUTO LEFT FACTORING
    LeftFactoringRuleTransformer lftrans = new LeftFactoringRuleTransformer(g.ast, ruleCollector.rules, g);
    lftrans.translateLeftFactoredRules();
    // 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);
    symcheck.checkForUnreachableTokens(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 : RuleAST(org.antlr.v4.tool.ast.RuleAST) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) LeftRecursiveRuleTransformer(org.antlr.v4.analysis.LeftRecursiveRuleTransformer) Rule(org.antlr.v4.tool.Rule) LeftFactoringRuleTransformer(org.antlr.v4.analysis.LeftFactoringRuleTransformer)

Aggregations

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