实现计算功能,可以由代码计算出变量的值 呵呵 赶紧睡觉去
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;
}
return "i";
}
public static bool IsDigit(string str)
{
if (str == "") return false;
if ((str[0] >= '0') && (str[0] <= '9')) return true;
return false;
}
}
class Word
{
string sym;
string id;
int address;
}
class OP
{
public string result;
public string arg1;
public string op;
public string arg2;
}
class CompilerExcute
{
SortedList<string, int> variables = new SortedList<string, int>();
public void Excute(List<OP> op_list)
{
variables.Clear();
foreach (OP op in op_list)
{
int arg1=0,arg2=0;
if (variables.IndexOfKey(op.result) < 0) variables.Add(op.result, 0);
if (CompilerTable.IsDigit(op.arg1))
arg1 = int.Parse(op.arg1);
else
arg1 = variables[op.arg1];
if (CompilerTable.IsDigit(op.arg2))
arg2 = int.Parse(op.arg2);
else if(op.arg2!="")
arg2 = variables[op.arg2];
if (op.op == "") variables[op.result] = arg1 ;
if (op.op == "+") variables[op.result] = arg1 + arg2;
if (op.op == "*") variables[op.result] = arg1 * arg2;
}
}
public int GetValue(string name)
{
return variables[name];
}
}
class Compiler
{
int count = 0;
string[] words;
List<OP> op_list = new List<OP>();
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();
CompilerExcute excute = new CompilerExcute();
excute.Excute(op_list);
int a = excute.GetValue("a");
int b = excute.GetValue("b");
}
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+ "; ";
OP op = new OP();
op.result = result;
op.arg1 = arg1;
op.op = _op;
op.arg2 = arg2;
op_list.Add(op);
}
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;
}
return "i";
}
public static bool IsDigit(string str)
{
if (str == "") return false;
if ((str[0] >= '0') && (str[0] <= '9')) return true;
return false;
}
}
class Word
{
string sym;
string id;
int address;
}
class OP
{
public string result;
public string arg1;
public string op;
public string arg2;
}
class CompilerExcute
{
SortedList<string, int> variables = new SortedList<string, int>();
public void Excute(List<OP> op_list)
{
variables.Clear();
foreach (OP op in op_list)
{
int arg1=0,arg2=0;
if (variables.IndexOfKey(op.result) < 0) variables.Add(op.result, 0);
if (CompilerTable.IsDigit(op.arg1))
arg1 = int.Parse(op.arg1);
else
arg1 = variables[op.arg1];
if (CompilerTable.IsDigit(op.arg2))
arg2 = int.Parse(op.arg2);
else if(op.arg2!="")
arg2 = variables[op.arg2];
if (op.op == "") variables[op.result] = arg1 ;
if (op.op == "+") variables[op.result] = arg1 + arg2;
if (op.op == "*") variables[op.result] = arg1 * arg2;
}
}
public int GetValue(string name)
{
return variables[name];
}
}
class Compiler
{
int count = 0;
string[] words;
List<OP> op_list = new List<OP>();
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();
CompilerExcute excute = new CompilerExcute();
excute.Excute(op_list);
int a = excute.GetValue("a");
int b = excute.GetValue("b");
}
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+ "; ";
OP op = new OP();
op.result = result;
op.arg1 = arg1;
op.op = _op;
op.arg2 = arg2;
op_list.Add(op);
}
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";
}
}
}