日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

java pl0 四元式,【编译原理】c++实现自下而上语法分析及中间代码(四元式)生成...

發布時間:2025/3/12 c/c++ 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java pl0 四元式,【编译原理】c++实现自下而上语法分析及中间代码(四元式)生成... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

寫在前面:本博客為本人原創,嚴禁任何形式的轉載!本博客只允許放在博客園(.cnblogs.com),如果您在其他網站看到這篇博文,請通過下面這個唯一的合法鏈接轉到原文!

本博客全網唯一合法URL:http://www.cnblogs.com/acm-icpcer/p/9173880.html

基于C++語言實現的PL/0語言的算術表達式的自下而上的語法分析程序。該語言的其他語法實現思想與此一致,故不贅述。

運行此程序前,必須先將代碼通過:【編譯原理】c++實現詞法分析器的詞法分析,生成詞法表(詞法表是txt文件,為了語法分析成功,務必刪除文件中最后空著的一行,即文件末尾不可以留空白行)。生成的該詞法表為此程序的必要輸入。

產生式:

S->X(AX)*|AX(AX)*

X->Y(MY)*

Y->I|N|(S)

A->+|-

M->*|/

C->=|#||>=

本次的代碼主要是在【編譯原理】c++實現自下而上語法分析器的基礎上,伴隨著歸約的過程,增加了生成四元式的過程,也就是一邊歸約一邊生成中間代碼。

Talk is cheap, show you my source code:

/*

this code was first initiated by TZ,COI,HZAU

contact email:xmb028@163.com

personal website:wnm1503303791.github.io

personal blogs:www.cnblogs.com/acm-icpcer/

this code has been posted on my personal blog,checking url:www.cnblogs.com/acm-icpcer/p/9173880.html

Copyright 2018/6/12 TZ.

All Rights Reserved.

*/

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

using namespace std;

//預處理函數

bool preproccess(char *a,char *b,char *c)

{

int i1=,i2=;//i2為原串指針

memset(b,,'\0');

while(a[i2]!=',')

{

b[i1]=a[i2];

++i1,++i2;

}

b[i1]='\0';

i1=;i2++;

while(a[i2]!=')')

{

c[i1]=a[i2];

++i1,++i2;

}

c[i1]='\0';

//cout<

return true;

}

fstream f2("stack.txt", ios::out);//打開文件,供寫

fstream f3("quaternary.txt", ios::out);//打開文件,供寫

static int mcount=;//存儲打印次數

//當移進或者歸約時打印棧內情況,以供分析

bool outf(int head,char data[][],fstream &f)

{

f<

f<

for(int i=head;i>=;i--)

{

f<

}

mcount++;

f<

return true;

}

//四元式寫入文件函數1

bool outf2(int top,char dt[][],fstream &f)

{

/*

char arg1[1024],arg2[1024],op[1024];

memset(arg1,sizeof(arg1),'\0');

strcpy(arg1,dt[top]);

memset(op,sizeof(op),'\0');

strcpy(op,dt[top-1]);

memset(arg2,sizeof(arg2),'\0');

strcpy(arg2,dt[top-2]);

f<

*/

f<

return true;

}

//四元式寫入文件函數2

bool outf3(int top,char dt[][],fstream &f)

{

f<

return true;

}

//四元式寫入文件函數3

bool outf4(int top,char dt[][],fstream &f,char T)

{

f<

return true;

}

//“策略”設計模式,面向對象方法

class presentation

