Flex&Bison: Using flex on OSX 在mac上使用Flex

来源:互联网 发布:脂本 知乎 编辑:程序博客网 时间:2024/06/07 01:58

一个简单的示例:文件名fb1-1.l (一杠一 后缀字母L)

%{int chars = 0;int words = 0;int lines = 0;%}%%[a-zA-Z]+ {words++; chars += strlen(yytext);}\n  { chars++; lines++; }.  { chars++; }%%int main(int argc, char **argv){   yylex();   printf("%8d%8d%8d\n", lines, words, chars);}

在终端输入以下命令:

flex fb1-1.l  //generates a new file named "lex.yy.c"
/*this action would generate a file named a.out which would be used for reading inputs and print outputs*/cc lex.yy.c -ll  //如果不是在mac上,是用参数-lfl
./a.out  //run the file and type in inputs from your command line

在终端输入一段文字,按control 加 D结束即可看到输出

0 0
原创粉丝点击