Inheritance: GrammarTreeVisitor
Ejemplo n.º 1
0
 internal virtual void IdentifyStartRules(SymbolCollector collector)
 {
     foreach (GrammarAST @ref in collector.rulerefs)
     {
         string ruleName = @ref.Text;
         Rule   r        = g.GetRule(ruleName);
         if (r != null)
         {
             r.isStartRule = false;
         }
     }
 }
Ejemplo n.º 2
0
        public SymbolChecks(Grammar g, SymbolCollector collector)
        {
            this.g = g;
            this.collector = collector;
            this.errMgr = g.tool.errMgr;

            foreach (GrammarAST tokenId in collector.tokenIDRefs)
            {
                tokenIDs.Add(tokenId.Text);
            }

            //System.Console.WriteLine("rules="+collector.rules);
            //System.Console.WriteLine("rulerefs="+collector.rulerefs);
            //System.Console.WriteLine("tokenIDRefs="+collector.tokenIDRefs);
            //System.Console.WriteLine("terminals="+collector.terminals);
            //System.Console.WriteLine("strings="+collector.strings);
            //System.Console.WriteLine("tokensDef="+collector.tokensDefs);
            //System.Console.WriteLine("actions="+collector.actions);
            //System.Console.WriteLine("scopes="+collector.scopes);
        }
Ejemplo n.º 3
0
        public SymbolChecks(Grammar g, SymbolCollector collector)
        {
            this.g         = g;
            this.collector = collector;
            this.errMgr    = g.tool.errMgr;

            foreach (GrammarAST tokenId in collector.tokenIDRefs)
            {
                tokenIDs.Add(tokenId.Text);
            }

            //System.Console.WriteLine("rules="+collector.rules);
            //System.Console.WriteLine("rulerefs="+collector.rulerefs);
            //System.Console.WriteLine("tokenIDRefs="+collector.tokenIDRefs);
            //System.Console.WriteLine("terminals="+collector.terminals);
            //System.Console.WriteLine("strings="+collector.strings);
            //System.Console.WriteLine("tokensDef="+collector.tokensDefs);
            //System.Console.WriteLine("actions="+collector.actions);
            //System.Console.WriteLine("scopes="+collector.scopes);
        }
Ejemplo n.º 4
0
        public virtual void Process()
        {
            if (g.ast == null)
            {
                return;
            }

            // COLLECT RULE OBJECTS
            RuleCollector ruleCollector = new RuleCollector(g);

            ruleCollector.Process(g.ast);

            // CLONE RULE ASTs FOR CONTEXT REFERENCE
            foreach (Rule rule in ruleCollector.rules.Values)
            {
                IList <RuleAST> list;
                if (!g.contextASTs.TryGetValue(rule.GetBaseContext(), out list) || list == null)
                {
                    list = new List <RuleAST>();
                    g.contextASTs[rule.GetBaseContext()] = list;
                }

                list.Add((RuleAST)rule.ast.DupTree());
            }

            // DO BASIC / EASY SEMANTIC CHECKS
            int prevErrors             = g.tool.errMgr.GetNumErrors();
            BasicSemanticChecks basics = new BasicSemanticChecks(g, ruleCollector);

            basics.Process();
            if (g.tool.errMgr.GetNumErrors() > prevErrors)
            {
                return;
            }

            // TRANSFORM LEFT-RECURSIVE RULES
            prevErrors = g.tool.errMgr.GetNumErrors();
            LeftRecursiveRuleTransformer lrtrans =
                new LeftRecursiveRuleTransformer(g.ast, ruleCollector.rules.Values, g);

            lrtrans.TranslateLeftRecursiveRules();

            // don't continue if we got errors during left-recursion elimination
            if (g.tool.errMgr.GetNumErrors() > prevErrors)
            {
                return;
            }

            // AUTO LEFT FACTORING
            LeftFactoringRuleTransformer lftrans = new LeftFactoringRuleTransformer(g.ast, ruleCollector.rules, g);

            lftrans.TranslateLeftFactoredRules();

            // STORE RULES IN GRAMMAR
            foreach (Rule r in ruleCollector.rules.Values)
            {
                g.DefineRule(r);
            }

            // COLLECT SYMBOLS: RULES, ACTIONS, TERMINALS, ...
            SymbolCollector collector = new SymbolCollector(g);

            collector.Process(g.ast);

            // CHECK FOR SYMBOL COLLISIONS
            SymbolChecks symcheck = new SymbolChecks(g, collector);

            symcheck.Process(); // side-effect: strip away redef'd rules.

            foreach (GrammarAST a in collector.namedActions)
            {
                g.DefineAction(a);
            }

            // LINK (outermost) ALT NODES WITH Alternatives
            foreach (Rule r in g.rules.Values)
            {
                for (int i = 1; i <= r.numberOfAlts; i++)
                {
                    r.alt[i].ast.alt = r.alt[i];
                }
            }

            // ASSIGN TOKEN TYPES
            g.ImportTokensFromTokensFile();
            if (g.IsLexer())
            {
                AssignLexerTokenTypes(g, collector.tokensDefs);
            }
            else
            {
                AssignTokenTypes(g, collector.tokensDefs,
                                 collector.tokenIDRefs, collector.terminals);
            }

            symcheck.CheckForModeConflicts(g);

            AssignChannelTypes(g, collector.channelDefs);

            // CHECK RULE REFS NOW (that we've defined rules in grammar)
            symcheck.CheckRuleArgs(g, collector.rulerefs);
            IdentifyStartRules(collector);
            symcheck.CheckForQualifiedRuleIssues(g, collector.qualifiedRulerefs);

            // don't continue if we got symbol errors
            if (g.tool.GetNumErrors() > 0)
            {
                return;
            }

            // CHECK ATTRIBUTE EXPRESSIONS FOR SEMANTIC VALIDITY
            AttributeChecks.CheckAllAttributeExpressions(g);

            UseDefAnalyzer.TrackTokenRuleRefsInActions(g);
        }