{

private:

char data[][];//棧

char dt[][];//四元式棧

fstream *infile;//詞法分析表

int head,top;//兩個棧的棧頂指針

public:

//first initiated the object

presentation(fstream *in_f)

{

this->infile=in_f;

memset(data,sizeof(data),'\0');

memset(dt,sizeof(dt),'\0');

head=top=-;

}

bool push()

{

head++;

top++;

infile->getline(data[head],);

char t1[],t2[];//存放字符標志

preproccess(data[head],t1,t2);

cout<

memset(data[head],,'\0');

strcpy(data[head],t1);

memset(dt[top],,'\0');

strcpy(dt[top],t2);

cout<

}

/*

S->X(AX)*|AX(AX)*

X->Y(MY)*

Y->I|N|(S)

A->+|-

M->*|/

C->=|#||>=

*/

//歸約函數

bool reduce()

{

//S->X(AX)*|AX(AX)*

if( head>=&&

(!strcmp(data[head],"X"))&&

(!strcmp(data[head-],"plus")||!strcmp(data[head-],"minus"))&&

(!strcmp(data[head-],"X"))&&

(!strcmp(data[head-],"plus")||!strcmp(data[head-],"minus"))&&

(!strcmp(data[head-],"X"))

)

{

memset(data[head],,'\0');

memset(data[head-],,'\0');

memset(data[head-],,'\0');

memset(data[head-],,'\0');

memset(data[head-],,'\0');

head=head-+;

strcpy(data[head],"S");//歸約

/*

stack description:

top-> arg

op

top-2-> arg

op

arg

*/

if(outf2(top,dt,f3)&&outf2(top-,dt,f3))

{

top==-;

memset(dt,sizeof(dt),'\0');

}

return true;

}

if( head>=&&

(!strcmp(data[head],"X"))&&

(!strcmp(data[head-],"plus")||!strcmp(data[head-],"minus"))&&

(!strcmp(data[head-],"X"))

)

{

memset(data[head],,'\0');

memset(data[head-],,'\0');

memset(data[head-],,'\0');

head=head-+;

strcpy(data[head],"S");

if(outf2(top,dt,f3))

{

top==-;

memset(dt,sizeof(dt),'\0');

}

return true;

}

if( head>=&&/*top>=3*/

(!strcmp(data[head],"plus")||!strcmp(data[head],"minus"))&&

(!strcmp(data[head-],"X"))&&

(!strcmp(data[head-],"plus")||!strcmp(data[head-],"minus"))&&

(!strcmp(data[head-],"X"))

)

{

memset(data[head],,'\0');

memset(data[head-],,'\0');

memset(data[head-],,'\0');

memset(data[head-],,'\0');

head=head-+;

strcpy(data[head],"S");

if(outf3(top,dt,f3)&&outf3(top-,dt,f3))

{

top==-;

memset(dt,sizeof(dt),'\0');

}

return true;

}

if( head>=&&

(!strcmp(data[head],"plus")||!strcmp(data[head],"minus"))&&

(!strcmp(data[head-],"X"))

)

{

memset(data[head],,'\0');

memset(data[head-],,'\0');

head=head-+;

strcpy(data[head],"S");

if(outf3(top,dt,f3))

{

top==-;

memset(dt,sizeof(dt),'\0');

}

return true;

}

//X->Y(MY)*

if( head>=&&

(!strcmp(data[head],"Y"))&&

(!strcmp(data[head-],"times")||!strcmp(data[head-],"slash"))&&

(!strcmp(data[head-],"Y"))&&

(!strcmp(data[head-],"times")||!strcmp(data[head-],"slash"))&&

(!strcmp(data[head-],"Y"))

)

{

memset(data[head],,'\0');

memset(data[head-],,'\0');

head=head-+;

strcpy(data[head],"X");

/*

current stack description:

top-> arg

op

top-2-> arg

op

arg

*/

if(outf2(top,dt,f3)&&outf2(top-,dt,f3))

{

top==-;

memset(dt,sizeof(dt),'\0');

}

return true;

}

if( head>=&&

(!strcmp(data[head],"Y"))&&

(!strcmp(data[head-],"times")||!strcmp(data[head-],"slash"))&&

(!strcmp(data[head-],"Y"))

)

{

memset(data[head],,'\0');

memset(data[head-],,'\0');

memset(data[head-],,'\0');

head=head-+;

strcpy(data[head],"X");

/*

current stack description:

top->arg

op

arg

*/

if(outf2(top,dt,f3))

{

top==-;

memset(dt,sizeof(dt),'\0');

}

return true;

}

if( head>=&&(!strcmp(data[head],"Y"))

)

{

memset(data[head],,'\0');

head=head-+;

strcpy(data[head],"X");

if(outf4(top,dt,f3,'X'))

{

top==-;

memset(dt,sizeof(dt),'\0');

}

return true;

}

//Y->I|N|(S)

if( head>=&&(!strcmp(data[head],"ident"))

)

{

memset(data[head],,'\0');

head=head-+;

strcpy(data[head],"Y");

if(outf4(top,dt,f3,'Y'))

{

top==-;

memset(dt,sizeof(dt),'\0');

}

return true;

}

if( head>=&&(!strcmp(data[head],"number"))

)

{

memset(data[head],,'\0');

head=head-+;

strcpy(data[head],"Y");

if(outf4(top,dt,f3,'Y'))

{

top==-;

memset(dt,sizeof(dt),'\0');

}

return true;

}

if( head>=&&

(!strcmp(data[head],"rparen"))&&

(!strcmp(data[head-],"S"))&&

(!strcmp(data[head-],"lparen"))

)

{

memset(data[head],,'\0');

memset(data[head-],,'\0');

memset(data[head-],,'\0');

head=head-+;

strcpy(data[head],"Y");

return true;

}

return false;

}

//遍歷棧

bool visit_data()

{

cout<

for(int i=head;i>=;i--)

{

cout<

}

return true;

}

//主控函數

bool mainf()

{

while(!infile->eof())

{

push();

bool t=reduce();

outf(head,data,f2);

//每當移進結束時就檢查一下是否有可規約串

while(t)//防止規約嵌套

{

t=reduce();

outf(head,data,f2);

}

//visit_data();

}

visit_data();

bool flag=false;

for(int i=head;i>=;i--)

{

if(!strcmp(data[i],"S"))

{

flag=true;

}

if( strcmp(data[i],"S")&&

strcmp(data[i],"X")&&

strcmp(data[i],"A")&&

strcmp(data[i],"Y")&&

strcmp(data[i],"M")&&

strcmp(data[i],"C")

)

{

return false;

}

}

return flag;

/*

while(head>0)

{

bool t=reduce();

//每當移進結束時就檢查一下是否有可規約串

while(t)//防止規約嵌套

{

t=reduce();

}

//visit_data();

outf(head,data,f2);

}

*/

}

};

