当前位置:网站首页>编译原理——预测表C语言实现
编译原理——预测表C语言实现
2022-07-06 09:43:00 【芣苢的成长之路】
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
/* 广西师范大学 计算机科学与工程学院 GuangXi Normal University College of Computer Science and Engineering Student STZ */
string map[6][7]={
{
"", "i", "+", "*", "(",")","#"},
{
"E", "TD", "", "", "TD", "", ""},
{
"D", "","+TD", "", "","e","e"},
{
"T", "FS", "", "", "FS", "", ""},
{
"S", "", "e","*FS", "","e","e"},
{
"F", "i", "", "","(E)", "", ""},
};
char input[] = "i+i*i#";
char start[] = "#E";
void match(int l,int c){
// int len = map[l][c].length();//替换串的长度
int i = 0;
int isfirst = 0;
cout<<start[strlen(start)-1]<<"->"<<map[l][c]<<endl;
for(string::reverse_iterator iter=map[l][c].rbegin();iter!=map[l][c].rend();iter++){
//反向迭代
if(isfirst==0){
start[strlen(start)-1] = *iter;
isfirst=1;
}else{
start[strlen(start)] = *iter;
}
for(int j=0;j<6;j++){
printf("%c",start[j]);
}cout<<endl;
}
int analysis(char l,char c){
int flag = 1;
int i,j;
string L = "";
L += l;
string C = "";
C +=c;
//cout<<l<<endl;
for(i=1;i<6;++i){
if(L.compare(map[i][0])==0){
//匹配到行
break;
}
}
// cout<<i<<endl;
// i--;
for(j=1;j<7;++j){
if(C.compare(map[0][j])==0){
//匹配到列
break;
}
}
// cout<<j<<endl;
// j--;
if(map[0][j].compare("")!=0){
match(i,j);//
return 1;
}else{
cout<<l<<"不匹配"<<map[0][j]<<" map为:"<<map[i][j]<<endl;
return 0;
}
}
int main(){
int flag = 1;
while(flag){
cout<<endl<<"当前start状态:";
for(int j=0;j<6;j++){
printf("%c",start[j]);
}cout<<endl;
cout<<"当前input状态:"<<input<<endl;
if(start[strlen(start)-1] == input[0]){
//如果栈尾和剩余字符串队首一样先消除;
start[strlen(start)-1]='\0';
for(int i=0;i<strlen(input)-1;i++){
input[i] = input[i+1];
}
input[strlen(input)-1]='\0';
cout<<endl<<"消除成功!\n当前start状态:"<<start<<"\n当前input状态:"<<input<<endl;
}
else if(start[strlen(start)-1]=='e'){
//如果最后一位是空,自动去掉
start[strlen(start)-1]='\0';
}
else
flag = analysis(start[strlen(start)-1],input[0]);//如果栈尾和剩余字符串队首不一样;
if(flag==0){
cout<<"编译出错!!"<<endl;
break;
}
if(strlen(input)==0){
cout<<"END!!"<<endl;
break;
}
}
return 0;
}
边栏推荐
- Summary of study notes for 2022 soft exam information security engineer preparation
- EasyCVR授权到期页面无法登录,该如何解决?
- Wordcloud colormap color set and custom colors
- Unity particle special effects series - treasure chest of shining stars
- Concept and basic knowledge of network layering
- 中移动、蚂蚁、顺丰、兴盛优选技术专家,带你了解架构稳定性保障
- Smart street lamp based on stm32+ Huawei cloud IOT design
- Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing
- Grafana 9 is officially released, which is easier to use and more cool!
- EasyCVR接入设备开启音频后,视频无法正常播放是什么原因?
猜你喜欢
Unity粒子特效系列-闪星星的宝箱
BearPi-HM_ Nano development board "flower protector" case
Chrome prompts the solution of "your company management" (the startup page is bound to the company's official website and cannot be modified)
Pytorch extract middle layer features?
[introduction to MySQL] third, common data types in MySQL
[ASM] introduction and use of bytecode operation classwriter class
Flink parsing (VII): time window
Awk command exercise
Flink analysis (I): basic concept analysis
Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing
随机推荐
VR panoramic wedding helps couples record romantic and beautiful scenes
BearPi-HM_ Nano development board "flower protector" case
How to submit data through post
虚拟机启动提示Probing EDD (edd=off to disable)错误
视频融合云平台EasyCVR增加多级分组,可灵活管理接入设备
pip install pyodbc : ERROR: Command errored out with exit status 1
node の SQLite
Start job: operation returned an invalid status code 'badrequst' or 'forbidden‘
Remote code execution penetration test - B module test
Single responsibility principle
kivy教程之在 Kivy 中支持中文以构建跨平台应用程序(教程含源码)
Hongmeng introduction and development environment construction
面试突击62:group by 有哪些注意事项?
How to use scroll bars to dynamically adjust parameters in opencv
Unity粒子特效系列-闪星星的宝箱
传统家装有落差,VR全景家装让你体验新房落成效果
JMeter interface test response data garbled
2022年大厂Android面试题汇总(二)(含答案)
[ASM] introduction and use of bytecode operation classwriter class
【MySQL入门】第三话 · MySQL中常见的数据类型