gcc 如何知道源代码来自哪里?

发布于 2024-12-27 22:26:32 字数 468 浏览 4 评论 0原文

今天我正在玩 Flex 和 Bison,一些奇怪的东西引起了我的注意。

localhost:c math4tots$ lex c.l
localhost:c math4tots$ yacc -d c.y
localhost:c math4tots$ rm c.l c.y
localhost:c math4tots$ gcc c.c lex.yy.c y.tab.c
c.y: In function ‘opr’:
c.y:120: error: ‘nodeType’ has no member named ‘oper’

我只通过了 cc lex.yy.c 和 y.tab.c (ch 和 y.tab.h 也作为标头包含在内),但不知何故 gcc 知道 cl 和 cy 事实上,即使在我删除之后cl和cy,gcc知道cy中代码中的错误在哪里。它是如何做到的?

我觉得我在过去使用过的一些不同工具中看到过类似的东西,但我不记得它们是什么。

I was playing with flex and bison today, and something kind of eerie came to my attention.

localhost:c math4tots$ lex c.l
localhost:c math4tots$ yacc -d c.y
localhost:c math4tots$ rm c.l c.y
localhost:c math4tots$ gcc c.c lex.yy.c y.tab.c
c.y: In function ‘opr’:
c.y:120: error: ‘nodeType’ has no member named ‘oper’

I've only passed c.c lex.yy.c, and y.tab.c (c.h and y.tab.h are also included as headers), but somehow gcc knows about c.l and c.y. In fact, even after I've deleted c.l and c.y, gcc knows where in c.y the error in the code was. How does it do that?

I feel like I've seen something similar for some different tools I've used in the past, but I can't exactly remember what they were.

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

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

发布评论

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

评论(2

远山浅 2025-01-03 22:26:32

您将在文件中找到一些标记,大致如下:

#line 75 "c.y"

正是用于此目的,能够针对生成您正在编译的实际文件的原始文件报告错误消息或警告。

基本上,您不必转到 cc 文件并尝试找出必须修复的 cy 文件中的等效行。

从这个意义上说,C 文件与目标文件没有什么不同。您不必关心其中的内容,因为它是自动生成的。如果出现问题,您希望能够直接返回到 cy 中的正确行并从源头修复它。

You'll find some markers in your files along the lines of:

#line 75 "c.y"

which is used for exactly this purpose, the ability to report error messages or warnings against the original file that produced the actual files you're compiling.

It's basically so that you don't have to go to the c.c file and try to figure out the equivalent line in the c.y file that you have to fix.

In this sense, the C file is no different to an object file. You don't care what's in it since it's automatically generated. If there's a problem with it, you want to be able to go directly back to the right line in c.y and fix it at the source.

踏雪无痕 2025-01-03 22:26:32

生成的 C 文件有一些 行控制预处理器指令(也生成),例如

 #line 119 "c.y"

然后编译器认为该指令之后的下一行位于文件 cy 的第 119 行,并相应地计算以下位置。

The generated C file has some line control preprocessor directives (also generated) like

 #line 119 "c.y"

and then the compiler believes that the next line after this directive is in file c.y at line 119 and compute following positions accordingly.

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