当前位置:网站首页>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;
}
边栏推荐
- 李书福为何要亲自挂帅造手机?
- HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
- Flet教程之 13 ListView最常用的滚动控件 基础入门(教程含源码)
- FlutterWeb瀏覽器刷新後無法回退的解决方案
- After entering Alibaba for the interview and returning with a salary of 35K, I summarized an interview question of Alibaba test engineer
- Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing
- Insert dial file of Jerry's watch [chapter]
- Principle and usage of extern
- Olivetin can safely run shell commands on Web pages (Part 1)
- The difference between parallelism and concurrency
猜你喜欢

10 advanced concepts that must be understood in learning SQL

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

What is the reason why the video cannot be played normally after the easycvr access device turns on the audio?

UDP协议:因性善而简单,难免碰到“城会玩”

F200 - UAV equipped with domestic open source flight control system based on Model Design

How to use scroll bars to dynamically adjust parameters in opencv

Is it meaningful for 8-bit MCU to run RTOS?

重磅!蚂蚁开源可信隐私计算框架“隐语”,主流技术灵活组装、开发者友好分层设计...

【Swoole系列2.1】先把Swoole跑起来

李书福为何要亲自挂帅造手机?
随机推荐
Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design
VR全景婚礼,帮助新人记录浪漫且美好的场景
Selected technical experts from China Mobile, ant, SF, and Xingsheng will show you the guarantee of architecture stability
带你穿越古罗马,元宇宙巴士来啦 #Invisible Cities
OpenEuler 会长久吗
JMeter interface test response data garbled
This article discusses the memory layout of objects in the JVM, as well as the principle and application of memory alignment and compression pointer
Easy introduction to SQL (1): addition, deletion, modification and simple query
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
Mysqlimport imports data files into the database
Jielizhi obtains the customized background information corresponding to the specified dial [chapter]
Pourquoi Li shufu a - t - il construit son téléphone portable?
Nodejs developer roadmap 2022 zero foundation Learning Guide
Pytest learning ----- pytest confitest of interface automation test Py file details
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
kivy教程之在 Kivy 中支持中文以构建跨平台应用程序(教程含源码)
Interview shock 62: what are the precautions for group by?
队列的实现
Take you through ancient Rome, the meta universe bus is coming # Invisible Cities
The shell generates JSON arrays and inserts them into the database