网站结构构图,企业网站建设意义,西安建设网站制作,查别人wordpress主题HNU编译原理lab1实验–根据cminux-f的词法补全lexical_analyer.l文件#xff0c;完成词法分析器。
本文没有添加任何图片#xff0c;但是以复制输出的形式展现出来了实验结果。
实验要求#xff1a;
根据cminux-f的此法补全lexical_analyer.l文件#xff0c;完成词法分析…HNU编译原理lab1实验–根据cminux-f的词法补全lexical_analyer.l文件完成词法分析器。
本文没有添加任何图片但是以复制输出的形式展现出来了实验结果。
实验要求
根据cminux-f的此法补全lexical_analyer.l文件完成词法分析器能够输出识别出的tokentypeline(刚出现的行数)pos_start(该行的开始位置)post_end(结束位置 不包含) 例如 文本输入
int a;则识别结果应为
int 280 1 2 5
a 285 1 6 7
; 270 1 7 8cminus-f词法
C MINUS是C语言的一个子集该语言的语法在《编译原理与实践》第九章附录中有详细的介绍。而cminus-f则是在C MINUS上追加了浮点操作。
关键字else if int return void while float专用符号 - * / ! ; , ( ) [ ] { } /* */标识符ID和整数NUM通过下列正则表达式定义:
letter a|...|z|A|...|Z
digit 0|...|9
ID letter
INTEGER digit
FLOAT (digit. | digit*.digit)注释用/*...*/表示可以超过一行。注释不能嵌套。
/*...*/注[, ], 和 [] 是三种不同的token。[]用于声明数组类型[]中间不得有空格。 a[]应被识别为两个token: a、[]a[1]应被识别为四个token: a, [, 1, ]
实验难点
git相关操作常用的命令在实验文档中
将实验仓库克隆到本地:打开本地的工作目录在命令行中输入
git clone https://gitee.com/你的gitee用户名/cminus_compiler-2022-fall.git
打开本地的工作目录在命令行中输入
git add *
git commit -m 注释语句
然后push到仓库
git push实验环境配置
sudo apt-get install llvm bison flex 输入flex --version和bison --version
FLEX工具的简单使用
首先FLEX从输入文件*.lex或者stdio读取词法扫描器的规范从而生成C代码源文件lex.yy.c。然后编译lex.yy.c并与-lfl库链接以生成可执行的a.out。最后a.out分析其加入的输入流将其转换为一系列token。 举例
%{
//在%{和%}中的代码会被原样照抄到生成的lex.yy.c文件的开头您可以在这里书写声明与定义
#include string.h
int chars 0;
int words 0;
%}
%%/*你可以在这里使用你熟悉的正则表达式来编写模式*//*你可以用C代码来指定模式匹配时对应的动作*//*yytext指针指向本次匹配的输入文本*//*左部分[a-zA-Z]为要匹配的正则表达式右部分{ chars strlen(yytext);words;}为匹配到该正则表达式后执行的动作*/
[a-zA-Z] { chars strlen(yytext);words;}
. {}/*对其他所有字符不做处理继续执行*/
%%
int main(int argc, char **argv){//yylex()是flex提供的词法分析例程默认读取stdin yylex(); printf(look, I find %d words of %d chars\n, words, chars);return 0;
}lex中的字符规定
格式含义a字符“a”元字符\a转义a*a的零次或者多次重复aa的一次或者多次重复a一个可选的aab(a)a本身[abc]字符abc中的任意一个[a-d]字符abcd的任意一个{xxxxx}名字xxx表示的正则表达式.除了新行之外的任意一个字符
实验设计
找到Token符号对应的字符
在cminux_compiler-2023-fall/include/lexical_analyzer.h 有定义cimux_token_type附录
对应正则表达式
根据cminus-f词法
1. 关键字
else if int return void while float2. 专用符号- * / ! ; , ( ) [ ] { } /* */3. 标识符ID和整数NUM通过下列正则表达式定义:
letter a|...|z|A|...|Z
digit 0|...|9
ID letter
INTEGER digit
FLOAT (digit. | digit*.digit)4. 注释用/*...*/表示可以超过一行。注释不能嵌套。
/*...*/
- 注[, ], 和 [] 是三种不同的token。[]用于声明数组类型[]中间不得有空格。- a[]应被识别为两个token: a、[]- a[1]应被识别为四个token: a, [, 1, ]写出对应的正则表达式和指定匹配对应的动作
C minus的词法单元规则有 关键字else if int return void while float 专用符号 - * / ! ; , ( ) [ ] { } /* */ 标识符ID和整数NUM通过下列正则表达式定义:
letter a|...|z|A|...|Z
digit 0|...|9
ID letter
INTEGER digit
FLOAT (digit. | digit*.digit)注释用/*...*/表示可以超过一行。注释不能嵌套。
此部分用于定义C Minus的词法单元的规则模式采用正则表达式表示注意当词法单元的pattern中包含特殊字符时需要使用转义字符\。动作使用C语言描述确定每个Token在每行的开始位置和结束位置并且返回该词法单元类型。该返回值为yylex()的返回值。 动作分为两步第一步更新lines、pos_start、post_end。第二步将识别结果token返回return。 运算
\ {pos_startpos_end;pos_endpos_start1;return ADD;}
\- {pos_startpos_end;pos_endpos_start1;return SUB;}
\* {pos_startpos_end;pos_endpos_start1;return MUL;}
\/ {pos_startpos_end;pos_endpos_start1;return DIV;}
\ {pos_startpos_end;pos_endpos_start1;return LT;}{pos_startpos_end;pos_endpos_start2;return LTE;}
\ {pos_startpos_end;pos_endpos_start1;return GT;}{pos_startpos_end;pos_endpos_start2;return GTE;}{pos_startpos_end;pos_endpos_start2;return EQ;}
! {pos_startpos_end;pos_endpos_start2;return NEQ;}
\ {pos_startpos_end;pos_endpos_start1;return ASSIN;}符号
\; {pos_startpos_end;pos_endpos_start1;return SEMICOLON;}
\, {pos_startpos_end;pos_endpos_start1;return COMMA;}
\( {pos_startpos_end;pos_endpos_start1;return LPARENTHESE;}
\) {pos_startpos_end;pos_endpos_start1;return RPARENTHESE;}
\[ {pos_startpos_end;pos_endpos_start1;return LBRACKET;}
\] {pos_startpos_end;pos_endpos_start1;return RBRACKET;}
\{ {pos_startpos_end;pos_endpos_start1;return LBRACE;}
\} {pos_startpos_end;pos_endpos_start1;return RBRACE;}关键字
else {pos_startpos_end;pos_endpos_start4;return ELSE;}
if {pos_startpos_end;pos_endpos_start2;return IF;}
int {pos_startpos_end;pos_endpos_start3;return INT;}
float {pos_startpos_end;pos_endpos_start5;return FLOAT;}
return {pos_startpos_end;pos_endpos_start6;return RETURN;}
void {pos_startpos_end;pos_endpos_start4;return VOID;}
while {pos_startpos_end;pos_endpos_start5;return WHILE;}标识符和整数NUM
[a-zA-Z] {pos_startpos_end;pos_endpos_startstrlen(yytext);return IDENTIFIER;}
[0-9] {pos_startpos_end;pos_endpos_startstrlen(yytext);return INTEGER;}
[0-9]*\.[0-9] {pos_startpos_end;pos_endpos_startstrlen(yytext);return FLOATPOINT;}
[] {pos_startpos_end;pos_endpos_start2;return ARRAY;}
[a-zA-Z] {pos_startpos_end;pos_endpos_start1;return LETTER;}
[0-9]\. {pos_startpos_end;pos_endpos_startstrlen(yytext);return FLOATPOINT;}其他的
当词法分析器扫描到换行符时Windows下为\r\nLinux下为\nMac下为\r行数lines自增pos_start与pos_end更新由于flex生成的词法分析器采用最长匹配策略且注释/**/包含正则的通配符正则规范较为复杂。当识别到一个注释时需要考虑词法单元开始位置和结束位置变化且多行注释要修改lines.错误的词法单元 当扫描到错误的词法单元仅返回ERROR
\n {return EOL;} #换行
\/\*([^\*]|(\*)*[^\*\/])*(\*)*\*\/ {return COMMENT;} #注释{return BLANK;} #空格
\t {return BLANK;} # 空格
. {pos_startpos_end;pos_endpos_startstrlen(yytext);return ERROR;} #错误最终的添加
/******************TODO*********************//****请在此补全所有flex的模式与动作 start******///STUDENT TO DO\ {pos_startpos_end;pos_endpos_start1;return ADD;}
\- {pos_startpos_end;pos_endpos_start1;return SUB;}
\* {pos_startpos_end;pos_endpos_start1;return MUL;}
\/ {pos_startpos_end;pos_endpos_start1;return DIV;}
\ {pos_startpos_end;pos_endpos_start1;return LT;}{pos_startpos_end;pos_endpos_start2;return LTE;}
\ {pos_startpos_end;pos_endpos_start1;return GT;}{pos_startpos_end;pos_endpos_start2;return GTE;}{pos_startpos_end;pos_endpos_start2;return EQ;}
! {pos_startpos_end;pos_endpos_start2;return NEQ;}
\ {pos_startpos_end;pos_endpos_start1;return ASSIN;}
\; {pos_startpos_end;pos_endpos_start1;return SEMICOLON;}
\, {pos_startpos_end;pos_endpos_start1;return COMMA;}
\( {pos_startpos_end;pos_endpos_start1;return LPARENTHESE;}
\) {pos_startpos_end;pos_endpos_start1;return RPARENTHESE;}
\[ {pos_startpos_end;pos_endpos_start1;return LBRACKET;}
\] {pos_startpos_end;pos_endpos_start1;return RBRACKET;}
\{ {pos_startpos_end;pos_endpos_start1;return LBRACE;}
\} {pos_startpos_end;pos_endpos_start1;return RBRACE;}
else {pos_startpos_end;pos_endpos_start4;return ELSE;}
if {pos_startpos_end;pos_endpos_start2;return IF;}
int {pos_startpos_end;pos_endpos_start3;return INT;}
float {pos_startpos_end;pos_endpos_start5;return FLOAT;}
return {pos_startpos_end;pos_endpos_start6;return RETURN;}
void {pos_startpos_end;pos_endpos_start4;return VOID;}
while {pos_startpos_end;pos_endpos_start5;return WHILE;}
[a-zA-Z] {pos_startpos_end;pos_endpos_startstrlen(yytext);return IDENTIFIER;}
[0-9] {pos_startpos_end;pos_endpos_startstrlen(yytext);return INTEGER;}
[0-9]*\.[0-9] {pos_startpos_end;pos_endpos_startstrlen(yytext);return FLOATPOINT;}
[] {pos_startpos_end;pos_endpos_start2;return ARRAY;}
[a-zA-Z] {pos_startpos_end;pos_endpos_start1;return LETTER;}
[0-9]\. {pos_startpos_end;pos_endpos_startstrlen(yytext);return FLOATPOINT;}
\n {return EOL;}
\/\*([^\*]|(\*)*[^\*\/])*(\*)*\*\/ {return COMMENT;}{return BLANK;}
\t {return BLANK;}
. {pos_startpos_end;pos_endpos_startstrlen(yytext);return ERROR;}/****请在此补全所有flex的模式与动作 end******/和补充C语言代码
注释可以分为多行所以在识别到注释的时候要进行额外的分析识别到换行符\n的时候要lines1重置pos_end. case COMMENT://STUDENT TO DO{pos_startpos_end;pos_endpos_start2;int i2;while(yytext[i]!* || yytext[i1]!/){ if(yytext[i]\n){lineslines1;pos_end1;}elsepos_endpos_end1;ii1;}pos_endpos_end2;break;}case BLANK://STUDENT TO DO{pos_startpos_end;pos_endpos_start1;break;}case EOL://STUDENT TO DO{lines1;pos_end1;break;}实验结果验证
实验结果
根据实验指导书上的流程输入命令并且得到反馈结果
sunny2004sunny2004-VirtualBox:~/lab1/cminus_compiler-2023-fall$ mkdir build
sunny2004sunny2004-VirtualBox:~/lab1/cminus_compiler-2023-fall$ cd build
sunny2004sunny2004-VirtualBox:~/lab1/cminus_compiler-2023-fall/build$ cmake ../
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c
-- Check for working CXX compiler: /usr/bin/c -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found FLEX: /usr/bin/flex (found version 2.6.4)
-- Found BISON: /usr/bin/bison (found version 3.5.1)
-- Found LLVM 10.0.0
-- Using LLVMConfig.cmake in: /usr/lib/llvm-10/cmake
-- Configuring done
-- Generating done
-- Build files have been written to: /home/sunny2004/lab1/cminus_compiler-2023-fall/build
sunny2004sunny2004-VirtualBox:~/lab1/cminus_compiler-2023-fall/build$ make lexer
[ 20%] [FLEX][lex] Building scanner with flex 2.6.4
lexical_analyzer.l:60: warning, 无法匹配规则
Scanning dependencies of target flex
[ 40%] Building C object src/lexer/CMakeFiles/flex.dir/lex.yy.c.o
lexical_analyzer.l: In function ‘analyzer’:
lexical_analyzer.l:92:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
lexical_analyzer.l: At top level:
/home/sunny2004/lab1/cminus_compiler-2023-fall/build/src/lexer/lex.yy.c:1320:17: warning: ‘yyunput’ defined but not used [-Wunused-function]static void yyunput (int c, char * yy_bp )^
/home/sunny2004/lab1/cminus_compiler-2023-fall/build/src/lexer/lex.yy.c:1363:16: warning: ‘input’ defined but not used [-Wunused-function]static int input (void)^
[ 60%] Linking C static library ../../libflex.a
[ 60%] Built target flex
Scanning dependencies of target lexer
[ 80%] Building C object tests/lab1/CMakeFiles/lexer.dir/main.c.o
[100%] Linking C executable ../../lexer
[100%] Built target lexer
sunny2004sunny2004-VirtualBox:~/lab1/cminus_compiler-2023-fall/build$ cd ..
sunny2004sunny2004-VirtualBox:~/lab1/cminus_compiler-2023-fall$ ./build/lexer
usage: lexer input_file output_file
sunny2004sunny2004-VirtualBox:~/lab1/cminus_compiler-2023-fall$ ./build/lexer ./tests/lab1/testcase/1.cminus out
[START]: Read from: ./tests/lab1/testcase/1.cminus
[END]: Analysis completed.
sunny2004sunny2004-VirtualBox:~/lab1/cminus_compiler-2023-fall$ head -n 5 out
int 280 1 1 4
gcd 285 1 5 8
( 272 1 9 10
int 280 1 10 13
u 285 1 14 15
sunny2004sunny2004-VirtualBox:~/lab1/cminus_compiler-2023-fall$ python3 ./tests/lab1/test_lexer.py
Find 6 files
[START]: Read from: ./tests/lab1/testcase/3.cminus
[END]: Analysis completed.
[START]: Read from: ./tests/lab1/testcase/2.cminus
[END]: Analysis completed.
[START]: Read from: ./tests/lab1/testcase/6.cminus
[END]: Analysis completed.
[START]: Read from: ./tests/lab1/testcase/1.cminus
[END]: Analysis completed.
[START]: Read from: ./tests/lab1/testcase/5.cminus
[END]: Analysis completed.
[START]: Read from: ./tests/lab1/testcase/4.cminus
[END]: Analysis completed.
sunny2004sunny2004-VirtualBox:~/lab1/cminus_compiler-2023-fall$ diff ./tests/lab1/token ./tests/lab1/TA_token
sunny2004sunny2004-VirtualBox:~/lab1/cminus_compiler-2023-fall$ 如果正确的话diff不会返回任何输出如果返回了就出错了
gitee上传
git commit -m lab1-result 如果是第一次提交Ubuntu会告诉你这样 请告诉我您是谁运行 git config --global user.email youexample.com git config --global user.name your Name 来自设置您账号的缺省身份标识。 如果仅在本地仓库设置身份标识则省略 --global参数 这个时候你就运行git config那两行命令之后再运行git commit -m lab1-result就可以了 然后git push 上传工作到gitee仓库这一部分忘记复制了实验指导书里写的很详细就按照那个来就行
实验反馈
学习和巩固了正则表达式 熟悉了gitee的操作 一路磕磕绊绊调试赶在验收之前完成了编译原理好难┭┮﹏┭┮
附录1cmius_token_type
typedef num cminus_token_type{
//运算
ADD 259, 加号
SUB 260, 减号-
MUL 261, 乘号*
DIV 262, 除法/
LT 263, 小于
LTE 264, 小于等于
GT 265, 大于
GTE 266, 大于等于
EQ 267, 相等
NEQ 268, 不相等!
ASSIN 269,单个等于号//符号
SEMICOLON 270, 分号;
COMMA 271, 逗号,
LPARENTHESE 272, 左括号(
RPARENTHESE 273, 右括号)
LBRACKET 274, 左中括号[
RBRACKET 275, 右中括号]
LBRACE 276, 左大括号{
RBRACE 277, 右大括号}//关键字
ELSE 278, else
IF 279, if
INT 280, int
FLOAT 281, float
RETURN 282, return
VOID 283, void
WHILE 284, while//ID和NUM
IDENTIFIER 285, 变量名例如a,low,high
INTEGER 286, 整数例如101
FLOATPOINT 287, 浮点数例如11.1
ARRAY 288, 数组例如[]
LETTER 289, 单个字母例如a,z //others
EOL 290, 换行符\n或\0
COMMENT 291, 注释
BLANK 292, 空格
ERROR 258 错误
} Token;typedef struct{char text[256];int token;int lines;int pos_start;int pos_end;
} Token_Node;