flex也可以从外部文本文件中读入要解析的文本,用yyrestart(f),可以打开多个文件
并循环打开。
flex也可以从外部文本文件中读入要解析的文本,用yyrestart(f),可以打开多个文件
并循环打开。
另外%option noyywrap,使用默认的flex设置(yywrap()在读完一个输入时调用)
/* Companion source code for "flex & bison", published by O'Reilly
* Media, ISBN 978-0-596-15597-1
* Copyright (c) 2009, Taughannock Networks. All rights reserved.
* See the README file for license conditions and contact info.
* $Header: /home/johnl/flnb/code/RCS/fb2-2.l,v 2.1 2009/11/08 02:53:18 johnl Exp $
*/
/* fb2-2 read several files */
%option noyywrap
%{
int chars = 0;
int words = 0;
int lines = 0;
int totchars = 0;
int totwords = 0;
int totlines = 0;
%}
%%
[a-zA-Z]+ { words++; chars += strlen(yytext); }
\n { chars++; lines++; }
. { chars++; }
%%
main(int argc, char **argv)
{
int i;
if(argc < 2) { /* just read stdin */
yylex();
printf("%8d%8d%8d\n", lines, words, chars);
return 0;
}
for(i = 1; i < argc; i++) {
FILE *f = fopen(argv[i], "r");
if(!f) {
perror(argv[1]);
return (1);
}
yyrestart(f);
yylex();
fclose(f);
printf("%8d%8d%8d %s\n", lines, words, chars, argv[i]);
totchars += chars; chars = 0;
totwords += words; words = 0;
totlines += lines; lines = 0;
}
if(argc > 1)
printf("%8d%8d%8d total\n", totlines, totwords, totchars);
return 0;
}
flex & bison学习(二)
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
上一篇:linux安装总结(亲测)
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
【CAD建模号】学习笔记(二):工作区
工作区由[工具提示],[自适应网格],[自适应坐标轴],[参考坐标轴],[绘制的图形]组成。
自适应 坐标轴 工具提示 工作区 CAD建模号 -
flex & bison学习(三)
flex和bison做一个简易计算器
flex bison 编译原理 语法分析 自由软件 -
flex & bison学习(一)
flex & bison的单词统计函数
yacc unix 编译器 软件开发 正则表达式