/**
         * <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());
            }
        }
예제 #2
0
        public override Choice GetChoiceBlock(BlockAST blkAST, IList <CodeBlockForAlt> alts, GrammarAST labelAST)
        {
            int    decision = ((DecisionState)blkAST.atnState).decision;
            Choice c;

            if (!g.tool.force_atn && AnalysisPipeline.Disjoint(g.decisionLOOK[decision]))
            {
                c = GetLL1ChoiceBlock(blkAST, alts);
            }
            else
            {
                c = GetComplexChoiceBlock(blkAST, alts);
            }

            if (labelAST != null)
            { // for x=(...), define x or x_list
                string label = labelAST.Text;
                Decl   d     = GetTokenLabelDecl(label);
                c.label = d;
                GetCurrentRuleFunction().AddContextDecl(labelAST.GetAltLabel(), d);
                if (labelAST.Parent.Type == ANTLRParser.PLUS_ASSIGN)
                {
                    string        listLabel = GetTarget().GetListLabel(label);
                    TokenListDecl l         = new TokenListDecl(this, listLabel);
                    GetCurrentRuleFunction().AddContextDecl(labelAST.GetAltLabel(), l);
                }
            }

            return(c);
        }
예제 #3
0
        protected virtual Handle MakeBlock([NotNull] BlockStartState start, [NotNull] BlockAST blkAST, [NotNull] IList <Handle> alts)
        {
            start.sll = IsSLLDecision(blkAST);

            BlockEndState end = NewState <BlockEndState>(blkAST);

            start.endState = end;
            foreach (Handle alt in alts)
            {
                // hook alts up to decision block
                Epsilon(start, alt.left);
                Epsilon(alt.right, end);
                // no back link in ATN so must walk entire alt to see if we can
                // strip out the epsilon to 'end' state
                TailEpsilonRemover opt = new TailEpsilonRemover(atn);
                opt.Visit(alt.left);
            }
            Handle h = new Handle(start, end);

            //		FASerializer ser = new FASerializer(g, h.left);
            //		System.out.println(blkAST.toStringTree()+":\n"+ser);
            blkAST.atnState = start;

            return(h);
        }
예제 #4
0
파일: Parser.cs 프로젝트: Stu042/SimpleC
 public FuncDefAST(Token token, TypeAST type, ArgAST[] args, BlockAST stmt)
 {
     this.token   = token;
     this.name    = token.value;
     this.retType = type;
     this.args    = args;
     this.stmt    = stmt;
 }
예제 #5
0
        public virtual bool ExpectNonGreedy([NotNull] BlockAST blkAST)
        {
            if (BlockHasWildcardAlt(blkAST))
            {
                return(true);
            }

            return(false);
        }
예제 #6
0
파일: Compile.cs 프로젝트: Stu042/SimpleC
 public object DoForBlock(BlockAST blk, object options = null)
 {
     foreach (var statement in blk.stmts)
     {
         statement.Accept(this, options);
         code.Append("\n");
     }
     return(options);
 }
예제 #7
0
        public virtual Choice GetChoiceBlock(BlockAST blkAST, IList <CodeBlockForAlt> alts, GrammarAST label)
        {
            Choice c = @delegate.GetChoiceBlock(blkAST, alts, label);

            foreach (CodeGeneratorExtension ext in extensions)
            {
                c = ext.GetChoiceBlock(c);
            }
            return(c);
        }
예제 #8
0
파일: Parser.cs 프로젝트: Stu042/SimpleC
 public object DoForBlock(BlockAST blk, object options = null)
 {
     PositionOutput(blk.token.indent);
     ConsoleEx.WriteLine("{0}(Block)", ConsoleColor.DarkBlue);
     foreach (var statement in blk.stmts)
     {
         statement.Accept(this, options);
     }
     return(options);
 }
예제 #9
0
        public override void FinishRule(RuleAST rule, GrammarAST ID, GrammarAST block)
        {
            if (rule.IsLexerRule())
            {
                return;
            }
            BlockAST   blk   = (BlockAST)rule.GetFirstChildWithType(BLOCK);
            int        nalts = blk.ChildCount;
            GrammarAST idAST = (GrammarAST)rule.GetChild(0);

            for (int i = 0; i < nalts; i++)
            {
                AltAST altAST = (AltAST)blk.GetChild(i);
                if (altAST.altLabel != null)
                {
                    string altLabel = altAST.altLabel.Text;
                    // first check that label doesn't conflict with a rule
                    // label X or x can't be rule x.
                    Rule r;
                    if (ruleCollector.rules.TryGetValue(Utils.Decapitalize(altLabel), out r) && r != null)
                    {
                        g.tool.errMgr.GrammarError(ErrorType.ALT_LABEL_CONFLICTS_WITH_RULE,
                                                   g.fileName, altAST.altLabel.Token,
                                                   altLabel,
                                                   r.name);
                    }
                    // Now verify that label X or x doesn't conflict with label
                    // in another rule. altLabelToRuleName has both X and x mapped.
                    string prevRuleForLabel;
                    if (ruleCollector.altLabelToRuleName.TryGetValue(altLabel, out prevRuleForLabel) && prevRuleForLabel != null && !prevRuleForLabel.Equals(rule.GetRuleName()))
                    {
                        g.tool.errMgr.GrammarError(ErrorType.ALT_LABEL_REDEF,
                                                   g.fileName, altAST.altLabel.Token,
                                                   altLabel,
                                                   rule.GetRuleName(),
                                                   prevRuleForLabel);
                    }
                }
            }
            IList <GrammarAST> altLabels;
            int numAltLabels = 0;

            if (ruleCollector.ruleToAltLabels.TryGetValue(rule.GetRuleName(), out altLabels) && altLabels != null)
            {
                numAltLabels = altLabels.Count;
            }
            if (numAltLabels > 0 && nalts != numAltLabels)
            {
                g.tool.errMgr.GrammarError(ErrorType.RULE_WITH_TOO_FEW_ALT_LABELS,
                                           g.fileName, idAST.Token, rule.GetRuleName());
            }
        }
예제 #10
0
        public PlusBlock(OutputModelFactory factory,
                         GrammarAST plusRoot,
                         IList <CodeBlockForAlt> alts)
            : base(factory, plusRoot, alts)
        {
            BlockAST            blkAST   = (BlockAST)plusRoot.GetChild(0);
            PlusBlockStartState blkStart = (PlusBlockStartState)blkAST.atnState;
            PlusLoopbackState   loop     = blkStart.loopBackState;

            stateNumber           = blkStart.loopBackState.stateNumber;
            blockStartStateNumber = blkStart.stateNumber;
            loopBackStateNumber   = loop.stateNumber;
            this.error            = GetThrowNoViableAlt(factory, plusRoot, null);
            decision = loop.decision;
        }
예제 #11
0
        public LL1PlusBlockSingleAlt(OutputModelFactory factory, GrammarAST plusRoot, IList <CodeBlockForAlt> alts)
            : base(factory, plusRoot, alts)
        {
            BlockAST            blkAST   = (BlockAST)plusRoot.GetChild(0);
            PlusBlockStartState blkStart = (PlusBlockStartState)blkAST.atnState;

            stateNumber           = blkStart.loopBackState.stateNumber;
            blockStartStateNumber = blkStart.stateNumber;
            PlusBlockStartState plus = (PlusBlockStartState)blkAST.atnState;

            this.decision = plus.loopBackState.decision;
            IntervalSet[] altLookSets = factory.GetGrammar().decisionLOOK[decision];

            IntervalSet loopBackLook = altLookSets[0];

            loopExpr = AddCodeForLoopLookaheadTempVar(loopBackLook);
        }
예제 #12
0
        public virtual Handle Block([NotNull] BlockAST blkAST, [NotNull] GrammarAST ebnfRoot, [NotNull] IList <Handle> alts)
        {
            if (ebnfRoot == null)
            {
                if (alts.Count == 1)
                {
                    Handle h = alts[0];
                    blkAST.atnState = h.left;
                    return(h);
                }
                BlockStartState start = NewState <BasicBlockStartState>(blkAST);
                if (alts.Count > 1)
                {
                    atn.DefineDecisionState(start);
                }
                return(MakeBlock(start, blkAST, alts));
            }
            switch (ebnfRoot.Type)
            {
            case ANTLRParser.OPTIONAL:
                BlockStartState start = NewState <BasicBlockStartState>(blkAST);
                atn.DefineDecisionState(start);
                Handle h = MakeBlock(start, blkAST, alts);
                return(Optional(ebnfRoot, h));

            case ANTLRParser.CLOSURE:
                BlockStartState star = NewState <StarBlockStartState>(ebnfRoot);
                if (alts.Count > 1)
                {
                    atn.DefineDecisionState(star);
                }
                h = MakeBlock(star, blkAST, alts);
                return(Star(ebnfRoot, h));

            case ANTLRParser.POSITIVE_CLOSURE:
                PlusBlockStartState plus = NewState <PlusBlockStartState>(ebnfRoot);
                if (alts.Count > 1)
                {
                    atn.DefineDecisionState(plus);
                }
                h = MakeBlock(plus, blkAST, alts);
                return(Plus(ebnfRoot, h));
            }
            return(null);
        }
예제 #13
0
        public virtual Handle Star([NotNull] GrammarAST starAST, [NotNull] Handle elem)
        {
            StarBlockStartState blkStart = (StarBlockStartState)elem.left;

            BlockEndState blkEnd = (BlockEndState)elem.right;

            preventEpsilonClosureBlocks.Add(Tuple.Create <Rule, ATNState, ATNState>(currentRule, blkStart, blkEnd));

            StarLoopEntryState entry = NewState <StarLoopEntryState>(starAST);

            entry.nonGreedy = !((QuantifierAST)starAST).GetGreedy();
            entry.sll       = false; // no way to express SLL restriction
            atn.DefineDecisionState(entry);
            LoopEndState      end  = NewState <LoopEndState>(starAST);
            StarLoopbackState loop = NewState <StarLoopbackState>(starAST);

            entry.loopBackState = loop;
            end.loopBackState   = loop;

            BlockAST blkAST = (BlockAST)starAST.GetChild(0);

            if (((QuantifierAST)starAST).GetGreedy())
            {
                if (ExpectNonGreedy(blkAST))
                {
                    g.tool.errMgr.GrammarError(ErrorType.EXPECTED_NON_GREEDY_WILDCARD_BLOCK, g.fileName, starAST.Token, starAST.Token.Text);
                }

                Epsilon(entry, blkStart);   // loop enter edge (alt 1)
                Epsilon(entry, end);        // bypass loop edge (alt 2)
            }
            else
            {
                // if not greedy, priority to exit branch; make it first
                Epsilon(entry, end);      // bypass loop edge (alt 1)
                Epsilon(entry, blkStart); // loop enter edge (alt 2)
            }
            Epsilon(blkEnd, loop);        // block end hits loop back
            Epsilon(loop, entry);         // loop back to entry/exit decision

            starAST.atnState = entry;     // decision is to enter/exit; blk is its own decision
            return(new Handle(entry, end));
        }
예제 #14
0
        public virtual Handle Plus([NotNull] GrammarAST plusAST, [NotNull] Handle blk)
        {
            PlusBlockStartState blkStart = (PlusBlockStartState)blk.left;

            BlockEndState blkEnd = (BlockEndState)blk.right;

            preventEpsilonClosureBlocks.Add(Tuple.Create <Rule, ATNState, ATNState>(currentRule, blkStart, blkEnd));

            PlusLoopbackState loop = NewState <PlusLoopbackState>(plusAST);

            loop.nonGreedy = !((QuantifierAST)plusAST).GetGreedy();
            loop.sll       = false; // no way to express SLL restriction
            atn.DefineDecisionState(loop);
            LoopEndState end = NewState <LoopEndState>(plusAST);

            blkStart.loopBackState = loop;
            end.loopBackState      = loop;

            plusAST.atnState = loop;
            Epsilon(blkEnd, loop);      // blk can see loop back

            BlockAST blkAST = (BlockAST)plusAST.GetChild(0);

            if (((QuantifierAST)plusAST).GetGreedy())
            {
                if (ExpectNonGreedy(blkAST))
                {
                    g.tool.errMgr.GrammarError(ErrorType.EXPECTED_NON_GREEDY_WILDCARD_BLOCK, g.fileName, plusAST.Token, plusAST.Token.Text);
                }

                Epsilon(loop, blkStart);    // loop back to start
                Epsilon(loop, end);         // or exit
            }
            else
            {
                // if not greedy, priority to exit branch; make it first
                Epsilon(loop, end);         // exit
                Epsilon(loop, blkStart);    // loop back to start
            }

            return(new Handle(blkStart, end));
        }
예제 #15
0
파일: Compile.cs 프로젝트: Stu042/SimpleC
            public string Compile(BlockAST start)
            {
                code.Append("; ModuleID = '" + start.token.filename + "'\nsource_filename = \"" + start.token.filename +
                            "\"\ntarget datalayout = \"e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128\"\n" +
                            /* windows: "target triple = \"x86_64-pc-windows-msvc19.26.28806\"\n\n");*/
                            "target triple = \"x86_64-pc-linux-gnu\"\n\n");

                start.Accept(this, null);
                code.Append("attributes #0 = { noinline nounwind optnone uwtable \"correctly-rounded-divide-sqrt-fp-math\"=\"false\"" +
                            " \"disable-tail-calls\"=\"false\" \"frame-pointer\"=\"none\" \"less-precise-fpmad\"=\"false\" \"min-legal-vector-width\"=\"0\"" +
                            " \"no-infs-fp-math\"=\"false\" \"no-jump-tables\"=\"false\" \"no-nans-fp-math\"=\"false\" \"no-signed-zeros-fp-math\"=\"false\"" +
                            " \"no-trapping-math\"=\"false\" \"stack-protector-buffer-size\"=\"8\" \"target-cpu\"=\"x86-64\" \"target-features\"=\"+cx8,+fxsr,+mmx,+sse,+sse2,+x87\"" +
                            " \"unsafe-fp-math\"=\"false\" \"use-soft-float\"=\"false\" }\n\n" +
                            "!llvm.module.flags = !{ !0, !1}\n" +
                            "!llvm.ident = !{ !2}\n\n" +
                            "!0 = !{ i32 1, !\"wchar_size\", i32 2}\n" +
                            "!1 = !{ i32 7, !\"PIC Level\", i32 2}\n" +
                            "!2 = !{ !\"simplec version 0.0.1\"}\n");
                irCode = code.ToString();
                return(irCode);
            }
