Example #1
0
        public override void DiscoverRule(RuleAST rule, GrammarAST ID,
                                 IList<GrammarAST> modifiers, ActionAST arg,
                                 ActionAST returns, GrammarAST thrws,
                                 GrammarAST options, ActionAST locals,
                                 IList<GrammarAST> actions,
                                 GrammarAST block)
        {
            int numAlts = block.ChildCount;
            Rule r;
            if (LeftRecursiveRuleAnalyzer.HasImmediateRecursiveRuleRefs(rule, ID.Text))
            {
                r = new LeftRecursiveRule(g, ID.Text, rule);
            }
            else
            {
                r = new Rule(g, ID.Text, rule, numAlts);
            }
            rules[r.name] = r;

            if (arg != null)
            {
                r.args = ScopeParser.ParseTypedArgList(arg, arg.Text, g);
                r.args.type = AttributeDict.DictType.ARG;
                r.args.ast = arg;
                arg.resolver = r.alt[currentOuterAltNumber];
            }

            if (returns != null)
            {
                r.retvals = ScopeParser.ParseTypedArgList(returns, returns.Text, g);
                r.retvals.type = AttributeDict.DictType.RET;
                r.retvals.ast = returns;
            }

            if (locals != null)
            {
                r.locals = ScopeParser.ParseTypedArgList(locals, locals.Text, g);
                r.locals.type = AttributeDict.DictType.LOCAL;
                r.locals.ast = locals;
            }

            foreach (GrammarAST a in actions)
            {
                // a = ^(AT ID ACTION)
                ActionAST action = (ActionAST)a.GetChild(1);
                r.namedActions[a.GetChild(0).Text] = action;
                action.resolver = r;
            }
        }
