当前位置:网站首页>Compilation Principle -- C language implementation of prediction table
Compilation Principle -- C language implementation of prediction table
2022-07-06 18:06:00 【The way of growth of Peng】



#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
/* Guangxi Normal University School of computer science and engineering 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();// The length of the replacement string
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++){
// Reverse iteration
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){
// Match to line
break;
}
}
// cout<<i<<endl;
// i--;
for(j=1;j<7;++j){
if(C.compare(map[0][j])==0){
// Match to column
break;
}
}
// cout<<j<<endl;
// j--;
if(map[0][j].compare("")!=0){
match(i,j);//
return 1;
}else{
cout<<l<<" Mismatch "<<map[0][j]<<" map by :"<<map[i][j]<<endl;
return 0;
}
}
int main(){
int flag = 1;
while(flag){
cout<<endl<<" At present start state :";
for(int j=0;j<6;j++){
printf("%c",start[j]);
}cout<<endl;
cout<<" At present input state :"<<input<<endl;
if(start[strlen(start)-1] == input[0]){
// If the tail of the stack is the same as the head of the remaining string, eliminate it first ;
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<<" Elimination success !\n At present start state :"<<start<<"\n At present input state :"<<input<<endl;
}
else if(start[strlen(start)-1]=='e'){
// If the last one is empty , Automatically remove
start[strlen(start)-1]='\0';
}
else
flag = analysis(start[strlen(start)-1],input[0]);// If the tail of the stack is different from the head of the remaining string ;
if(flag==0){
cout<<" Compilation error !!"<<endl;
break;
}
if(strlen(input)==0){
cout<<"END!!"<<endl;
break;
}
}
return 0;
}
边栏推荐
- Dichotomy (integer dichotomy, real dichotomy)
- Insert dial file of Jerry's watch [chapter]
- Scratch epidemic isolation and nucleic acid detection Analog Electronics Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
- UDP协议:因性善而简单,难免碰到“城会玩”
- Codeforces Round #803 (Div. 2)
- Flet教程之 13 ListView最常用的滚动控件 基础入门(教程含源码)
- Interview shock 62: what are the precautions for group by?
- Unity tips - draw aiming Center
- 开源与安全的“冰与火之歌”
- 基本磁盘与动态磁盘 RAID磁盘冗余阵列区分
猜你喜欢

基本磁盘与动态磁盘 RAID磁盘冗余阵列区分

编译原理——预测表C语言实现

Scratch epidemic isolation and nucleic acid detection Analog Electronics Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022

Video fusion cloud platform easycvr adds multi-level grouping, which can flexibly manage access devices

面试突击63:MySQL 中如何去重?

How to solve the error "press any to exit" when deploying multiple easycvr on one server?

重磅!蚂蚁开源可信隐私计算框架“隐语”,主流技术灵活组装、开发者友好分层设计...
![[Android] kotlin code writing standardization document](/img/d5/53d6a75e87af15799bf7e5d6eb92a5.png)
[Android] kotlin code writing standardization document

Alibaba brand data bank: introduction to the most complete data bank

李書福為何要親自掛帥造手機?
随机推荐
Redis的五种数据结构
Introduction to the usage of model view delegate principal-agent mechanism in QT
MySQL 8 sub database and table backup database shell script
Transfer data to event object in wechat applet
容器里用systemctl运行服务报错:Failed to get D-Bus connection: Operation not permitted(解决方法)
After entering Alibaba for the interview and returning with a salary of 35K, I summarized an interview question of Alibaba test engineer
Jerry's updated equipment resource document [chapter]
Smart street lamp based on stm32+ Huawei cloud IOT design
MS-TCT:Inria&SBU提出用于动作检测的多尺度时间Transformer,效果SOTA!已开源!(CVPR2022)...
Interview shock 62: what are the precautions for group by?
从交互模型中蒸馏知识!中科大&美团提出VIRT,兼具双塔模型的效率和交互模型的性能,在文本匹配上实现性能和效率的平衡!...
Jerry's watch deletes the existing dial file [chapter]
Nodejs 开发者路线图 2022 零基础学习指南
Pytest learning ----- pytest confitest of interface automation test Py file details
關於這次通信故障,我想多說幾句…
Summary of Android interview questions of Dachang in 2022 (I) (including answers)
Optimization of middle alignment of loading style of device player in easycvr electronic map
[introduction to MySQL] the first sentence · first time in the "database" Mainland
Interesting - questions about undefined
Compilation principle - top-down analysis and recursive descent analysis construction (notes)