예제 #16
0
파일: Program.cs 프로젝트: Stu042/SimpleC
        static void Main(string[] args)
        {
            Token[]      tokens;
            List <Token> tokenList = new List <Token>();
            Tokeniser    tokeniser = new Tokeniser();

            ConsoleEx.WriteLine("{0}Starting Lex", ConsoleColor.DarkGray);
            foreach (var filename in args)
            {
                tokenList.AddRange(tokeniser.Run(filename));
            }
            tokens = tokenList.ToArray();
            tokeniser.Show(tokens);
            ConsoleEx.WriteLine("\n{0}Parsing", ConsoleColor.DarkGray);
            Parser parser = new Parser(tokens);

            parser.Parse();
            parser.Show();
            BlockAST blockAST = parser.GetAst();
            Compiler compiler = new Compiler();
            string   output   = compiler.Compile(blockAST);

            Console.WriteLine(output);
        }
예제 #17
0
        // BLOCKS

        public virtual Choice GetChoiceBlock(BlockAST blkAST, IList <CodeBlockForAlt> alts, GrammarAST label)
        {
            return(null);
        }
예제 #18
0
파일: Compile.cs 프로젝트: Stu042/SimpleC
 public string Compile(BlockAST blockAST)
 {
     return(compilerVisitor.Compile(blockAST));
 }
