资源描述:
《实验二~~语法分析程序的设计~.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、......Word文档...范文范例...供学习参考实验二语法分析程序的设计姓名:学号:专业班级一、实验目的通过设计、编制、调试一个典型的语法分析程序,实现对词法分析程序所提供的单词序列进行语法检查和结构分析,进一步掌握常用的语法分析中预测分析方法。 二、实验内容设计一个文法的预测分析程序,判断特定表达式的正确性。三、实验要求1、给出文法如下:G[E]E->T
2、E+T;T->F
3、T*F;F->i
4、(E);2、根据该文法构造相应的LL(1)文法及LL(1)分析表,并为该文法设计预测分析程序,利用C语言或C++语言或Java语言实现;3、利用预测分析
5、程序完成下列功能:1)手工将测试的表达式写入文本文件,每个表达式写一行,用“;”表示结束;2)读入文本文件中的表达式;3)调用实验一中的词法分析程序搜索单词;4)把单词送入预测分析程序,判断表达式是否正确(是否是给出文法的语言),若错误,应给出错误信息;5)完成上述功能,有余力的同学可以进一步完成通过程序实现对非LL(1)文法到LL(1)文法的自动转换(见实验二附加资料1)。 四、实验环境PC微机DOS操作系统或Windows操作系统TurboC程序集成环境或VisualC++程序集成环境 五、实验步骤1、分析文法,将给出的文法转化为LL(1)文法
6、;2、学习预测分析程序的结构,设计合理的预测分析程序;3、编写测试程序,包括表达式的读入和结果的输出;4、测试程序运行效果,测试数据可以参考下列给出的数据。 六、测试数据.....专业资料...可分享.下载......Word文档...范文范例...供学习参考输入数据:编辑一个文本文文件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)
7、(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输出:错误七、源代码importjava.util.*;importjava.io.*;publicclasstest2{staticString[]key_word={"main","if","then","while","do","int","else"};staticString[]cal_word={"+","-","*","/","<",">","
8、{","}","(",")","[","]","==","!=","!","=",">=","<=","+=","-=","*=","/=",";"};/**给定文法G[E]:E->T
9、E+T;T->F
10、T*F;F->i
11、(E);*/staticString[]gram={"E->TA","A->+TA","A->@","T->FB","B->*FB","B->@","F->P","F->(E)"};staticString[]followE={")","#"};.....专业资料...可分享.下载......Word文档...范文范例...供学习
12、参考staticString[]followEA={")","#"};staticString[]followT={"+",")","#"};staticString[]followTB={"+",")","#"};staticString[]followF={"*","+",")","#"};staticString[]firstE={"i","("};staticString[]firstEA={"+","@"};staticString[]firstT={"i","("};staticString[]firstTB={"*","@"};sta
13、ticString[]firstF={"i","("};staticString[][]list={{"","i","+","*","(",")","#"},{"E","TA",null,null,"TA",null,null},{"A",null,"+TA",null,null,"@","@"},{"T","FB",null,null,"FB",null,null},{"B",null,"@","*FB",null,"@","@"},{"F","i",null,null,"(E)",null,null}};publicstaticvoidscan
14、(Stringinfile,Stringoutfile,Stack[]word,Stack