Example #1
0
        private void ParseDeclarations()
        {
            int prec = 0;

            while (token != GrammarToken.EndOfSection && token != GrammarToken.Eof)
            {
                switch (token)
                {
                case GrammarToken.Prolog:
                {
                    grammar.prologCode += scanner.yylval;
                    Advance();
                    break;
                }

                case GrammarToken.Start:
                {
                    Advance();
                    if (token == GrammarToken.Symbol)
                    {
                        grammar.startSymbol = grammar.LookupNonTerminal(scanner.yylval);
                        Advance();
                    }
                    break;
                }

                case GrammarToken.Left:
                {
                    Advance();
                    prec += 10;
                    while (token == GrammarToken.Symbol || token == GrammarToken.Literal)
                    {
                        Terminal t = grammar.LookupTerminal(token, scanner.yylval);
                        t.prec = new Precedence(PrecType.left, prec);
                        Advance();
                    }
                    break;
                }

                case GrammarToken.Right:
                {
                    Advance();
                    prec += 10;
                    while (token == GrammarToken.Symbol || token == GrammarToken.Literal)
                    {
                        Terminal t = grammar.LookupTerminal(token, scanner.yylval);
                        t.prec = new Precedence(PrecType.right, prec);
                        Advance();
                    }
                    break;
                }

                case GrammarToken.NonAssoc:
                {
                    Advance();
                    prec += 10;
                    while (token == GrammarToken.Symbol || token == GrammarToken.Literal)
                    {
                        Terminal t = grammar.LookupTerminal(token, scanner.yylval);
                        t.prec = new Precedence(PrecType.nonassoc, prec);
                        Advance();
                    }
                    break;
                }

                case GrammarToken.Token:
                {
                    Advance();
                    string kind = null;
                    if (token == GrammarToken.Kind)
                    {
                        kind = scanner.yylval;
                        Advance();
                    }
                    while (token == GrammarToken.Symbol)
                    {
                        Terminal terminal = grammar.LookupTerminal(token, scanner.yylval);
                        terminal.kind = kind;
                        Advance();
                    }
                    break;
                }

                case GrammarToken.Type:
                {
                    Advance();
                    string kind = null;
                    if (token == GrammarToken.Kind)
                    {
                        kind = scanner.yylval;
                        Advance();
                    }
                    while (token == GrammarToken.Symbol)
                    {
                        NonTerminal nonterminal = grammar.LookupNonTerminal(scanner.yylval);
                        nonterminal.kind = kind;
                        Advance();
                    }
                    break;
                }

                case GrammarToken.Union:
                {
                    grammar.unionType = scanner.yylval;
                    Advance();
                    break;
                }

                case GrammarToken.Namespace:
                {
                    Advance();
                    grammar.Namespace = scanner.yylval;
                    Advance();
                    while (scanner.yylval == ".")
                    {
                        Advance();
                        grammar.Namespace += "." + scanner.yylval;
                        Advance();
                    }
                    break;
                }

                case GrammarToken.Output:
                {
                    grammar.OutFName = scanner.yylval;
                    Advance();
                    break;
                }

                case GrammarToken.Defines:
                {
                    grammar.TokFName = baseName + ".tokens";
                    Advance();
                    break;
                }

                case GrammarToken.Partial:
                {
                    Advance();
                    grammar.IsPartial = true;
                    break;
                }

                case GrammarToken.ParserName:
                {
                    Advance();
                    grammar.ParserName = scanner.yylval;
                    Advance();
                    break;
                }

                case GrammarToken.TokenName:
                {
                    Advance();
                    grammar.TokenName = scanner.yylval;
                    Advance();
                    break;
                }

                case GrammarToken.LocationTypeName:
                {
                    Advance();
                    grammar.LocationTypeName = scanner.yylval;
                    Advance();
                    break;
                }

                case GrammarToken.ValueTypeName:
                {
                    Advance();
                    grammar.ValueTypeName = scanner.yylval;
                    Advance();
                    break;
                }

                case GrammarToken.Visibility:
                {
                    Advance();
                    grammar.Visibility = scanner.yylval;
                    Advance();
                    break;
                }

                case GrammarToken.Locations:
                {
                    Advance();         // does nothing in this version: location tracking is always on.
                    break;
                }

                default:
                {
                    scanner.ReportError("Unexpected token {0} in declaration section", token);
                    Advance();
                    break;
                }
                }
            }
            Advance();
        }
Example #2
0
        private void ParseRhs(NonTerminal lhs)
        {
            Production production = new Production(lhs);
            int        num        = 0;

            while (this.token == GrammarToken.Symbol || this.token == GrammarToken.Literal || this.token == GrammarToken.Action || this.token == GrammarToken.Prec)
            {
                GrammarToken grammarToken = this.token;
                switch (grammarToken)
                {
                case GrammarToken.Symbol:
                    if (this.grammar.terminals.ContainsKey(this.scanner.yylval))
                    {
                        production.rhs.Add(this.grammar.terminals[this.scanner.yylval]);
                    }
                    else
                    {
                        production.rhs.Add(this.grammar.LookupNonTerminal(this.scanner.yylval));
                    }
                    this.Advance();
                    num++;
                    break;

                case GrammarToken.Literal:
                    production.rhs.Add(this.grammar.LookupTerminal(this.token, this.scanner.yylval));
                    this.Advance();
                    num++;
                    break;

                case GrammarToken.Action:
                {
                    SemanticAction semanticAction = new SemanticAction(production, this.tokenStartLine, num, this.scanner.yylval);
                    this.Advance();
                    if (this.token == GrammarToken.Divider || this.token == GrammarToken.SemiColon || this.token == GrammarToken.Prec)
                    {
                        production.semanticAction = semanticAction;
                    }
                    else
                    {
                        Grammar     arg_1BA_0   = this.grammar;
                        string      arg_1B5_0   = "@";
                        int         num2        = ++this.grammar.NumActions;
                        NonTerminal nonTerminal = arg_1BA_0.LookupNonTerminal(arg_1B5_0 + num2.ToString());
                        Production  production2 = new Production(nonTerminal);
                        production2.semanticAction = semanticAction;
                        this.grammar.AddProduction(production2);
                        production.rhs.Add(nonTerminal);
                    }
                    num++;
                    break;
                }

                default:
                    if (grammarToken == GrammarToken.Prec)
                    {
                        this.Advance();
                        if (this.token == GrammarToken.Symbol)
                        {
                            production.prec = this.grammar.LookupTerminal(this.token, this.scanner.yylval).prec;
                            this.Advance();
                        }
                        else
                        {
                            this.scanner.ReportError("Expected symbol after %prec", new object[0]);
                        }
                    }
                    break;
                }
            }
            this.grammar.AddProduction(production);
            Precedence.Calculate(production);
        }