Search in sources :

Example 1 with Grammar

use of org.antlr.tool.Grammar in project antlrworks by antlr.

the class TokensDFA method getDOTString.

@Override
public String getDOTString() throws Exception {
    ANTLRGrammarEngine eg = window.getGrammarEngine().getANTLRGrammarEngine();
    eg.analyze();
    Grammar g = eg.getLexerGrammar();
    if (g == null) {
        throw new Exception("Cannot show tokens DFA because there is no lexer grammar");
    }
    Rule r = g.getRule(Grammar.ARTIFICIAL_TOKENS_RULENAME);
    NFAState s = (NFAState) r.startState.transition(0).target;
    DFA dfa = g.getLookaheadDFA(s.getDecisionNumber());
    DOTGenerator dg = new DOTGenerator(g);
    dg.setArrowheadType("none");
    // Left-to-right
    dg.setRankdir("LR");
    return dg.getDOT(dfa.startState);
}
Also used : ANTLRGrammarEngine(org.antlr.works.grammar.antlr.ANTLRGrammarEngine) DOTGenerator(org.antlr.tool.DOTGenerator) NFAState(org.antlr.analysis.NFAState) Grammar(org.antlr.tool.Grammar) Rule(org.antlr.tool.Rule) DFA(org.antlr.analysis.DFA)

Example 2 with Grammar

use of org.antlr.tool.Grammar in project antlrworks by antlr.

the class DecisionDFAEngine method getDecisionDFAItems.

public List<DecisionDFAItem> getDecisionDFAItems() {
    List<DecisionDFAItem> items = new ArrayList<DecisionDFAItem>();
    for (int lineIndex : decisionDFA.keySet()) {
        for (int columnIndex : decisionDFA.get(lineIndex)) {
            DFA dfa = getDFAAtPosition(lineIndex, columnIndex);
            if (dfa == null) {
                System.err.println("DFA is null for line " + lineIndex + " and column " + columnIndex);
                continue;
            }
            Grammar g = discoveredLexerGrammar;
            if (g != null) {
                Rule r = g.getRule(Grammar.ARTIFICIAL_TOKENS_RULENAME);
                NFAState s = (NFAState) r.startState.transition(0).target;
                if (s == null) {
                    System.err.println("NFAState s is null for rule " + r.name);
                    continue;
                }
                // Ignore tokens DFA
                if (dfa.getDecisionNumber() == s.getDecisionNumber())
                    continue;
            }
            Color c = new Color(0, 128, 64);
            String title = "DFA decision " + dfa.getDecisionNumber();
            String info = "";
            if (usesSemPreds.contains(dfa.getDecisionNumber())) {
                info += "uses semantic predicate";
                c = new Color(255, 220, 0);
            } else if (usesSynPreds.contains(dfa.getDecisionNumber())) {
                info += "uses syntactic predicate";
                c = new Color(255, 220, 0);
            }
            if (dfa.isCyclic()) {
                if (info.length() > 0)
                    info += ", ";
                info += "cyclic";
            }
            if (info.length() > 0)
                info += ", ";
            if (dfa.getNumberOfStates() != 0) {
                info += dfa.getNumberOfStates() + " states";
            } else {
                info += "<=" + dfa.getMaxStateNumber() + " states";
            }
            Point p = window.textEditor.getLineTextPositionsAtLineIndex(lineIndex - 1);
            if (p != null) {
                DecisionDFAItem item = new DecisionDFAItem(window);
                item.setAttributes(null, p.x + columnIndex - 1, p.x + columnIndex, lineIndex - 1, c, title + " (" + info + ")");
                item.shape = ATEOverlayManager.SHAPE_RECT;
                items.add(item);
            }
        }
    }
    return items;
}
Also used : NFAState(org.antlr.analysis.NFAState) Grammar(org.antlr.tool.Grammar) Rule(org.antlr.tool.Rule) DFA(org.antlr.analysis.DFA)

Example 3 with Grammar

use of org.antlr.tool.Grammar in project antlrworks by antlr.

the class GrammarEngineImpl method getGeneratedClassName.

