实现编译
int a,b=5,c=4;
int d,e;
a=5;
a=a+b*c+100;
d=a+b;
产生式如下
Statement->st{st} //声明变量
st->int st1{,st1} ;
st1->i{=i};
Body->S{S} //主体赋值表达式
S->i=E;
E->T{+T}
T->i{*T}
Code->{ Statement} Body //
将代码" int a , b = 5 , c = 4 ; int d , e ; a = 5 ; a = a + b * c + 100 ; d = a + b ; ; ";
编译为"variable:a ; variable:b ; b=5; variable:c ; c=4; variable:d ; variable:e ; a=5; T0=b*c; T1=a+T0; T2=T1+100; a=T2; T3=a+b; d=T3; "
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<string> variables = new List<string>();
List<int> values = new List<int>();
public Compiler()
{
//string code = "int a ; int b , c ; a = a + 10 + b + c ; #";
// string code = "a = a + 10 + b * 10 * 3 + c ; ";
string code=" int a , b = 5 , c = 4 ; int d , e ; a = 5 ; a = a + b * c + 100 ; d = a + b ; ; ";
string[] ss = code.Split(' ');
words = new string[500];
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();
Code();
}
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;
}
// // 产生式如下
//Statement->st{st} //声明变量
//st->int st1{,st1} ;
//st1->i{=i};
//Body->S{S} //主体赋值表达式
//S->i=E;
//E->T{+T}
//T->i{*T}
//Code->{ Statement} Body //
public void Code()
{
if(sym=="int") Statement();
Body();
}
public void Statement()
{
while (sym == "int") St();
}
public void St()
{
Scaner();
St1();
while (sym == ",")
{
Scaner();
St1();
}
Scaner(); //should be ;
}
public void St1()
{
string _id = id;
AddVariable(id);
Scaner();
if (sym == "=")
{
Scaner();
//得到初始值
emit(_id, id, "", "");
Scaner();
}
}
public void AddVariable(string _id)
{
_s += "variable:" + _id + " ; ";
}
public void Body()
{
S();
while (sym == "i") S();
}
public string S()
{
string S1 = T();
if (sym == "=")
{
Scaner();
emit(S1, E(), "", "");
}
Scaner(); //should be ;
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";
}
}
}
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<string> variables = new List<string>();
List<int> values = new List<int>();
public Compiler()
{
//string code = "int a ; int b , c ; a = a + 10 + b + c ; #";
// string code = "a = a + 10 + b * 10 * 3 + c ; ";
string code=" int a , b = 5 , c = 4 ; int d , e ; a = 5 ; a = a + b * c + 100 ; d = a + b ; ; ";
string[] ss = code.Split(' ');
words = new string[500];
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();
Code();
}
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;
}
// // 产生式如下
//Statement->st{st} //声明变量
//st->int st1{,st1} ;
//st1->i{=i};
//Body->S{S} //主体赋值表达式
//S->i=E;
//E->T{+T}
//T->i{*T}
//Code->{ Statement} Body //
public void Code()
{
if(sym=="int") Statement();
Body();
}
public void Statement()
{
while (sym == "int") St();
}
public void St()
{
Scaner();
St1();
while (sym == ",")
{
Scaner();
St1();
}
Scaner(); //should be ;
}
public void St1()
{
string _id = id;
AddVariable(id);
Scaner();
if (sym == "=")
{
Scaner();
//得到初始值
emit(_id, id, "", "");
Scaner();
}
}
public void AddVariable(string _id)
{
_s += "variable:" + _id + " ; ";
}
public void Body()
{
S();
while (sym == "i") S();
}
public string S()
{
string S1 = T();
if (sym == "=")
{
Scaner();
emit(S1, E(), "", "");
}
Scaner(); //should be ;
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";
}
}
}