예제 #19
0
        protected virtual bool TranslateLeftFactoredDecision(GrammarAST block, string factoredRule, bool variant, DecisionFactorMode mode, bool includeFactoredElement)
        {
            if (mode == DecisionFactorMode.PARTIAL_UNFACTORED && includeFactoredElement)
            {
                throw new ArgumentException("Cannot include the factored element in unfactored alternatives.");
            }
            else if (mode == DecisionFactorMode.COMBINED_FACTOR && !includeFactoredElement)
            {
                throw new ArgumentException("Cannot return a combined answer without the factored element.");
            }

            if (!ExpandOptionalQuantifiersForBlock(block, variant))
            {
                return(false);
            }

            IList <GrammarAST> alternatives = block.GetAllChildrenWithType(ANTLRParser.ALT);

            GrammarAST[] factoredAlternatives   = new GrammarAST[alternatives.Count];
            GrammarAST[] unfactoredAlternatives = new GrammarAST[alternatives.Count];
            IntervalSet  factoredIntervals      = new IntervalSet();
            IntervalSet  unfactoredIntervals    = new IntervalSet();

            for (int i = 0; i < alternatives.Count; i++)
            {
                GrammarAST alternative = alternatives[i];
                if (mode.IncludeUnfactoredAlts())
                {
                    GrammarAST unfactoredAlt = TranslateLeftFactoredAlternative(alternative.DupTree(), factoredRule, variant, DecisionFactorMode.PARTIAL_UNFACTORED, false);
                    unfactoredAlternatives[i] = unfactoredAlt;
                    if (unfactoredAlt != null)
                    {
                        unfactoredIntervals.Add(i);
                    }
                }

                if (mode.IncludeFactoredAlts())
                {
                    GrammarAST factoredAlt = TranslateLeftFactoredAlternative(alternative, factoredRule, variant, mode == DecisionFactorMode.COMBINED_FACTOR ? DecisionFactorMode.PARTIAL_FACTORED : DecisionFactorMode.FULL_FACTOR, includeFactoredElement);
                    factoredAlternatives[i] = factoredAlt;
                    if (factoredAlt != null)
                    {
                        factoredIntervals.Add(alternative.ChildIndex);
                    }
                }
            }

            if (factoredIntervals.IsNil && !mode.IncludeUnfactoredAlts())
            {
                return(false);
            }
            else if (unfactoredIntervals.IsNil && !mode.IncludeFactoredAlts())
            {
                return(false);
            }

            if (unfactoredIntervals.IsNil && factoredIntervals.Count == alternatives.Count && mode.IncludeFactoredAlts() && !includeFactoredElement)
            {
                for (int i = 0; i < factoredAlternatives.Length; i++)
                {
                    GrammarAST translatedAlt = factoredAlternatives[i];
                    if (translatedAlt.ChildCount == 0)
                    {
                        adaptor.AddChild(translatedAlt, adaptor.Create(ANTLRParser.EPSILON, "EPSILON"));
                    }

                    adaptor.SetChild(block, i, translatedAlt);
                }

                return(true);
            }
            else if (factoredIntervals.IsNil && unfactoredIntervals.Count == alternatives.Count && mode.IncludeUnfactoredAlts())
            {
                for (int i = 0; i < unfactoredAlternatives.Length; i++)
                {
                    GrammarAST translatedAlt = unfactoredAlternatives[i];
                    if (translatedAlt.ChildCount == 0)
                    {
                        adaptor.AddChild(translatedAlt, adaptor.Create(ANTLRParser.EPSILON, "EPSILON"));
                    }

                    adaptor.SetChild(block, i, translatedAlt);
                }

                return(true);
            }

            if (mode == DecisionFactorMode.FULL_FACTOR)
            {
                return(false);
            }

            /* for a, b, c being arbitrary `element` trees, this block performs
             * this transformation:
             *
             * factoredElement a
             * | factoredElement b
             * | factoredElement c
             * | ...
             *
             * ==>
             *
             * factoredElement (a | b | c | ...)
             */
            GrammarAST newChildren = (GrammarAST)adaptor.Nil();

            for (int i = 0; i < alternatives.Count; i++)
            {
                if (mode.IncludeFactoredAlts() && factoredIntervals.Contains(i))
                {
                    bool combineWithPrevious = i > 0 && factoredIntervals.Contains(i - 1) && (!mode.IncludeUnfactoredAlts() || !unfactoredIntervals.Contains(i - 1));
                    if (combineWithPrevious)
                    {
                        GrammarAST translatedAlt = factoredAlternatives[i];
                        if (translatedAlt.ChildCount == 0)
                        {
                            adaptor.AddChild(translatedAlt, adaptor.Create(ANTLRParser.EPSILON, "EPSILON"));
                        }

                        GrammarAST previous = (GrammarAST)newChildren.GetChild(newChildren.ChildCount - 1);

#if false
                        if (LOGGER.isLoggable(Level.FINE))
                        {
                            LOGGER.log(Level.FINE, previous.ToStringTree());
                            LOGGER.log(Level.FINE, translatedAlt.ToStringTree());
                        }
#endif

                        if (previous.ChildCount == 1 || previous.GetChild(1).Type != ANTLRParser.BLOCK)
                        {
                            GrammarAST newBlock = new BlockAST(adaptor.CreateToken(ANTLRParser.BLOCK, "BLOCK"));
                            GrammarAST newAlt   = new AltAST(adaptor.CreateToken(ANTLRParser.ALT, "ALT"));
                            adaptor.AddChild(newBlock, newAlt);
                            while (previous.ChildCount > 1)
                            {
                                adaptor.AddChild(newAlt, previous.DeleteChild(1));
                            }

                            if (newAlt.ChildCount == 0)
                            {
                                adaptor.AddChild(newAlt, adaptor.Create(ANTLRParser.EPSILON, "EPSILON"));
                            }

                            adaptor.AddChild(previous, newBlock);
                        }

                        if (translatedAlt.ChildCount == 1 || translatedAlt.GetChild(1).Type != ANTLRParser.BLOCK)
                        {
                            GrammarAST newBlock = new BlockAST(adaptor.CreateToken(ANTLRParser.BLOCK, "BLOCK"));
                            GrammarAST newAlt   = new AltAST(adaptor.CreateToken(ANTLRParser.ALT, "ALT"));
                            adaptor.AddChild(newBlock, newAlt);
                            while (translatedAlt.ChildCount > 1)
                            {
                                adaptor.AddChild(newAlt, translatedAlt.DeleteChild(1));
                            }

                            if (newAlt.ChildCount == 0)
                            {
                                adaptor.AddChild(newAlt, adaptor.Create(ANTLRParser.EPSILON, "EPSILON"));
                            }

                            adaptor.AddChild(translatedAlt, newBlock);
                        }

                        GrammarAST combinedBlock = (GrammarAST)previous.GetChild(1);
                        adaptor.AddChild(combinedBlock, translatedAlt.GetChild(1).GetChild(0));

#if false
                        if (LOGGER.isLoggable(Level.FINE))
                        {
                            LOGGER.log(Level.FINE, previous.ToStringTree());
                        }
#endif
                    }
                    else
                    {
                        GrammarAST translatedAlt = factoredAlternatives[i];
                        if (translatedAlt.ChildCount == 0)
                        {
                            adaptor.AddChild(translatedAlt, adaptor.Create(ANTLRParser.EPSILON, "EPSILON"));
                        }

                        adaptor.AddChild(newChildren, translatedAlt);
                    }
                }

                if (mode.IncludeUnfactoredAlts() && unfactoredIntervals.Contains(i))
                {
                    GrammarAST translatedAlt = unfactoredAlternatives[i];
                    if (translatedAlt.ChildCount == 0)
                    {
                        adaptor.AddChild(translatedAlt, adaptor.Create(ANTLRParser.EPSILON, "EPSILON"));
                    }

                    adaptor.AddChild(newChildren, translatedAlt);
                }
            }

            adaptor.ReplaceChildren(block, 0, block.ChildCount - 1, newChildren);

            if (!variant && block.Parent is RuleAST)
            {
                RuleAST            ruleAST   = (RuleAST)block.Parent;
                string             ruleName  = ruleAST.GetChild(0).Text;
                Rule               r         = _rules[ruleName];
                IList <GrammarAST> blockAlts = block.GetAllChildrenWithType(ANTLRParser.ALT);
                r.numberOfAlts = blockAlts.Count;
                r.alt          = new Alternative[blockAlts.Count + 1];
                for (int i = 0; i < blockAlts.Count; i++)
                {
                    r.alt[i + 1]     = new Alternative(r, i + 1);
                    r.alt[i + 1].ast = (AltAST)blockAlts[i];
                }
            }

            return(true);
        }
