相关疑难解决方法(0)

使用 antlr 和 line 指令插入 C++ 代码

我正在使用 antlr 将自定义语言转换为 C++ 代码。在这种语言中,用户可以在$code...$endcode指令之间嵌入 C++ 代码片段,按原样插入到翻译后的 C++ 代码中。

我有以下问题:当代码片段中有错误时,我希望编译器指向源文件而不是翻译后的 C++ 代码。

我尝试按如下方式使用行指令,但没有奏效:

"foo.custom_laguage"
1 $code
2 ...some c++ code...
3 $endcode
Run Code Online (Sandbox Code Playgroud)

被翻译成

"auto-generated.cpp"
42 #line 2 "foo.custom_language"
43 ...some c++ code...
44 #line __LINE__ __FILE__
Run Code Online (Sandbox Code Playgroud)

这不起作用,我认为是因为 #line 指令会修改__LINE__宏稍后写入的内容。如何将行号设置回已翻译的 C++ 代码中的实际行号?antlr 是如何做到这一点的?

这是我希望自动生成的代码的样子:

"auto-generated.cpp"
42 #line 2 "foo.custom_language"
43 ...some c++ code...
44 #line 44 "auto-generated.cpp"  //actual line in this file
45 ..some more C++ code ...
Run Code Online (Sandbox Code Playgroud)

编辑: 我刚刚发现有一种方法可以通过使用#line default指令在 C# 语言中做到这一点: …

c++ translation antlr

5
推荐指数
1
解决办法
581
查看次数

标签 统计

antlr ×1

c++ ×1

translation ×1