Lex和Yacc/Flex和bison语法错误

Lex和Yacc/Flex和bison语法错误,bison,flex-lexer,yacc,lex,Bison,Flex Lexer,Yacc,Lex,我最近发现了lex和yacc(以及flex和bison),当我试图告诉程序是否给出了一个句子时,出现了一个错误 以下是.lex文件: %{ #include <stdio.h> #include "1.tab.h" %} %% tweety|sylvester return NP; a|the return AR; cat|bird return NC; run|fly return VI; eat|hate return VT; "." retur

我最近发现了lex和yacc(以及flex和bison),当我试图告诉程序是否给出了一个句子时,出现了一个错误

以下是.lex文件:

%{
        #include <stdio.h>
        #include "1.tab.h"
%}

%%
tweety|sylvester return NP;
a|the return AR;
cat|bird return NC;
run|fly return VI;
eat|hate return VT;
"." return POINT;
.|\n
%%
当我试着运行这个程序时,它在第一次输入时运行良好,但是它经常给我一个错误。下面是一个例子:

./test
tweety fly.
Sentence found !
tweety hate sylvester.
error: syntax error
你知道吗


谢谢。

您的语法只允许输入一个句子。您需要新的第一条规则:

P : S | P S ;
其中p是新的目标符号(“程序”、“段落”等,随你喜欢)

./test
tweety fly.
Sentence found !
tweety hate sylvester.
error: syntax error
P : S | P S ;