【问题标题】:Python generating different files from one file [closed]Python从一个文件生成不同的文件[关闭]
【发布时间】:2015-02-13 10:20:37
【问题描述】:

我要做的是使用 Python 解析脚本 bob.ps 并根据用户输入输出 bob.pybob.cpp

假设我们有 bob.ps,这是一种类似 python 的简单语言

#comment
use ShowBase
# Load the environment model.
environ = loadModel 'cube'
# Reparent the model to render.
render environ
run

用户需要使用以下命令运行 python 脚本: $ python main.py -py -c++ 这将导致生成以下 python 和 c++ 脚本:

from direct.showbase.ShowBase import ShowBase
class MyApp(ShowBase):

    def __init__(self):
       ShowBase.__init__(self)
       # Load the environment model.
          self.environ = self.loader.loadModel("models/environment")
       # Reparent the model to render.
       self.environ.reparentTo(self.render)
app = MyApp()
app.run()

和 c++

#include "pandaFramework.h"
#include "pandaSystem.h"

int main(int argc, char *argv[]) {
      // Load the window and set its title.
   PandaFramework framework;
   framework.open_framework(argc, argv);
   framework.set_window_title("My Panda3D Window");
   WindowFramework *window = framework.open_window();
      // Load the environment model.
   NodePath environ = window->load_model(framework.get_models(), "models/environment");
      // Reparent the model to render.
   environ.reparent_to(window->get_render());
      // Run the engine.
   framework.main_loop();
      // Shut down the engine when done.
   framework.close_framework();
   return (0);
}

问题

我花了一些时间浏览互联网寻找答案。我发现我需要解析 bob.ps 并使用词法分析器。我试图把 PLY-3.4 弄得一团糟,但它并没有完全达到我想要完成的效果。我不需要/不想在解析时执行代码,而我的意图只是生成等效的 python/c++ 代码。

解决这个问题的最佳方法是什么,有没有关于这个特定主题的模块/书籍/文章/教程?我真的撞墙了,不知道该往哪里看。 非常感谢任何帮助。

【问题讨论】:

  • 欢迎来到 Stack Overflow!没有必要在你的帖子上放这么大的免责声明。如果有任何可以通过编辑解决的问题,我们会这样做。 :-)

标签: python c++ parsing code-generation ply


【解决方案1】:

你想在这里做的有点像编译器。 你想把“ps”编译成python或者cpp。

标准的工具是 lex 和 yacc,到处都有大量的文献。

【讨论】:

猜你喜欢
  • 2015-03-15
  • 2017-12-27
  • 2010-09-30
  • 1970-01-01
  • 1970-01-01
  • 2012-11-12
  • 1970-01-01
  • 1970-01-01
  • 2016-04-24
相关资源
最近更新 更多