Exemplo n.º 1
0
 //number→real_number |integer 产生实数或整数
 public static Value eval_number(NonterminalStackElement number_node)
 {
     if (number_node.branches.Count != 1)
     {
         throw new ExecutorException("文法发生了变化", number_node.linenum);
     }
     else
     {
         StackElement number = (StackElement)number_node.branches[0];
         if (number.type_code == 2)
         {
             IntStackElement int_number = (IntStackElement)number;
             return(new IntValue("int", true, int_number.content, int_number.linenum));
         }
         else if (number.type_code == 5)
         {
             RealStackElement real_number = (RealStackElement)number;
             return(new RealValue("real", true, real_number.content, real_number.linenum));
         }
         else
         {
             throw new ExecutorException("文法发生了变化", number_node.linenum);
         }
     }
 }
Exemplo n.º 2
0
        public override string ToString()
        {
            string text = "";

            if (type_code == 1)
            {
                IdentifierStackElement e = (IdentifierStackElement)this;
                text += "第" + e.linenum + "行标识符";
                text += e.content;
            }
            else if (type_code == 2)
            {
                IntStackElement e = (IntStackElement)this;
                text += "第" + e.linenum + "行整数";
                text += e.content;
            }
            else if (type_code == 3)
            {
                NonterminalStackElement e = (NonterminalStackElement)this;
                text += "非终结符";
                text += e.name;
                //递归调用
                foreach (StackElement s in e.branches)
                {
                    text += Environment.NewLine;
                    for (int i = 1; i < s.layers; i++)
                    {
                        text += "  ";
                    }
                    text += s.ToString();
                }
            }
            else if (type_code == 4)
            {
                OtherTerminalStackElement e = (OtherTerminalStackElement)this;
                text += "第" + e.linenum + "行终结符";
                text += e.content;
            }
            else if (type_code == 5)
            {
                RealStackElement e = (RealStackElement)this;
                text += "第" + e.linenum + "行实数";
                text += e.content;
            }
            else if (type_code == 7)
            {
                CharStackElement e = (CharStackElement)this;
                text += "第" + e.linenum + "行字符";
                text += e.content;
            }
            else if (type_code == 8)
            {
                StringStackElement e = (StringStackElement)this;
                text += "第" + e.linenum + "行字符串";
                text += e.content;
            }
            else
            {
                StateStackElement e = (StateStackElement)this;
                text += "状态" + e.state;
            }
            return(text);
        }
 private void recursiveAddNodes(StackElement e, TreeViewItem newItem)
 {
     try
     {
         if (e.type_code == 6)
         {
             throw new ParserException("这个表示状态的栈元素不应该出现在Tree的递归子结点中!");
         }
         else if (e.type_code == 1)
         {
             IdentifierStackElement ele = (IdentifierStackElement)e;
             newItem.Header = "第" + ele.linenum + "行的标识符:" + ele.content;
         }
         else if (e.type_code == 2)
         {
             IntStackElement ele = (IntStackElement)e;
             newItem.Header = "第" + ele.linenum + "行的整数:" + ele.content;
         }
         else if (e.type_code == 3)
         {
             NonterminalStackElement ele = (NonterminalStackElement)e;
             newItem.Header = "非终结符:" + ele.name;
         }
         else if (e.type_code == 4)
         {
             OtherTerminalStackElement ele = (OtherTerminalStackElement)e;
             newItem.Header = "第" + ele.linenum + "行的终结符:" + ele.content;
         }
         else if (e.type_code == 5)
         {
             RealStackElement ele = (RealStackElement)e;
             newItem.Header = "第" + ele.linenum + "行的实数:" + ele.content;
         }
         else if (e.type_code == 7)
         {
             CharStackElement ele = (CharStackElement)e;
             newItem.Header = "第" + ele.linenum + "行的字符:" + ele.content;
         }
         else if (e.type_code == 8)
         {
             StringStackElement ele = (StringStackElement)e;
             newItem.Header = "第" + ele.linenum + "行的字符串:" + ele.content;
         }
         else
         {
             throw new ParserException("这是啥栈元素?");
         }
         //若是终结符,branches就是空,若不是非终结符,它也终究会走到末端叶子节点,也就是非终结符,然后递归终止
         if (e.branches.Count != 0)
         {
             foreach (StackElement ele in e.branches)
             {
                 TreeViewItem new_node = new TreeViewItem();
                 newItem.Items.Add(new_node);
                 recursiveAddNodes(ele, new_node);
             }
         }
     }
     catch (ParserException pe)
     {
         MessageBox.Show(pe.err_msg);
     }
     catch (Exception ee)
     {
         MessageBox.Show(ee.ToString());
     }
 }