Beispiel #1
0
		public void CreateSpecialProduction(NonTerminal root)
		{
			this.rootProduction = new Production(this.LookupNonTerminal("$accept"));
			this.AddProduction(this.rootProduction);
			this.rootProduction.rhs.Add(root);
			this.rootProduction.rhs.Add(this.LookupTerminal(GrammarToken.Symbol, "EOF"));
		}
Beispiel #2
0
        private void ComputeLA()
        {
            // LA(q, A->w) = Union { Follow(p,A) | p -> w -> q }

            foreach (State q in states)
            {
                foreach (ProductionItem item in q.all_items)
                {
                    if (item.isReduction())
                    {
                        item.LA = new Set <Terminal>();
                        foreach (State p in states)
                        {
                            if (PathTo(p, item.production, item.pos) == q)
                            {
                                NonTerminal A = item.production.lhs;
                                if (p.nonTerminalTransitions.ContainsKey(A))
                                {
                                    Transition pA = p.nonTerminalTransitions[A];
                                    item.LA.AddRange(pA.Follow);
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #3
0
        public void CreateSpecialProduction(NonTerminal root)
        {
            this.rootProduction = new Production(this.LookupNonTerminal("$accept"));
            this.AddProduction(this.rootProduction);
            this.rootProduction.rhs.Add(root);
            Terminal terminal = this.LookupTerminal(GrammarToken.Symbol, "EOF");

            terminal.SetValue(0);
            this.rootProduction.rhs.Add(terminal);
        }
Beispiel #4
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);
		}
Beispiel #5
0
 public Transition(State p, NonTerminal A, State next)
 {
     this.p = p;
     this.A = A;
     this.next = next;
 }
Beispiel #6
0
 public Transition(State p, NonTerminal A, State next)
 {
     this.p    = p;
     this.A    = A;
     this.next = next;
 }
Beispiel #7
0
        public NonTerminal LookupNonTerminal(string name)
        {
            if (!nonTerminals.ContainsKey(name))
                nonTerminals[name] = new NonTerminal(name);

            return nonTerminals[name];
        }
Beispiel #8
0
		public Production(NonTerminal lhs)
		{
			this.lhs = lhs;
			lhs.productions.Add(this);
		}
Beispiel #9
0
 public Production(NonTerminal/*!*/ lhs)
 {
     Debug.Assert(lhs != null);
     this.lhs = lhs;
     lhs.productions.Add(this);
 }
Beispiel #10
0
 public Production(NonTerminal lhs)
 {
     this.lhs = lhs;
     lhs.productions.Add(this);
 }
Beispiel #11
0
        private void ParseRhs(NonTerminal lhs)
        {
            Production production = new Production(lhs);
            int pos = 0;

            while (token == GrammarToken.Symbol || token == GrammarToken.Literal ||
                   token == GrammarToken.Action || token == GrammarToken.Prec)
            {
                switch (token)
                {
                    case GrammarToken.Literal:
                    {
                        production.rhs.Add(grammar.LookupTerminal(token, scanner.yylval));
                        Advance();
                        pos++;
                        break;
                    }
                    case GrammarToken.Symbol:
                    {
                        if (grammar.terminals.ContainsKey(scanner.yylval))
                            production.rhs.Add(grammar.terminals[scanner.yylval]);
                        else
                            production.rhs.Add(grammar.LookupNonTerminal(scanner.yylval));
                        Advance();
                        pos++;
                        break;
                    }
                    case GrammarToken.Prec:
                    {
                        Advance();
                        if (token == GrammarToken.Symbol)
                        {
                            production.prec = grammar.LookupTerminal(token, scanner.yylval).prec;
                            Advance();
                        }
                        else
                            scanner.ReportError("Expected symbol after %prec");
                        break;
                    }
                    case GrammarToken.Action:
                    {
                        SemanticAction semanticAction = new SemanticAction(production, pos, scanner.yylval);
                        Advance();

                        if (token == GrammarToken.Divider || token == GrammarToken.SemiColon || token == GrammarToken.Prec) // reduce action
                            production.semanticAction = semanticAction;
                        else
                        {
                            NonTerminal node = grammar.LookupNonTerminal("@" + (++grammar.NumActions).ToString());
                            Production nullProduction = new Production(node);
                            nullProduction.semanticAction = semanticAction;
                            grammar.AddProduction(nullProduction);
                            production.rhs.Add(node);
                        }
                        pos++;
                        break;
                    }
                }
            }

            grammar.AddProduction(production);

            Precedence.Calculate(production);
        }
Beispiel #12
0
 public void CreateSpecialProduction(NonTerminal root)
 {
     rootProduction = new Production(LookupNonTerminal("$accept"));
     AddProduction(rootProduction);
     rootProduction.rhs.Add(root);
     rootProduction.rhs.Add(LookupTerminal(GrammarToken.Symbol, "END_OF_FILE"));
 }