예제 #20
0
        protected virtual RuleVariants CreateLeftFactoredRuleVariant(Rule rule, string factoredElement)
        {
            RuleAST  ast   = (RuleAST)rule.ast.DupTree();
            BlockAST block = (BlockAST)ast.GetFirstChildWithType(ANTLRParser.BLOCK);

            RuleAST  unfactoredAst   = null;
            BlockAST unfactoredBlock = null;

            if (TranslateLeftFactoredDecision(block, factoredElement, true, DecisionFactorMode.FULL_FACTOR, false))
            {
                // all alternatives factored
            }
            else
            {
                ast   = (RuleAST)rule.ast.DupTree();
                block = (BlockAST)ast.GetFirstChildWithType(ANTLRParser.BLOCK);
                if (!TranslateLeftFactoredDecision(block, factoredElement, true, DecisionFactorMode.PARTIAL_FACTORED, false))
                {
                    // no left factored alts
                    return(RuleVariants.NONE);
                }

                unfactoredAst   = (RuleAST)rule.ast.DupTree();
                unfactoredBlock = (BlockAST)unfactoredAst.GetFirstChildWithType(ANTLRParser.BLOCK);
                if (!TranslateLeftFactoredDecision(unfactoredBlock, factoredElement, true, DecisionFactorMode.PARTIAL_UNFACTORED, false))
                {
                    throw new InvalidOperationException("expected unfactored alts for partial factorization");
                }
            }

            /*
             * factored elements
             */
            {
                string variantName = ast.GetChild(0).Text + ATNSimulator.RuleLfVariantMarker + factoredElement;
                ((GrammarAST)ast.GetChild(0)).Token = adaptor.CreateToken(ast.GetChild(0).Type, variantName);
                GrammarAST ruleParent = (GrammarAST)rule.ast.Parent;
                ruleParent.InsertChild(rule.ast.ChildIndex + 1, ast);
                ruleParent.FreshenParentAndChildIndexes(rule.ast.ChildIndex);

                IList <GrammarAST> alts = block.GetAllChildrenWithType(ANTLRParser.ALT);
                Rule variant            = new Rule(_g, ast.GetChild(0).Text, ast, alts.Count);
                _g.DefineRule(variant);
                for (int i = 0; i < alts.Count; i++)
                {
                    variant.alt[i + 1].ast = (AltAST)alts[i];
                }
            }

            /*
             * unfactored elements
             */
            if (unfactoredAst != null)
            {
                string variantName = unfactoredAst.GetChild(0).Text + ATNSimulator.RuleNolfVariantMarker + factoredElement;
                ((GrammarAST)unfactoredAst.GetChild(0)).Token = adaptor.CreateToken(unfactoredAst.GetChild(0).Type, variantName);
                GrammarAST ruleParent = (GrammarAST)rule.ast.Parent;
                ruleParent.InsertChild(rule.ast.ChildIndex + 1, unfactoredAst);
                ruleParent.FreshenParentAndChildIndexes(rule.ast.ChildIndex);

                IList <GrammarAST> alts = unfactoredBlock.GetAllChildrenWithType(ANTLRParser.ALT);
                Rule variant            = new Rule(_g, unfactoredAst.GetChild(0).Text, unfactoredAst, alts.Count);
                _g.DefineRule(variant);
                for (int i = 0; i < alts.Count; i++)
                {
                    variant.alt[i + 1].ast = (AltAST)alts[i];
                }
            }

            /*
             * result
             */
            return(unfactoredAst == null ? RuleVariants.FULLY_FACTORED : RuleVariants.PARTIALLY_FACTORED);
        }
