为什么当v立即在s之后的V位置时,程序上存在语法错误? (lex yacc)

发布于 2025-02-05 08:09:29 字数 2147 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文