Examples of DFA


Examples of org.antlr.analysis.DFA

    }

    assertEquals("unexpected number of expected problems",
           expectingNumWarnings, equeue.size());

    DFA dfa = g.getLookaheadDFA(decision);
    FASerializer serializer = new FASerializer(g);
    String result = serializer.serialize(dfa.startState);
    //System.out.print(result);
    List unreachableAlts = dfa.getUnreachableAlts();

    // make sure unreachable alts are as expected
    if ( expectingUnreachableAlts!=null ) {
      BitSet s = new BitSet();
      s.addAll(expectingUnreachableAlts);
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

Examples of org.antlr.analysis.DFA

      g.setCodeGenerator(generator);
      g.buildNFA();
      g.createLookaheadDFAs(false);
    }

    DFA dfa = g.getLookaheadDFA(decision);
    assertNotNull("unknown decision #"+decision, dfa);
    FASerializer serializer = new FASerializer(g);
    String result = serializer.serialize(dfa.startState);
    //System.out.print(result);
    List nonDetAlts = dfa.getUnreachableAlts();
    //System.out.println("alts w/o predict state="+nonDetAlts);

    // first make sure nondeterministic alts are as expected
    if ( expectingUnreachableAlts==null ) {
      if ( nonDetAlts!=null && nonDetAlts.size()!=0 ) {
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((IntervalSet)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 = (IntervalSet) 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

        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

    }

    assertEquals("unexpected number of expected problems",
           expectingNumWarnings, equeue.size());

    DFA dfa = g.getLookaheadDFA(decision);
    assertNotNull("no DFA for decision "+decision, dfa);
    FASerializer serializer = new FASerializer(g);
    String result = serializer.serialize(dfa.startState);

    List unreachableAlts = dfa.getUnreachableAlts();

    // make sure unreachable alts are as expected
    if ( expectingUnreachableAlts!=null ) {
      BitSet s = new BitSet();
      s.addAll(expectingUnreachableAlts);
View Full Code Here

Examples of org.antlr.analysis.DFA

        Grammar g = new Grammar(
                "lexer grammar t;\n"+
                "A : {;}'a' | 'b' | 'c';");
    g.buildNFA();
    g.createLookaheadDFAs(false);
        DFA dfa = g.getLookaheadDFA(1);
        checkPrediction(dfa,"a",1);
        checkPrediction(dfa,"b",2);
        checkPrediction(dfa,"c",3);
        checkPrediction(dfa,"d", NFA.INVALID_ALT_NUMBER);
    }
View Full Code Here

Examples of org.antlr.analysis.DFA

        Grammar g = new Grammar(
                "lexer grammar t;\n"+
                "A : {;}'a'..'z' | ';' | '0'..'9' ;");
    g.buildNFA();
        g.createLookaheadDFAs(false);
        DFA dfa = g.getLookaheadDFA(1);
        checkPrediction(dfa,"a",1);
        checkPrediction(dfa,"q",1);
        checkPrediction(dfa,"z",1);
        checkPrediction(dfa,";",2);
        checkPrediction(dfa,"9",3);
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.