예제 #21
0
파일: Parser.cs 프로젝트: Stu042/SimpleC
 public ObjDefAST(Token token, BlockAST stmt)
 {
     this.token = token;
     this.name  = token.value;
     this.stmt  = stmt;
 }
예제 #22
0
 public bool IsSLLDecision([NotNull] BlockAST blkAST)
 {
     return(true.ToString().Equals(blkAST.GetOptionString("sll"), StringComparison.OrdinalIgnoreCase));
 }
        /** Build lexer grammar from combined grammar that looks like:
         *
         *  (COMBINED_GRAMMAR A
         *      (tokens { X (= Y 'y'))
         *      (OPTIONS (= x 'y'))
         *      (@ members {foo})
         *      (@ lexer header {package jj;})
         *      (RULES (RULE .+)))
         *
         *  Move rules and actions to new tree, don't dup. Split AST apart.
         *  We'll have this Grammar share token symbols later; don't generate
         *  tokenVocab or tokens{} section.  Copy over named actions.
         *
         *  Side-effects: it removes children from GRAMMAR &amp; RULES nodes
         *                in combined AST.  Anything cut out is dup'd before
         *                adding to lexer to avoid "who's ur daddy" issues
         */
        public virtual GrammarRootAST ExtractImplicitLexer(Grammar combinedGrammar)
        {
            GrammarRootAST combinedAST = combinedGrammar.ast;
            //tool.log("grammar", "before="+combinedAST.toStringTree());
            GrammarASTAdaptor adaptor = new GrammarASTAdaptor(combinedAST.Token.InputStream);

            GrammarAST[] elements = combinedAST.GetChildrenAsArray();

            // MAKE A GRAMMAR ROOT and ID
            string         lexerName = combinedAST.GetChild(0).Text + "Lexer";
            GrammarRootAST lexerAST  =
                new GrammarRootAST(new CommonToken(ANTLRParser.GRAMMAR, "LEXER_GRAMMAR"), combinedGrammar.ast.tokenStream);

            lexerAST.grammarType       = ANTLRParser.LEXER;
            lexerAST.Token.InputStream = combinedAST.Token.InputStream;
            lexerAST.AddChild((ITree)adaptor.Create(ANTLRParser.ID, lexerName));

            // COPY OPTIONS
            GrammarAST optionsRoot =
                (GrammarAST)combinedAST.GetFirstChildWithType(ANTLRParser.OPTIONS);

            if (optionsRoot != null && optionsRoot.ChildCount != 0)
            {
                GrammarAST lexerOptionsRoot = (GrammarAST)adaptor.DupNode(optionsRoot);
                lexerAST.AddChild(lexerOptionsRoot);
                GrammarAST[] options = optionsRoot.GetChildrenAsArray();
                foreach (GrammarAST o in options)
                {
                    string optionName = o.GetChild(0).Text;
                    if (Grammar.lexerOptions.Contains(optionName) &&
                        !Grammar.doNotCopyOptionsToLexer.Contains(optionName))
                    {
                        GrammarAST optionTree = (GrammarAST)adaptor.DupTree(o);
                        lexerOptionsRoot.AddChild(optionTree);
                        lexerAST.SetOption(optionName, (GrammarAST)optionTree.GetChild(1));
                    }
                }
            }

            // COPY all named actions, but only move those with lexer:: scope
            IList <GrammarAST> actionsWeMoved = new List <GrammarAST>();

            foreach (GrammarAST e in elements)
            {
                if (e.Type == ANTLRParser.AT)
                {
                    lexerAST.AddChild((ITree)adaptor.DupTree(e));
                    if (e.GetChild(0).Text.Equals("lexer"))
                    {
                        actionsWeMoved.Add(e);
                    }
                }
            }

            foreach (GrammarAST r in actionsWeMoved)
            {
                combinedAST.DeleteChild(r);
            }

            GrammarAST combinedRulesRoot =
                (GrammarAST)combinedAST.GetFirstChildWithType(ANTLRParser.RULES);

            if (combinedRulesRoot == null)
            {
                return(lexerAST);
            }

            // MOVE lexer rules

            GrammarAST lexerRulesRoot = (GrammarAST)adaptor.Create(ANTLRParser.RULES, "RULES");

            lexerAST.AddChild(lexerRulesRoot);
            IList <GrammarAST> rulesWeMoved = new List <GrammarAST>();

            GrammarASTWithOptions[] rules;
            if (combinedRulesRoot.ChildCount > 0)
            {
                rules = combinedRulesRoot.Children.Cast <GrammarASTWithOptions>().ToArray();
            }
            else
            {
                rules = new GrammarASTWithOptions[0];
            }

            foreach (GrammarASTWithOptions r in rules)
            {
                string ruleName = r.GetChild(0).Text;
                if (Grammar.IsTokenName(ruleName))
                {
                    lexerRulesRoot.AddChild((ITree)adaptor.DupTree(r));
                    rulesWeMoved.Add(r);
                }
            }

            foreach (GrammarAST r in rulesWeMoved)
            {
                combinedRulesRoot.DeleteChild(r);
            }

            // Will track 'if' from IF : 'if' ; rules to avoid defining new token for 'if'
            IList <System.Tuple <GrammarAST, GrammarAST> > litAliases =
                Grammar.GetStringLiteralAliasesFromLexerRules(lexerAST);

            ISet <string> stringLiterals = combinedGrammar.GetStringLiterals();
            // add strings from combined grammar (and imported grammars) into lexer
            // put them first as they are keywords; must resolve ambigs to these rules
            //		tool.log("grammar", "strings from parser: "+stringLiterals);
            int insertIndex = 0;

            foreach (string lit in stringLiterals)
            {
                // if lexer already has a rule for literal, continue
                if (litAliases != null)
                {
                    foreach (System.Tuple <GrammarAST, GrammarAST> pair in litAliases)
                    {
                        GrammarAST litAST = pair.Item2;
                        if (lit.Equals(litAST.Text))
                        {
                            goto continueNextLit;
                        }
                    }
                }
                // create for each literal: (RULE <uniquename> (BLOCK (ALT <lit>))
                string rname = combinedGrammar.GetStringLiteralLexerRuleName(lit);
                // can't use wizard; need special node types
                GrammarAST  litRule = new RuleAST(ANTLRParser.RULE);
                BlockAST    blk     = new BlockAST(ANTLRParser.BLOCK);
                AltAST      alt     = new AltAST(ANTLRParser.ALT);
                TerminalAST slit    = new TerminalAST(new CommonToken(ANTLRParser.STRING_LITERAL, lit));
                alt.AddChild(slit);
                blk.AddChild(alt);
                CommonToken idToken = new CommonToken(ANTLRParser.TOKEN_REF, rname);
                litRule.AddChild(new TerminalAST(idToken));
                litRule.AddChild(blk);
                lexerRulesRoot.InsertChild(insertIndex, litRule);
                //			lexerRulesRoot.getChildren().add(0, litRule);
                lexerRulesRoot.FreshenParentAndChildIndexes(); // reset indexes and set litRule parent

                // next literal will be added after the one just added
                insertIndex++;

continueNextLit:
                ;
            }

            // TODO: take out after stable if slow
            lexerAST.SanityCheckParentAndChildIndexes();
            combinedAST.SanityCheckParentAndChildIndexes();
            //		tool.log("grammar", combinedAST.toTokenString());

            combinedGrammar.tool.Log("grammar", "after extract implicit lexer =" + combinedAST.ToStringTree());
            combinedGrammar.tool.Log("grammar", "lexer =" + lexerAST.ToStringTree());

            if (lexerRulesRoot.ChildCount == 0)
            {
                return(null);
            }
            return(lexerAST);
        }
