南昌大学编译原理实验语法分析含代码文档格式.docx

上传人:b****6 文档编号:18717071 上传时间:2022-12-31 格式:DOCX 页数:13 大小:61.16KB
下载 相关 举报
南昌大学编译原理实验语法分析含代码文档格式.docx_第1页
第1页 / 共13页
南昌大学编译原理实验语法分析含代码文档格式.docx_第2页
第2页 / 共13页
南昌大学编译原理实验语法分析含代码文档格式.docx_第3页
第3页 / 共13页
南昌大学编译原理实验语法分析含代码文档格式.docx_第4页
第4页 / 共13页
南昌大学编译原理实验语法分析含代码文档格式.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

南昌大学编译原理实验语法分析含代码文档格式.docx

《南昌大学编译原理实验语法分析含代码文档格式.docx》由会员分享,可在线阅读,更多相关《南昌大学编译原理实验语法分析含代码文档格式.docx(13页珍藏版)》请在冰豆网上搜索。

南昌大学编译原理实验语法分析含代码文档格式.docx

1、分析文法,将给出的文法转化为LL

(1)文法;

2、学习预测分析程序的结构,设计合理的预测分析程序;

3、编写测试程序,包括表达式的读入和结果的输出;

4、测试程序运行效果,测试数据可以参考下列给出的数据。

六、测试数据

输入数据:

编辑一个文本文文件expression.txt,在文件中输入如下内容:

10;

1+2;

(1+2)*3+(5+6*7);

