为什么当v立即在s之后的V位置时,程序上存在语法错误? (lex yacc)
LEX代码 ...
%option noyywrap
%{
#include<stdio.h>
#include"y.tab.h"
%}
%%
I|you|we|they|he|she|it return SUBJECT;
will return AUX1;
did return AUX2;
not return NOT;
eat|drink|run|play return VERB1;
ate|drank|ran|played return VERB2;
\. return DOT;
\? return QUE;
%%
...
YACC代码 ...
%{
#include<stdio.h>
#include<stdlib.h>
void yyerror(char *msg);
%}
%token SUBJECT VERB1 DOT QUE VERB2 AUX1 AUX2 NOT
%%
sentence:
past DOT {printf(" Sentence Approved "); exit(0);}
| pastneg DOT {printf(" Sentence Approved "); exit(0);}
| pastint QUE {printf(" Sentence Approved "); exit(0);}
| fut DOT {printf(" Sentence Approved "); exit(0);}
| futneg DOT {printf(" Sentence Approved "); exit(0);}
| futint QUE {printf(" Sentence Approved "); exit(0);}
;
present:
s v {printf(" Simple Present Tense");}
;
past:
s p {printf(" Simple Past Tense");}
;
pastneg:
s x2 neg v {printf(" Negative Simple Past Tense");}
;
pastint:
x2 present {printf(" Interrogative Simple Past Tense");}
;
fut:
s x1 v {printf(" Simple Future Tense");}
;
futneg:
s x1 neg v {printf(" Negative Simple Future Tense");}
;
futint:
x1 present {printf(" Interrogative Simple Future Tense");}
;
s:
SUBJECT {printf("Subject");}
;
p:
VERB2 {printf("Verb2");}
;
x1:
AUX1 {printf("will");}
;
x2:
AUX2 {printf("did");}
;
v:
VERB1 {printf("Verb");}
;
neg:
NOT {printf("Negative");}
;
%%
void yyerror(char *msg){
fprintf(stderr, "%s\n", msg);
exit(1);
}
int main()
{
yyparse();
return 0;
}
每当我尝试运行过去和futint时,似乎都会出现语法错误。现在也是一个错误,因为在受试者后立即遵循动词(v)。我期望如果我输入“他会吃吗?”或“你跑了吗?”输出将是“接受的辅助主题动词Verb1简单的过去/将来时态句子接受”。但是我目前得到的输出是“辅助主题语法错误”。谁能帮我解决这个问题?
Lex Code
...
%option noyywrap
%{
#include<stdio.h>
#include"y.tab.h"
%}
%%
I|you|we|they|he|she|it return SUBJECT;
will return AUX1;
did return AUX2;
not return NOT;
eat|drink|run|play return VERB1;
ate|drank|ran|played return VERB2;
\. return DOT;
\? return QUE;
%%
...
Yacc code
...
%{
#include<stdio.h>
#include<stdlib.h>
void yyerror(char *msg);
%}
%token SUBJECT VERB1 DOT QUE VERB2 AUX1 AUX2 NOT
%%
sentence:
past DOT {printf(" Sentence Approved "); exit(0);}
| pastneg DOT {printf(" Sentence Approved "); exit(0);}
| pastint QUE {printf(" Sentence Approved "); exit(0);}
| fut DOT {printf(" Sentence Approved "); exit(0);}
| futneg DOT {printf(" Sentence Approved "); exit(0);}
| futint QUE {printf(" Sentence Approved "); exit(0);}
;
present:
s v {printf(" Simple Present Tense");}
;
past:
s p {printf(" Simple Past Tense");}
;
pastneg:
s x2 neg v {printf(" Negative Simple Past Tense");}
;
pastint:
x2 present {printf(" Interrogative Simple Past Tense");}
;
fut:
s x1 v {printf(" Simple Future Tense");}
;
futneg:
s x1 neg v {printf(" Negative Simple Future Tense");}
;
futint:
x1 present {printf(" Interrogative Simple Future Tense");}
;
s:
SUBJECT {printf("Subject");}
;
p:
VERB2 {printf("Verb2");}
;
x1:
AUX1 {printf("will");}
;
x2:
AUX2 {printf("did");}
;
v:
VERB1 {printf("Verb");}
;
neg:
NOT {printf("Negative");}
;
%%
void yyerror(char *msg){
fprintf(stderr, "%s\n", msg);
exit(1);
}
int main()
{
yyparse();
return 0;
}
There seems to be a syntax error whenever I try to run the pastint and futint. The present is also an error since VERB1 (v) is followed immediately after SUBJECT (s). I was expecting that if I input "Will he eat?" or "Did you run?" the output would be "Auxiliary Subject Verb1 Simple Past/Future Tense Sentence Accepted". But the output that I got at the moment is "Auxiliary subject Syntax error". Can anyone help me solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论