简单编译器的实现 (二) 加入乘法

令产生式为  S->T=E;   E->T{+T};  T->F{*F}, F->i;

代码如下     很垃圾的代码  乱写的 仅仅做个示范 很多东西要改  呵呵

把a = a + 10 + b * 10 * 3 + c ;编译成"T0=a+10; T1=b*10; T2=T1*3; T3=T0+T2; T4=T3+c; a=T4; "

 

减法 除法等同处理即可

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace compiler
{
    

    
class CompilerTable
    {
        
public static string[] tables = new string[] { "int""+","-","*","/""="";""i" };

        
public static string Sym(string str)
        {           
            
foreach (string s in tables)
            {
                
if (str == s) return s;
            }

            
//if ((str[0] >= '0') && (str[0] <= '9')) return "d";

            
return "i";

        }
    }

    
class Word
    {
        
string sym;
        
string id;
        
int address;
    }

    
class Compiler
    {
        
int count = 0;
        
string[] words;
        List
<Word> list_words = new List<Word>();

        
public Compiler()
        {
            
//string code = "int a ; int b , c ;  a = a + 10 + b + c ; #";

            
string code = "a = a + 10 + b * 10 * 3 + c ; ";

            
string[] ss = code.Split(' ');

            words 
= new string[50];
            
foreach (string s in ss)
            {
                
if (s.Length> 0) words[count++= s;
            }

            count 
= 0;

            Start();

        }

        
string sym;
        
string id;
        
public void Scaner()
        {
            sym 
= CompilerTable.Sym(words[count]);

            id 
= words[count];
            
            count
++;

        }


        
public void Start()
        {
            Scaner();
            S();
        }

        
static int _t = 0;

        
public string NewTemp()
        {           
            
return "T" + (_t++).ToString();
        }


        
string _s = "";
        
public void emit(string result, string arg1, string op, string arg2)
        {
            _s 
+= result + "=" + arg1 + op + arg2+ "";
        }

        
public string Lookup(string name)
        {
            
return name;
        }

        
// S->T=E;   E->T{+T};  T->F{*F}, F->i;

        
public string S()
        {
            
string S1 = T();

            
if (sym == "=")
            {
                Scaner();

                emit(S1, E(), 
"""");
            }

            
return S1;
        }

        
public string E()
        {
            
string E1 = T();

            
while (sym == "+"
            {
                Scaner();

                
string E2 = T();

                
string temp = NewTemp();


                emit(temp, E1, 
"+", E2);


                E1 
= temp;
            }

            
return E1;
        }


        
public string T()
        {
            
string T1 = F();

            
while (sym == "*")
            {

                Scaner();

                
string T2 = F();

                
string temp = NewTemp();


                emit(temp, T1, 
"*", T2);


                T1 
= temp;
            }

            
return T1;

        }

        
public string F()
        {
            
if (sym == "i")
            {
                
string _id = id;

                Scaner();
                
return _id;
            }

            
return "error";
            
        }
        
    }
}

转载于:https://www.cnblogs.com/feathersky/archive/2008/12/13/1354401.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值