HNU-编译原理实验-cminus_compiler-2021-fall-master【0】-环境配置

  • Win 10使用WSL,在Window上运行Ubuntu 20.04LTS版本

参考链接:(20条消息) WSL2的安装详细过程_文艺圈不知名刘先生-CSDN博客_wsl2安装

1:使用管理员权限打开powershell(在搜索栏中搜索)

2:启用“适于Linux的Windows子系统”可选功能

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

3:启用“虚拟机平台”可选组件

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

4:重启电脑

5:重新管理员身份打开powershell,用下面的命令将wsl2设置为默认

wsl --set-default-version 2

出现如下信息表示wsl为虚拟机环境配置好了
【此处省略图片】

6:选择Ubuntu 20.04版本,通过如下链接获取

购买 Ubuntu 20.04 LTS - Microsoft Store zh-CN

7:进行安装并启动即可运行Ubuntu 20.04LTS版本

若安装不成功,请查询排查适用于 Linux 的 Windows 子系统问题 | Microsoft Docs或者网上查询

  • 在本地建立本地仓库,并链接gitee远程仓库,将仓库内容克隆到本地
  • 在本地创建本地仓库

1:建立learngit文件夹,进入文件夹内部,显示当前文件夹路径

$ mkdir learngit
$ cd learngit
$ pwd
/Users/michael/learngit

2:使用git init命令将这个目录变成git可以管理的仓库

zhou@ubuntu:~/learngit$ git init
Initialized empty Git repository in /home/zhou/learngit/.git/
  • 与远程库关联并克隆到本地

1:进入之前的本地仓库

2:在Linux中生成SSH公钥方法 ssh-keygen -t rsa -C "这里换上你的邮箱" //生成密钥

【linux】在linux上生成SSH-key 简单原理介绍+生成步骤 - Angel挤一挤 - 博客园 (cnblogs.com)](https://www.cnblogs.com/sxdcgaq8080/p/10570150.html)

3:查看生成的公钥SSHcat ./ssh/id_ras.pub,将其加入到gitee中的公钥中

4:与远程库相联:git remote add origin ssh公钥

5:克隆:git clone 远程仓库SSH

  • Ubuntu 20.04子系统在Window下路径目录:\\wsl$

  • 在Linux中配置实验环境,sudo apt-get install llvm bison flex

  • 安装g++和GNU环境:sudo apt-get install build-essential

LLVM 10.0.1 Flex 2.6.4 Bison 3.5.1

  • 编译、运行、验证

编译:

$ mkdir build
$ cd build
$ cmake ../
$ make lexer

编译如上实验要求,只是实验一lab1编译要使用make lexer

运行:

$ cd cminus_compiler-2021-fall
#运行build文件夹下的lexer命令,来处理.cminus文件
$ ./build/lexer ./tests/lab1/testcase/1.cminus out
#选择head头部前10行输出
$ head -n 10 out 

验证:

#test_lexer.py脚本将./tests/lab1/testcase下所有为.cminus文件,将结果保存在./tests/lab1/token文件夹下
$ python3 ./tests/lab1/test_lexer.py
#显示出./test/lab1/token文件夹下的分析结果
$ ls ./tests/lab1/token
#用diff指令来验证token结果不一致,如果正确不会输出,否则具体汇报哪一个部分文件不一致
$ diff ./tests/lab1/token ./tests/lab1/TA_token
  • 提交
$ git add .
$ git commit -m '修改report.md'
$ git push

在这里插入图片描述

  • 解决和VBox的冲突
  • 启用VBox

    用管理员权限打开cmd

    bcdedit /set hypervisorlaunchtype off

    随后重启电脑

  • 启用WSL

    用管理员权限打开cmd

    bcdedit /set hypervisorlaunchtype auto

    随后重启电脑

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
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); } } }
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值