用Python生成代码? [英] Using Python to generate code?

查看:80
本文介绍了用Python生成代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的:


我需要你就此事提出建议。我正在开发一个程序,其中
需要一些System-C代码,并生成一些其他的System-C

代码。 (System-C代码只是带有一些特殊库的C ++,因此

你可以把它当作C ++)。


现在,编写生成器程序在C ++中。而且我觉得不太适合使用C ++。我觉得C ++太过分了。因为,我需要

生成一些代码,因此在程序中有很多东西

这样:


printf(out," for(%s = 1,%s<%s,%s<%s)",varName,varName,

varName1,varname,varName2);


如果我有超过20行,那就太乱了。


所以我的问题是:

1. Python可以帮助解决这个问题吗?

2. Python有解析器吗?在我看来,现在没有标准的用于Python的解析器。如果是这样的话,我可以用现有的杯子和lex接口Python

吗?


非常感谢!

Tuan Anh

P / S:也因为它不是大代码生成器,它只需要生成一些简单的代码。因此,使用一些抽象语法树

库也太过分了。

Dear all:

I need your advice on this matter. I am working on a program which
takes some pieces of System-C code in and generate some other System-C
code. (System-C code is just C++ with some special libraries, hence
you can consider it as C++).

Right now, the generator program is written in C++. And I feel not so
comfortable with C++. I feel C++ is an overkill. Because, I need to
generate some code, hence in the program there are a lot of something
like this:

printf(out, "for (%s = 1, %s < %s, %s < %s )", varName, varName,
varName1, varname, varName2);

It is just too messy if I have more than 20 lines like this.

So my question is:
1. Can Python help to solve this issue?
2. Does Python has a parser? It seems to me that there is no-standard
Parser for Python right now. If it is the case, can I interface Python
with existing cup and lex?

Many thanks!
Tuan Anh
P/S: Also because it is NO big code generator, it only needs to
generate some simple codes. Hence, using some abstract-syntax-tree
library is also too an overkill.

推荐答案

9月7日星期二2004 23:51:38 -0700,Tran Tuan Anh写道:
On Tue, 07 Sep 2004 23:51:38 -0700, Tran Tuan Anh wrote:
printf(out," for(%s = 1,%s<%s,%s<%s)" ;,varName,varName,
varName1,varname,varName2);

如果我有超过20行像这样太乱了。


看起来你想要一个模板。我不知道这些程序是什么存在

for C ++,如果有的话,或者它们有多好。


你可能会得到很多的milage只需添加一些实用功能,就可以将它变成类似


forFromOne(out,var,max,min);


如果你有可靠的可信代码,那么改变语言是非常激烈的。

(如果这些话没有描述你的代码,那么评估可能会改变。)

所以我的问题是:
1. Python可以帮助解决这个问题吗?


也许一点点,但如果上面的代码片段具有代表性,那么相当于它的
Python等等就不会短得多。如果你想开始用数据做更聪明的东西,Python会开始闪耀

但是没有

知道数据很难具体。基本上,当你不仅仅是组装字符串时,Python很可能*具有

优势。


(我发现这个问题的Pythonic展望很可能会在可接受的努力程度上实现显着的简化,但我不能承诺* b $ b *承诺*;如果你真的需要的话,老实说需要的是printf,我认为

是罕见的,但当然可以想象,那么你可能没有任何优势。)

2. Python有没有解析器?在我看来,现在没有标准的Python解析器。如果是这样的话,我可以将Python与现有的cup和lex接口吗?
printf(out, "for (%s = 1, %s < %s, %s < %s )", varName, varName,
varName1, varname, varName2);

It is just too messy if I have more than 20 lines like this.
It looks like you want a templater. I don''t know what such programs exist
for C++, if any, or how good they are.

You may get a lot of milage out of just adding some utility functions, to
turn that to something like

forFromOne(out, var, max, min);

Changing languages is pretty drastic if you have working, trusted code.
(If those words don''t describe your code, then that assessment may change.)
So my question is:
1. Can Python help to solve this issue?
Maybe a little, but if the above code snippet is representative, the
Python equivalent of that isn''t much shorter. Python would start to shine
if you want to start doing smarter things with the data, but without
knowing about the data it is hard to be specific. Basically, the moment
you aren''t just assembling strings, Python will *quite likely* have an
advantage.