((1+2)*3+4;

1+2+3+(*4+5);

(a+b)*(c+d);

((ab3+de4)**5)+1;

正确结果:

(1)10;

输出:

正确

(2)1+2;

(3)(1+2)*3+(5+6*7);

(4)((1+2)*3+4

错误

(5)1+2+3+(*4+5)

(6)(a+b)*(c+d)

(7)((ab3+de4)**5)+1

七、实验报告要求

实验报告应包括以下几个部分:

1、给定文法的LL

(1)形式;

2、预测分析程序的算法和结构;

3、程序运行流程;

4、程序的测试结果和问题;

5、实验总结。

packagecompile2;

importjava.util.Stack;

publicclasstext2{

/*

*给定文法G[E]:

*/

staticString[]gram={"

E->

TA"

"

A->

+TA"

@"

T->

FB"

B->

*FB"

"

F->

P"

(E)"

};

staticString[]followE={"

)"

#"

staticString[]followEA={"

staticString[]followT={"

+"

staticString[]followTB={"

staticString[]followF={"

*"

staticString[]firstE={"

i"

("

staticString[]firstEA={"

staticString[]firstT={"

staticString[]firstTB={"

staticString[]firstF={"

staticString[][]list={{"

"

},

{"

E"

null,null,"

null,null},

A"

null,"

T"

B"

F"

null,null}};

publicstaticvoidmain(String[]args)throwsException{

Stringinfile="

D:

/expression.txt"

;

Stringoutfile="

/result2.txt"

Stack<

String>

[]tmpword=newStack[20];

[]expression=newStack[20];

for(inti=0;

i<

tmpword.length;

i++){

tmpword[i]=newStack<

();

expression[i]=newStack<

}

main.scan(infile,outfile,tmpword,expression);

inti=0;

while(tmpword[i].size()>

2){

String[]tmp=expression[i].toArray(newString[0]);

printArray(tmp);

isLL1(tmpword[i]);

i++;

}

publicstaticvoidprintArray(String[]s){

s.length;

i++){

System.out.print(s[i]);

System.out.println();

publicstaticvoidisLL1(Stack<

tmpword){

String[]input=tmpword.toArray(newString[0]);

intinputCount=0;

status=newStack<

status.push(input[inputCount++]);

status.push("

);

booleanflag=true;

booleanresult=true;

while(flag){

if(isVt(status.peek())){

if(status.peek().equals(input[inputCount])){

status.pop();

inputCount++;

}

else{

flag=false;

result=false;

}

elseif(status.peek().equals("

)){

if(status.peek().equals(input[inputCount]))

elseif(list[indexInList(status.peek(),input[inputCount])[0]][indexInList(status.peek(),input[inputCount])[1]]!

=null){

int[]a=indexInList(status.peek(),input[inputCount]);

if(list[a[0]][a[1]].endsWith("

))

for(inti=list[a[0]][a[1]].length()-1;

i>

=0;

i--){

status.push("

+list[a[0]][a[1]].charAt(i));

}

//inputCount++;

else{

flag=false;

result=false;

if(result)

System.out.println("

正确"

else

错误"

publicstaticbooleanisVt(Strings){

for(inti=1;

list[0].length-1;

if(s.equals(list[0][i])){

returntrue;

returnfalse;

publicstaticint[]indexInList(Stringm,Stringa){

inti=0,j=0;

for(intc=1;

c<

list.length;

c++){

if(m.equals(list[c][0]))

i=c;

list[0].length;

if(a.equals(list[0][c]))

j=c;

returnnewint[]{i,j};

}

importjava.io.*;

importjava.util.*;

publicclassmain{

staticString[]key_word={"

main"

if"

then"

while"

do"

int"

else"

staticString[]cal_word={"

-"

/"

<

>

{"

}"

["

]"

=="

!

="

+="

-="

*="

/="

/program.txt"

/result.txt"

Stack[]word=newStack[5];

Stack[]expression=newStack[word.length];

word.length;

word[i]=newStack<

scan(infile,outfile,word,expression);

publicstaticvoidscan(Stringinfile,Stringoutfile,Stack<

[]word,Stack<

[]expression)

throwsException{

java.io.Filefile=newjava.io.File(infile);

Scannerinput=newScanner(file);

java.io.PrintWriteroutput=newPrintWriter(outfile);

intcount=0;

word[count].push("

while(input.hasNext()){

Stringtmp=input.next();

inti=0;

while(i<

tmp.length()){

if(tmp.charAt(i)<

='

9'

&

&

tmp.charAt(i)>

1'

){//检查十进制数字

Stringnum="

while(tmp.charAt(i)<

0'

){

num+=tmp.charAt(i);

i++;

if(i==tmp.length())

break;

output.println("

"

+1+"

+num+"

word[count].push("

expression[count].push(num);

if(i+2<

tmp.length())//检查十六进制数字

if(tmp.charAt(i)=='

tmp.charAt(i+1)=='

x'

i+=2;

Stringnum="

while((tmp.charAt(i)<

)||

(tmp.charAt(i)<

f'

a'

)){

num+=tmp.charAt(i);

i++;

if(i==tmp.length())

break;

}

output.println("

+3+"

word[count].push("

expression[count].push(num);

if(i+1<

tmp.length())//检查八进制数字

while(tmp.charAt(i)<

7'

+2+"

//检查关键字和变量

if(i<

if(i<

tmp.length()&

&

tmp.charAt(i)<

z'

Stringtmp_word="

while(tmp.charAt(i)>

tmp_word+=tmp.charAt(i);

booleanis_keyword=false;

for(intj=0;

j<

key_word.length;

j++){

if(tmp_word.equals(key_word[j])){

output.println("

+key_word[j]+"

+"

->

word[count].push(key_word[j]);

expression[count].push(key_word[j]);

is_keyword=true;

}

if(!

is_keyword){

output.println("

+0+"

+tmp_word+"

word[count].push("

expression[count].push(tmp_word);

//检查运算符以及'

'

if(i+1<

if(tmp.charAt(i+1)=='

='

for(intj=0;

cal_word.length;

if(cal_word[j].equals("

+tmp.charAt(i)

+tmp.charAt(i+1))){

output.println("

+cal_word[j]+"

"

+"

word[count].push(cal_word[j]);

expression[count].push("

if(word[count].peek()=="

word[count].pop();

word[count].push("

count++;

}

i+=2;

break;

}

for(intj=0;

if(cal_word[j].equals("

+tmp.charAt(i))){

word[count].push(cal_word[j]);

expression[count].push(cal_word[j]);

if(word[count].peek()=="

word[count].pop();

word[count].push("

count++;

input.close();

output.close();

八、思考题

1、如果使用递归下降分析法来进行语法分析,为什么文法必须先转化为LL

(1)文法再做递归下降分析?

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 实习总结

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1