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

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

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

}

return new int[] { i, j };

} }

package compile2; import java.io.*; import java.util.*;

public class main {

public static void scan(String infile,String outfile, Stack[] word, Stack[]

throws Exception {

expression)

java.io.File file = new java.io.File(infile); Scanner input = new Scanner(file);

java.io.PrintWriter output = new PrintWriter(outfile); int count = 0; word[count].push(\while (input.hasNext()) { }

public static void main(String[] args) throws Exception {

String infile = \String outfile = \Stack[] word = new Stack[5];

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

scan(infile, outfile, word, expression);

word[i] = new Stack(); expression[i] = new Stack();

static String[] key_word = { \

\

\\

static String[] cal_word = { \

String tmp = input.next(); int i = 0;

while (i < tmp.length()) {

if (tmp.charAt(i) <= '9' && tmp.charAt(i) >= '1') {//检查十进制数字 }

if (i + 2 < tmp.length())// 检查十六进制数字

if (tmp.charAt(i) == '0' && tmp.charAt(i + 1) == 'x') { }

if (tmp.charAt(i) == '0') {

i++;

String num = \

while (tmp.charAt(i) <= '7' && tmp.charAt(i) >= '0') { }

output.println(\word[count].push(\

num += tmp.charAt(i); i++;

if (i == tmp.length())

break;

i += 2; String num = \

while ((tmp.charAt(i) <= '9' && tmp.charAt(i) >= '0') || }

output.println(\word[count].push(\expression[count].push(num);

i++;

if (i == tmp.length())

break;

(tmp.charAt(i) <= 'f' && tmp.charAt(i) >= 'a')) { num += tmp.charAt(i);

String num = \

while (tmp.charAt(i) <= '9' && tmp.charAt(i) >= '0') { }

output.println(\word[count].push(\expression[count].push(num);

num += tmp.charAt(i); i++;

if (i == tmp.length())

break;

if (i + 1 < tmp.length())// 检查八进制数字

}

expression[count].push(num);

// 检查关键字和变量 if (i < tmp.length()) { }

// 检查运算符以及';' if (i < tmp.length()) {

if (i + 1 < tmp.length()) {

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

for (int j = 0; j < cal_word.length; j++) {

if (cal_word[j].equals(\

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

+ \

output.println(\word[count].push(cal_word[j]); expression[count].push(\

if (i < tmp.length() && tmp.charAt(i) >= 'a' }

&& tmp.charAt(i) <= 'z') { String tmp_word = \

while (tmp.charAt(i) >= 'a' && tmp.charAt(i) <= 'z') { }

boolean is_keyword = false;

for (int j = 0; j < key_word.length; j++) { }

if (!is_keyword) { }

output.println(\word[count].push(\

expression[count].push(tmp_word); if (tmp_word.equals(key_word[j])) { }

output.println(\word[count].push(key_word[j]); expression[count].push(key_word[j]); is_keyword = true; break;

tmp_word += tmp.charAt(i); i++;

if (i == tmp.length())

break;

}

}

}

}

}

}

}

}

}

if (word[count].peek() == \ } i += 2; break;

word[count].pop(); word[count].push(\count++;

word[count].push(\

for (int j = 0; j < cal_word.length; j++) { }

if (cal_word[j].equals(\ }

output.println(\word[count].push(cal_word[j]); expression[count].push(cal_word[j]); if (word[count].peek() == \ } i++; break;

word[count].pop(); word[count].push(\count++;

word[count].push(\

input.close(); output.close();

八、思考题

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

再做递归下降分析?

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

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