Ejemplo n.º 5
0
        public virtual void Process()
        {
            if (g.ast == null)
                return;

            // COLLECT RULE OBJECTS
            RuleCollector ruleCollector = new RuleCollector(g);
            ruleCollector.Process(g.ast);

            // CLONE RULE ASTs FOR CONTEXT REFERENCE
            foreach (Rule rule in ruleCollector.rules.Values)
            {
                IList<RuleAST> list;
                if (!g.contextASTs.TryGetValue(rule.GetBaseContext(), out list) || list == null)
                {
                    list = new List<RuleAST>();
                    g.contextASTs[rule.GetBaseContext()] = list;
                }

                list.Add((RuleAST)rule.ast.DupTree());
            }

            // DO BASIC / EASY SEMANTIC CHECKS
            int prevErrors = g.tool.errMgr.GetNumErrors();
            BasicSemanticChecks basics = new BasicSemanticChecks(g, ruleCollector);
            basics.Process();
            if (g.tool.errMgr.GetNumErrors() > prevErrors)
                return;

            // TRANSFORM LEFT-RECURSIVE RULES
            prevErrors = g.tool.errMgr.GetNumErrors();
            LeftRecursiveRuleTransformer lrtrans =
                new LeftRecursiveRuleTransformer(g.ast, ruleCollector.rules.Values, g);
            lrtrans.TranslateLeftRecursiveRules();

            // don't continue if we got errors during left-recursion elimination
            if (g.tool.errMgr.GetNumErrors() > prevErrors)
            {
                return;
            }

            // AUTO LEFT FACTORING
            LeftFactoringRuleTransformer lftrans = new LeftFactoringRuleTransformer(g.ast, ruleCollector.rules, g);
            lftrans.TranslateLeftFactoredRules();

            // STORE RULES IN GRAMMAR
            foreach (Rule r in ruleCollector.rules.Values)
            {
                g.DefineRule(r);
            }

            // COLLECT SYMBOLS: RULES, ACTIONS, TERMINALS, ...
            SymbolCollector collector = new SymbolCollector(g);
            collector.Process(g.ast);

            // CHECK FOR SYMBOL COLLISIONS
            SymbolChecks symcheck = new SymbolChecks(g, collector);
            symcheck.Process(); // side-effect: strip away redef'd rules.

            foreach (GrammarAST a in collector.namedActions)
            {
                g.DefineAction(a);
            }

            // LINK (outermost) ALT NODES WITH Alternatives
            foreach (Rule r in g.rules.Values)
            {
                for (int i = 1; i <= r.numberOfAlts; i++)
                {
                    r.alt[i].ast.alt = r.alt[i];
                }
            }

            // ASSIGN TOKEN TYPES
            g.ImportTokensFromTokensFile();
            if (g.IsLexer())
            {
                AssignLexerTokenTypes(g, collector.tokensDefs);
            }
            else
            {
                AssignTokenTypes(g, collector.tokensDefs,
                                 collector.tokenIDRefs, collector.terminals);
            }

            symcheck.CheckForModeConflicts(g);

            AssignChannelTypes(g, collector.channelDefs);

            // CHECK RULE REFS NOW (that we've defined rules in grammar)
            symcheck.CheckRuleArgs(g, collector.rulerefs);
            IdentifyStartRules(collector);
            symcheck.CheckForQualifiedRuleIssues(g, collector.qualifiedRulerefs);

            // don't continue if we got symbol errors
            if (g.tool.GetNumErrors() > 0)
                return;

            // CHECK ATTRIBUTE EXPRESSIONS FOR SEMANTIC VALIDITY
            AttributeChecks.CheckAllAttributeExpressions(g);

            UseDefAnalyzer.TrackTokenRuleRefsInActions(g);
        }
Ejemplo n.º 6
0
 internal virtual void IdentifyStartRules(SymbolCollector collector)
 {
     foreach (GrammarAST @ref in collector.rulerefs)
     {
         string ruleName = @ref.Text;
         Rule r = g.GetRule(ruleName);
         if (r != null)
             r.isStartRule = false;
     }
 }