开发者

why calles function yyerror() in sample programm

开发者 https://www.devze.com 2023-01-21 06:34 出处:网络
curs.l : %{ #include <stdlib.h> #include \"tree.c\" #include \"yycurs.h\" %} L [a-zA-Z_] D [0-9] D4 [0-3]
curs.l :
%{
#include <stdlib.h>
#include "tree.c"
#include "yycurs.h"
%}

L [a-zA-Z_]

D [0-9]

D4 [0-3]

IDENTIFIER ({L})({L}|{D})*

INT4 {D4}+'q'

INT {D}+

%%

{IDENTIFIER} {return VARIABLE;}

%%

int yywrap(void){
   return 0;
}



curs.y:
%{
#include stdio.h


void yyerror(char*);
int yylex(void);
%}

%token VARIABLE INTEGER

%%

var: VARIABLE {printf($1);};

%%

void yyerror(char *s){
   fprintf(stderr, "11\n");
   fprintf(stderr, "%s\n", s);
}

int main(void){
   yyparse();
   return 0;
}

when i run my com开发者_JAVA百科piled progrum, i have such result:

./curs 

ff  //I introduced

   //result

ff //I introduced

11 //result

syntax error //result

evgeniy@evgeniy-desktop:~/documents/compilers$ 

Can anybody explain me, why there appears 'syntax error'? Thanks in advance.


Your grammar defiles that a valid file consists of exactly one VARIABLE. To have more then one, you need to introduce a recursive rule.

%start vars

%%

var: VARIABLE {printf($1);};

vars: var
    | vars var;

%%
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号