Example #2
0
        /** Override this method so that we can record which alternative
         *  was taken at each decision point. For non-left recursive rules,
         *  it's simple. Set decisionStatesThatSetOuterAltNumInContext
         *  indicates which decision states should set the outer alternative number.
         *
         *  <p>Left recursive rules are much more complicated to deal with:
         *  there is typically a decision for the primary alternatives and a
         *  decision to choose between the recursive operator alternatives.
         *  For example, the following left recursive rule has two primary and 2
         *  recursive alternatives.</p>
         *
         *   e : e '*' e
         | '-' INT
         | e '+' e
         | ID
         |     ;
         |
         *  <p>ANTLR rewrites that rule to be</p>
         *
         *   e[int precedence]
         *       : ('-' INT | ID)
         *       ( {...}? '*' e[5]
         | {...}? '+' e[3]
         |       )*
         |      ;
         |
         *
         *  <p>So, there are two decisions associated with picking the outermost alt.
         *  This complicates our tracking significantly. The outermost alternative number
         *  is a function of the decision (ATN state) within a left recursive rule and the
         *  predicted alternative coming back from adaptivePredict().</p>
         *
         *  We use stateToAltsMap as a cache to avoid expensive calls to
         *  getRecursiveOpAlts().
         */
        protected override int VisitDecisionState(DecisionState p)
        {
            int predictedAlt = base.VisitDecisionState(p);

            if (p.NumberOfTransitions > 1)
            {
                //			System.out.println("decision "+p.decision+": "+predictedAlt);
                if (p.decision == this.overrideDecision &&
                    this._input.Index == this.overrideDecisionInputIndex)
                {
                    overrideDecisionRoot = (GrammarInterpreterRuleContext)Context;
                }
            }

            GrammarInterpreterRuleContext ctx = (GrammarInterpreterRuleContext)_ctx;

            if (decisionStatesThatSetOuterAltNumInContext.Get(p.stateNumber))
            {
                ctx.OuterAlternative = predictedAlt;
                Rule r = g.GetRule(p.ruleIndex);
                if (atn.ruleToStartState[r.index].isPrecedenceRule)
                {
                    int[]             alts = stateToAltsMap[p.stateNumber];
                    LeftRecursiveRule lr   = (LeftRecursiveRule)g.GetRule(p.ruleIndex);
                    if (p.StateType == StateType.BlockStart)
                    {
                        if (alts == null)
                        {
                            alts = lr.GetPrimaryAlts();
                            stateToAltsMap[p.stateNumber] = alts; // cache it
                        }
                    }
                    else if (p.StateType == StateType.StarBlockStart)
                    {
                        if (alts == null)
                        {
                            alts = lr.GetRecursiveOpAlts();
                            stateToAltsMap[p.stateNumber] = alts; // cache it
                        }
                    }
                    ctx.OuterAlternative = alts[predictedAlt];
                }
            }

            return(predictedAlt);
        }
        public LeftRecursiveRuleFunction(OutputModelFactory factory, LeftRecursiveRule r)
            : base(factory, r)
        {
            // Since we delete x=lr, we have to manually add decls for all labels
            // on left-recur refs to proper structs
            foreach (System.Tuple<GrammarAST, string> pair in r.leftRecursiveRuleRefLabels)
            {
                GrammarAST idAST = pair.Item1;
                string altLabel = pair.Item2;
                string label = idAST.Text;
                GrammarAST rrefAST = (GrammarAST)idAST.Parent.GetChild(1);
                if (rrefAST.Type == ANTLRParser.RULE_REF)
                {
                    Rule targetRule = factory.GetGrammar().GetRule(rrefAST.Text);
                    string ctxName = factory.GetTarget().GetRuleFunctionContextStructName(targetRule);
                    RuleContextDecl d;
                    if (idAST.Parent.Type == ANTLRParser.ASSIGN)
                    {
                        d = new RuleContextDecl(factory, label, ctxName);
                    }
                    else
                    {
                        d = new RuleContextListDecl(factory, label, ctxName);
                    }

                    StructDecl @struct = ruleCtx;
                    if (altLabelCtxs != null)
                    {
                        AltLabelStructDecl s;
                        if (altLabel != null && altLabelCtxs.TryGetValue(altLabel, out s) && s != null)
                            @struct = s; // if alt label, use subctx
                    }

                    @struct.AddDecl(d); // stick in overall rule's ctx
                }
            }
        }
        /** Return true if successful */
        public virtual bool TranslateLeftRecursiveRule(GrammarRootAST ast,
                                                  LeftRecursiveRule r,
                                                  string language)
        {
            //tool.log("grammar", ruleAST.toStringTree());
            GrammarAST prevRuleAST = r.ast;
            string ruleName = prevRuleAST.GetChild(0).Text;
            LeftRecursiveRuleAnalyzer leftRecursiveRuleWalker =
                new LeftRecursiveRuleAnalyzer(prevRuleAST, tool, ruleName, language);
            bool isLeftRec;
            try
            {
                //System.Console.WriteLine("TESTING ---------------\n" +
                //                   leftRecursiveRuleWalker.Text(ruleAST));
                isLeftRec = leftRecursiveRuleWalker.rec_rule();
            }
            catch (RecognitionException)
            {
                isLeftRec = false; // didn't match; oh well
            }
            if (!isLeftRec)
                return false;

            // replace old rule's AST; first create text of altered rule
            GrammarAST RULES = (GrammarAST)ast.GetFirstChildWithType(ANTLRParser.RULES);
            string newRuleText = leftRecursiveRuleWalker.GetArtificialOpPrecRule();
            //System.Console.WriteLine("created: " + newRuleText);
            // now parse within the context of the grammar that originally created
            // the AST we are transforming. This could be an imported grammar so
            // we cannot just reference this.g because the role might come from
            // the imported grammar and not the root grammar (this.g)
            RuleAST t = ParseArtificialRule(prevRuleAST.g, newRuleText);

            // reuse the name token from the original AST since it refers to the proper source location in the original grammar
            ((GrammarAST)t.GetChild(0)).Token = ((GrammarAST)prevRuleAST.GetChild(0)).Token;

            // update grammar AST and set rule's AST.
            RULES.SetChild(prevRuleAST.ChildIndex, t);
            r.ast = t;

            // Reduce sets in newly created rule tree
            GrammarTransformPipeline transform = new GrammarTransformPipeline(g, g.tool);
            transform.ReduceBlocksToSets(r.ast);
            transform.ExpandParameterizedLoops(r.ast);

            // Rerun semantic checks on the new rule
            RuleCollector ruleCollector = new RuleCollector(g);
            ruleCollector.Visit(t, "rule");
            BasicSemanticChecks basics = new BasicSemanticChecks(g, ruleCollector);
            // disable the assoc element option checks because they are already
            // handled for the pre-transformed rule.
            basics.checkAssocElementOption = false;
            basics.Visit(t, "rule");

            // track recursive alt info for codegen
            r.recPrimaryAlts = new List<LeftRecursiveRuleAltInfo>();
            foreach (var altInfo in leftRecursiveRuleWalker.prefixAndOtherAlts)
                r.recPrimaryAlts.Add(altInfo);
            if (r.recPrimaryAlts.Count == 0)
            {
                tool.errMgr.GrammarError(ErrorType.NO_NON_LR_ALTS, g.fileName, ((GrammarAST)r.ast.GetChild(0)).Token, r.name);
            }

            r.recOpAlts = new OrderedHashMap<int, LeftRecursiveRuleAltInfo>();
            foreach (var pair in leftRecursiveRuleWalker.binaryAlts)
                r.recOpAlts[pair.Key] = pair.Value;
            foreach (var pair in leftRecursiveRuleWalker.ternaryAlts)
                r.recOpAlts[pair.Key] = pair.Value;
            foreach (var pair in leftRecursiveRuleWalker.suffixAlts)
                r.recOpAlts[pair.Key] = pair.Value;

            // walk alt info records and set their altAST to point to appropriate ALT subtree
            // from freshly created AST
            SetAltASTPointers(r, t);

            // update Rule to just one alt and add prec alt
            ActionAST arg = (ActionAST)r.ast.GetFirstChildWithType(ANTLRParser.ARG_ACTION);
            if (arg != null)
            {
                r.args = ScopeParser.ParseTypedArgList(arg, arg.Text, g);
                r.args.type = AttributeDict.DictType.ARG;
                r.args.ast = arg;
                arg.resolver = r.alt[1]; // todo: isn't this Rule or something?
            }

            // define labels on recursive rule refs we delete; they don't point to nodes of course
            // these are so $label in action translation works
            foreach (System.Tuple<GrammarAST, string> pair in leftRecursiveRuleWalker.leftRecursiveRuleRefLabels)
            {
                GrammarAST labelNode = pair.Item1;
                GrammarAST labelOpNode = (GrammarAST)labelNode.Parent;
                GrammarAST elementNode = (GrammarAST)labelOpNode.GetChild(1);
                LabelElementPair lp = new LabelElementPair(g, labelNode, elementNode, labelOpNode.Type);
                r.alt[1].labelDefs.Map(labelNode.Text, lp);
            }
            // copy to rule from walker
            r.leftRecursiveRuleRefLabels = leftRecursiveRuleWalker.leftRecursiveRuleRefLabels;

            tool.Log("grammar", "added: " + t.ToStringTree());
            return true;
        }
 /**
  * <pre>
  * (RULE e int _p (returns int v)
  * 	(BLOCK
  * 	  (ALT
  * 		(BLOCK
  * 			(ALT INT {$v = $INT.int;})
  * 			(ALT '(' (= x e) ')' {$v = $x.v;})
  * 			(ALT ID))
  * 		(* (BLOCK
  *			(OPTIONS ...)
  * 			(ALT {7 &gt;= $_p}? '*' (= b e) {$v = $a.v * $b.v;})
  * 			(ALT {6 &gt;= $_p}? '+' (= b e) {$v = $a.v + $b.v;})
  * 			(ALT {3 &gt;= $_p}? '++') (ALT {2 &gt;= $_p}? '--'))))))
  * </pre>
  */
 public virtual void SetAltASTPointers(LeftRecursiveRule r, RuleAST t)
 {
     //System.Console.WriteLine("RULE: " + t.ToStringTree());
     BlockAST ruleBlk = (BlockAST)t.GetFirstChildWithType(ANTLRParser.BLOCK);
     AltAST mainAlt = (AltAST)ruleBlk.GetChild(0);
     BlockAST primaryBlk = (BlockAST)mainAlt.GetChild(0);
     BlockAST opsBlk = (BlockAST)mainAlt.GetChild(1).GetChild(0); // (* BLOCK ...)
     for (int i = 0; i < r.recPrimaryAlts.Count; i++)
     {
         LeftRecursiveRuleAltInfo altInfo = r.recPrimaryAlts[i];
         altInfo.altAST = (AltAST)primaryBlk.GetChild(i);
         altInfo.altAST.leftRecursiveAltInfo = altInfo;
         altInfo.originalAltAST.leftRecursiveAltInfo = altInfo;
         //altInfo.originalAltAST.Parent = altInfo.altAST.Parent;
         //System.Console.WriteLine(altInfo.altAST.ToStringTree());
     }
     for (int i = 0; i < r.recOpAlts.Count; i++)
     {
         LeftRecursiveRuleAltInfo altInfo = r.recOpAlts.GetElement(i);
         altInfo.altAST = (AltAST)opsBlk.GetChild(i);
         altInfo.altAST.leftRecursiveAltInfo = altInfo;
         altInfo.originalAltAST.leftRecursiveAltInfo = altInfo;
         //altInfo.originalAltAST.Parent = altInfo.altAST.Parent;
         //System.Console.WriteLine(altInfo.altAST.ToStringTree());
     }
 }
        public virtual void BuildLeftRecursiveRuleFunction(LeftRecursiveRule r, LeftRecursiveRuleFunction function)
        {
            BuildNormalRuleFunction(r, function);

            // now inject code to start alts
            AbstractTarget target = @delegate.GetTarget();
            TemplateGroup codegenTemplates = target.GetTemplates();

            // pick out alt(s) for primaries
            CodeBlockForOuterMostAlt outerAlt = (CodeBlockForOuterMostAlt)function.code[0];
            IList<CodeBlockForAlt> primaryAltsCode = new List<CodeBlockForAlt>();
            SrcOp primaryStuff = outerAlt.ops[0];
            if (primaryStuff is Choice)
            {
                Choice primaryAltBlock = (Choice)primaryStuff;
                foreach (var alt in primaryAltBlock.alts)
                    primaryAltsCode.Add(alt);
            }
            else
            { // just a single alt I guess; no block
                primaryAltsCode.Add((CodeBlockForAlt)primaryStuff);
            }

            // pick out alt(s) for op alts
            StarBlock opAltStarBlock = (StarBlock)outerAlt.ops[1];
            CodeBlockForAlt altForOpAltBlock = opAltStarBlock.alts[0];
            IList<CodeBlockForAlt> opAltsCode = new List<CodeBlockForAlt>();
            SrcOp opStuff = altForOpAltBlock.ops[0];
            if (opStuff is AltBlock)
            {
                AltBlock opAltBlock = (AltBlock)opStuff;
                foreach (var alt in opAltBlock.alts)
                    opAltsCode.Add(alt);
            }
            else
            { // just a single alt I guess; no block
                opAltsCode.Add((CodeBlockForAlt)opStuff);
            }

            // Insert code in front of each primary alt to create specialized context if there was a label
            for (int i = 0; i < primaryAltsCode.Count; i++)
            {
                LeftRecursiveRuleAltInfo altInfo = r.recPrimaryAlts[i];
                if (altInfo.altLabel == null)
                    continue;
                Template altActionST = codegenTemplates.GetInstanceOf("recRuleReplaceContext");
                altActionST.Add("ctxName", Utils.Capitalize(altInfo.altLabel));
                AltLabelStructDecl ctx = null;
                if (altInfo.altLabel != null)
                    function.altLabelCtxs.TryGetValue(altInfo.altLabel, out ctx);
                Action altAction = new Action(@delegate, ctx, altActionST);
                CodeBlockForAlt alt = primaryAltsCode[i];
                alt.InsertOp(0, altAction);
            }

            // Insert code to set ctx.stop after primary block and before op * loop
            Template setStopTokenAST = codegenTemplates.GetInstanceOf("recRuleSetStopToken");
            Action setStopTokenAction = new Action(@delegate, function.ruleCtx, setStopTokenAST);
            outerAlt.InsertOp(1, setStopTokenAction);

            // Insert code to set _prevctx at start of * loop
            Template setPrevCtx = codegenTemplates.GetInstanceOf("recRuleSetPrevCtx");
            Action setPrevCtxAction = new Action(@delegate, function.ruleCtx, setPrevCtx);
            opAltStarBlock.AddIterationOp(setPrevCtxAction);

            // Insert code in front of each op alt to create specialized context if there was an alt label
            for (int i = 0; i < opAltsCode.Count; i++)
            {
                Template altActionST;
                LeftRecursiveRuleAltInfo altInfo = r.recOpAlts.GetElement(i);
                string templateName;
                if (altInfo.altLabel != null)
                {
                    templateName = "recRuleLabeledAltStartAction";
                    altActionST = codegenTemplates.GetInstanceOf(templateName);
                    altActionST.Add("currentAltLabel", altInfo.altLabel);
                }
                else
                {
                    templateName = "recRuleAltStartAction";
                    altActionST = codegenTemplates.GetInstanceOf(templateName);
                    altActionST.Add("ctxName", Utils.Capitalize(r.name));
                }
                altActionST.Add("ruleName", r.name);
                // add label of any LR ref we deleted
                altActionST.Add("label", altInfo.leftRecursiveRuleRefLabel);
                if (altActionST.impl.FormalArguments.Any(x => x.Name == "isListLabel"))
                {
                    altActionST.Add("isListLabel", altInfo.isListLabel);
                }
                else if (altInfo.isListLabel)
                {
                    @delegate.GetGenerator().tool.errMgr.ToolError(ErrorType.CODE_TEMPLATE_ARG_ISSUE, templateName, "isListLabel");
                }
                AltLabelStructDecl ctx = null;
                if (altInfo.altLabel != null)
                    function.altLabelCtxs.TryGetValue(altInfo.altLabel, out ctx);
                Action altAction = new Action(@delegate, ctx, altActionST);
                CodeBlockForAlt alt = opAltsCode[i];
                alt.InsertOp(0, altAction);
            }
        }