int main()

{

fstream f1;

f1.open("lexical.txt", ios::in);//打開詞法分析表,供讀

presentation* s1=new presentation(&f1);

bool result=s1->mainf();

if(result)

cout<

else

cout<

f1.close();

f2.close();

return ;

}

運行示例:

(1)合法的語句:

(2)不合法的語句:

tz@COI HZAU

2018/6/12

現代編譯原理——第二章:語法分析之LL(K)

轉自:?http://www.cnblogs.com/BlackWalnut/p/4472122.html LL(K)語法分析技術是建立在預測分析的技術之上的.我們先來了解預測分析技術.考慮以下文法: ...

編譯原理 &num;04&num; 中綴表達式轉化為四元式(JavaScript實現)

// 實驗存檔 運行截圖: 代碼中的總體轉化流程:中綴表達式字符串→tokens→逆波蘭tokens(即后綴表達式)→四元式. 由后綴表達式寫出四元式非常容易,比較繁瑣的地方在于中綴轉逆波蘭,這里采用 ...

編譯原理實驗之SLR1文法分析

---內容開始--- 這是一份編譯原理實驗報告,分析表是手動造的,可以作為借鑒. 基于? SLR(1) 分析法的語法制導翻譯及中間代碼生成程序設計原理與實現1 .理論傳授語法制導的基本概念,目標代碼結 ...

編譯原理(一)緒論概念&amp&semi;文法與語言

