【文件属性】:
文件名称:简易编译器
文件大小:120KB
文件格式:RAR
更新时间:2016-06-23 03:45:40
编译器
C#简易编译器
namespace 编译
{
public class Grammar
{
List tokens;
List symbles;
public string error = "";
int i = 0;
public Grammar(Morphology m)
{
tokens = m.tokens;
symbles = m.symbles;
Dispose();
}
private void Next()
{
if (i < tokens.Count - 1)
{
i++;
}
}
private void Before()
{
if (i > 0)
{
i--;
}
}
#region 主要函数
private void Dispose()
{
if (tokens[i].Code == 12)//含有program
{
Next();
if (tokens[i].Code == 18)//是标识符
{
//执行程序体
Next();
ProBody();
}
else
{
error = "该程序program缺少方法名";
}
}
else
{
error = "该程序缺少关键字:program";
}
}
#endregion
#region 程序体
private void ProBody()
{
if (tokens[i].Code == 16)
{
Next();
VarDef();
}
else if (tokens[i].Code == 2)
{
Next();
ComSent();
}
else
{
error = "程序体缺少var或begin";
}
}
#endregion
#region 变量定义
private void VarDef()
{
if (IsIdlist())
{
Next();
if (tokens[i].Code == 29)//:
{
Next();
if (tokens[i].Code == 9 || tokens[i].Code == 3 || tokens[i].Code == 13)//integer,bool,real
{
int j = i;
j = j - 2;
symbles[tokens[j].Addr].Type = tokens[i].Code;
j--;
while (tokens[j].Code == 28)
{
j--;
symbles[tokens[j].Addr].Type = tokens[i].Code;
}
Next();
if (tokens[i].Code == 30)
{
Next();
if (tokens[i].Code == 2)
{
Next();
ComSent();
}
else
{
VarDef();
}
}
else
{
error = "变量定义后面缺少;";
}
}
else
{
error = "变量定义缺少类型或类型定义错误";
return;
}
}
else
{
error = "var后面缺少冒号";
}
}
else
{
error = "变量定义标识符出错";
}
}
#endregion
#region 判断是不是标识符表
private bool IsIdlist()
{
if (tokens[i].Code == 18)
{
Next();
if (tokens[i].Code == 28)//,
{
Next();
return IsIdlist();
}
else
{
Before();
return true;
}
}
else
{
return false;
}
}
#endregion
#region 复合句
private void ComSent()
{
SentList();
if (error == "")
{
if (tokens[i].Code == 6)
{
return;
}
else
{
error = "复合句末尾缺少end";
}
}
}
#endregion
#region 语句表
private void SentList()
{
ExecSent();
if (error == "")
{
Next();
if (tokens[i].Code == 30)
{
Next();
SentList();
}
}
}
#endregion
#region 执行句
private void ExecSent()
{
if (tokens[i].Code == 18)
{
Next();
AssiSent();
}
else if (tokens[i].Code == 2 || tokens[i].Code == 8 || tokens[i].Code == 17)
{
StructSent();
}
else
{
Before();
}
}
#endregion
#region 赋值句
private void AssiSent()
{
if (tokens[i].Code == 31)//:=
{
Next();
Expression();
}
else
{
error = "赋值句变量后缺少:=";
}
}
#endregion
#region 表达式
private void Expression()
{
if (tokens[i].Code == 7 || tokens[i].Code == 15 || (tokens[i].Addr != -1 && symbles[tokens[i].Addr].Type == 3))
{
BoolExp();
}
else
{
AritExp();
}
}
#endregion
#region 布尔表达式
private void BoolExp()
{
BoolItem();
if (error == "")
{
Next();
if (tokens[i].Code == 11)
{
Next();
BoolExp();
}
else
{
Before();
}
}
else
{
return;
}
}
#endregion
#region 布尔项
private void BoolItem()
{
BoolFactor();
if (error == "")
{
Next();
if (tokens[i].Code == 1)
{
Next();
BoolItem();
}
else
{
Before();
}
}
}
#endregion
#region 布尔因子
private void BoolFactor()
{
if (tokens[i].Code == 10)
{
Next();
BoolFactor();
}
else
{
BoolValue();
}
}
#endregion
#region 布尔量
private void BoolValue()
{
if (tokens[i].Code == 15 || tokens[i].Code == 7)
{
return;
}
else if (tokens[i].Code == 18)
{
Next();
if (tokens[i].Code == 34 || tokens[i].Code == 33 || tokens[i].Code == 32 || tokens[i].Code == 37 || tokens[i].Code == 36 || tokens[i].Code == 35)
{
Next();
if (tokens[i].Code == 18)
{
}
else
{
error = "关系运算符后缺少标识符";
}
}
else
{
Before();
}
}
else if (tokens[i].Code == 21)
{
BoolExp();
//?
if (tokens[i].Code == 22)
{
return;
}
else
{
error = "布尔量中的布尔表达式缺少一个)";
}
}
else
{
error = "布尔量出错";
}
}
#endregion
#region 算数表达式
private void AritExp()
{
Item();
if (error == "")
{
Next();
if (tokens[i].Code == 23 || tokens[i].Code == 24)
{
Next();
AritExp();
}
else
{
Before();
return;
}
}
else
{
return;
}
}
#endregion
#region 项
private void Item()
{
Factor();
if (error == "")
{
Next();
if (tokens[i].Code == 25 || tokens[i].Code == 26)
{
Next();
Item();
}
else
{
Before();
return;
}
}
else
{
return;
}
}
#endregion
#region 因子
private void Factor()
{
if (tokens[i].Code == 21)
{
Next();
AritExp();
Next();
if (tokens[i].Code == 22)
{
return;
}
else
{
error = "因子中算数表达式缺少)";
}
}
else
{
CalQua();
}
}
#endregion
#region 算数量
private void CalQua()
{
if (tokens[i].Code == 18 || tokens[i].Code == 19 || tokens[i].Code == 20)
{
return;
}
else
{
error = "算数量出错";
}
}
#endregion
#region 结构句
private void StructSent()
{
if (tokens[i].Code == 2)
{
Next();
ComSent();
}
else if (tokens[i].Code == 8)
{
Next();
IfSent();
}
else if (tokens[i].Code == 17)
{
Next();
WhileSent();
}
}
#endregion
#region if语句
private void IfSent()
{
BoolExp();
if (error == "")
{
Next();
if (tokens[i].Code == 14)
{
Next();
ExecSent();
Next();
if (tokens[i].Code == 5)
{
Next();
ExecSent();
}
else
{
Before();
return;
}
}
else
{
error = "if...then语句缺少then";
}
}
else
{
error = "if语句布尔表达式出错";
}
}
#endregion
#region while语句
private void WhileSent()
{
BoolExp();
if (error == "")
{
Next();
if (tokens[i].Code == 4)
{
Next();
ExecSent();
}
else
{
error = "while语句缺少do";
}
}
}
#endregion
}
}
【文件预览】:
编译原理
----finishe()
--------编译()
----测试代码.txt(154B)