我是靠谱客的博主 舒服铃铛,这篇文章主要介绍Flex&Bison 简单入门Flex&Bison 简单入门,现在分享给大家,希望可以做个参考。

Flex&Bison 简单入门

  • Ref: 《flex与bison(中文版)》

1. Flex&Bison安装

  1. 安装flex
复制代码
1
2
sudo apt install flex
  1. 安装bison
复制代码
1
2
sudo apt install bison
  1. 安装gcc(若缺少)
复制代码
1
2
sudo apt-get install -y build-essential

2. Flex&Bison使用

2.1 第一个flex程序

  1. Flex程序 例1-1: 字数统计 fb1-1.l
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* 正如Unix的wc程序 */ %{ int chars = 0; int words = 0; int lines = 0; %} %% [a-zA-Z]+ { words++; chars += strlen(yytext); } n { chars++; lines++; } . { chars++; } %% main(int argc, char **argv) { yylex(); printf("%8d%8d%8dn", lines, words, chars); }
  1. 运行程序:
复制代码
1
2
3
4
5
6
7
8
$ flex fb1-1.l # 使用flex翻译程序 $ cc lex.yy.c -lfl # 编译程序, 与flex库文件链接 $ ./a.out The boy stood on the burining deck shelling peanuts by the peck ^D 2 12 63
  • 注: ^D是Unix/Linux文件结束符, 此处用于结束输入
  1. 运行截图
    在这里插入图片描述

2.2 Flex&Bison协同工作

1. Flex程序1

例1-3: 一个简单的flex词法分析器 fb1-3.l

  1. 代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
/* 识别出用于计数器的记号并把它们输出 */ %% "+" { printf("PLUSn"); } "-" { printf("MINUSn"); } "*" { printf("TIMESn"); } "/" { printf("DIVIDEn"); } "|" { printf("ABSn"); } [0-9]+ { printf("NUMBER %sn", yytext); } n { printf("NEWLINEn"); } [ t] { } . { printf("Mystery character %sn", yytext); } %%
  1. 运行程序:
    在这里插入图片描述

2. Flex程序2

例1-4: 计算器词法分析器 fb1-4.l

  1. 代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* 识别出用于计数器的记号并把它们输出 */ %{ enum yytokentype { NUMBER = 258, ADD = 259, SUB = 260, MUL = 261, DIV = 262, ABS = 263, EOL = 264 }; int yylval; %} %% "+" { return ADD; } "-" { return SUB; } "*" { return MUL; } "/" { return DIV; } "|" { return ABS; } [0-9]+ { yylval = atoi(yytext); return NUMBER; } n { return EOL; } [ t] { /* 忽略空白字符*/ } . { printf("Mystery character %cn", *yytext); } %% main(int argc, char **argv) { int tok; while(tok = yylex()) { printf("%d", tok); if(tok == NUMBER) printf(" = %dn", yylval); else printf("n"); } }
  1. 运行程序:
    在这里插入图片描述

3. 简单的计算器程序

  1. Bison代码:
    例1-5: 简单的计算器 fb1-5.y
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* 计算器的最简版本 */ %{ #include <stdio.h> %} %token NUMBER %token ADD SUB MUL DIV ABS %token EOL %% calclist: /* 空规则 */ | calclist exp EOL { printf("= %dn", $2); } ; exp: factor { $$ = $1; } | exp ADD factor { $$ = $1 + $3; } | exp SUB factor { $$ = $1 - $3; } ; factor: term { $$ = $1; } | factor MUL term { $$ = $1 * $3; } | factor DIV term { $$ = $1 / $3; } ; term: NUMBER { $$ = $1; } | ABS term { $$ = $2 >= 0? $2 : -$2; } ; %% main(int argc, char **argv) { yyparse(); } yyerror(char* s) { fprintf(stderr, "error: %sn", s); }
  • 注: 书上代码有误, 上已修正
  1. Flex代码:
    例1-6: 计算器词法分析器 fb1-5.l
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
%{ #include "fb1-5.tab.h" %} %% "+" { return ADD; } "-" { return SUB; } "*" { return MUL; } "/" { return DIV; } "|" { return ABS; } [0-9]+ { yylval = atoi(yytext); return NUMBER; } n { return EOL; } [ t] { /* 忽略空白字符*/ } . { printf("Mystery character %cn", *yytext); }
  1. 编译的Makefile:
复制代码
1
2
3
4
5
fb1-5: fb1-5.l fb1-5.y bison -d fb1-5.y # 运行bison, 创建fb1-5.tab.c和 fb1-5.tab.h flex fb1-5.l # 运行flex, 创建lex.yy.c cc -o $@ fb1-5.tab.c lex.yy.c -lfl
  1. 进行编译
复制代码
1
2
$ make
  • 遇到问题: Makefile:2: *** 遗漏分隔符 (null)。 停止。
    解决: Makefile文件中, 缩进是TAB分隔符, 不能是空格
  • 遇到问题:
  • 在这里插入图片描述
    在这里插入图片描述
    解决: 见上#1代码
    编译信息:
    在这里插入图片描述
  1. 运行结果:
    在这里插入图片描述

最后

以上就是舒服铃铛最近收集整理的关于Flex&Bison 简单入门Flex&Bison 简单入门的全部内容,更多相关Flex&Bison内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(71)

评论列表共有 0 条评论

立即
投稿
返回
顶部