开发工具:
文件大小: 120kb
下载次数: 0
上传时间: 2013-06-03
详细说明: 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 } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.