0
對於輸入abb
答案被接受,但爲什麼?瞭解爲何輸入被解析器接受
輸入ab
正確答案
輸入aab
拒絕正確的答案
輸入aabb
接受正確的答案
// parser file
%{
#include<stdio.h>
%}
%left '-' '+'
%left '*' '/'
%%
A: S { printf("accepted\n"); exit(1);}
| error
|
;
S : 'a' S 'b'
| 'a' 'b'
|
;
%%
void yyerror(char *s)
{
printf("%s",s);
exit(1);
}
int main()
{
yyparse();
return 1;
}
// lex file
%option noyywrap
%{
#include "first.tab.h"
%}
%%
[a-b] {yylval=(atoi(yytext));
return *yytext;}
\+|\-|\/|\* {return *yytext;}
. { yyerror("Invalid Character");}
\n { return 1;}
%%
謝謝你的回答。如果我想讓abb不應該被接受。我應該怎麼做 –
檢查輸入的結束。也就是說,不要讓你的解析器尋找** a b **,而是** a b DONE **。 –
非常感謝你..現在它工作。 –