lex简单程序2则

程序1(test1.l):

%{
#include <stdio.h>
%}

%%
begin  printf("Started\n");
hello  printf("Hello yourself!\n");
thanks printf("Your welcome\n");
end    printf("Stopped\n");
%%

在linux中:

第一步:flex test1.l

第二步:gcc -o test1 lex.yy.c -lfl

第三步:运行./test1  输入begin 程序将在屏幕上打印Started.

程序2:(test2.l)

%{
#include <stdio.h>
int num_lines = 0, num_chars = 0;
%}
%%
\n      ++num_lines;
.       ++num_chars;
%%
main() {
 yylex();
 printf("lines = %d, chars = %d\n", num_lines, num_chars);
}
 

第一步:flex test2.l

第二步:gcc -o test2 lex.yy.c -lfl

第三步:运行./test2 

在屏幕上输入任意字符,任意行.结束输入按ctrl+d

将在屏幕上打印您输出了多少字符,多少行!

原文地址:https://www.cnblogs.com/hnrainll/p/2014162.html