当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day
【Swoole系列2.1】先把Swoole跑起来
FMT开源自驾仪 | FMT中间件:一种高实时的分布式日志模块Mlog
面试突击63:MySQL 中如何去重?
第三季百度网盘AI大赛盛夏来袭,寻找热爱AI的你!
After entering Alibaba for the interview and returning with a salary of 35K, I summarized an interview question of Alibaba test engineer
Appium automated test scroll and drag_ and_ Drop slides according to element position
李書福為何要親自掛帥造手機?
STM32 key state machine 2 - state simplification and long press function addition
面试突击62:group by 有哪些注意事项?
随机推荐
Nodejs developer roadmap 2022 zero foundation Learning Guide
偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
I want to say more about this communication failure
How to use scroll bars to dynamically adjust parameters in opencv
Selected technical experts from China Mobile, ant, SF, and Xingsheng will show you the guarantee of architecture stability
Summary of Android interview questions of Dachang in 2022 (II) (including answers)
STM32 key state machine 2 - state simplification and long press function addition
Declval (example of return value of guidance function)
UDP协议:因性善而简单,难免碰到“城会玩”
VR panoramic wedding helps couples record romantic and beautiful scenes
How to output special symbols in shell
TCP packet sticking problem
二分(整数二分、实数二分)
Establishment of graphical monitoring grafana
C语言通过指针交换两个数
1700C - Helping the Nature
FMT open source self driving instrument | FMT middleware: a high real-time distributed log module Mlog
编译原理——自上而下分析与递归下降分析构造(笔记)
F200 - UAV equipped with domestic open source flight control system based on Model Design
78 year old professor Huake has been chasing dreams for 40 years, and the domestic database reaches dreams to sprint for IPO