Flex/Bison-启动条件
你好,我有以下代码Flex/Bison-启动条件,bison,flex-lexer,Bison,Flex Lexer,你好,我有以下代码 %s expectWord %% <expectWord>"and"+{word} { BEGIN( INITIAL );} <expectWord>["and"]* { /* Skip */;} "and" { BEGIN( expectWordAfterAND ); return AND; } %s预期单词 %% 和“+{word}{BEGIN(首字母);}” [“和”]*{/*跳过*
%s expectWord
%%
<expectWord>"and"+{word} { BEGIN( INITIAL );}
<expectWord>["and"]* { /* Skip */;}
"and" { BEGIN( expectWordAfterAND ); return AND; }
%s预期单词
%%
和“+{word}{BEGIN(首字母);}”
[“和”]*{/*跳过*/;}
和{BEGIN(expectWordAfterAND);return and;}
代码应该检查用户是否输入了“and”,如果输入了,那么如果用户输入了多个and,它们将被忽略,当最终有一个单词时,该单词将被返回。
所以,若用户输入:a和b,lexer应该返回:a和b。因此,只有一个和将被返回
现在,它又回来了:a b。
如何修复此代码
谢谢这里有一种方法可以实现您的目标:
%{
#include <iostream>
using namespace std;
#define YY_DECL extern "C" int yylex()
%}
WORD [:alnum:]+
%x SPACE
%x AND
%%
WORD ECHO;
^[ ]*and[ ] BEGIN(AND);
[ ]* { cout << " "; BEGIN(SPACE); }
<SPACE>{
and[ ] ECHO; BEGIN(AND);
.|\n ECHO; BEGIN(INITIAL);
}
<AND>{
and$
([ ]*and[ ])*
.|\n ECHO; BEGIN(INITIAL);
}
%%
main()
{
// lex through the input:
yylex();
}
input> 'a and and b'
output> 'a and b'
input> 'a and and and b'
output> 'a and b'
input> 'a b and and c'
output> 'a b and c'
input> 'and and b c and a'
output> 'b c and a'