是ANTLR解析器生成最适合于具有约束内存C ++应用程序? [英] Is the ANTLR parser generator best for a C++ app with constrained memory?

查看:118
本文介绍了是ANTLR解析器生成最适合于具有约束内存C ++应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一个良好的解析器生成器,我可以用它来阅读我们的大型商业应用程序自定义文本文件格式。目前这个特定的文件格式读取一个手工制作的递归解析器但格式已经成长和complexified的地步,这种做法已经变得无法控制。

这似乎是最终的解决办法是建立一个正确的语法为这种格式,然后使用一个真正的解析器生成像YACC读它,但我无法决定使用哪一个发电机等,甚至如果他们值得所有的麻烦。我看着ANTLR和精神,但我们的项目已经超出更早的答案具体限制这让我怀疑他们是为适合我们。特别是,我需要:

我喜欢ANTLRworksIDE和调试工具,但它看起来像获得其C目标实际上我们的应用程序的工作将是一个艰巨的任务。在我踏上上拍马,是ANTLR这个工作的工具?

在讨论的文本格式看起来类似:

 属性FluxCapacitance实常数资产德罗宁
{
    //注释富酒吧巴兹
    模式delorean.mdl
    动画gullwing.anm
    引用君子好逑
    loadonce
}时间机器模板
{
    属性FluxCapacitance 10
    资产德罗宁
}



  

3 ANTLR不支持C ++;它声称
  生成直C,但在文档
  得到它的实际工作有几分
  的混乱。


它生成C,而且,它与Visual Studio和C ++工程。我知道这是因为我以前做过,并提交了一个补丁才能正常使用STDCALL工作。


  

记忆是在我们的应用程序一个巨大的premium
  甚至微小的泄漏是致命的。我需要
  能够覆盖解析器的
  内存分配器使用我们的自定义
  的malloc(),或者至少是我需要的
  给它从一个连续的游泳池
  它吸引了所有内存(并
  我可以释放整块
  之后)。我能抽出约200KB
  解析器可执行文件本身,但是
  无论动态堆它分配在
  解析要得到事后释放。


该antlr3c运行时,我最后一次检查没有内存泄漏,并且使用你所描述的内存池的范例。然而,它确实有它的作者拒绝改变的API,它是,如果你请求一个节点的字符串,它会每次直到解放整个分析器创建一个新副本在一个缺憾。

我就放心了使用自定义的malloc任何评论,但它确实有一个宏定义在整个项目中使用什么样的malloc函数。

对于可执行文件的大小,我的编辑约100 KB的大小,包括一个小间preTER。

我给你的建议是保持学习ANTLR,因为它仍然符合你的要求,你可能需要牺牲一点时间前,将开始为你工作。

I'm looking for a good parser generator that I can use to read a custom text-file format in our large commercial app. Currently this particular file format is read with a handmade recursive parser but the format has grown and complexified to the point where that approach has become unmanageable.

It seems like the ultimate solution would be to build a proper grammar for this format and then use a real parser generator like yacc to read it, but I'm having trouble deciding which such generator to use or even if they're worth the trouble at all. I've looked at ANTLR and Spirit, but our project has specific constraints beyond earlier answers that make me wonder if they're as appropriate for us. In particular, I need:

  • A parser that generates C or C++ code with MSVC. ANTLR 3 doesn't support C++; it claims to generate straight C but the docs on getting it to actually work are sort of confusing.
  • Severely constrained memory usage. Memory is at a huge premium in our app and even tiny leaks are fatal. I need to be able to override the parser's memory allocator to use our custom malloc(), or at the very least I need to give it a contiguous pool from which it draws all its memory (and which I can deallocate en bloc afterwards). I can spare about 200kb for the parser executable itself, but whatever dynamic heap it allocates in parsing has to get freed afterwards.
  • Good performance. This is less critical but we ought to be able to parse 100kb of text in no more than a second on a 3ghz processor.
  • Must be GPL-free. We can't use GNU code.

I like ANTLRworks' IDE and debugging tools, but it looks like getting its C target to actually work with our app will be a huge undertaking. Before I embark on that palaver, is ANTLR the right tool for this job?

The text format in question looks something like:

attribute "FluxCapacitance"  real constant

asset DeLorean
{
    //comment foo bar baz
    model "delorean.mdl"
    animation "gullwing.anm"
    references "Marty"
    loadonce
}

template TimeMachine
{
    attribute FluxCapacitance 10      
    asset DeLorean
}

解决方案

ANTLR 3 doesn't support C++; it claims to generate straight C but the docs on getting it to actually work are sort of confusing.

It does generate C, and furthermore, it works with Visual Studio and C++. I know this because I've done it before and submitted a patch to get it to work with stdcall.

Memory is at a huge premium in our app and even tiny leaks are fatal. I need to be able to override the parser's memory allocator to use our custom malloc(), or at the very least I need to give it a contiguous pool from which it draws all its memory (and which I can deallocate en bloc afterwards). I can spare about 200kb for the parser executable itself, but whatever dynamic heap it allocates in parsing has to get freed afterwards.

The antlr3c runtime, last time I checked does not have a memory leak, and uses the Memory pool paradigm which you describe. However, it does have one shortcoming in the API which the author refuses to change, which is that if you request the string of a node, it will create a new copy each time until you free the entire parser.

I have no comment on the ease of using a custom malloc, but it does have a macro to define what malloc function to use in the entire project.

As for the executable size, my compilation was about 100 kb in size including a small interpreter.

My suggestion to you is to keep learning ANTLR, because it still fits your requirements and you probably need to sacrifice a little more time before it will start working for you.

这篇关于是ANTLR解析器生成最适合于具有约束内存C ++应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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