预处理器和空格规则
我有兴趣在 C++ 块中定义我自己的语言(例如 main),为此目的,我需要使用预处理器及其指令,我的问题依赖于以下规则:
#define INSERT create() ...
被称为类似函数的定义,并且预处理器确实我们定义的内容中不允许有任何空格,
因此,当我使用自己语言的函数时,我必须立即解析以下语句:
INSERT INTO variable_name VALUES(arg_list)
对于不同的两个函数调用,可以说,
insertINTO(variable_name) and valuePARSE(arg_list)
但由于预处理器指令规则不允许我有空格在我的定义中,如何到达variable_name,然后调用我想要实现的第一个函数调用?
任何线索都会有帮助。
PS:我尝试使用 g++ -E file.cpp 来查看预处理器如何工作并将语法调整为有效的 c++ 规则。
I am interested in defining my own language inside a C++ block (lets say for example main) and for that purpose I need to use the preprocessor and its directives my problem relies to the below rule:
#define INSERT create() ...
Is called a function-like definition and preprocessor does not allow any whitespaces in what we define ,
So when I use a function of my own language I got to parse right handy the below statement:
INSERT INTO variable_name VALUES(arg_list)
to a different two function calls lets say
insertINTO(variable_name) and valuePARSE(arg_list)
but since the preprocessor directive rules do not allow me to have whitespaces in my definition how I can reach the variable_name and then make the call to the first function call I want to achieve?
Any clues would be helpful.
PS: I tried using g++ -E file.cpp to see how preprocessor works and to adjust the syntax to be valid c++ rules.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
大多数 C++ 编译器附带的预处理器对于此类任务来说可能太弱了。它从来就不是为这种滥用而设计的。 boost 预处理器库可以帮助您,但我仍然认为您正在走一条单行道。
如果您确实想以这种方式定义您的语言,我建议您要么编写自己的预处理器,要么使用比默认预处理器更强大的预处理器。 这里是一位尝试使用 Python 作为C++ 预处理器。
The preprocessor included with most C++ compilers is probably way too weak for this kind of task. It was never designed for this kind of abuse. The boost preprocessor library could help you on the way, but I still think you're heading down a one-way street here.
If you really want to define your language this way, I suggest you either write your own preprocessor, or use one that is more powerful than the default one. Here is one chap who tried using Python as a C++ preprocessor.
1)
define INSERT create()
不是一个类似函数的宏,它是类似对象的,类似于define INSERT(a, b, c) create(a, b, c)
代码>将是;2) 如果要将
INSERT INTO variable_name VALUES(arg_list)
扩展为insertINTO(variable_name); valuePARSE(arg_list);
你可以这样做:3) 因为你可以看到宏很容易变得丑陋,即使语法中最轻微的错误也会让你花很多时间跟踪它
4) 因为它被标记了C++ 看一下 Boost.Proto 或 Boost.Preprocessor。
1)
define INSERT create()
is not a function-like macro it's object-like, something likedefine INSERT(a, b, c) create(a, b, c)
would be;2) if you want to expand
INSERT INTO variable_name VALUES(arg_list)
intoinsertINTO(variable_name); valuePARSE(arg_list);
you can do something like:3) as you can see macros get ugly pretty easy and even the slightest error in your syntax will have you spend a lot of time tracking it down
4) since it's tagged C++ take a look at Boost.Proto or Boost.Preprocessor.