做一个公司网站一般多少钱,网站后台模板论坛,怎么看一个网站做没做竞价,网络营销方式的优点Lex Flex 简介Lex是lexical compiler的缩写#xff0c;是Unix环境下非常著名的工具#xff0c; Lex (最早是埃里克施密特和 Mike Lesk 制作)是许多 UNIX 系统的标准词法分析器(lexical analyzer)产生程式#xff0c;而且这个工具所作的行为被详列为 POSIX 标准的一部分…Lex Flex 简介Lex是lexical compiler的缩写是Unix环境下非常著名的工具 Lex (最早是埃里克·施密特和 Mike Lesk 制作)是许多 UNIX 系统的标准词法分析器(lexical analyzer)产生程式而且这个工具所作的行为被详列为 POSIX 标准的一部分。Lex的基本工作原理为由正则表达式生成NFA将NFA变换成DFADFA经化简后模拟生成词法分析器。Lex 主要功能是生成一个词法分析器(scanner)的 C 源码,描述规则采用正则表达式(regular expression)。描述词法分析器的文件 *.l 经过lex编译后生成一个lex.yy.c 的文件然后由 C 编译器编译生成一个词法分析器。词法分析器简言之就是将输入的各种符号转化成相应的标识符(token)转化后的标识符很容易被后续阶段处理如Yacc 或 Bison过程如图 在linux系统上我们最常用的是FlexFlex (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%%下表显示了一些模式匹配。PatternIt can match with[0-9]all the digits between 0 and 9[09]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 lettersa{2, 4}either aa, aaa or aaaaa{2, }two or more occurrences of aa{4}exactly 4 a’s i.e, aaaa.any character except newlinea*0 or more occurrences of aa1 or more occurrences of a[a-z]all lower case letters[a-zA-Z]any alphabetic letterw(x \y)zwxz or wyz3 用户代码部分这部分包含C语句和其他功能。我们还可以分别编译这些函数并使用词法分析器加载。如何运行程序要运行该程序首先应将其保存为扩展名.l或.lex。在终端上运行以下命令以运行程序文件。步骤1lex filename.l或lex filename.lex取决于扩展文件步骤2gcc lex.yy.c步骤3./ a.out步骤4在需要时将输入提供给程序注意按Ctrl D或使用某些规则停止接受用户输入。请查看以下程序的输出图像以清除是否有疑问以运行程序。简单例子计算字符串中的字符数/*** Definition Section has one variablewhich can be accessed inside yylex()and main() ***/%{int count 0;%}/*** Rule Section has three rules, first rulematches with capital letters, second rulematches with any character except newline andthird 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 ofcapital letter present in the given input***/int yywrap(){}int main(){// Explanation:// yywrap() - wraps the above rule section/* yyin - takes the file pointerwhich contains the input*//* yylex() - this is the main flex functionwhich 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;}运行参考文章