• 大小: 6KB
    文件类型: .cpp
    金币: 1
    下载: 0 次
    发布日期: 2021-10-12
  • 语言: C/C++
  • 标签:

资源简介

三、设计要求 1、使用模块化设计思想来设计该编译器; 2、词法分析模块用于读入输入串,并将其转换成供语法分析模块使用的记号流。其中包括滤掉空格和注释、识别常数、识别标识符和关键字等功能; 3、要求在语法分析模块中利用语法制导翻译技术完成具体的中缀表达式到后缀表达式的翻译,其中包括按前述翻译器的规格说明构建对应表达式、项、因子的非终结符expr、term和factor的函数以及检查记号是否匹配的函数;并在不匹配时调用错误处理模块; 4、要求符号表管理模块主要完成符号表对应数据结构的具体实现功能; 5、错误处理模块负责报告错误信息及位置,并终止分析过程; 6、输出模块完成翻译后所得到的后缀表达式的输出。 四、运行结果 1、从键盘输入任意中缀表达式,如: 4 - 5 * 6 DIV 4 + 8 MOD 2 输出相应的后缀表达式: 456*4DIV-82MOD+ 1、 若键盘输入串为非中缀表达式时,如: 4 !+* 5 - 6 DIV 4 + 8 MOD 2 输出相应语法错误报告信息,并停止语法分析,如: line 1 : compiler error !

资源截图

代码片段和文件信息

#include 
#include 
#include 
#include 
#include
using namespace std;
bool isOper(char c)
//判断是否为操作符
{
 if ((c==‘+‘)||(c== ‘-‘)||(c== ‘*‘)||(c== ‘/‘)||(c== ‘(‘)||(c== ‘)‘)||(c==1)||(c==2))
  return true;
 return false;
}

bool isRealOper(char c)
//判断是否为操作符
{
 if ((c==‘+‘)||(c== ‘-‘)||(c== ‘*‘)||(c== ‘/‘)||(c==1)||(c==2))
  return true;
 return false;
}

bool isHigh(char top_opchar InfixExp_op)
//判断操作符的优先级
//top_op为栈顶操作符
//InfixExp_op为当前读入操作符
//如果栈顶操作符优先级高,则弹出栈顶操作符
//如果栈顶操作符优先级低,则压入当前读入操作符
{
 if ((top_op== ‘+‘)&&(InfixExp_op== ‘+‘)) return true;
 if ((top_op== ‘+‘)&&(InfixExp_op== ‘-‘)) return true;
 if ((top_op== ‘-‘)&&(InfixExp_op== ‘+‘)) return true;
 if ((top_op== ‘-‘)&&(InfixExp_op== ‘-‘)) return true;
 if ((top_op== ‘*‘)&&(InfixExp_op== ‘+‘)) return true;
 if ((top_op== ‘*‘)&&(InfixExp_op== ‘-‘)) return true;
 if ((top_op== ‘*‘)&&(InfixExp_op== ‘*‘)) return true;
 if ((top_op== ‘*‘)&&(InfixExp_op== ‘/‘)) return true;
 if ((top_op== ‘*‘)&&(InfixExp_op== 1)) return true;
 if ((top_op== ‘*‘)&&(InfixExp_op== 2)) return true;
 if ((top_op== ‘/‘)&&(InfixExp_op== ‘+‘)) return true;
 if ((top_op== ‘/‘)&&(InfixExp_op== ‘-‘)) return true;
 if ((top_op== ‘/‘)&&(InfixExp_op== ‘*‘)) return true;
 if ((top_op== ‘/‘)&&(InfixExp_op== ‘/‘)) return true;
 if ((top_op== ‘/‘)&&(InfixExp_op== 1)) return true;
 if ((top_op== ‘/‘)&&(InfixExp_op== 2)) return true;
 if ((top_op== 1)&&(InfixExp_op== ‘+‘)) return true;
 if ((top_op== 1)&&(InfixExp_op== ‘-‘)) return true;
 if ((top_op== 1)&&(InfixExp_op== ‘*‘)) return true;
 if ((top_op== 1)&&(InfixExp_op== ‘/‘)) return true;
 if ((top_op== 1)&&(InfixExp_op== 1)) return true;
 if ((top_op== 1)&&(InfixExp_op== 2)) return true;
 if ((top_op== 2)&&(InfixExp_op== ‘+‘)) return true;
 if ((top_op== 2)&&(InfixExp_op== ‘-‘)) return true;
 if ((top_op== 2)&&(InfixExp_op== ‘*‘)) return true;
 if ((top_op== 2)&&(InfixExp_op== ‘/‘)) return true;
 if ((top_op== 2)&&(InfixExp_op== 1)) return true;
 if ((top_op== 2)&&(InfixExp_op== 2)) return true;
 if (InfixExp_op== ‘)‘) return true;
 return false;
}
void input(vector  *InfixExp)
{


 int i = 0j;
 
 char c[100];
 char d;
 cin>>d;
 while(d!= ‘#‘)
 {
  c[i] = d;
  cin>>d;
  i++;
 }
  c[i]=‘\0‘;  
   
   i = 0;
   
   while(c[i]!= ‘\0‘)
   {       
          if(c[i]==‘ ‘)
            {  c[i]=c[i+1];

            }

          i++;
   }
   i =0;
   while(c[i]!= ‘\0‘)
   {       
          if(c[i]==‘D‘ && c[i+1]==‘I‘ && c[i+2]==‘V‘)
            {  c[i]=1;
               j=i;
               while(c[j]!= ‘\0‘){c[j+1]=c[j+3];j++;}
            }

          i++;
   }
   i = 0;
   while(c[i]!= ‘\0‘)
   {       
          if(c[i]==‘M‘ && c[i+1]==‘O‘ && c[i+2]==‘D‘)
            {  c[i]=2;
               j=i;
               while(c[j]!= ‘\0‘){c[j+1]=c[j+3];j++;}
            }

          i++;
   }
 
 i=0;
 
 while(c[i]!= ‘\0‘)
 {
  

评论

共有 条评论

相关资源