public String getGeneratedClassName(int type) throws Exception {
    String name = null;
    antlrEngine.createGrammars();
    if (type == ElementGrammarName.LEXER) {
        Grammar g = antlrEngine.getLexerGrammar();
        if (g == null)
            return null;
        name = g.name + getSuffix(type);
    } else if (type == ElementGrammarName.PARSER) {
        Grammar g = antlrEngine.getParserGrammar();
        if (g == null)
            return null;
        name = g.name + getSuffix(type);
    } else if (type == ElementGrammarName.TREEPARSER) {
        Grammar g = antlrEngine.getParserGrammar();
        if (g == null)
            return null;
        if (!isTreeParserGrammar())
            return null;
        name = g.name + getSuffix(type);
    }
    return name;
}
Also used : Grammar(org.antlr.tool.Grammar)

Example 4 with Grammar

use of org.antlr.tool.Grammar in project antlrworks by antlr.

the class InterpreterTab method process.

protected void process() {
    progress.setInfo("Interpreting...");
    window.consoleTab.println("Interpreting...");
    CharStream input = new ANTLRStringStream(Utils.convertRawTextWithEOL(textPane.getText(), eolCombo));
    ANTLRGrammarEngine eg = window.getGrammarEngine().getANTLRGrammarEngine();
    try {
        eg.createGrammars();
    } catch (Exception e) {
        window.consoleTab.println(e);
        return;
    }
    Grammar parser = eg.getParserGrammar();
    Grammar lexer = eg.getLexerGrammar();
    if (lexer == null) {
        throw new RuntimeException("Lexer is null. Check the grammar before running the interpreterTab.");
    }
    Interpreter lexEngine = new CustomInterpreter(lexer, input);
    FilteringTokenStream tokens = new FilteringTokenStream(lexEngine);
    StringTokenizer tk = new StringTokenizer(tokensToIgnoreLabel.getText(), " ");
    while (tk.hasMoreTokens()) {
        String tokenName = tk.nextToken();
        tokens.setTokenTypeChannel(lexer.getTokenType(tokenName), Token.HIDDEN_CHANNEL);
    }
    Interpreter parseEngine = new CustomInterpreter(parser, tokens);
    ParseTree t = null;
    try {
        if (ATEToken.isLexerName(startSymbol)) {
            t = lexEngine.parse(startSymbol);
        } else {
            t = parseEngine.parse(startSymbol);
        }
    } catch (Exception e) {
        window.consoleTab.println(e);
    }
    if (parser != null && t != null) {
        SwingUtilities.invokeLater(new Refresh(parser, t));
    }
}
Also used : Interpreter(org.antlr.tool.Interpreter) Grammar(org.antlr.tool.Grammar) ANTLRGrammarEngine(org.antlr.works.grammar.antlr.ANTLRGrammarEngine) StringTokenizer(java.util.StringTokenizer) ParseTree(org.antlr.runtime.tree.ParseTree)

Example 5 with Grammar

use of org.antlr.tool.Grammar in project antlrworks by antlr.

the class GrammarPropertiesImpl method getAllGeneratedNames.

public List<String> getAllGeneratedNames() throws Exception {
    List<String> names = new ArrayList<String>();
    Grammar g = antlrEngine.getDefaultGrammar();
    if (g != null) {
        names.add(g.getRecognizerName());
        for (Grammar gd : g.getDelegates()) {
            names.add(gd.getRecognizerName());
        }
    }
    Grammar lexer = antlrEngine.getLexerGrammar();
    if (lexer != null) {
        names.add(lexer.getRecognizerName());
        for (Grammar gd : lexer.getDelegates()) {
            names.add(gd.getRecognizerName());
        }
    }
    return names;
}
Also used : Grammar(org.antlr.tool.Grammar)

Aggregations

Grammar (org.antlr.tool.Grammar)7 DFA (org.antlr.analysis.DFA)3 NFAState (org.antlr.analysis.NFAState)2 DOTGenerator (org.antlr.tool.DOTGenerator)2 Rule (org.antlr.tool.Rule)2 ANTLRGrammarEngine (org.antlr.works.grammar.antlr.ANTLRGrammarEngine)2 StringTokenizer (java.util.StringTokenizer)1 Tool (org.antlr.Tool)1 CodeGenerator (org.antlr.codegen.CodeGenerator)1 ParseTree (org.antlr.runtime.tree.ParseTree)1 Interpreter (org.antlr.tool.Interpreter)1