使用flex和bison的问题

发布于 2024-11-14 08:52:00 字数 400 浏览 8 评论 0原文

有人研究过 Flex 和 Bison 吗?

我有一个例子。我运行它,但它显示没有野牛,

例如:在 .l 文件的 Flex 中,我定义了 id 并为其放置 printf{"id"} 。

在野牛中,我定义了这样的内容:

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

但是当我输入 id 时运行 main.exe 时,它​​只显示 id(不应该打印 id-list::=id 吗???) 我知道我解释得不好,如果我遗漏了什么,请告诉我或让我输入代码。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无所的.畏惧 2024-11-21 08:52:00

有一些事情可能是错误的。第一个错误是printf。 Printf 缺少 %s,因此它只会打印您的文本。这是我

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

C 文件中执行此操作的方法

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;
}

在lex 文件中的

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

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; } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文