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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

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

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

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

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

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

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

產(chǎn)生式:

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

X->Y(MY)*

Y->I|N|(S)

A->+|-

M->*|/

C->=|#||>=

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

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;

//預(yù)處理函數(shù)

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=;//存儲(chǔ)打印次數(shù)

//當(dāng)移進(jìn)或者歸約時(shí)打印棧內(nèi)情況,以供分析

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

{

f<

f<

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

{

f<

}

mcount++;

f<

return true;

}

//四元式寫入文件函數(shù)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;

}

//四元式寫入文件函數(shù)2

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

{

f<

return true;

}

//四元式寫入文件函數(shù)3

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

{

f<

return true;

}

//“策略”設(shè)計(jì)模式,面向?qū)ο蠓椒?/p>

class presentation

{

private:

char data[][];//棧

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

fstream *infile;//詞法分析表

int head,top;//兩個(gè)棧的棧頂指針

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[];//存放字符標(biāo)志

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->=|#||>=

*/

//歸約函數(shù)

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;

}

//主控函數(shù)

bool mainf()

{

while(!infile->eof())

{

push();

bool t=reduce();

outf(head,data,f2);

//每當(dāng)移進(jìn)結(jié)束時(shí)就檢查一下是否有可規(guī)約串

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

{

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();

//每當(dāng)移進(jìn)結(jié)束時(shí)就檢查一下是否有可規(guī)約串

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

{

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 ;

}

運(yùn)行示例:

(1)合法的語句:

(2)不合法的語句:

tz@COI HZAU

2018/6/12

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

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

編譯原理 &num;04&num; 中綴表達(dá)式轉(zhuǎn)化為四元式(JavaScript實(shí)現(xiàn))

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

編譯原理實(shí)驗(yàn)之SLR1文法分析

---內(nèi)容開始--- 這是一份編譯原理實(shí)驗(yàn)報(bào)告,分析表是手動(dòng)造的,可以作為借鑒. 基于? SLR(1) 分析法的語法制導(dǎo)翻譯及中間代碼生成程序設(shè)計(jì)原理與實(shí)現(xiàn)1 .理論傳授語法制導(dǎo)的基本概念,目標(biāo)代碼結(jié) ...

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

緒論概念&文法與語言 以老師PPT為標(biāo)準(zhǔn),借鑒部分教材內(nèi)容,AlvinZH學(xué)習(xí)筆記. 緒論基本概念 1. 低級(jí)語言:字位碼.機(jī)器語言.匯編語言.與特定的機(jī)器有關(guān),功效高,但使用復(fù)雜.繁瑣.費(fèi)時(shí) ...

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

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

編譯原理&lowbar;P1004

龍書相關(guān)知識(shí)點(diǎn)總結(jié) //*************************引論***********************************// 1. 編譯器(compiler):從一中語言( ...

python實(shí)現(xiàn)算術(shù)表達(dá)式的詞法語法語義分析(編譯原理應(yīng)用)

本學(xué)期編譯原理的一個(gè)大作業(yè),我的選題是算術(shù)表達(dá)式的詞法語法語義分析,當(dāng)時(shí)由于學(xué)得比較渣,只用了遞歸下降的方法進(jìn)行了分析. 首先,用戶輸入算術(shù)表達(dá)式,其中算術(shù)表達(dá)式可以包含基本運(yùn)算符,括號(hào),數(shù)字,以及用 ...

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

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

Java 實(shí)現(xiàn)《編譯原理》中間代碼生成 -逆波蘭式生成與計(jì)算 - 程序解析

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

隨機(jī)推薦

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

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

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

iframe在大部分瀏覽 ...

定時(shí)任務(wù) Crontab命令 詳解

crontab是Unix和Linux用于設(shè)置周期性被執(zhí)行的指令,是互聯(lián)網(wǎng)很常用的技術(shù),很多任務(wù)都會(huì)設(shè)置在crontab循環(huán)執(zhí)行,如果不使用 crontab,那么任務(wù)就是常駐程序,這對你的程序要求比較高 ...

php拓展ssh功能

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

【ASP&period;NET Web API教程】2&period;3&period;7 創(chuàng)建首頁

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

maven 項(xiàng)目中使用 jstl標(biāo)簽

在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——數(shù)據(jù)庫無法連接問題

報(bào)錯(cuò) 對不起,無法連接數(shù)據(jù)庫,請先檢查數(shù)據(jù)庫配置再繼續(xù)進(jìn)行安裝 解決方案 創(chuàng)建數(shù)據(jù)庫 reate database databaseName; 遠(yuǎn)程權(quán)限 開啟遠(yuǎn)程權(quán)限 GRANT ALL PRIVIL ...

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

當(dāng)我們用maven下載jar包時(shí),有時(shí)希望下載jar包的源代碼,此時(shí)可以在pom.xml文件中,進(jìn)行設(shè)置. ??? WebProject&l ...

sysbench壓力工具報(bào)錯(cuò):

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

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。