Lex & Flex 简介

Lex是lexical compiler的缩写,是Unix环境下非常著名的工具, Lex (最早是埃里克·施密特和 Mike Lesk 制作)是许多 UNIX 系统的标准词法分析器(lexical analyzer)产生程式,而且这个工具所作的行为被详列为 POSIX 标准的一部分。

Lex的基本工作原理为:由正则表达式生成NFA,将NFA变换成DFA,DFA经化简后,模拟生成词法分析器。

Lex 主要功能是生成一个词法分析器(scanner)的 C 源码,描述规则采用正则表达式(regular expression)。描述词法分析器的文件 *.l 经过lex编译后,生成一个lex.yy.c 的文件,然后由 C 编译器编译生成一个词法分析器。词法分析器,简言之,就是将输入的各种符号,转化成相应的标识符(token),转化后的标识符很容易被后续阶段处理,如Yacc 或 Bison,过程如图 :

在linux系统上,我们最常用的是Flex,Flex (fast lexical analyser generator) 是 Lex 的另一个替代品。它经常和自由软件 Bison 语法分析器生成器 一起使用。Flex 最初由 Vern Paxson 于 1987 年用C语言写成。Flex手册里对 Flex 描述如下:

FLEX (fast lexical analyzer generator) is a tool/computer program for generating lexical analyzers (scanners or lexers) written by Vern Paxson in C around 1987. It is used together with Berkeley Yacc parser generator or GNU Bison parser generator. Flex and Bison both are more flexible than Lex and Yacc and produces faster code.

Bison produces parser from the input file provided by the user. The function yylex() is automatically generated by the flex when it is provided with a .l file and this yylex() function is expected by parser to call to retrieve tokens from current/this token stream.

Lex & Flex 输入文件格式

Flex 的输入文件包含了三部分,分别是定义区(definitions)、规则区(rules)和用户代码区(user code)并且由单独占一行的两个连续的百分号("%%")分隔开:

definitions

%%

rules

%%

user code

下面对 Flex 输入文件的三个部分做出解释:

1 定义部分:定义部分包含变量的声明,正则定义,清单常量。在定义部分,文本放在“%{%}”括号中。用花括号括起来的所有内容都会直接复制到lex.yy.c文件中。

语法

%{

// Definitions

%}

2 规则部分:rules部分包含一系列规则,格式为:pattern action,并且模式 pattern 位于行首不能缩进,action 也应该起始于同一行,规则部分包含在“%% %%”中。

语法:

%%

pattern action

%%

下表显示了一些模式匹配。

Pattern

It can match with

[0-9]

all the digits between 0 and 9

[0+9]

either 0, + or 9

[0, 9]

either 0, ‘, ‘ or 9

[0 9]

either 0, ‘ ‘ or 9

[-09]

either -, 0 or 9

[-0-9]

either – or all digit between 0 and 9

[0-9]+

one or more digit between 0 and 9

[^a]

all the other characters except a

[^A-Z]

all the other characters except the upper case letters

a{2, 4}

either aa, aaa or aaaa

a{2, }

two or more occurrences of a

a{4}

exactly 4 a’s i.e, aaaa

.

any character except newline

a*

0 or more occurrences of a

a+

1 or more occurrences of a

[a-z]

all lower case letters

[a-zA-Z]

any alphabetic letter

w(x \

y)z

wxz or wyz

3 用户代码部分:这部分包含C语句和其他功能。我们还可以分别编译这些函数并使用词法分析器加载。

如何运行程序:

要运行该程序,首先应将其保存为扩展名.l或.lex。在终端上运行以下命令以运行程序文件。

步骤1:lex filename.l或lex filename.lex取决于扩展文件

步骤2:gcc lex.yy.c

步骤3:./ a.out

步骤4:在需要时将输入提供给程序

注意:按Ctrl + D或使用某些规则停止接受用户输入。请查看以下程序的输出图像以清除是否有疑问以运行程序。

简单例子

计算字符串中的字符数

/*** Definition Section has one variable

which can be accessed inside yylex()

and main() ***/

%{

int count = 0;

%}

/*** Rule Section has three rules, first rule

matches with capital letters, second rule

matches with any character except newline and

third rule does not take input after the enter***/

%%

[A-Z] {printf("%s capital letter\n", yytext);

count++;}

. {printf("%s not a capital letter\n", yytext);}

\n {return 0;}

%%

/*** Code Section prints the number of

capital letter present in the given input***/

int yywrap(){}

int main(){

// Explanation:

// yywrap() - wraps the above rule section

/* yyin - takes the file pointer

which contains the input*/

/* yylex() - this is the main flex function

which runs the Rule Section*/

// yytext is the text in the buffer

// Uncomment the lines below

// to take input from file

// FILE *fp;

// char filename[50];

// printf("Enter the filename: \n");

// scanf("%s",filename);

// fp = fopen(filename,"r");

// yyin = fp;

yylex();

printf("\nNumber of Captial letters "

"in the given input - %d\n", count);

return 0;

}

运行:

查找读取文本所有整数

%{

/*

用flex写一个查找读取文本所有整数的程序

*/

int count = 0;

%}

%%

[+-]?[0-9]+ { count++; printf("%s\n", yytext); } /* Print integers */

\n {} /* newline */

