开发者

problem with using flex and bison

开发者 https://www.devze.com 2023-03-10 17:58 出处:网络
did anybody worked on flex and bison?? i have an example of them.i run it but it shows without bison, for example: in flex in .l file i defined id and put printf{\"id\"} for it.

did anybody worked on flex and bison??

i have an example of them.i run it but it shows without bison,

for example: in flex in .l file i defined id and put printf{"id"} for it.

and in bison i defined something like this:

  id_list       :   ID          {printf("id-list::=id\n");}
    |   id_list   ','   ID      {printf("id-list::=<id-list>,id\n");}

but when i run main.exe when i entered id it just show id(shouldnt it print id-list::=id????) i know i didnt explain well,if im missing something please say me or say me to put codes开发者_JAVA技巧.


Theres a few things that could be wrong. First which is wrong is the printf. Printf is missing %s so it will only print your text. Heres how i'd do it

id_list:
                  ID        { $$ = id_list( 0, $1); }
    | id_list ',' ID        { $$ = id_list($1, $2); }

in your C file

IdList* id_list(IdList*p, ID*pp) {
    //pp == yylval in this case a c-string from strdup
    printf("id_list: %X %s", p, pp); //i rather set a breakpoint instead
    if(p==0) p=new IdList;
    p->deque.push_back(pp);
    return p;
}

in the lex file

//makes a copy bc yytext will be reused for other things
....        { yylval=strdup(yytext); return ID; } 
0

精彩评论

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