Example #1
0
        private void ReductionEvent(ReduceEventArgs pValue)
        {
            switch (pValue.Token.Symbol.ToString())
            {
                case "<Value>":
                    string Value = "";//pValue.Token.Tokens[0].ToString().Trim(new char[] { '{', '}' });
                    //System.Console.Write(Value);
                    this.NodeStack.Push(new TreeNode(Value));
                    break;
                case "<Negate Exp>":
                    System.Collections.Generic.LinkedListNode<string> Node = new System.Collections.Generic.LinkedListNode<string>("-");
                    //System.Console.Write("-");
                    TreeNode NegateNode = new TreeNode("-");
                    NegateNode.Right = this.NodeStack.Pop();
                    this.NodeStack.Push(NegateNode);
                    break;
                case "<Add Exp>":
                    //System.Console.Write("+");
                    TreeNode AddExpNode = new TreeNode("+");
                    AddExpNode.Right = this.NodeStack.Pop();
                    AddExpNode.Left = this.NodeStack.Pop();
                    this.NodeStack.Push(AddExpNode);
                    break;
                case "<Assign_Statement>":
                    string Identifier = "";//args.Token.Tokens[0].ToString().Trim(new char[] { '[', ']' });
                    //System.Console.Write(Identifier);
                    //System.Console.Write("=");
                    TreeNode AssignNode = new TreeNode("=");
                    this.TreeList.GetLast().Right = AssignNode;
                    AssignNode.Right = this.NodeStack.Pop();
                    AssignNode.Left = new TreeNode(Identifier);
                    this.NodeStack.Push(AssignNode);
                    break;
                case "<Statement>":
                    TreeNode Statement = new TreeNode("statement\n");
                    Statement.Right = this.NodeStack.Pop();
                    Statement.Left = this.NodeStack.Pop();
                    this.NodeStack.Push(Statement);
                    break;

                case "<StatementList>":
                    TreeNode StatementList = new TreeNode("sl\n");
                    StatementList.Right = this.NodeStack.Pop();
                    StatementList.Left = this.NodeStack.Pop();
                    this.NodeStack.Push(StatementList);
                    break;
                case "<Program>":
                    TreeNode Program = new TreeNode("program");
                    Program.Right = this.NodeStack.Pop();
                    Program.Left = this.NodeStack.Pop();
                    this.NodeStack.Push(Program);
                    break;
                default:
                    System.Console.WriteLine(pValue.Token.Symbol.ToString());
                    break;
            }
        }
Example #2
0
        private void DoReduce(Token token, ReduceAction action)
        {
            int reduceLength = action.Rule.Rhs.Length;

            State currentState;
            // Do not reduce if the rule is single nonterminal and TrimReductions is on
            bool skipReduce = ((TrimReductions) && (reduceLength == 1) && (action.Rule.Rhs[0] is SymbolNonterminal));

            if (skipReduce)
            {
                stateStack.Pop();
                currentState = stateStack.Peek();
            }
            else
            {
                Token[] tokens = new Token[reduceLength];
                for (int i = 0; i < reduceLength; i++)
                {
                    stateStack.Pop();
                    tokens[reduceLength - i - 1] = tokenStack.Pop();
                }
                NonterminalToken nttoken;
                if (reduceLength == 0)
                {
                    nttoken = new NonterminalToken(action.Rule, tokens, token.Location);
                }
                else
                {
                    nttoken = new NonterminalToken(action.Rule, tokens, tokens[reduceLength - 1].Location);
                }

                tokenStack.Push(nttoken);
                currentState = stateStack.Peek();

                if (OnReduce != null)
                {
                    ReduceEventArgs args = new ReduceEventArgs(action.Rule, nttoken, currentState);
                    OnReduce(this, args);
                    DoReleaseTokens(args.Token);

                    continueParsing = args.Continue;
                }
            }
            Action gotoAction = currentState.Actions.Get(action.Rule.Lhs);

            if (gotoAction is GotoAction)
            {
                DoGoto(token, (GotoAction)gotoAction);
            }
            else
            {
                throw new ParserException("Invalid action table in state");
            }
        }
Example #3
0
 private void ReduceEvent(LALRParser parser, ReduceEventArgs args)
 {
     try
     {
         args.Token.UserObject = CreateObject(args.Token);
     }
     catch (Exception e)
     {
         args.Continue = false;
         MainForm.mainForm.WriteResult(e.Message);
     }
 }
