flex和bison无法检测到错误

时间:2016-05-02 02:07:50

标签: c bison flex-lexer

flex和bison未正确检测到错误。它应该能够检测到关闭支架但没有开口支架。谁能发现我做错了什么?

这是我的野牛档案:

%{
#include <stdio.h>
#include <stdlib.h>
extern yylex();
extern yytext[];
%}
%start   start
%token   num att rel Lbracket Rbracket comma
%%
start                   : one_relation_expression {printf("ACCEPT\n"); exit(0);} ;
one_relation_expression : projection {printf("16 in .y\n");};
term                    : relation {printf("20 in .y\n");};                                                                                 
projection              : term {printf("23\n");}                                     
                        | term Lbracket attribute_commalist Rbracket {printf("24\n");};
attribute_commalist     : attribute {printf("25 in .y\n");}                              
                        | attribute comma attribute_commalist {printf("26 in .y\n");};
attribute               : att  {printf("34 in .y\n");};
relation                : rel  {printf("35 in .y\n");};
%%
yyerror()
{
   printf("REJECT\n");
   exit(0);
}
yywrap()
{
}
 main()
{
   yyparse();
   //printf("ACCEPT\n");
}

这是我的flex文件:

%{
#include "test.tab.h"
#include <stdio.h>
#include <stdlib.h>
extern int yylval;
%}
    /* patterns */
Delimiter    [ \t]
WhiteSpace   {Delimiter}+
Letter       [A-Za-z]
Digit        [0-9]
%%
    /* actions */
[0-9]       {
            yylval = atoi(yytext);
            return(num);
            }
"["         {return(Lbracket);}
"]"         {return(Rbracket);}
","         {return(comma);}
CNO         {return(att);}
CUST        {return(rel);}
.  ;
%%
/*int yywrap(void)
{
    return 1;
}*/

这是我的测试文件:

CUST CNO]

它应该能够检测到关闭支架但没有开口支架。

输出:

$ ./p5 < a5.txt
35 in .y
20 in .y
23
16 in .y
ACCEPT

0 个答案:

没有答案