public virtual NFAConfiguration AddNFAConfiguration(NFAState state, int alt, NFAContext context, SemanticContext semanticContext) { NFAConfiguration c = new NFAConfiguration(state.StateNumber, alt, context, semanticContext); AddNFAConfiguration(state, c); return(c); }
public static SemanticContext And(SemanticContext a, SemanticContext b) { //System.Console.Out.WriteLine( "AND: " + a + "&&" + b ); if (a == EmptySemanticContext || a == null) { return(b); } if (b == EmptySemanticContext || b == null) { return(a); } if (a.Equals(b)) { return(a); // if same, just return left one } //System.Console.Out.WriteLine( "## have to AND" ); return(new AND(a, b)); }
public static SemanticContext Or(SemanticContext a, SemanticContext b) { //System.Console.Out.WriteLine( "OR: " + a + "||" + b ); if (a == EmptySemanticContext || a == null) { return(b); } if (b == EmptySemanticContext || b == null) { return(a); } if (a is TruePredicate) { return(a); } if (b is TruePredicate) { return(b); } if (a is NOT && b is Predicate) { NOT n = (NOT)a; // check for !p||p if (n.ctx.Equals(b)) { return(new TruePredicate()); } } else if (b is NOT && a is Predicate) { NOT n = (NOT)b; // check for p||!p if (n.ctx.Equals(a)) { return(new TruePredicate()); } } else if (a.Equals(b)) { return(a); } //System.Console.Out.WriteLine( "## have to OR" ); return(new OR(a, b)); }
/** Two predicates are the same if they are literally the same * text rather than same node in the grammar's AST. * Or, if they have the same constant value, return equal. * As of July 2006 I'm not sure these are needed. */ public override bool Equals(SemanticContext other) { Predicate p = other as Predicate; if (p == null) { return(false); } if (this._constantValue != InvalidPredValue) { return(this._constantValue == p._constantValue); } else if (p._constantValue != InvalidPredValue) { return(false); } return(StringComparer.Ordinal.Equals(_predicateAST.Text, p._predicateAST.Text)); }
public OR(SemanticContext a, SemanticContext b) { _operands = new HashSet <object>(); if (a is OR) { _operands.addAll(((OR)a)._operands); } else if (a != null) { _operands.Add(a); } if (b is OR) { _operands.addAll(((OR)b)._operands); } else if (b != null) { _operands.Add(b); } }
/** For gated productions, we need an OR'd list of all predicates for the * target of an edge so we can gate the edge based upon the predicates * associated with taking that path (if any). * * For syntactic predicates, we only want to generate predicate * evaluations as it transitions to an accept state; waste to * do it earlier. So, only add gated preds derived from manually- * specified syntactic predicates if this is an accept state. * * Also, since configurations w/o gated predicates are like true * gated predicates, finding a configuration whose alt has no gated * predicate implies we should evaluate the predicate to true. This * means the whole edge has to be ungated. Consider: * * X : ('a' | {p}?=> 'a') * | 'a' 'b' * ; * * Here, you 'a' gets you from s0 to s1 but you can't test p because * plain 'a' is ok. It's also ok for starting alt 2. Hence, you can't * test p. Even on the edge going to accept state for alt 1 of X, you * can't test p. You can get to the same place with and w/o the context. * Therefore, it is never ok to test p in this situation. * * TODO: cache this as it's called a lot; or at least set bit if >1 present in state */ public virtual SemanticContext GetGatedPredicatesInNFAConfigurations() { SemanticContext unionOfPredicatesFromAllAlts = null; int numConfigs = _nfaConfigurations.Count; for (int i = 0; i < numConfigs; i++) { NFAConfiguration configuration = (NFAConfiguration)_nfaConfigurations[i]; SemanticContext gatedPredExpr = configuration.SemanticContext.GatedPredicateContext; if (gatedPredExpr == null) { // if we ever find a configuration w/o a gated predicate // (even if it's a nongated predicate), we cannot gate // the indident edges. return(null); } else if (IsAcceptState || !configuration.SemanticContext.IsSyntacticPredicate) { // at this point we have a gated predicate and, due to elseif, // we know it's an accept or not a syn pred. In this case, // it's safe to add the gated predicate to the union. We // only want to add syn preds if it's an accept state. Other // gated preds can be used with edges leading to accept states. if (unionOfPredicatesFromAllAlts == null) { unionOfPredicatesFromAllAlts = gatedPredExpr; } else { unionOfPredicatesFromAllAlts = SemanticContext.Or(unionOfPredicatesFromAllAlts, gatedPredExpr); } } } if (unionOfPredicatesFromAllAlts is SemanticContext.TruePredicate) { return(null); } return(unionOfPredicatesFromAllAlts); }
public virtual HashSet <SemanticContext> GetGatedSyntacticPredicatesInNFAConfigurations() { int numConfigs = _nfaConfigurations.Count; HashSet <SemanticContext> synpreds = new HashSet <SemanticContext>(); for (int i = 0; i < numConfigs; i++) { NFAConfiguration configuration = (NFAConfiguration)_nfaConfigurations[i]; SemanticContext gatedPredExpr = configuration.SemanticContext.GatedPredicateContext; // if this is a manual syn pred (gated and syn pred), add if (gatedPredExpr != null && configuration.SemanticContext.IsSyntacticPredicate) { synpreds.Add(configuration.SemanticContext); } } if (synpreds.Count == 0) { return(null); } return(synpreds); }
/** Make a semantic predicate label */ public PredicateLabel(GrammarAST predicateASTNode) : base(SEMPRED) { this._semanticContext = new SemanticContext.Predicate(predicateASTNode); }
/** Make a semantic predicates label */ public PredicateLabel( SemanticContext semCtx ) : base(SEMPRED) { this._semanticContext = semCtx; }
/** Make a semantic predicate label */ public PredicateLabel( GrammarAST predicateASTNode ) : base(SEMPRED) { this._semanticContext = new SemanticContext.Predicate( predicateASTNode ); }
public static SemanticContext Or(SemanticContext a, SemanticContext b) { //System.Console.Out.WriteLine( "OR: " + a + "||" + b ); if ( a == EmptySemanticContext || a == null ) { return b; } if ( b == EmptySemanticContext || b == null ) { return a; } if ( a is TruePredicate ) { return a; } if ( b is TruePredicate ) { return b; } if ( a is NOT && b is Predicate ) { NOT n = (NOT)a; // check for !p||p if ( n.ctx.Equals( b ) ) { return new TruePredicate(); } } else if ( b is NOT && a is Predicate ) { NOT n = (NOT)b; // check for p||!p if ( n.ctx.Equals( a ) ) { return new TruePredicate(); } } else if ( a.Equals( b ) ) { return a; } //System.Console.Out.WriteLine( "## have to OR" ); return new OR( a, b ); }
public static SemanticContext Not(SemanticContext a) { NOT nota = a as NOT; if ( nota != null ) return nota.ctx; return new NOT( a ); }
public AND( SemanticContext a, SemanticContext b ) { this._left = a; this._right = b; }
public virtual NFAConfiguration AddNFAConfiguration( NFAState state, int alt, NFAContext context, SemanticContext semanticContext ) { NFAConfiguration c = new NFAConfiguration( state.stateNumber, alt, context, semanticContext ); AddNFAConfiguration( state, c ); return c; }
/** Make a semantic predicates label */ public PredicateLabel(SemanticContext semCtx) : base(SEMPRED) { this._semanticContext = semCtx; }
public virtual void SynPredUsedInDFA( DFA dfa, SemanticContext semCtx ) { decisionsWhoseDFAsUsesSynPreds.Add( dfa ); semCtx.TrackUseOfSyntacticPredicates( this ); // walk ctx looking for preds }
protected virtual SemanticContext GetPredicatesCore(NFAState s, NFAState altStartState) { //[email protected]("_getPredicates("+s+")"); if (s.IsAcceptState) { return(null); } // avoid infinite loops from (..)* etc... if (_lookBusy.Contains(s)) { return(null); } _lookBusy.Add(s); Transition transition0 = s.transition[0]; // no transitions if (transition0 == null) { return(null); } // not a predicate and not even an epsilon if (!(transition0.label.IsSemanticPredicate || transition0.label.IsEpsilon)) { return(null); } SemanticContext p = null; SemanticContext p0 = null; SemanticContext p1 = null; if (transition0.label.IsSemanticPredicate) { //[email protected]("pred "+transition0.label); p = transition0.label.SemanticContext; // ignore backtracking preds not on left edge for this decision if (((SemanticContext.Predicate)p).predicateAST.Type == ANTLRParser.BACKTRACK_SEMPRED && s == altStartState.transition[0].target) { p = null; // don't count } } // get preds from beyond this state p0 = GetPredicatesCore((NFAState)transition0.target, altStartState); // get preds from other transition Transition transition1 = s.transition[1]; if (transition1 != null) { p1 = GetPredicatesCore((NFAState)transition1.target, altStartState); } // join this&following-right|following-down return(SemanticContext.And(p, SemanticContext.Or(p0, p1))); }
protected virtual int DetectConfoundingPredicatesCore(NFAState s, Rule enclosingRule, bool chaseFollowTransitions) { //[email protected]("_detectNonAutobacktrackPredicates("+s+")"); if (!chaseFollowTransitions && s.IsAcceptState) { if (_grammar.type == GrammarType.Lexer) { // FOLLOW makes no sense (at the moment!) for lexical rules. // assume all char can follow return(DETECT_PRED_NOT_FOUND); } return(DETECT_PRED_EOR); } if (_lookBusy.Contains(s)) { // return a copy of an empty set; we may modify set inline return(DETECT_PRED_NOT_FOUND); } _lookBusy.Add(s); Transition transition0 = s.transition[0]; if (transition0 == null) { return(DETECT_PRED_NOT_FOUND); } if (!(transition0.label.IsSemanticPredicate || transition0.label.IsEpsilon)) { return(DETECT_PRED_NOT_FOUND); } if (transition0.label.IsSemanticPredicate) { //[email protected]("pred "+transition0.label); SemanticContext ctx = transition0.label.SemanticContext; SemanticContext.Predicate p = (SemanticContext.Predicate)ctx; if (p.predicateAST.Type != ANTLRParser.BACKTRACK_SEMPRED) { return(DETECT_PRED_FOUND); } } /* * if ( transition0.label.isSemanticPredicate() ) { * [email protected]("pred "+transition0.label); * SemanticContext ctx = transition0.label.getSemanticContext(); * SemanticContext.Predicate p = (SemanticContext.Predicate)ctx; * // if a non-syn-pred found not in enclosingRule, say we found one * if ( p.predicateAST.getType() != ANTLRParser.BACKTRACK_SEMPRED && * !p.predicateAST.enclosingRuleName.equals(enclosingRule.name) ) * { * [email protected]("found pred "+p+" not in "+enclosingRule.name); * return DETECT_PRED_FOUND; * } * } */ int result = DetectConfoundingPredicatesCore((NFAState)transition0.target, enclosingRule, chaseFollowTransitions); if (result == DETECT_PRED_FOUND) { return(DETECT_PRED_FOUND); } if (result == DETECT_PRED_EOR) { if (transition0 is RuleClosureTransition) { // we called a rule that found the end of the rule. // That means the rule is nullable and we need to // keep looking at what follows the rule ref. E.g., // a : b A ; where b is nullable means that LOOK(a) // should include A. RuleClosureTransition ruleInvocationTrans = (RuleClosureTransition)transition0; NFAState following = (NFAState)ruleInvocationTrans.followState; int afterRuleResult = DetectConfoundingPredicatesCore(following, enclosingRule, chaseFollowTransitions); if (afterRuleResult == DETECT_PRED_FOUND) { return(DETECT_PRED_FOUND); } } } Transition transition1 = s.transition[1]; if (transition1 != null) { int t1Result = DetectConfoundingPredicatesCore((NFAState)transition1.target, enclosingRule, chaseFollowTransitions); if (t1Result == DETECT_PRED_FOUND) { return(DETECT_PRED_FOUND); } } return(DETECT_PRED_NOT_FOUND); }
public OR( SemanticContext a, SemanticContext b ) { _operands = new HashSet<object>(); if ( a is OR ) { _operands.addAll( ( (OR)a )._operands ); } else if ( a != null ) { _operands.Add( a ); } if ( b is OR ) { _operands.addAll( ( (OR)b )._operands ); } else if ( b != null ) { _operands.Add( b ); } }
public NOT( SemanticContext ctx ) { this.ctx = ctx; }
public AND(SemanticContext a, SemanticContext b) { this._left = a; this._right = b; }
protected abstract SemanticContext CombinePredicates(SemanticContext a, SemanticContext b);
public NOT(SemanticContext ctx) { this.ctx = ctx; }
public OR(SemanticContext a, SemanticContext b) : base(a, b) { }
protected override SemanticContext CombinePredicates(SemanticContext a, SemanticContext b) { return(Or(a, b)); }
public static SemanticContext And(SemanticContext a, SemanticContext b) { //System.Console.Out.WriteLine( "AND: " + a + "&&" + b ); if ( a == EmptySemanticContext || a == null ) { return b; } if ( b == EmptySemanticContext || b == null ) { return a; } if ( a.Equals( b ) ) { return a; // if same, just return left one } //System.Console.Out.WriteLine( "## have to AND" ); return new AND( a, b ); }