English 中文(简体)
C++ 使用 antlr 和 线性指令插入 C++ 代码
原标题:C++ Code insertion using antlr and the line directive

I am using antlr to translate a custom language to C++ code. In this language, user can embed C++ code snippets between $code...$endcode directives, that gets inserted into the translated C++code as-is.

我有一个问题:当代码片断有错误时, 我希望编译者指出源文件, 而不是翻译的 C++ 代码 。

我尝试了使用线性指令如下, 但它不起作用:

"foo.custom_laguage"
1 $code
2 ...some c++ code...
3 $endcode

翻译到

"auto-generated.cpp"
42 #line 2 "foo.custom_language"
43 ...some c++ code...
44 #line __LINE__ __FILE__

这不起作用, 我想是因为 #line 指令会修改 LINE/code> 宏的写法。 我如何才能将行号设置回翻译的 C++ 代码中的 < em> actual 行号? 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 ...

edit: I just found out that there is a way to do this in C# language by using a #line default directive: http://msdn.microsoft.com/en-us/library/34dk387t.aspx But couldn t find anything similar for C++

问题回答

这个问题并不清楚, 但是您正在生成您自己给 源代码线 源源文件 的指令? 抱歉, 我对Antlr不熟悉 。

事实上, 只不过是将 宏指定给自己。

由于预处理器的评估语义, 您无法轻易地将 的数字值指定为宏 。 (您只能从字面上定义一个新宏以映射到宏 , 返回其 < em> 现时 < / em > 值 。 ) 但是为什么您需要这个? 除非 Antlr 自己使用 宏, 您不需要将它恢复到以前的值 。

如果这是一个问题,最直接的解决办法是将原始 C+++ 代码单独放在 中, 包括 文件, 并放弃内嵌嵌入。 为了防止信头文件扩散, 您可以使用类似构建

$code
#define USE_SNIPPET_FOO
#include "snippets.h"
$endcode

$code
#define USE_SNIPPET_BAR
#include "snippets.h"
$endcode

在首页上,有一种倒行逆施的卫兵,

#ifdef USE_SNIPPET_FOO
#undef USE_SNIPPET_FOO
class foo {};

#elif defined USE_SNIPPET_BAR
#undef USE_SNIPPET_BAR
class bar {};

#else
#error no snippet selected
#endif




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?