帮助解析S表达式
我正在尝试制作一个简单的绘图程序,该程序读取 translate (rect 10 10 10 10) 50 50
。我想做的是将其拆分,以便 50 50
与 translate
一起使用,而 rect
保留所有 10
。
这是 PostScript 填充。我听说过哈希表和堆栈,但我不知道如何使用它们。我已经完成了其他所有工作(例如所有形状的计算)。我只是不明白如何解析这些行,以便我可以获得指向正确变量的数字。
I'm trying to make a simple drawing program that reads in translate (rect 10 10 10 10) 50 50
. What I'm trying to do is split it so that the 50 50
goes with the translate
and the rect
keeps all the 10
s.
This is a PostScript fill. I've heard of hash tables and stacks, but I'm not sure how to use them. I've done everything else (e.x. all the calculations for the shapes). I just don't understand how to parse the lines so that I can get the numbers pointing to the right variables.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的示例看起来像 Lisp s 表达式,因此请尝试搜索“s 表达式解析器”。出现了一些点击。
如果您想“彻底”,您可以将形状例程实现为 C++ 类,使用 SWIG 将它们公开给 GNU Guile,并在 Scheme 中编写您的应用程序。不过,这可能不是您的想法。 :-)
Your example looks like a Lisp s-expression, so try searching for "s-expression parser". A few hits come up.
If you want to go "whole hog", you could implement your shape routines as C++ classes, use SWIG to expose them to GNU Guile, and write your application in Scheme. That is probably not what you had in mind, though. :-)
嗯,这可能有点过时,但它很简单,而且没有比这更快的了。
如果您有许多“(translate (rect ...”)副本,只需一遍又一遍地调用解析例程,直到它返回 false。
Well, this might be a little old-fashioned, but it's simple and there's nothing faster.
If you've got many copies of "(translate (rect ...", just call the parse routine over and over until it returns false.
以下是如何使用 AXE 库 编写此 C++ 解析器:
这将解析单个翻译PS文件中的声明。如果您需要解析所有翻译语句并且不介意为 postscript 格式编写完整的解析器,则可以使用 *r_find(translate) 规则来跳过您不关心的输入。
r_find(R)
规则搜索输入,直到找到规则R
。现在这非常简单,而且它还会生成非常快的代码,可能比使用“if”-s 和“else”-s 手写的代码更快。免责声明:我没有测试上面的代码,因此可能会出现小错误。
Here is how you can write this C++ parser using AXE library:
That will parse a single translate statement in PS file. If you need to parse all translate statements and don't care to write a full blown parser for postscript format, you can use
*r_find(translate)
rule to skip input that you don't care about.r_find(R)
rule searches the input until the ruleR
is found. Now that's pretty easy and it will also generate very fast code, probably faster than hand-written with "if"-s and "else"-s.Disclaimer: I din't test the code above, so minor errors are possible.