【发布时间】:2017-09-16 10:43:35
【问题描述】:
我有此代码,但使用 c++ 时该操作无法正常工作。 我试过了,但我没有收到任何错误,你怎么看?有谁知道是哪个错误?
ejem05.l
%x use
%{
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;
#define MAX_USE_NUM 10
YY_BUFFER_STATE use_stack[MAX_USE_NUM];
int use_stack_ptr = 0;
%}
%option c++ noyywrap
%%
<INITIAL,use>[0-9]+ {cout<< "Number found: "<< endl;}
use[[:blank:]]+ {BEGIN(use);}
<use>[[:alpha:][:punct:][:digit:]]+ {
cout << "Nombre de archivo: "<< YYText() << endl;
if ( use_stack_ptr >= MAX_USE_NUM ) {
fprintf( stderr, "Too much files" );
exit(1);
}
use_stack[use_stack_ptr++] = YY_CURRENT_BUFFER;
ifstream in(YYText());
yyin = ∈
if (!yyin) {
cout<< "ERROR file not found" << endl;
exit(1);
}
yy_switch_to_buffer(
yy_create_buffer( yyin, YY_BUF_SIZE ) );
BEGIN(0);
}
<<EOF>> {
if (--use_stack_ptr < 0 ) {
yyterminate();
} else {
yy_delete_buffer( YY_CURRENT_BUFFER );
yy_switch_to_buffer(use_stack[use_stack_ptr] );
}
}
%%
int main(int argc, char** argv) {
++argv, --argc; /* skip over program name */
ifstream in(argv[0]);
yyFlexLexer* lexer = new yyFlexLexer(&in);
while(lexer->yylex()!=0)
;
return 0;
}
我的入口文件是:
entrada.txt
use entrada1.txt
use entrada2.txt
文件内容:
entrada1.txt
45
56
文件内容:
entrada2.txt
34
67
89
我得到了这个结果:
档案名称:entrada1.txt
档案名称:entrada2.txt
我已经使用这些命令编译了这个文件:
flex ejem05.l
c++ lex.yy.cc -o ejem05
./ejem05 entrada.txt
无效的规则是:
<INITIAL,use>[0-9]+ {cout<< "Number found: "<< endl;}
提前致谢。对不起,我的英语不好。
【问题讨论】:
标签: c++ macos flex-lexer lexer