Examples of DFA


Examples of org.antlr.analysis.DFA

            throw new Exception("No decision in the current line");

        CodeGenerator generator = new CodeGenerator(new Tool(), g,
                (String) g.getOption("language"));

        DFA dfa = g.getLookaheadDFAFromPositionInFile(line, adjustedColumn);
        decisionNumber = dfa.getDecisionNumber();
        DOTGenerator dg = new DOTGenerator(g);
        g.setCodeGenerator(generator);
        dg.setArrowheadType("none");
        dg.setRankdir("LR");    // Left-to-right
        return dg.getDOT( dfa.startState );
View Full Code Here

Examples of org.antlr.analysis.DFA

    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 += ", ";
                info += dfa.getNumberOfStates()+" 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+")");
View Full Code Here

Examples of org.antlr.analysis.DFA

        }
        return items;
    }

    public DFA getDFAAtPosition(int line, int column) {
        DFA dfa = null;
        if(discoveredParserGrammar != null) {
            dfa = discoveredParserGrammar.getLookaheadDFAFromPositionInFile(line, column);
        }
        if(dfa == null) {
            if(discoveredLexerGrammar != null) {
View Full Code Here

Examples of org.antlr.analysis.DFA

        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");
        dg.setRankdir("LR");    // Left-to-right
        return dg.getDOT( dfa.startState );
View Full Code Here

Examples of org.antlr.analysis.DFA

        s.nfa.grammar.getTokenDisplayName(t));
        */
      // CASE 1: decision state
      if ( s.getDecisionNumber()>0 && s.nfa.grammar.getNumberOfAltsForDecisionNFA(s)>1 ) {
        // decision point, must predict and jump to alt
        DFA dfa = s.nfa.grammar.getLookaheadDFA(s.getDecisionNumber());
        /*
        if ( s.nfa.grammar.type!=Grammar.LEXER ) {
          System.out.println("decision: "+
                   dfa.getNFADecisionStartState().getDescription()+
                   " input="+s.nfa.grammar.getTokenDisplayName(t));
        }
        */
        int m = input.mark();
        int predictedAlt = predict(dfa);
        if ( predictedAlt == NFA.INVALID_ALT_NUMBER ) {
          String description = dfa.getNFADecisionStartState().getDescription();
          NoViableAltException nvae =
            new NoViableAltException(description,
                            dfa.getDecisionNumber(),
                            s.stateNumber,
                            input);
          if ( actions!=null ) {
            actions.recognitionException(nvae);
          }
View Full Code Here

Examples of org.antlr.analysis.DFA

        if ( !externalAnalysisAbort && decisionStartState.getNumberOfTransitions()>1 ) {
          Rule r = decisionStartState.enclosingRule;
          if ( r.isSynPred && !synPredNamesUsedInDFA.contains(r.name) ) {
            continue;
          }
          DFA dfa = null;
          // if k=* or k=1, try LL(1)
          if ( getUserMaxLookahead(decision)==0 ||
             getUserMaxLookahead(decision)==1 )
          {
            dfa = createLL_1_LookaheadDFA(decision);
View Full Code Here

Examples of org.antlr.analysis.DFA

    if ( decisionIsLL_1 && !foundConfoundingPredicate ) {
      // build an LL(1) optimized DFA with edge for each altLook[i]
      if ( NFAToDFAConverter.debug ) {
        System.out.println("decision "+decision+" is simple LL(1)");
      }
      DFA lookaheadDFA = new LL1DFA(decision, decisionStartState, altLook);
      setLookaheadDFA(decision, lookaheadDFA);
      updateLineColumnToLookaheadDFAMap(lookaheadDFA);
      return lookaheadDFA;
    }

    // not LL(1) but perhaps we can solve with simplified predicate search
    // even if k=1 set manually, only resolve here if we have preds; i.e.,
    // don't resolve etc...

    /*
    SemanticContext visiblePredicates =
      ll1Analyzer.getPredicates(decisionStartState);
    boolean foundConfoundingPredicate =
      ll1Analyzer.detectConfoundingPredicates(decisionStartState);
      */

    // exit if not forced k=1 or we found a predicate situation we
    // can't handle: predicates in rules invoked from this decision.
    if ( getUserMaxLookahead(decision)!=1 || // not manually set to k=1
       !getAutoBacktrackMode(decision) ||
       foundConfoundingPredicate )
    {
      //System.out.println("trying LL(*)");
      return null;
    }

    List<IntervalSet> edges = new ArrayList<IntervalSet>();
    for (int i = 1; i < altLook.length; i++) {
      LookaheadSet s = altLook[i];
      edges.add(s.tokenTypeSet);
    }
    List<IntervalSet> disjoint = makeEdgeSetsDisjoint(edges);
    //System.out.println("disjoint="+disjoint);

    MultiMap<IntervalSet, Integer> edgeMap = new MultiMap<IntervalSet, Integer>();
    for (int i = 0; i < disjoint.size(); i++) {
      IntervalSet ds = disjoint.get(i);
      for (int alt = 1; alt < altLook.length; alt++) {
        LookaheadSet look = altLook[alt];
        if ( !ds.and(look.tokenTypeSet).isNil() ) {
          edgeMap.map(ds, alt);
        }
      }
    }
    //System.out.println("edge map: "+edgeMap);

    // TODO: how do we know we covered stuff?

    // build an LL(1) optimized DFA with edge for each altLook[i]
    DFA lookaheadDFA = new LL1DFA(decision, decisionStartState, edgeMap);
    setLookaheadDFA(decision, lookaheadDFA);

    // create map from line:col to decision DFA (for ANTLRWorks)
    updateLineColumnToLookaheadDFAMap(lookaheadDFA);

View Full Code Here

Examples of org.antlr.analysis.DFA

                 +decisionStartState.getDecisionNumber()+") for "+
                 decisionStartState.getDescription());
      startDFA = System.currentTimeMillis();
    }

    DFA lookaheadDFA = new DFA(decision, decisionStartState);
    // Retry to create a simpler DFA if analysis failed (non-LL(*),
    // recursion overflow, or time out).
    boolean failed =
      lookaheadDFA.probe.isNonLLStarDecision() ||
      lookaheadDFA.probe.analysisOverflowed();
    if ( failed && lookaheadDFA.okToRetryDFAWithK1() ) {
      // set k=1 option and try again.
      // First, clean up tracking stuff
      decisionsWhoseDFAsUsesSynPreds.remove(lookaheadDFA);
      // TODO: clean up synPredNamesUsedInDFA also (harder)
      d.blockAST.setBlockOption(this, "k", Utils.integer(1));
      if ( composite.watchNFAConversion ) {
        System.out.print("trying decision "+decision+
                 " again with k=1; reason: "+
                 lookaheadDFA.getReasonForFailure());
      }
      lookaheadDFA = null; // make sure other memory is "free" before redoing
      lookaheadDFA = new DFA(decision, decisionStartState);
    }

    setLookaheadDFA(decision, lookaheadDFA);

    if ( wackTempStructures ) {
      for (DFAState s : lookaheadDFA.getUniqueStates().values()) {
        s.reset();
      }
    }

    // create map from line:col to decision DFA (for ANTLRWorks)
    updateLineColumnToLookaheadDFAMap(lookaheadDFA);

    if ( composite.watchNFAConversion ) {
      stopDFA = System.currentTimeMillis();
      System.out.println("cost: "+lookaheadDFA.getNumberOfStates()+
                 " states, "+(int)(stopDFA-startDFA)+" ms");
    }
    //System.out.println("after create DFA; synPredNamesUsedInDFA="+synPredNamesUsedInDFA);
    return lookaheadDFA;
  }
View Full Code Here

Examples of org.antlr.analysis.DFA

  }

  protected ST genSemanticPredicateExpr(STGroup templates,
                            Transition edge)
  {
    DFA dfa = ((DFAState)edge.target).dfa; // which DFA are we in
    Label label = edge.label;
    SemanticContext semCtx = label.getSemanticContext();
    return semCtx.genExpr(this,templates,dfa);
  }
View Full Code Here

Examples of org.antlr.analysis.DFA

    // This is ok because we've already verified there are no problems
    // with the enter/exit decision
    DFAOptimizer optimizer = new DFAOptimizer(g);
    optimizer.optimize();
    FASerializer serializer = new FASerializer(g);
    DFA dfa = g.getLookaheadDFA(1);
    String result = serializer.serialize(dfa.startState);
    expecting = ".s0-'x'->:s1=>1\n";
    assertEquals(expecting, result);
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.