. {} /* For others, do nothing */

%%

void main() {

yylex();

printf("Number count is %d\n", count);

}

int yywrap() {

return 1;

}

运行:

参考文章:

linux终端lex程序运行,lex的简单使用相关推荐

  1. 如何查看Linux系统下程序运行时使用的库?

    Linux系统下程序运行会实时的用到相关动态库,某些场景下,比如需要裁剪不必要的动态库时,就需要查看哪些动态库被用到了. 以运行VLC为例. VLC开始运行后,首先查看vlc的PID,比如这次查到的V ...

  2. Linux终端进程后台运行与前后台切换

    1 Linux终端常用命令 pwd 显示当前工作路径 cd 目标目录 改变路径,'.'表示当前目录,'-'表示上一级目录 举例: cd ../ cd A/B/C/ ls list的缩写,列出当前路径下 ...

  3. linux 信号sigabrt,程序运行产生SIGABRT信号的原因---转

    程序运行产生SIGABRT信号的原因 发表于2017/2/8 14:53:31  2444人阅读 分类: C++ Linux 编程基础 原文网址: http://m.blog.csdn.net/Sea ...

  4. Linux终端下后台运行程序被Stopped的原因以及解决

    一.问题以及原因 为设备写了一个测试WIFI连通性的C++小程序,登录设备后在终端运行程序,使其后台执行: ./AP_Connect & 终端打印此信息:[1]+  已停止           ...

  5. windows使用linux终端模拟器,程序员必备之终端模拟器,让你的终端世界多一抹“颜色”...

    作者:HelloGitHub-ChungZH 当你逐渐厌倦了 cmd 的枯燥黑屏.powershell 的单调蓝底,可以来尝试一下使用可定制性更高的终端模拟器.下面是 HelloGitHub 从 Gi ...

  6. Linux 启动jar项目相关命令(解决关闭Linux终端,程序自动停止问题)

    Linux 启动jar项目相关命令 序言 启动jar包命令相关问题及解决办法 问题描述 解决办法 结语 序言 开发中在服务器上部署项目避免不了的,无论是测试还是开发都会遇到要重启项目及部署项目,所以部 ...

  7. linux线程超过1024报错,Linux定位c++程序运行异常的经历《实操》

    对于c++程序来说,以segment fault为代表的程序异常行为千奇百怪,没有一套比较丰富的工具集去对付他们,在处理实际问题时就会显得捉襟见肘.本文列举几种程序异常的定位方法. 子线程创建不出来 ...

  8. linux上原生程序运行QQ,微信,百度网盘,王者荣耀,cf

    在 Linux 上运行王者荣耀游戏的截图: 运行微信的截图: 与现有的各种Android模拟器不同,xDroid没有使用虚拟机,而是使Android应用以Linux原生程序的形式运行,性能媲美标准的L ...

  9. 小米linux终端,Android里运行ubuntu的方法(以小米2S为例)

    8种机械键盘轴体对比 本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选? 1,事先准备: (1)一部已经root过的安卓手机(实例:小米2S,开发板系统) (2)一个叫"终端模拟器&q ...

最新文章

  1. web.config配置
  2. Qt三种方式实现FTP上传功能
  3. win10快速运行vue项目跑起来 - 方法篇
  4. Codeforces-741A-Arpa's loud Owf and Mehrdad's evil plan(找有向图环及最大公倍数计算)
  5. python标准库sys_Python——标准库 Sys模块
  6. IOCP实现聊天服务
  7. gvim 命令行粘贴_vim基本命令之剪切复制粘贴替换
  8. linux查看445端口状态,linux和Windows如何查看端口占用情况
  9. A Neural Algorithm of Artistic Style : Neural Style Transfer with Eager Executon
  10. HDU4699Editor
  11. python pyecharts 数据可视化 饼状图绘制
  12. 塑源码是什么_源码是什么意思啊
  13. RAW图像详解及使用Python读取raw格式图像并显示
  14. Android JNI --函数调用大全
  15. java 判断文件损坏_Java校验文件是否损坏
  16. 农妇守护瘫痪丈夫27年 单独抚育女儿撑起家庭
  17. 【RPA学习天地:版本解读】艺赛旗iS-RPA2021.2版本亮点
  18. 自己动手,丰衣足食 | 记录对OBS-Studio主题的简单修改
  19. 耳机插入电脑为啥没声音,解决方案
  20. 揭秘深度强化学习-6状态空间过大之神经网络来帮忙

热门文章

  1. MongoDB与Spring Data项目
  2. Linux 命令之 passwd -- 修改用户账号密码
  3. Linux 命令之 make -- GNU的工程化编译工具
  4. 3模型大小_Github推荐一个国内牛人开发的超轻量级通用人脸检测模型
  5. 用户登陆_华为路由器AAA用户密码登陆你了解吗?
  6. mysql explain 为空_车祸现场!我的MySQL千万级数据表选错索引了!
  7. python条形堆积图_python – 使用DataFrame.plot显示堆积条形图中...
  8. matlab 传输速率,处理速率转换 - MATLAB Simulink - MathWorks 中国
  9. html语义化有哪些优点,语义化的HTML结构到底有什么好处?
  10. background 互联网图片_cssbackground-image和layer-background-image的区别