【编译原理】实验一:熟悉实验环境VSCode并完成正则表达式转换为NFA

本文介绍了如何使用VSCode进行编译原理实验,内容包括安装与启动VSCode、克隆项目到本地、登录平台、查看项目文件及正则表达式转换为NFA的步骤。实验过程涉及正则表达式到NFA片段的构造,并提供了对不同正则表达式构造NFA的解析和状态图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package analysis; import java.util.ArrayList; import java.util.List; import library.Digit; import library.KeyWords; import library.Symbol; /** * * @author 周弘懿 * */ public class AnalyseWords { private List temp = new ArrayList(); public List getTemp() { return temp; } /* 关键字或者标识符的长度。 */ private static final int LENGTHOFKEYWORDS = 500; /* 整型数据的长度。 */ private static final int LENGTHOFINT = 500; /* 字符串常量长度。 */ private static final int LENGTHOFSTRING = 1024; String result = ""; int position; /** * 处理程序 * * @return 词法分析后的结果 */ public void process(String aLine) { /* 存放关键字和标识符 */ byte[] word = new byte[LENGTHOFKEYWORDS]; /* 存放数字 */ byte[] number = new byte[LENGTHOFINT]; /* 存放运算符 */ byte[] symbol = new byte[500]; /* 存放字符串常数或是字符常数 */ byte[] string = new byte[LENGTHOFSTRING]; byte temp[]; temp = aLine.getBytes(); if(new String(temp).trim().length() == 0) this.temp.add("请输入C语言程序!"); /** * 主要逻辑 */ for (position = 0; position= temp.length) { return; } /* 滤掉空格、制表键。 */ if (temp[position] != ' ' || temp[position] != '\t') { /* 允许大小写字母和下划线 */ if ((temp[position] >= 65 && temp[position] = 97 && temp[position] = 48 && temp[position] = temp.length) /* 如果已经超过界限就终止循环。 */{ String checkdWord = new String(word, 0, tempPosition + 1); decideWord(checkdWord); return position; } } while ((temp[position] >= 65 && temp[position] = 97 && temp[position] = 48 && temp[position] = 48 && temp[position] = temp.length) /* 如果已经超过界限就终止循环。 */{ String checkdNumber = new String(number, 0, tempPosition + 1); decideNum(checkdNumber); return position; } } while (temp[position] >= '0' && temp[position] <= '9' || temp[position] == '.'); String checkdNumber = new String(number, 0, tempPosition + 1); decideNum(checkdNumber); // 因为上面的已经移到末尾,因为for还要+1,所以要-1 position--; } return position; } /** * 分析字段是实型还是整型常数 * @param checkdNumber 分析数字字段 */ private void decideNum(String checkdNumber) { if (Digit.hasDot(checkdNumber)) { try { double num = Double.parseDouble(checkdNumber); this.temp.add("(" + num + ", 实型常数)\n"); } catch (Exception e) { this.temp.add("(" + checkdNumber + ", 错误,发现多个.)\n"); } } else { try { long num = Long.parseLong(checkdNumber); this.temp.add("(" + num + ", 整型常数)\n"); } catch (Exception e) { this.temp.add("(" + checkdNumber + ", 错误,不是整型常数!)\n"); } } } /** * 分析字符常数,字符串常数和边界符 * @param position 词法分析语句遍历指针 * @param string 字符,字符串和边界符的数组 * @param temp 词法分析语句数组 * @return 返回词法分析语句遍历指针的新位置 */ public int analysisLimitSymble(int position, byte[] string, byte temp[]) { // 过滤字符串常数 if (temp[position] == '"') { // 存储数组的指针 int tempPosition = -1; do { tempPosition++; string[tempPosition] = temp[position]; position++; } while (temp[position] != '"'); //要把最后个"给过滤掉,所以要向后移个位置。 tempPosition++; string[tempPosition] = '"'; String checkdNumber=new String(string,0,tempPosition+1); this.temp.add("(" + checkdNumber + ", 字符串常数)\n"); } // 过滤字符常数 else if (temp[position] == '\'') { // 存储数组的指针 int tempPosition = -1; do { tempPosition++; string[tempPosition] = temp[position]; position++; } while (temp[position] != '\''); tempPosition++; string[tempPosition] = '\''; //要把最后个"给过滤掉,所以要向后移个位置。 String checkdNumber=new String(string,0,tempPosition+1); //c语言语法规定字符常量的字符数只能是1,再加上2个’,刚好应该是3。 if(tempPosition+1= 2) { this.temp.add("(" + new String(symbol,0,tempPosition+1)+ ", 错误,字符长度仅限2位)\n"); return position; } position++; if(position >= temp.length) /* 如果已经超过界限就终止循环。 */{ String checkdWord = new String(symbol, 0, tempPosition+1); position = decideSymble(checkdWord, temp, position); return position; } } while(Symbol.isSingleSymble(new String(temp, position, 1))); String checkdWord = new String(symbol, 0, tempPosition+1); //单个运算符可能组成符合运算符 position = decideSymble(checkdWord, temp, position); position--; return position; } private int decideSymble(String checkdWord, byte temp[], int position) { if(Symbol.isComboSymbol(checkdWord)) { this.temp.add("(" + checkdWord + ", 复合运算符)\n"); } else if(Symbol.isSingleSymble(checkdWord)) { this.temp.add("(" + checkdWord + ", 运算符)\n"); }else if(checkdWord.equals("/*")) { //过滤注释 this.temp.add("(" + checkdWord + ", 前注释)\n"); do { position++; if(position >= temp.length) { return position; } } while(temp[position] != '*'); /* 直到阅读到*,表示将其间的注释都过滤 */ position++; if(position>=temp.length) { /*如果已经超过界限就终止循环。*/ this.temp.add("错误,后注释缺少结束符'/'\n"); return position; } if(temp[position]!='/') { this.temp.add("错误,后注释缺少结束符'/'\n"); } else { this.temp.add("(" + new String(temp, position-1, 2) + ", 后注释 )\n"); return position-1; } } return position; } public static void main(String[] args) { AnalyseWords aw = new AnalyseWords(); aw.process("sd"); for (String str : aw.temp) { System.out.println(str); } } }
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不牌不改

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值