当前位置:网站首页>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;
}
边栏推荐
- What is the reason why the video cannot be played normally after the easycvr access device turns on the audio?
- 78 year old professor Huake has been chasing dreams for 40 years, and the domestic database reaches dreams to sprint for IPO
- Markdown grammar - better blogging
- 最新财报发布+天猫618双榜第一,耐克蓄力领跑下个50年
- MSF横向之MSF端口转发+路由表+SOCKS5+proxychains
- Codeforces Round #803 (Div. 2)
- std::true_ Type and std:: false_ type
- MS-TCT:Inria&SBU提出用于动作检测的多尺度时间Transformer,效果SOTA!已开源!(CVPR2022)...
- Summary of Android interview questions of Dachang in 2022 (II) (including answers)
- After entering Alibaba for the interview and returning with a salary of 35K, I summarized an interview question of Alibaba test engineer
猜你喜欢

分布式不来点网关都说不过去

模板于泛型编程之declval

F200——搭载基于模型设计的国产开源飞控系统无人机

C语言通过指针交换两个数

Olivetin can safely run shell commands on Web pages (Part 1)

Optimization of middle alignment of loading style of device player in easycvr electronic map

Getting started with pytest ----- test case rules

酷雷曼多种AI数字人形象,打造科技感VR虚拟展厅

10 advanced concepts that must be understood in learning SQL

面试突击63:MySQL 中如何去重?
随机推荐
传统家装有落差,VR全景家装让你体验新房落成效果
SAP UI5 框架的 manifest.json
Getting started with pytest ----- test case pre post, firmware
Alibaba brand data bank: introduction to the most complete data bank
78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
历史上的今天:Google 之母出生;同一天诞生的两位图灵奖先驱
二分(整数二分、实数二分)
Smart street lamp based on stm32+ Huawei cloud IOT design
78 year old professor Huake has been chasing dreams for 40 years, and the domestic database reaches dreams to sprint for IPO
[introduction to MySQL] the first sentence · first time in the "database" Mainland
Interview assault 63: how to remove duplication in MySQL?
Video fusion cloud platform easycvr adds multi-level grouping, which can flexibly manage access devices
面试突击62:group by 有哪些注意事项?
《ASP.NET Core 6框架揭秘》样章发布[200页/5章]
面向程序员的精品开源字体
Easy introduction to SQL (1): addition, deletion, modification and simple query
Interview shock 62: what are the precautions for group by?
基于STM32+华为云IOT设计的智能路灯
[Android] kotlin code writing standardization document
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?