緒論概念&文法與語言 以老師PPT為標準,借鑒部分教材內容,AlvinZH學習筆記. 緒論基本概念 1. 低級語言:字位碼.機器語言.匯編語言.與特定的機器有關,功效高,但使用復雜.繁瑣.費時 ...

MOOC 編譯原理筆記(一):編譯原理概述以及程序設計語言的定義

編譯原理概述 什么是編譯程序 編譯程序指:把某一種高級語言程序等價地轉換成另一張低級語言程序(如匯編語言或機器代碼)的程序. 高級語言程序-翻譯->機器語言程序-運行->結果. 其中編譯程 ...

編譯原理&lowbar;P1004

龍書相關知識點總結 //*************************引論***********************************// 1. 編譯器(compiler):從一中語言( ...

python實現算術表達式的詞法語法語義分析(編譯原理應用)

本學期編譯原理的一個大作業,我的選題是算術表達式的詞法語法語義分析,當時由于學得比較渣,只用了遞歸下降的方法進行了分析. 首先,用戶輸入算術表達式,其中算術表達式可以包含基本運算符,括號,數字,以及用 ...

《編譯原理》控制流語句 if 和 while 語句的翻譯 - 例題解析

控制流語句 if 和 while 語句的翻譯 - 例題解析 將 if 和 while 語句翻譯成四元式 注:不同教材會有小差異,使用 _ 或者 - ,如果是 -,請注意區分 - ...

Java 實現《編譯原理》中間代碼生成 -逆波蘭式生成與計算 - 程序解析

Java 實現中間代碼生成 -逆波蘭式生成與計算 - 程序解析 編譯原理學習筆記 (一)逆波蘭式是什么? 逆波蘭式(Reverse Polish notation,RPN,或逆 ...

隨機推薦

安裝 14&period;04&period;1 Ubuntu 到 Lenovo thinkpad t460p

在 Lenovo Thinkpad T460p 安裝 ubuntu, BIOS 需要做一些設定, 沒設定的現象:不斷地停在 usb disk 設定 可以 使用 usb disk install 了!

iframe框架在IE瀏覽器,360兼容瀏覽器下將白色背景設為透明色

iframe在大部分瀏覽 ...

定時任務 Crontab命令 詳解

crontab是Unix和Linux用于設置周期性被執行的指令,是互聯網很常用的技術,很多任務都會設置在crontab循環執行,如果不使用 crontab,那么任務就是常駐程序,這對你的程序要求比較高 ...

php拓展ssh功能

1.下載拓展ssh需要的兩個軟件包,libssh2和ssh2. libssh2下載地址:http://pan.baidu.com/s/1hq7XOhu libssh2官網下載地址:http://www ...

【ASP&period;NET Web API教程】2&period;3&period;7 創建首頁

原文:[ASP.NET Web API教程]2.3.7 創建首頁 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,請先看前面的內容. Part 7: Crea ...

maven 項目中使用 jstl標簽

在pom.xml文件下面增加如下的依賴包: javax.servlet

voa 2015 &sol; 4 &sol; 14

Even with falling oil prices and strong U.S. growth, the head of the International Monetary Fund sai ...

Typecho——數據庫無法連接問題

報錯 對不起,無法連接數據庫,請先檢查數據庫配置再繼續進行安裝 解決方案 創建數據庫 reate database databaseName; 遠程權限 開啟遠程權限 GRANT ALL PRIVIL ...

maven 總結整理(二)——download source code

當我們用maven下載jar包時,有時希望下載jar包的源代碼,此時可以在pom.xml文件中,進行設置. ??? WebProject&l ...

sysbench壓力工具報錯:

[root@ sysbench-]# /usr/local/sysbench/bin/sysbench --version : cannot open shared object file: No s ...

總結

以上是生活随笔為你收集整理的java pl0 四元式,【编译原理】c++实现自下而上语法分析及中间代码(四元式)生成...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。