(I find it very likely that a Pythonic outlook on the problem would allow
significant simplification at acceptable effort levels, but I can''t
*promise* that; if all you really, honestly need is printf, which I think
would be rare but certainly conceivable, then you may get no advantage.)
2. Does Python has a parser? It seems to me that there is no-standard
Parser for Python right now. If it is the case, can I interface Python
with existing cup and lex?




当你在谈论*输出时,为什么要询问解析器? *

代码?问题描述中是否还有其他内容?


Python有很多解析器,涵盖了广泛的需求。



Why are you asking about a parser when you are talking about *outputting*
code? Is there something left out of the problem description?

There are a lot of parsers for Python, covering a wide range of needs.


Tran Tuan Anh< anhtt< at> hotmail.com>写道:
Tran Tuan Anh <anhtt <at> hotmail.com> writes:
1. Python可以帮助解决这个问题吗?


我使用Python进行非常简单的代码生成/模板化 - 不是用C ++,而是使用
没有区别。对于

代码生成,甚至还有一些特殊而非复杂的工具,例如: Cog( http://www.nedbatchelder.com/code/cog /index.html)

2. Python有解析器吗?在我看来,没有标准
1. Can Python help to solve this issue?
I have used Python for quite simple code generation/templating - not in C++, but
that makes no difference. There are even special and not that complex tools for
code generation, e.g. Cog (http://www.nedbatchelder.com/code/cog/index.html).
2. Does Python has a parser? It seems to me that there is no-standard



有各种解析器,但我从来没有使用过任何一种解析器。在搜索python parser时,Google会提供
吨的链接,因此更具体的搜索

将是明智的。


你的,


Andrei



There are all kinds of parsers, but I''ve never used any of them. Google gives
tons of links when searching for "python parser", so a more specific search
would be wise.

Yours,

Andrei


我使用Python生成相当多的HTML和

JavaScript代码,效果很好。如果你是
可以使用Python的类来抽象单个

元素并给每个元素一个__str__方法

你可以构建非常复杂的代码生成器类

知道如何呈现自己的输出。


Larry Bates

Syscon,Inc。


Tran Tuan Anh <一个*** @ hotmail.com>在消息中写道

news:ed ************************** @ posting.google.c om ...
I use Python to generate quite a bit of HTML and
JavaScript code and it works very well. If you
can use Python''s classes to abstract individual
elements and give each of them a __str__ method
you can build very complex code generator classes
that know how to render their own output.

Larry Bates
Syscon, Inc.

"Tran Tuan Anh" <an***@hotmail.com> wrote in message
news:ed**************************@posting.google.c om...
亲爱的:

我需要你就此事提出建议。我正在开发一个程序,它可以获取一些System-C代码并生成一些其他的System-C代码。 (System-C代码只是带有一些特殊库的C ++,因此你可以将它视为C ++)。

现在,生成器程序是用C ++编写的。我觉得C ++不太舒服。我觉得C ++太过分了。因为,我需要生成一些代码,因此在程序中有很多东西像这样:

printf(out," for(%s = 1) ,%s<%s,%s<%s)",varName,varName,
varName1,varname,varName2);

如果我有更多,那太乱了像这样的20行。

所以我的问题是:
1. Python可以帮助解决这个问题吗?
2. Python有解析器吗?在我看来,现在没有标准的Python解析器。如果是这样的话,我可以将Python与现有的杯子和lex接口吗?

非常感谢!
Tuan Anh
P / S:也因为它不是大代码生成器,它只需要生成一些简单的代码。因此,使用一些抽象语法树
库也太过分了。
Dear all:

I need your advice on this matter. I am working on a program which
takes some pieces of System-C code in and generate some other System-C
code. (System-C code is just C++ with some special libraries, hence
you can consider it as C++).

Right now, the generator program is written in C++. And I feel not so
comfortable with C++. I feel C++ is an overkill. Because, I need to
generate some code, hence in the program there are a lot of something
like this:

printf(out, "for (%s = 1, %s < %s, %s < %s )", varName, varName,
varName1, varname, varName2);

It is just too messy if I have more than 20 lines like this.

So my question is:
1. Can Python help to solve this issue?
2. Does Python has a parser? It seems to me that there is no-standard
Parser for Python right now. If it is the case, can I interface Python
with existing cup and lex?

Many thanks!
Tuan Anh
P/S: Also because it is NO big code generator, it only needs to
generate some simple codes. Hence, using some abstract-syntax-tree
library is also too an overkill.



这篇关于用Python生成代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