仅当找到EOF时,使bison减少到开始符号

仅当找到EOF时,使bison减少到开始符号,bison,yacc,lex,Bison,Yacc,Lex,我正在使用Bison和Flex。 我的Yacc输入文件中有以下规则: program : PROGRAM m2 declarations m0 block {cout << "Success\n"} ; program:program m2声明m0块{cout下面是一个这样做的示例: 首先是lex文件: %{ #include "grammar.tab.h" %} %x REALLYEND %option noinput nounput %% "END"

我正在使用Bison和Flex。 我的Yacc输入文件中有以下规则:

program     : PROGRAM m2 declarations m0 block {cout << "Success\n"} ;

program:program m2声明m0块{cout下面是一个这样做的示例:

首先是lex文件:

%{
#include "grammar.tab.h"
%}
%x REALLYEND
%option noinput nounput
%%
"END"                   { return END; }
.                       { return TOK; }
<INITIAL><<EOF>>        { BEGIN(REALLYEND); return EOP; }
<REALLYEND><<EOF>>      { return 0; }
%%

int yywrap(void)
{
        return 1;
}

标题中的问题有点误导,因为如果找到
EOF
,则
bison
始终只会减少内部开始符号,这就是内部文件结束标记的用途。不同之处在于,您希望语法中的操作打印
success
,仅在
EOF
完成后执行en找到了,而不是之前。也就是说,减少您的开始符号。

如果有疑问,请使用开始条件来制作您需要的令牌。非常好。
%{
#include "grammar.tab.h"
%}
%x REALLYEND
%option noinput nounput
%%
"END"                   { return END; }
.                       { return TOK; }
<INITIAL><<EOF>>        { BEGIN(REALLYEND); return EOP; }
<REALLYEND><<EOF>>      { return 0; }
%%

int yywrap(void)
{
        return 1;
}
%token END EOP TOK
%{
#include <stdio.h>
void yyerror(char * msg)
{
        fprintf(stderr, "%s\n", msg);
}
extern int yylex(void);
%}
%%
prog : END EOP { printf ("ok\n"); };
%%

int main(void)
{
        return yyparse();
}