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

📅  最后修改于: 2021-05-28 02:10:57             🧑  作者: Mango

FLEX(快速词法分析器生成器)是由Vern Paxson在1987年左右用C编写的用于生成词法分析器(扫描器或词法分析器)的工具/计算机程序。它与Berkeley Yacc解析器生成器或GNU Bison解析器生成器一起使用。 Flex和Bison都比Lex和Yacc更加灵活,并产生更快的代码。
Bison从用户提供的输入文件中生成解析器。当函数yylex()提供有.l文件时,它会自动生成该函数,并且解析器希望此yylex()函数从当前/此令牌流中调用以检索令牌。

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

在Ubuntu上安装Flex:

sudo apt-get update
sudo apt-get install flex

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

给定的图像描述了Flex的使用方式:

步骤1:一个输入文件描述了要生成的词法分析器,名为lex.l用lex语言编写。 lex编译器在一个始终名为lex.yy.c的文件中将lex.l转换为C程序。
步骤2: C编译器将lex.yy.c文件编译为名为a.out的可执行文件。
步骤3:输出文件a.out接受输入字符流并产生令牌流。

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

句法:

%{
   // Definitions
%}

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

%%
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语句和其他功能。我们还可以分别编译这些函数并使用词法分析器加载。

基本程序结构:

%{
// Definitions
%}

%%
Rules
%%

User code section

如何运行程序:
要运行该程序,首先应将其保存为扩展名.l或.lex 。在终端上运行以下命令以运行程序文件。
步骤1: lex filename.l或lex filename.lex(取决于扩展文件)保存在
步骤2: gcc lex.yy.c
步骤3: ./ a.out
步骤4:在需要时将输入提供给程序

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

实施例1:计数在一个字符串的字符数

/*** 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;
}

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

/* Decalring 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***/
%%
\n      ++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\n",
       no_of_lines, no_of_chars );
  
return 0;
}

输出:

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。