1 /* 2 * Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. 3 * Use of this file is governed by the BSD 3-clause license that 4 * can be found in the LICENSE.txt file in the project root. 5 */ 6 7 package org.antlr.v4.runtime.atn; 8 9 import org.antlr.v4.runtime.dfa.DFA; 10 11 import java.util.ArrayList; 12 import java.util.List; 13 14 /** 15 * This class provides access to specific and aggregate statistics gathered 16 * during profiling of a parser. 17 * 18 * @since 4.3 19 */ 20 public class ParseInfo { 21 protected final ProfilingATNSimulator atnSimulator; 22 ParseInfo(ProfilingATNSimulator atnSimulator)23 public ParseInfo(ProfilingATNSimulator atnSimulator) { 24 this.atnSimulator = atnSimulator; 25 } 26 27 /** 28 * Gets an array of {@link DecisionInfo} instances containing the profiling 29 * information gathered for each decision in the ATN. 30 * 31 * @return An array of {@link DecisionInfo} instances, indexed by decision 32 * number. 33 */ getDecisionInfo()34 public DecisionInfo[] getDecisionInfo() { 35 return atnSimulator.getDecisionInfo(); 36 } 37 38 /** 39 * Gets the decision numbers for decisions that required one or more 40 * full-context predictions during parsing. These are decisions for which 41 * {@link DecisionInfo#LL_Fallback} is non-zero. 42 * 43 * @return A list of decision numbers which required one or more 44 * full-context predictions during parsing. 45 */ getLLDecisions()46 public List<Integer> getLLDecisions() { 47 DecisionInfo[] decisions = atnSimulator.getDecisionInfo(); 48 List<Integer> LL = new ArrayList<Integer>(); 49 for (int i=0; i<decisions.length; i++) { 50 long fallBack = decisions[i].LL_Fallback; 51 if ( fallBack>0 ) LL.add(i); 52 } 53 return LL; 54 } 55 56 /** 57 * Gets the total time spent during prediction across all decisions made 58 * during parsing. This value is the sum of 59 * {@link DecisionInfo#timeInPrediction} for all decisions. 60 */ getTotalTimeInPrediction()61 public long getTotalTimeInPrediction() { 62 DecisionInfo[] decisions = atnSimulator.getDecisionInfo(); 63 long t = 0; 64 for (int i=0; i<decisions.length; i++) { 65 t += decisions[i].timeInPrediction; 66 } 67 return t; 68 } 69 70 /** 71 * Gets the total number of SLL lookahead operations across all decisions 72 * made during parsing. This value is the sum of 73 * {@link DecisionInfo#SLL_TotalLook} for all decisions. 74 */ getTotalSLLLookaheadOps()75 public long getTotalSLLLookaheadOps() { 76 DecisionInfo[] decisions = atnSimulator.getDecisionInfo(); 77 long k = 0; 78 for (int i = 0; i < decisions.length; i++) { 79 k += decisions[i].SLL_TotalLook; 80 } 81 return k; 82 } 83 84 /** 85 * Gets the total number of LL lookahead operations across all decisions 86 * made during parsing. This value is the sum of 87 * {@link DecisionInfo#LL_TotalLook} for all decisions. 88 */ getTotalLLLookaheadOps()89 public long getTotalLLLookaheadOps() { 90 DecisionInfo[] decisions = atnSimulator.getDecisionInfo(); 91 long k = 0; 92 for (int i = 0; i < decisions.length; i++) { 93 k += decisions[i].LL_TotalLook; 94 } 95 return k; 96 } 97 98 /** 99 * Gets the total number of ATN lookahead operations for SLL prediction 100 * across all decisions made during parsing. 101 */ getTotalSLLATNLookaheadOps()102 public long getTotalSLLATNLookaheadOps() { 103 DecisionInfo[] decisions = atnSimulator.getDecisionInfo(); 104 long k = 0; 105 for (int i = 0; i < decisions.length; i++) { 106 k += decisions[i].SLL_ATNTransitions; 107 } 108 return k; 109 } 110 111 /** 112 * Gets the total number of ATN lookahead operations for LL prediction 113 * across all decisions made during parsing. 114 */ getTotalLLATNLookaheadOps()115 public long getTotalLLATNLookaheadOps() { 116 DecisionInfo[] decisions = atnSimulator.getDecisionInfo(); 117 long k = 0; 118 for (int i = 0; i < decisions.length; i++) { 119 k += decisions[i].LL_ATNTransitions; 120 } 121 return k; 122 } 123 124 /** 125 * Gets the total number of ATN lookahead operations for SLL and LL 126 * prediction across all decisions made during parsing. 127 * 128 * <p> 129 * This value is the sum of {@link #getTotalSLLATNLookaheadOps} and 130 * {@link #getTotalLLATNLookaheadOps}.</p> 131 */ getTotalATNLookaheadOps()132 public long getTotalATNLookaheadOps() { 133 DecisionInfo[] decisions = atnSimulator.getDecisionInfo(); 134 long k = 0; 135 for (int i = 0; i < decisions.length; i++) { 136 k += decisions[i].SLL_ATNTransitions; 137 k += decisions[i].LL_ATNTransitions; 138 } 139 return k; 140 } 141 142 /** 143 * Gets the total number of DFA states stored in the DFA cache for all 144 * decisions in the ATN. 145 */ getDFASize()146 public int getDFASize() { 147 int n = 0; 148 DFA[] decisionToDFA = atnSimulator.decisionToDFA; 149 for (int i = 0; i < decisionToDFA.length; i++) { 150 n += getDFASize(i); 151 } 152 return n; 153 } 154 155 /** 156 * Gets the total number of DFA states stored in the DFA cache for a 157 * particular decision. 158 */ getDFASize(int decision)159 public int getDFASize(int decision) { 160 DFA decisionToDFA = atnSimulator.decisionToDFA[decision]; 161 return decisionToDFA.states.size(); 162 } 163 } 164