Example #4
0
 private void ReduceEvent(LALRParser parser, ReduceEventArgs args)
 {
     try
     {
         args.Token.UserObject = CreateObject(args.Token);
     }
     catch (Exception e)
     {
         args.Continue = false;
         //todo: Report message to UI?
     }
 }
Example #5
0
 private void ReduceEvent(LALRParser parser, ReduceEventArgs args)
 {
     try
     {
         args.Token.UserObject = CreateObject(args.Token);
     }
     catch (Exception e)
     {
         _mainForm.setLeituraState(false);
         _mainForm.setSyntaxState(false);
         MessageBox.Show(e.ToString());
         args.Continue = false;
         //todo: Report message to UI?
     }
 }
Example #6
0
		private void DoReduce(Token token, ReduceAction action)
		{
			int reduceLength = action.Rule.Rhs.Length;

			State currentState;
			// Do not reduce if the rule is single nonterminal and TrimReductions is on
			bool skipReduce = ((TrimReductions) &&
				(reduceLength == 1) && ( action.Rule.Rhs[0] is SymbolNonterminal));
			if (skipReduce)
			{
				stateStack.Pop();
				currentState = stateStack.Peek();
			}
			else
			{
				Token[] tokens = new Token[reduceLength];
				for (int i = 0; i < reduceLength; i++)
				{
					stateStack.Pop();
					tokens[reduceLength-i-1] = tokenStack.Pop();
				}
				NonterminalToken nttoken = new NonterminalToken(action.Rule,tokens);
				tokenStack.Push(nttoken);
				currentState = stateStack.Peek();

				if (OnReduce != null)
				{
					ReduceEventArgs args = new ReduceEventArgs(action.Rule,
						                                       nttoken,
						                                       currentState);
					OnReduce(this,args);
					DoReleaseTokens(args.Token);

					continueParsing = args.Continue;
				}
			}
			Action gotoAction = currentState.Actions.Get(action.Rule.Lhs);

			if (gotoAction is GotoAction)
			{
				DoGoto(token,(GotoAction)gotoAction);
			}
			else
			{
				throw new ParserException("Invalid action table in state");
			}
		}
Example #7
0
 private void ReduceEvent(LALRParser parser, ReduceEventArgs args)
 {
     try
     {
         args.Token.UserObject = CreateObject(args.Token);
         //Console.WriteLine(args.Token);
     }
     catch (Exception e)
     {
         args.Continue = false;
         Console.WriteLine(args.Token.ToString());
         //todo: Report message to UI?
     }
 }
 private void ReduceEvent(LALRParser parser, ReduceEventArgs args)
 {
     try
     {
         args.Token.UserObject = CreateObject(args.Token);
     }
     catch (ApplicationException ex)
     {
         args.Continue = false;
         Logger.Log(DateTime.Now + ":  " + ex.Message);
         //todo: Report message to UI?
     }
 }
Example #9
0
 /// <summary>
 /// Event handler for the reduce action.
 /// </summary>
 /// <param name="parser">parser that is the source of this event</param>
 /// <param name="args">event arguments</param>
 private void ReduceEvent(LALRParser parser, ReduceEventArgs args)
 {
     //WriteLn(args.Rule.Id.ToString());
     //WriteLn(args.Rule.Lhs.Id.ToString());
     AddViewItem("Reduce",
         null,
         args.Token.Rule.ToString(),
         "",
         args.NewState.Id.ToString(),
         2);
 }
Example #10
0
 private void OnReduce(LALRParser argParser, ReduceEventArgs args)
 {
     args.Token.UserObject = CreateObject(args.Token, argParser);
     if (endSymbol == 2)
     {
         TerminalToken eofToken =
             new TerminalToken(SymbolCollection.EOF, SymbolCollection.EOF.Name, args.Token.Location);
         argParser.lookahead = eofToken;
         endSymbol = 3;
     }
 }
Example #11
0
 private void ReduceEvent(LALRParser parser, ReduceEventArgs args)
 {
     try
     {
         args.Token.UserObject = CreateObject(args.Token);
     }
     catch (Exception e)
     {
         args.Continue = false;
         MainForm.mainForm.WriteResult(e.Message);
     }
 }