Exemplo n.º 1
0
        public override void SuffixAlt(AltAST originalAltTree, int alt)
        {
            AltAST altTree  = (AltAST)originalAltTree.DupTree();
            string altLabel = altTree.altLabel != null ? altTree.altLabel.Text : null;

            string     label       = null;
            bool       isListLabel = false;
            GrammarAST lrlabel     = StripLeftRecursion(altTree);

            if (lrlabel != null)
            {
                label       = lrlabel.Text;
                isListLabel = lrlabel.Parent.Type == PLUS_ASSIGN;
                leftRecursiveRuleRefLabels.Add(Tuple.Create(lrlabel, altLabel));
            }

            StripAltLabel(altTree);
            string altText = Text(altTree);

            altText = altText.Trim();
            LeftRecursiveRuleAltInfo a =
                new LeftRecursiveRuleAltInfo(alt, altText, label, altLabel, isListLabel, originalAltTree);

            suffixAlts[alt] = a;
            //		System.out.println("suffixAlt " + alt + ": " + altText + ", rewrite=" + rewriteText);
        }
Exemplo n.º 2
0
        public override void BinaryAlt(AltAST originalAltTree, int alt)
        {
            AltAST altTree  = (AltAST)originalAltTree.DupTree();
            string altLabel = altTree.altLabel != null ? altTree.altLabel.Text : null;

            string     label       = null;
            bool       isListLabel = false;
            GrammarAST lrlabel     = StripLeftRecursion(altTree);

            if (lrlabel != null)
            {
                label       = lrlabel.Text;
                isListLabel = lrlabel.Parent.Type == PLUS_ASSIGN;
                leftRecursiveRuleRefLabels.Add(Tuple.Create(lrlabel, altLabel));
            }

            StripAltLabel(altTree);

            // rewrite e to be e_[rec_arg]
            int nextPrec = NextPrecedence(alt);

            altTree = AddPrecedenceArgToRules(altTree, nextPrec);

            StripAltLabel(altTree);
            string altText = Text(altTree);

            altText = altText.Trim();
            LeftRecursiveRuleAltInfo a =
                new LeftRecursiveRuleAltInfo(alt, altText, label, altLabel, isListLabel, originalAltTree);

            a.nextPrec      = nextPrec;
            binaryAlts[alt] = a;
            //System.out.println("binaryAlt " + alt + ": " + altText + ", rewrite=" + rewriteText);
        }
        /**
         * <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());
            }
        }
Exemplo n.º 4
0
        public override void OtherAlt(AltAST originalAltTree, int alt)
        {
            AltAST altTree = (AltAST)originalAltTree.DupTree();

            StripAltLabel(altTree);
            string altText             = Text(altTree);
            string altLabel            = altTree.altLabel != null ? altTree.altLabel.Text : null;
            LeftRecursiveRuleAltInfo a =
                new LeftRecursiveRuleAltInfo(alt, altText, null, altLabel, false, originalAltTree);

            // We keep other alts with prefix alts since they are all added to the start of the generated rule, and
            // we want to retain any prior ordering between them
            prefixAndOtherAlts.Add(a);
            //		System.out.println("otherAlt " + alt + ": " + altText);
        }
Exemplo n.º 5
0
        // --------- get transformed rules ----------------

        public virtual string GetArtificialOpPrecRule()
        {
            Template ruleST = recRuleTemplates.GetInstanceOf("recRule");

            ruleST.Add("ruleName", ruleName);
            Template ruleArgST = codegenTemplates.GetInstanceOf("recRuleArg");

            ruleST.Add("argName", ruleArgST);
            Template setResultST = codegenTemplates.GetInstanceOf("recRuleSetResultAction");

            ruleST.Add("setResultAction", setResultST);
            ruleST.Add("userRetvals", retvals);

            LinkedHashMap <int, LeftRecursiveRuleAltInfo> opPrecRuleAlts = new LinkedHashMap <int, LeftRecursiveRuleAltInfo>();

            foreach (var pair in binaryAlts)
            {
                opPrecRuleAlts[pair.Key] = pair.Value;
            }
            foreach (var pair in ternaryAlts)
            {
                opPrecRuleAlts[pair.Key] = pair.Value;
            }
            foreach (var pair in suffixAlts)
            {
                opPrecRuleAlts[pair.Key] = pair.Value;
            }
            foreach (int alt in opPrecRuleAlts.Keys)
            {
                LeftRecursiveRuleAltInfo altInfo = opPrecRuleAlts[alt];
                Template altST  = recRuleTemplates.GetInstanceOf("recRuleAlt");
                Template predST = codegenTemplates.GetInstanceOf("recRuleAltPredicate");
                predST.Add("opPrec", Precedence(alt));
                predST.Add("ruleName", ruleName);
                altST.Add("pred", predST);
                altST.Add("alt", altInfo);
                altST.Add("precOption", LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME);
                altST.Add("opPrec", Precedence(alt));
                ruleST.Add("opAlts", altST);
            }

            ruleST.Add("primaryAlts", prefixAndOtherAlts);

            tool.Log("left-recursion", ruleST.Render());

            return(ruleST.Render());
        }
Exemplo n.º 6
0
        public override void PrefixAlt(AltAST originalAltTree, int alt)
        {
            AltAST altTree = (AltAST)originalAltTree.DupTree();

            StripAltLabel(altTree);

            int nextPrec = Precedence(alt);

            // rewrite e to be e_[prec]
            altTree = AddPrecedenceArgToRules(altTree, nextPrec);
            string altText = Text(altTree);

            altText = altText.Trim();
            string altLabel            = altTree.altLabel != null ? altTree.altLabel.Text : null;
            LeftRecursiveRuleAltInfo a =
                new LeftRecursiveRuleAltInfo(alt, altText, null, altLabel, false, originalAltTree);

            a.nextPrec = nextPrec;
            prefixAndOtherAlts.Add(a);
            //System.out.println("prefixAlt " + alt + ": " + altText + ", rewrite=" + rewriteText);
        }
 public override void OtherAlt(AltAST originalAltTree, int alt)
 {
     AltAST altTree = (AltAST)originalAltTree.DupTree();
     StripAltLabel(altTree);
     string altText = Text(altTree);
     string altLabel = altTree.altLabel != null ? altTree.altLabel.Text : null;
     LeftRecursiveRuleAltInfo a =
         new LeftRecursiveRuleAltInfo(alt, altText, null, altLabel, false, originalAltTree);
     // We keep other alts with prefix alts since they are all added to the start of the generated rule, and
     // we want to retain any prior ordering between them
     prefixAndOtherAlts.Add(a);
     //		System.out.println("otherAlt " + alt + ": " + altText);
 }
        public override void SuffixAlt(AltAST originalAltTree, int alt)
        {
            AltAST altTree = (AltAST)originalAltTree.DupTree();
            string altLabel = altTree.altLabel != null ? altTree.altLabel.Text : null;

            string label = null;
            bool isListLabel = false;
            GrammarAST lrlabel = StripLeftRecursion(altTree);
            if (lrlabel != null)
            {
                label = lrlabel.Text;
                isListLabel = lrlabel.Parent.Type == PLUS_ASSIGN;
                leftRecursiveRuleRefLabels.Add(Tuple.Create(lrlabel, altLabel));
            }

            StripAltLabel(altTree);
            string altText = Text(altTree);
            altText = altText.Trim();
            LeftRecursiveRuleAltInfo a =
                new LeftRecursiveRuleAltInfo(alt, altText, label, altLabel, isListLabel, originalAltTree);
            suffixAlts[alt] = a;
            //		System.out.println("suffixAlt " + alt + ": " + altText + ", rewrite=" + rewriteText);
        }
        public override void PrefixAlt(AltAST originalAltTree, int alt)
        {
            AltAST altTree = (AltAST)originalAltTree.DupTree();
            StripAltLabel(altTree);

            int nextPrec = Precedence(alt);
            // rewrite e to be e_[prec]
            altTree = AddPrecedenceArgToRules(altTree, nextPrec);
            string altText = Text(altTree);
            altText = altText.Trim();
            string altLabel = altTree.altLabel != null ? altTree.altLabel.Text : null;
            LeftRecursiveRuleAltInfo a =
                new LeftRecursiveRuleAltInfo(alt, altText, null, altLabel, false, originalAltTree);
            a.nextPrec = nextPrec;
            prefixAndOtherAlts.Add(a);
            //System.out.println("prefixAlt " + alt + ": " + altText + ", rewrite=" + rewriteText);
        }
Exemplo n.º 10
0
        public override void BinaryAlt(AltAST originalAltTree, int alt)
        {
            AltAST altTree = (AltAST)originalAltTree.DupTree();
            string altLabel = altTree.altLabel != null ? altTree.altLabel.Text : null;

            string label = null;
            bool isListLabel = false;
            GrammarAST lrlabel = StripLeftRecursion(altTree);
            if (lrlabel != null)
            {
                label = lrlabel.Text;
                isListLabel = lrlabel.Parent.Type == PLUS_ASSIGN;
                leftRecursiveRuleRefLabels.Add(Tuple.Create(lrlabel, altLabel));
            }

            StripAltLabel(altTree);

            // rewrite e to be e_[rec_arg]
            int nextPrec = NextPrecedence(alt);
            altTree = AddPrecedenceArgToRules(altTree, nextPrec);

            StripAltLabel(altTree);
            string altText = Text(altTree);
            altText = altText.Trim();
            LeftRecursiveRuleAltInfo a =
                new LeftRecursiveRuleAltInfo(alt, altText, label, altLabel, isListLabel, originalAltTree);
            a.nextPrec = nextPrec;
            binaryAlts[alt] = a;
            //System.out.println("binaryAlt " + alt + ": " + altText + ", rewrite=" + rewriteText);
        }