Examples of DFA


Examples of org.antlr.v4.runtime.dfa.DFA

  }

  @Override
  public void clearDFA() {
    for (int d = 0; d < decisionToDFA.length; d++) {
      decisionToDFA[d] = new DFA(atn.getDecisionState(d), d);
    }
  }
View Full Code Here

Examples of org.antlr.v4.runtime.dfa.DFA

      proposed.isAcceptState = true;
      proposed.lexerActionExecutor = ((LexerATNConfig)firstConfigWithRuleStopState).getLexerActionExecutor();
      proposed.prediction = atn.ruleToTokenType[firstConfigWithRuleStopState.state.ruleIndex];
    }

    DFA dfa = decisionToDFA[mode];
    synchronized (dfa.states) {
      DFAState existing = dfa.states.get(proposed);
      if ( existing!=null ) return existing;

      DFAState newState = proposed;
View Full Code Here

Examples of org.antlr.v4.runtime.dfa.DFA

          if (CLEAR_DFA) {
            int index = FILE_GRANULARITY ? 0 : ((NumberedThread)Thread.currentThread()).getThreadNumber();
            if (sharedLexers.length > 0 && sharedLexers[index] != null) {
              ATN atn = sharedLexers[index].getATN();
              for (int j = 0; j < sharedLexers[index].getInterpreter().decisionToDFA.length; j++) {
                sharedLexers[index].getInterpreter().decisionToDFA[j] = new DFA(atn.getDecisionState(j), j);
              }
            }

            if (sharedParsers.length > 0 && sharedParsers[index] != null) {
              ATN atn = sharedParsers[index].getATN();
              for (int j = 0; j < sharedParsers[index].getInterpreter().decisionToDFA.length; j++) {
                sharedParsers[index].getInterpreter().decisionToDFA[j] = new DFA(atn.getDecisionState(j), j);
              }
            }

            if (FILE_GRANULARITY) {
              Arrays.fill(sharedLexers, null);
View Full Code Here

Examples of org.antlr.v4.runtime.dfa.DFA

        int states = 0;
        int configs = 0;
        Set<ATNConfig> uniqueConfigs = new HashSet<ATNConfig>();

        for (int i = 0; i < modeToDFA.length; i++) {
          DFA dfa = modeToDFA[i];
          if (dfa == null) {
            continue;
          }

          states += dfa.states.size();
          for (DFAState state : dfa.states.values()) {
            configs += state.configs.size();
            uniqueConfigs.addAll(state.configs);
          }
        }

        System.out.format("There are %d lexer DFAState instances, %d configs (%d unique).%n", states, configs, uniqueConfigs.size());

        if (DETAILED_DFA_STATE_STATS) {
          System.out.format("\tMode\tStates\tConfigs\tMode%n");
          for (int i = 0; i < modeToDFA.length; i++) {
            DFA dfa = modeToDFA[i];
            if (dfa == null || dfa.states.isEmpty()) {
              continue;
            }

            int modeConfigs = 0;
            for (DFAState state : dfa.states.values()) {
              modeConfigs += state.configs.size();
            }

            String modeName = lexer.getModeNames()[i];
            System.out.format("\t%d\t%d\t%d\t%s%n", dfa.decision, dfa.states.size(), modeConfigs, modeName);
          }
        }
      }
    }

    if (RUN_PARSER && sharedParsers.length > 0) {
      int index = FILE_GRANULARITY ? 0 : ((NumberedThread)Thread.currentThread()).getThreadNumber();
      Parser parser = sharedParsers[index];
            // make sure the individual DFAState objects actually have unique ATNConfig arrays
            final ParserATNSimulator interpreter = parser.getInterpreter();
            final DFA[] decisionToDFA = interpreter.decisionToDFA;

            if (SHOW_DFA_STATE_STATS) {
                int states = 0;
        int configs = 0;
        Set<ATNConfig> uniqueConfigs = new HashSet<ATNConfig>();

                for (int i = 0; i < decisionToDFA.length; i++) {
                    DFA dfa = decisionToDFA[i];
                    if (dfa == null) {
                        continue;
                    }

                    states += dfa.states.size();
          for (DFAState state : dfa.states.values()) {
            configs += state.configs.size();
            uniqueConfigs.addAll(state.configs);
          }
                }

                System.out.format("There are %d parser DFAState instances, %d configs (%d unique).%n", states, configs, uniqueConfigs.size());

        if (DETAILED_DFA_STATE_STATS) {
          if (COMPUTE_TRANSITION_STATS) {
            System.out.format("\tDecision\tStates\tConfigs\tPredict (ALL)\tPredict (LL)\tNon-SLL\tTransitions\tTransitions (ATN)\tTransitions (LL)\tLA (SLL)\tLA (LL)\tRule%n");
          }
          else {
            System.out.format("\tDecision\tStates\tConfigs\tRule%n");
          }

          for (int i = 0; i < decisionToDFA.length; i++) {
            DFA dfa = decisionToDFA[i];
            if (dfa == null || dfa.states.isEmpty()) {
              continue;
            }

            int decisionConfigs = 0;
            for (DFAState state : dfa.states.values()) {
              decisionConfigs += state.configs.size();
            }

            String ruleName = parser.getRuleNames()[parser.getATN().decisionToState.get(dfa.decision).ruleIndex];

            long calls = 0;
            long fullContextCalls = 0;
            long nonSllCalls = 0;
            long transitions = 0;
            long computedTransitions = 0;
            long fullContextTransitions = 0;
            double lookahead = 0;
            double fullContextLookahead = 0;
            String formatString;
            if (COMPUTE_TRANSITION_STATS) {
              for (long[] data : decisionInvocationsPerFile[currentPass]) {
                calls += data[i];
              }

              for (long[] data : fullContextFallbackPerFile[currentPass]) {
                fullContextCalls += data[i];
              }

              for (long[] data : nonSllPerFile[currentPass]) {
                nonSllCalls += data[i];
              }

              for (long[] data : totalTransitionsPerDecisionPerFile[currentPass]) {
                transitions += data[i];
              }

              for (long[] data : computedTransitionsPerDecisionPerFile[currentPass]) {
                computedTransitions += data[i];
              }

              for (long[] data : fullContextTransitionsPerDecisionPerFile[currentPass]) {
                fullContextTransitions += data[i];
              }

              if (calls > 0) {
                lookahead = (double)(transitions - fullContextTransitions) / (double)calls;
              }

              if (fullContextCalls > 0) {
                fullContextLookahead = (double)fullContextTransitions / (double)fullContextCalls;
              }

              formatString = "\t%1$d\t%2$d\t%3$d\t%4$d\t%5$d\t%6$d\t%7$d\t%8$d\t%9$d\t%10$f\t%11$f\t%12$s%n";
            }
            else {
              calls = 0;
              formatString = "\t%1$d\t%2$d\t%3$d\t%12$s%n";
            }

            System.out.format(formatString, dfa.decision, dfa.states.size(), decisionConfigs, calls, fullContextCalls, nonSllCalls, transitions, computedTransitions, fullContextTransitions, lookahead, fullContextLookahead, ruleName);
          }
        }
            }

            int localDfaCount = 0;
            int globalDfaCount = 0;
            int localConfigCount = 0;
            int globalConfigCount = 0;
            int[] contextsInDFAState = new int[0];

            for (int i = 0; i < decisionToDFA.length; i++) {
                DFA dfa = decisionToDFA[i];
                if (dfa == null) {
                    continue;
                }

                if (SHOW_CONFIG_STATS) {
View Full Code Here

Examples of org.antlr.v4.runtime.dfa.DFA

            lexer.addErrorListener(DescriptiveErrorListener.INSTANCE);

            if (lexer.getInterpreter().decisionToDFA[0] == null) {
              ATN atn = lexer.getATN();
              for (int i = 0; i < lexer.getInterpreter().decisionToDFA.length; i++) {
                lexer.getInterpreter().decisionToDFA[i] = new DFA(atn.getDecisionState(i), i);
              }
            }

                        CommonTokenStream tokens = new CommonTokenStream(lexer);
                        tokens.fill();
                        tokenCount.addAndGet(currentPass, tokens.size());

            if (COMPUTE_CHECKSUM) {
              for (Token token : tokens.getTokens()) {
                updateChecksum(checksum, token);
              }
            }

                        if (!RUN_PARSER) {
                            return new FileParseResult(input.getSourceName(), (int)checksum.getValue(), null, tokens.size(), startTime, lexer, null);
                        }

            final long parseStartTime = System.nanoTime();
            Parser parser = sharedParsers[thread];
                        if (REUSE_PARSER && parser != null) {
                            parser.setInputStream(tokens);
                        } else {
              Parser previousParser = parser;

              if (USE_PARSER_INTERPRETER) {
                Parser referenceParser = parserCtor.newInstance(tokens);
                parser = new ParserInterpreter(referenceParser.getGrammarFileName(), referenceParser.getVocabulary(), Arrays.asList(referenceParser.getRuleNames()), referenceParser.getATN(), tokens);
              }
              else {
                parser = parserCtor.newInstance(tokens);
              }

              DFA[] decisionToDFA = (FILE_GRANULARITY || previousParser == null ? parser : previousParser).getInterpreter().decisionToDFA;
              if (!REUSE_PARSER_DFA || (!FILE_GRANULARITY && previousParser == null)) {
                decisionToDFA = new DFA[decisionToDFA.length];
              }

              if (COMPUTE_TRANSITION_STATS) {
                parser.setInterpreter(new StatisticsParserATNSimulator(parser, parser.getATN(), decisionToDFA, parser.getInterpreter().getSharedContextCache()));
              } else if (!REUSE_PARSER_DFA) {
                parser.setInterpreter(new ParserATNSimulator(parser, parser.getATN(), decisionToDFA, parser.getInterpreter().getSharedContextCache()));
              }

              sharedParsers[thread] = parser;
                        }

            parser.removeParseListeners();
            parser.removeErrorListeners();
            if (!TWO_STAGE_PARSING) {
              parser.addErrorListener(DescriptiveErrorListener.INSTANCE);
              parser.addErrorListener(new SummarizingDiagnosticErrorListener());
            }

            if (parser.getInterpreter().decisionToDFA[0] == null) {
              ATN atn = parser.getATN();
              for (int i = 0; i < parser.getInterpreter().decisionToDFA.length; i++) {
                parser.getInterpreter().decisionToDFA[i] = new DFA(atn.getDecisionState(i), i);
              }
            }

            parser.getInterpreter().setPredictionMode(TWO_STAGE_PARSING ? PredictionMode.SLL : PREDICTION_MODE);
            parser.setBuildParseTree(BUILD_PARSE_TREES);
View Full Code Here

Examples of org.antlr.v4.runtime.dfa.DFA

                  String inputString, int expectedAlt)
  {
    Tool.internalOption_ShowATNConfigsInDFA = true;
    ATN lexatn = createATN(lg, true);
    LexerATNSimulator lexInterp =
      new LexerATNSimulator(lexatn,new DFA[] { new DFA(lexatn.modeToStartState.get(Lexer.DEFAULT_MODE)) },new PredictionContextCache());
    IntegerList types = getTokenTypesViaATN(inputString, lexInterp);
    System.out.println(types);

    semanticProcess(lg);
    g.importVocab(lg);
View Full Code Here

Examples of org.antlr.v4.runtime.dfa.DFA

                   String[] inputString, String[] dfaString)
  {
//    Tool.internalOption_ShowATNConfigsInDFA = true;
    ATN lexatn = createATN(lg, true);
    LexerATNSimulator lexInterp =
      new LexerATNSimulator(lexatn,new DFA[] { new DFA(lexatn.getDecisionState(Lexer.DEFAULT_MODE)) }, new PredictionContextCache());

    semanticProcess(lg);
    g.importVocab(lg);
    semanticProcess(g);

    ParserInterpreterForTesting interp = new ParserInterpreterForTesting(g, null);
    for (int i=0; i<inputString.length; i++) {
      // Check DFA
      IntegerList types = getTokenTypesViaATN(inputString[i], lexInterp);
      System.out.println(types);
      TokenStream input = new IntTokenStream(types);
      try {
        interp.adaptivePredict(input, decision, ParserRuleContext.EMPTY);
      }
      catch (NoViableAltException nvae) {
        nvae.printStackTrace(System.err);
      }
      DFA dfa = interp.parser.decisionToDFA[decision];
      assertEquals(dfaString[i], dfa.toString(g.getVocabulary()));
    }
  }
View Full Code Here

Examples of org.antlr.v4.runtime.dfa.DFA

      super(input);
      this.g = g;
      this.atn = atn;
      this.decisionToDFA = new DFA[atn.getNumberOfDecisions()];
      for (int i = 0; i < decisionToDFA.length; i++) {
        decisionToDFA[i] = new DFA(atn.getDecisionState(i), i);
      }
    }
View Full Code Here

Examples of org.antlr.v4.runtime.dfa.DFA

  public List<String> getTokenTypes(LexerGrammar lg,
                    ATN atn,
                    CharStream input)
  {
    LexerATNSimulator interp = new LexerATNSimulator(atn,new DFA[] { new DFA(atn.modeToStartState.get(Lexer.DEFAULT_MODE)) },null);
    List<String> tokenTypes = new ArrayList<String>();
    int ttype;
    boolean hitEOF = false;
    do {
      if ( hitEOF ) {
View Full Code Here

Examples of org.antlr.v4.runtime.dfa.DFA

  }

  void checkRuleDFA(Grammar g, DecisionState blk, String expecting)
    throws Exception
  {
    DFA dfa = createDFA(g, blk);
    String result = null;
    if ( dfa!=null ) result = dfa.toString();
    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.