第一范文网 - 专业文章范例文档资料分享平台

南昌大学编译原理实验--语法分析(含代码)

来源:用户分享 时间:2020-06-20 本文由沧月哀荒 分享 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

南昌大学实验报告

学生姓名: 学 号: 专业班级:

实验类型:□ 验证□综合 □ 设计 □ 创新 实验日期: 实验成绩:

]实验二 语法分析程序的设计(1)

一、实验目的

通过设计、编制、调试一个典型的语法分析程序,实现对词法分析程序所提供的单词序列进行语法检查和结构分析,进一步掌握常用的语法分析中预测分析方法。

二、实验内容

设计一个文法的预测分析程序,判断特定表达式的正确性。

三、实验要求

1、 给出文法如下:

G[E] E->T|E+T; T->F|T*F; F->i|(E);

2、 根据该文法构造相应的LL(1)文法及LL(1)分析表,并为该文法设计预测分析程序,

利用C语言或C++语言或Java语言实现; 3、 利用预测分析程序完成下列功能:

1) 手工将测试的表达式写入文本文件,每个表达式写一行,用“;”表示结束; 2) 读入文本文件中的表达式;

3) 调用实验一中的词法分析程序搜索单词;

4) 把单词送入预测分析程序,判断表达式是否正确(是否是给出文法的语言),

若错误,应给出错误信息;

5) 完成上述功能,有余力的同学可以进一步完成通过程序实现对非LL(1)文法到

LL(1)文法的自动转换(见实验二附加资料1)。

四、实验环境

PC微机

DOS操作系统或 Windows 操作系统

Turbo C 程序集成环境或 Visual C++ 程序集成环境

五、实验步骤

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

2、 学习预测分析程序的结构,设计合理的预测分析程序; 3、 编写测试程序,包括表达式的读入和结果的输出; 4、 测试程序运行效果,测试数据可以参考下列给出的数据。

六、测试数据

输入数据:

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

正确结果:

(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 输出:错误

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、 给定文法的LL(1)形式; 2、 预测分析程序的算法和结构; 3、 程序运行流程; 4、 程序的测试结果和问题; 5、 实验总结。

package compile2;

import java.util.Stack;

public class text2 {

/*

* 给定文法G[E]: E->T|E+T; T->F|T*F; F->i|(E); */

static String[] gram = { \, \, \, \, \,

\, \, \ };

static String[] followE = { \, \ }; static String[] followEA = { \, \ };

static String[] followT = { \, \, \ }; static String[] followTB = { \, \, \ };

static String[] followF = { \, \, \, \ }; static String[] firstE = { \, \ }; static String[] firstEA = { \, \ }; static String[] firstT = { \, \ }; static String[] firstTB = { \, \ }; static String[] firstF = { \, \ };

static String[][] list = { { \, \, \, \, \, \, \ }, { \, \, null, null, \, null, null }, { \, null, \, null, null, \, \ }, { \, \, null, null, \, null, null }, { \, null, \, \, null, \, \ },

{ \, \, null, null, \, null, null } };

public static void main(String[] args) throws Exception { String infile = \; String outfile = \;

Stack[] tmpword = new Stack[20]; Stack[] expression = new Stack[20]; for (int i = 0; i < tmpword.length; i++) { tmpword[i] = new Stack(); expression[i] = new Stack(); }

main.scan(infile, outfile, tmpword, expression); int i = 0;

while (tmpword[i].size() > 2){

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

isLL1(tmpword[i]); i++; }

}

public static void printArray(String[] s){ for (int i = 0; i < s.length; i++){ System.out.print(s[i]); }

System.out.println(); }

public static void isLL1(Stack tmpword){ String[] input = tmpword.toArray(new String[0]); int inputCount = 0;

Stack status = new Stack(); status.push(input[inputCount++]); status.push(\); boolean flag = true; boolean result = true; while (flag) {

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

if (status.peek().equals(input[inputCount])){ status.pop(); inputCount++; }

else{

flag = false; result = false; } }

else if (status.peek().equals(\)){

if (status.peek().equals(input[inputCount])) flag = false; else{

flag = false; result = false; } } else if (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(\)) status.pop(); else{

status.pop();

for (int i = 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

System.out.println(\错误\); }

public static boolean isVt(String s) {

for (int i = 1; i < list[0].length - 1; i++) { if (s.equals(list[0][i])) { return true; } }

return false; }

public static int[] indexInList(String m, String a) { int i = 0, j = 0;

for (int c = 1; c < list.length; c++) { if (m.equals(list[c][0])) i = c; }

for (int c = 1; c < list[0].length ; c++) { if (a.equals(list[0][c])) j = c;

搜索“diyifanwen.net”或“第一范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,第一范文网,提供最新医药卫生南昌大学编译原理实验--语法分析(含代码) 全文阅读和word下载服务。

南昌大学编译原理实验--语法分析(含代码) .doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/wenku/1088742.html(转载请注明文章来源)
热门推荐
Copyright © 2018-2022 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top