当前位置:网站首页>编译原理——预测表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;
}
边栏推荐
- FlutterWeb浏览器刷新后无法回退的解决方案
- How to output special symbols in shell
- Unity小技巧 - 绘制瞄准准心
- Automatic operation and maintenance sharp weapon ansible Playbook
- 分布式(一致性协议)之领导人选举( DotNext.Net.Cluster 实现Raft 选举 )
- EasyCVR接入设备开启音频后,视频无法正常播放是什么原因?
- Chrome prompts the solution of "your company management" (the startup page is bound to the company's official website and cannot be modified)
- [rapid environment construction] openharmony 10 minute tutorial (cub pie)
- 中移动、蚂蚁、顺丰、兴盛优选技术专家,带你了解架构稳定性保障
- Zen integration nails, bugs, needs, etc. are reminded by nails
猜你喜欢
![[translation] principle analysis of X Window Manager (I)](/img/40/6e15e1acebb47061d6e0e4c8ff82ea.jpg)
[translation] principle analysis of X Window Manager (I)

视频融合云平台EasyCVR增加多级分组,可灵活管理接入设备

网络分层概念及基本知识

JMeter interface test response data garbled

yarn : 无法加载文件 D:\ProgramFiles\nodejs\yarn.ps1,因为在此系统上禁止运行脚本

Unity粒子特效系列-闪星星的宝箱

Flink analysis (II): analysis of backpressure mechanism

BearPi-HM_ Nano development board "flower protector" case

Solr appears write Lock, solrexception: could not get leader props in the log

Cool Lehman has a variety of AI digital human images to create a vr virtual exhibition hall with a sense of technology
随机推荐
What is the reason why the video cannot be played normally after the easycvr access device turns on the audio?
How to use scroll bars to dynamically adjust parameters in opencv
中移动、蚂蚁、顺丰、兴盛优选技术专家,带你了解架构稳定性保障
Xin'an Second Edition: Chapter 26 big data security demand analysis and security protection engineering learning notes
C version selenium operation chrome full screen mode display (F11)
Xin'an Second Edition; Chapter 11 learning notes on the principle and application of network physical isolation technology
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
Optimization of middle alignment of loading style of device player in easycvr electronic map
Alertmanager sends the alarm email and specifies it as the Alibaba mailbox of the company
OpenCV中如何使用滚动条动态调整参数
RepPoints:可形变卷积的进阶
pip install pyodbc : ERROR: Command errored out with exit status 1
Unity particle special effects series - treasure chest of shining stars
How uipath determines that an object is null
Yarn: unable to load file d:\programfiles\nodejs\yarn PS1, because running scripts is prohibited on this system
开源与安全的“冰与火之歌”
C# NanoFramework 点灯和按键 之 ESP32
微信小程序获取手机号
Solrcloud related commands
Interpretation of Flink source code (II): Interpretation of jobgraph source code