⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1.cpp

📁 词法分析器 大家共享哈 呵呵 /************************************/ /* 程序名称: 词法分析器*/ /* 程序用途: 编译原理实验(一) */ /*
💻 CPP
字号:
/*文法表达式为
E->TE'
E'->+TE'|^
T->FT'
T'->+TF'|^
F->(E)|i
*/
#include <iostream.h>
char token[50];
int current_pos=0;
int sign;
void e();
void e1();
void t();
void t1();
void f();

void main()
{
 sign=1;
 cout<<"文法表达式为"<<endl;
 cout<<"E->TE'"<<endl;
 cout<<"E'->+TE'|^"<<endl;
 cout<<"T->FT'"<<endl;
 cout<<"T'->+TF'|^"<<endl;
 cout<<"F->(E)|i"<<endl;
 cout<<"请输入您要分析的字符串以#结束"<<endl;
 cin>>token;
 e();
 if((token[current_pos]=='#')&&sign)
  cout<<"分析成功"<<endl;
 else
  cout<<"分析失败"<<endl;
}

void e()
{
 cout<<"E=>TE'"<<endl;
 t();
 e1();
}

void e1()
{
 if(token[current_pos]=='+')
 {
  cout<<"E'=>+TE'"<<endl;
  current_pos++;
  t();
  e1();
 }
 else
  cout<<"T'=>^"<<endl;
}

void t()
{
 cout<<"T=>FT'"<<endl;
 f();
 t1();
}
void f()
{
 if(token[current_pos]=='i')
 {
  cout<<"F=>i"<<endl;
  current_pos++;
 }
 else
 {
  if(token[current_pos]=='(')
  {
   cout<<"F=>(E)"<<endl;
   current_pos++;
   e();
  }
  else if(token[current_pos]==')')
  {
   cout<<"F=>(E)"<<endl;
   current_pos++;
  }
  else
   sign=0;
 }
}

void t1()
{
 if(token[current_pos]=='*')
 {
  cout<<"T'=>*FT'"<<endl;
  current_pos++;
  f();
  t1();
 }
 else
  cout<<"T'=>^"<<endl;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -