当前位置:网站首页>编译原理——预测表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;
}
边栏推荐
- 网络分层概念及基本知识
- [ASM] introduction and use of bytecode operation classwriter class
- Nodejs 开发者路线图 2022 零基础学习指南
- JMeter interface test response data garbled
- How to submit data through post
- 面试突击62:group by 有哪些注意事项?
- Binary search strategy
- Solrcloud related commands
- 李书福为何要亲自挂帅造手机?
- Wordcloud colormap color set and custom colors
猜你喜欢
10 advanced concepts that must be understood in learning SQL
The easycvr authorization expiration page cannot be logged in. How to solve it?
Kali2021 installation and basic configuration
There is a gap in traditional home decoration. VR panoramic home decoration allows you to experience the completion effect of your new house
The most complete tcpdump and Wireshark packet capturing practice in the whole network
Interview assault 63: how to remove duplication in MySQL?
Selected technical experts from China Mobile, ant, SF, and Xingsheng will show you the guarantee of architecture stability
面试突击62:group by 有哪些注意事项?
Unity tips - draw aiming Center
基于STM32+华为云IOT设计的智能路灯
随机推荐
TCP connection is more than communicating with TCP protocol
node の SQLite
connection reset by peer
Example of batch update statement combining update and inner join in SQL Server
Unity tips - draw aiming Center
Alibaba brand data bank: introduction to the most complete data bank
Xin'an Second Edition; Chapter 11 learning notes on the principle and application of network physical isolation technology
scratch疫情隔离和核酸检测模拟 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
Summary of study notes for 2022 soft exam information security engineer preparation
面试突击62:group by 有哪些注意事项?
Automatic operation and maintenance sharp weapon ansible Playbook
Easy introduction to SQL (1): addition, deletion, modification and simple query
EasyCVR接入设备开启音频后,视频无法正常播放是什么原因?
Establishment of graphical monitoring grafana
Pytest learning ----- detailed explanation of the request for interface automation test
Binary search strategy
Smart street lamp based on stm32+ Huawei cloud IOT design
传统家装有落差,VR全景家装让你体验新房落成效果
Virtual machine startup prompt probing EDD (edd=off to disable) error
一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升