예제 #24
0
 public virtual Choice GetComplexChoiceBlock(BlockAST blkAST, IList <CodeBlockForAlt> alts)
 {
     return(null);
 }
예제 #25
0
파일: Parser.cs 프로젝트: Stu042/SimpleC
 // simple token list:
 //	ID: int MAIN: main OPEN_BRACKET: ( CLOSE_BRACKET: ) END_LINE:
 //	INDENTATION:     COMMAND: return INT: 2 END_LINE:
 public void Parse()
 {
     ast = Statments();
 }
예제 #26
0
 public override Choice GetComplexChoiceBlock(BlockAST blkAST, IList <CodeBlockForAlt> alts)
 {
     return(new AltBlock(this, blkAST, alts));
 }
예제 #27
0
파일: Parser.cs 프로젝트: Stu042/SimpleC
        BlockAST Statments(int startIndent = 0)
        {
            List <AST> stmts    = new List <AST>();
            Token      firstTok = tokens.current;

            do
            {
                string found = GetStmntMatch(tokens);
                switch (found)
                {
                case "Command": {
                    AST stmtAST = null;
                    switch (tokens.curTokenType)
                    {
                    case TokenType.Return:
                        Token   retTok = tokens.current;
                        ExprAST expAST = ParseExpr();
                        stmtAST = new RetCmdAST(retTok, expAST);
                        break;

                    default:
                        ShowError("Unknown command", tokens.current);
                        break;
                    }
                    if (stmtAST != null)
                    {
                        stmts.Add(stmtAST);
                    }
                    break;
                }

                case "Main": {
                    TypeAST  typeAST = new TypeAST(tokens.current);
                    Token    mainTok = tokens.Next();
                    ArgAST[] argsAST = ParseArgs();
                    symbols.AddParent(typeAST, mainTok.value, argsAST);
                    BlockAST stmtAST = Statments(startIndent + 1);
                    symbols.RemParent();
                    AST functAST = new FuncDefAST(mainTok, typeAST, argsAST, stmtAST);
                    stmts.Add(functAST);
                    break;
                }

                case "Function": {
                    TypeAST  typeAST = new TypeAST(tokens.current);
                    Token    instTok = tokens.Next();
                    ArgAST[] argsAST = ParseArgs();
                    symbols.AddParent(typeAST, instTok.value, argsAST);
                    BlockAST stmtAST = Statments(startIndent + 1);
                    symbols.RemParent();
                    AST functAST = new FuncDefAST(instTok, typeAST, argsAST, stmtAST);
                    stmts.Add(functAST);
                    break;
                }

                case "ObjectDef": {                             // TokenType.Identifier, TokenType.Colon
                    TypeAST typeAST = new TypeAST(tokens.current);
                    tokens.Next();
                    symbols.AddParent(typeAST, "", null);
                    tokens.Next();
                    BlockAST stmtAST = Statments(startIndent + 1);
                    symbols.RemParent();
                    ObjDefAST odAST = new ObjDefAST(tokens.current, stmtAST);
                    stmts.Add(odAST);
                    break;
                }

                case "SimpleObjectInst": {                          // TokenType.AllTypes, TokenType.Identifier
                    TypeAST typeAST = new TypeAST(tokens.current);
                    tokens.Next();
                    symbols.Add(typeAST, tokens.current.value, null);
                    ObjInstAST odAST = new ObjInstAST(tokens.current, typeAST, null);
                    stmts.Add(odAST);
                    break;
                }

                case "ObjectInst": {
                    TypeAST typeAST = new TypeAST(tokens.current);
                    tokens.Next();
                    symbols.Add(typeAST, tokens.current.value, null);
                    ObjInstAST odAST = new ObjInstAST(tokens.current, typeAST, null);
                    stmts.Add(odAST);
                    break;
                }

                case "SimpleObjectInstAssigned": {
                    TypeAST typeAST = new TypeAST(tokens.current);
                    Token   instTok = tokens.Next();
                    symbols.Add(typeAST, instTok.value, null);
                    tokens.Next();
                    NumAST     numAST = new NumAST(tokens.current, tokens.current.value, tokens.curTokenType);
                    ObjInstAST odAST  = new ObjInstAST(instTok, typeAST, numAST);
                    stmts.Add(odAST);
                    break;
                }

                case "ObjectInstAssigned": {
                    TypeAST typeAST = new TypeAST(tokens.current);
                    Token   instTok = tokens.Next();
                    symbols.Add(typeAST, instTok.value, null);
                    tokens.Next();
                    NumAST     numAST = new NumAST(tokens.current, tokens.current.value, tokens.curTokenType);
                    ObjInstAST odAST  = new ObjInstAST(instTok, typeAST, numAST);
                    stmts.Add(odAST);
                    break;
                }

                default:
                    ShowError("Unable to parse statement", tokens.current);
                    break;
                }
            } while (tokens.Remaining() > 0);
            return(new BlockAST(firstTok, stmts.ToArray()));
        }