令产生式为 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; "
减法 除法等同处理即可
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";
}
}
}
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";
}
}
}