Flex(快速词法分析器生成器)

FLEX(快速词法分析器生成器) 是一个工具/计算机程序,用于生成词汇分析器(扫描器或词法分析器),由Vern Paxson于1987年左右在C语言中编写。它与 伯克利Yacc解析器生成器 GNU Bison解析器生成器 .Flex和Bison都比Lex和Yacc更灵活,生成的代码更快。 Bison根据用户提供的输入文件生成解析器。功能 yylex() 当flex提供了 .l文件 解析器希望调用这个yylex()函数从当前/这个令牌流中检索令牌。

null

注: 函数yylex()是运行规则部分的主要flex函数,扩展名(.l)是用于保存程序的扩展名。

在Ubuntu上安装Flex:

sudo apt-get update
sudo apt-get install flex

注: 如果计算机上有一段时间没有运行Update命令,最好先运行它,以便安装较新的版本,因为较旧的版本可能无法与安装的其他软件包一起使用,或者现在可能不存在。

下图描述了Flex的使用方式:

图片[1]-Flex(快速词法分析器生成器)-yiteyi-C++库

第一步: 输入文件描述了要生成的名为lex的词法分析器。我是用莱克斯语写的。lex编译器转换lex。l到C程序,在一个总是被命名为lex的文件中。yy。C 第二步: C编译器编译lex。yy。将c文件转换为名为a.out的可执行文件。 第三步: 输出文件a.out获取一个输入字符流并生成一个令牌流。

课程结构:

在输入文件中,有3个部分:

1.定义部分: 定义部分包含变量、常规定义和清单常量的声明。在定义部分,文本包含在 “%{ %}” 括号。括号中的任何内容都会直接复制到文件中 莱克斯。yy。C

语法:

%{
   // Definitions
%}

2.规则部分: 规则部分包含以下格式的一系列规则: 模式动作 和模式必须是无意的,并且操作开始于{}括号中的同一行。规则部分包含在 “%% %%” .

语法:

%
pattern  action
%

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

图案 它可以与
[0-9] 0到9之间的所有数字
[0+9] 0、+或9
[0, 9] 要么是0,,,要么是9
[0 9] 要么是0,要么是9
[-09] -、0或9
[-0-9] 或0到9之间的所有数字
[0-9]+ 0和9之间的一个或多个数字
[^a] 除了一个
[^A-Z] 除大写字母外的所有其他字符
a{2,4} aa、aaa或aaaa
a{2,} 两次或两次以上的
a{4} 正好是4A,也就是aaaa
. 除了新行以外的任何字符
a* 0次或多次出现
a+ 一次或多次出现
[a-z] 所有小写字母
[a-zA-Z] 有字母吗
w(x | y)z wxz还是wyz

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

基本程序结构:

%{
// Definitions
%}

%
Rules
%

User code section

如何运行该程序: 要运行该程序,首先应将其与扩展名一起保存 .l或。莱克斯 .在终端上运行以下命令以运行程序文件。

第一步: lex文件名。l或lex文件名。lex取决于扩展名文件的保存方式 第二步: gcc莱克斯。yy。C 第三步: /a.out 第4步: 在需要时向程序提供输入

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

示例1:计算字符串中的字符数

C

/*** 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" , yytext);
count++;}
.     { printf ( "%s not a capital letter" , yytext);}
{ 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: ");
// scanf("%s",filename);
// fp = fopen(filename,"r");
// yyin = fp;
yylex();
printf ( "Number of Capital letters "
"in the given input - %d" , count);
return 0;
}


输出:

图片[2]-Flex(快速词法分析器生成器)-yiteyi-C++库

例2:计算输入中的字符数和行数

C

/* Declaring two counters one for number
of lines other for number of characters */
%{
int no_of_lines = 0;
int no_of_chars = 0;
%}
/***rule 1 counts the number of lines,
rule 2 counts the number of characters
and rule 3 specifies when to stop
taking input***/
%
++no_of_lines;
.       ++no_of_chars;
end return 0;
%
/*** User code section***/
int yywrap(){}
int main( int argc, char **argv)
{
yylex();
printf ( "number of lines = %d, number of chars = %d" ,
no_of_lines, no_of_chars );
return 0;
}


输出:

图片[3]-Flex(快速词法分析器生成器)-yiteyi-C++库

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享