Search in sources :

Example 1 with ANTLRGrammarEngine

use of org.antlr.works.grammar.antlr.ANTLRGrammarEngine 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 ANTLRGrammarEngine

use of org.antlr.works.grammar.antlr.ANTLRGrammarEngine in project antlrworks by antlr.

the class DecisionDFAEngine method discover.

private void discover(int start, int end) throws Exception {
    Set<Integer> lineIndexes = new HashSet<Integer>();
    for (int index = start; index < end; index++) {
        lineIndexes.add(window.getTextEditor().getLineIndexAtTextPosition(index));
    }
    ANTLRGrammarEngine antlrEngineGrammar = window.getGrammarEngine().getANTLRGrammarEngine();
    antlrEngineGrammar.analyze();
    discoveredLexerGrammar = antlrEngineGrammar.getLexerGrammar();
    discoveredParserGrammar = antlrEngineGrammar.getParserGrammar();
    decisionDFA.clear();
    usesSynPreds.clear();
    usesSemPreds.clear();
    discover(discoveredLexerGrammar, lineIndexes, usesSemPreds, usesSynPreds);
    discover(discoveredParserGrammar, lineIndexes, usesSemPreds, usesSynPreds);
}
Also used : ANTLRGrammarEngine(org.antlr.works.grammar.antlr.ANTLRGrammarEngine)

Example 3 with ANTLRGrammarEngine

use of org.antlr.works.grammar.antlr.ANTLRGrammarEngine 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)

Aggregations

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