From 90aa2cbdf9946dfb9bbed8b4c7825210254207c0 Mon Sep 17 00:00:00 2001 From: ridethepig Date: Sat, 18 Mar 2023 16:23:33 +0800 Subject: [PATCH] trivial commit --- .gitignore | 8 ++++++++ CS143体验报告.md | 12 +++++++++++- assignments/PA2/cool-lex.d | 8 ++++---- assignments/PA2/cool.flex | 29 ++++++++++++++++++++++++++++- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 5683457..d63bf1d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,9 @@ *.s +assignments/PA2/cgen +assignments/PA2/cool-lex.cc +assignments/PA2/parser +assignments/PA2/semant +assignments/PA2/lexer +*.o +.cache/ +compile_commands.json diff --git a/CS143体验报告.md b/CS143体验报告.md index f35d839..8df6f3e 100644 --- a/CS143体验报告.md +++ b/CS143体验报告.md @@ -31,4 +31,14 @@ - `let` 语法也是究极奇葩,我是不能理解为什么要设计成这个样子。想定义一个局部变量都得加一层嵌套,这个嵌套多得让我觉得我 tm 是在写 `scheme`。 - 它的 `case` 也是奇葩,居然是用来做动态类型匹配的,匹配的是类型而不是值,和正常的 `switch-case` 完全不一样,倒是有点像 `rust` 里面的那种感觉。 -测试的话,虽然 handout 里面说是直接 `make test` 然后对比输出,不过我也没看到它哪里有所谓的 reference implementation,自己看了看测例觉着没啥问题就行了。 \ No newline at end of file +测试的话,虽然 handout 里面说是直接 `make test` 然后对比输出,不过我也没看到它哪里有所谓的 reference implementation,自己看了看测例觉着没啥问题就行了。 + +### PA2 + +PA 2-5 正式写编译器。PA2 写词法分析器,首先读一遍 README 和 handout。 + +> 环境配置 +> 因为这个项目的结构非常的智障,导致需要进行一些配置才能让 `clangd` 正常工作。因为是 `Makefile` 项目,所以不能直接生成 `compile_commands.json`。 +> 1. 安装 `apt install bear`,这个工具可以拦截 `make` 命令来生成上述的文件。 +> 2. 在 PA2 目录下,运行 `make clean && bear -- make lexer`,然后 `clangd` 应该就不会找不到头文件之类的了 + diff --git a/assignments/PA2/cool-lex.d b/assignments/PA2/cool-lex.d index 6baab0b..88ebb77 100644 --- a/assignments/PA2/cool-lex.d +++ b/assignments/PA2/cool-lex.d @@ -1,5 +1,5 @@ cool-lex.o cool-lex.d : cool-lex.cc ../../include/PA2/cool-parse.h \ - ../../include/PA2/copyright.h ../../include/PA2/cool-io.h \ - ../../include/PA2/tree.h ../../include/PA2/stringtab.h \ - ../../include/PA2/list.h ../../include/PA2/stringtab.h \ - ../../include/PA2/utilities.h + ../../include/PA2/copyright.h ../../include/PA2/cool-io.h \ + ../../include/PA2/tree.h ../../include/PA2/stringtab.h \ + ../../include/PA2/list.h ../../include/PA2/stringtab.h \ + ../../include/PA2/utilities.h diff --git a/assignments/PA2/cool.flex b/assignments/PA2/cool.flex index acb7d8a..700d0ab 100644 --- a/assignments/PA2/cool.flex +++ b/assignments/PA2/cool.flex @@ -1,3 +1,4 @@ +%{ /* * The scanner definition for COOL. */ @@ -7,7 +8,6 @@ * output, so headers and global definitions are placed here to be visible * to the code in the file. Don't remove anything that was here initially */ -%{ #include #include #include @@ -50,6 +50,33 @@ extern YYSTYPE cool_yylval; */ DARROW => +DIGIT [0-9] +DIGITS {DIGIT}+ +/* */ +/* Keywords Definition*/ +/* Except for the constants true and false, keywords are case insensitive */ +K_CLASS (?i:class) +K_ELSE (?i:else) +K_FI (?i:fi) +K_IF (?i:if) +K_IN (?i:in) +K_INHERITS (?i:inherits) +K_ISVOID (?i:isvoid) +K_LET (?i:let) +K_LOOP (?i:loop) +K_POOL (?i:pool) +K_THEN (?i:then) +K_WHILE (?i:while) +K_CASE (?i:case) +K_ESAC (?i:esac) +K_NEW (?i:new) +K_OF (?i:of) +K_NOT (?i:not) +/* the first letter of true/false must be lowercase; the trailing may be upper or lower case. */ +K_TRUE t(?i:rue) +K_FALSE f(?i:alse) + + %%