用 Perl 编写编译器的一部分(用 c++ 编写)
我想了解更多有关编译器和编程语言的知识,不幸的是我的大学没有提供有关编译器的课程,所以我必须自己学习(谢谢互联网)。
目前,我正在尝试理解并为我的语言实现词法分析器,并且我需要正则表达式。
我习惯于快速编写 Perl 正则表达式脚本,并且我认为我可以将 Perl 嵌入到我的 C++ 词法分析器中。现在的问题是:
- 这会造成很大的开销吗?
- 我应该尝试与 BOOST (或任何其他 C++ 库或正则表达式)和平相处吗?
感谢您阅读本文:)
i am trying to learn more about compilers and programming languages, unfortunately my university doesnt offer a course about compilers and so i have to do myself (thank you internet).
At the moment im tryin to understand and to implement a lexer for my language and i need regular expressions.
I am used to script perl regex pretty quickly and i thought that i could embed Perl in my C++ lexer. Now the questions are:
- Will it cause Heavy overhead?
- Should i try to make peace with BOOST (or any other c++ library good gor regex) ?
Thank you for reading this :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在项目中嵌入 Perl 只是为了执行正则表达式,就像试图将大象塞进 Miata 以获得更多的后备箱空间一样。 (Badump!)
Boost 是处理正则表达式的一种方法,或者如果您在支持 POSIX.2 的环境中编写,请查看
regcomp()
、regexec()< /code> 和
regfree()
函数。编写完自己的词法分析器后,请研究一个名为
lex
的工具,它几乎是开发词法分析器的黄金标准。它有一个名为YACC
的合作伙伴来开发解析器。两者都经过时间测试并生成紧凑、无错误的代码。 (GNU-ish 环境将这些程序称为flex
和bison
。)Embedding Perl in your project just to do regular expressions would be like trying to stuff an elephant into a Miata to get more trunk space. (Badump!)
Boost would be one way to handle regular expressions, or if you're writing in an environment that supports POSIX.2, look into the
regcomp()
,regexec()
andregfree()
functions.After you've written your own lexer, investigate a tool called
lex
which is pretty much the gold standard for developing lexical analyzers. It has a partner calledYACC
for developing parsers. Both are time tested and generate tight, bug-free code. (GNU-ish environments call these programsflex
andbison
.)没有理由不能,成为一名优秀程序员的一部分就是使用正确的工具来完成工作,而 Perl 非常擅长文本处理。
然而,你不应该考虑将基于 perl 的词法分析器填充到你的 C++ 编译器中(我希望用 C++ 编写,而不是编译 C++),而应该考虑用 C++ 编写一个 perl 模块,并让编译器驱动程序用 perl 编写,进行词法分析,填充数据结构,然后调用 C++ 模块的函数来完成编译。
No reason you can't, part of being a good programmer is using the right tool for the job, and perl is VERY good at text processing.
However, instead of thinking about stuffing a perl-based lexer into your C++ compiler (written in C++, not compiling C++, I hope), you should think about writing a perl module in C++, and letting the compiler driver be written in perl, do the lexing, fill in data structures, and then call the C++ module's functions to finish the compile.
如果您真正想要的是 Perl 风格的正则表达式,请查看 libpcre 库。它经过了充分的测试,非常便携,而且根据我的经验,它很容易使用。推荐软件。 (并且可能已经在您的机器上。:)
If all you really want is Perl-style regular expressions, look into the libpcre library. It's very well tested, very portable, and in my experience easy to work with. Recommended software. (And probably already on your machine. :)
请参阅“正则表达式中的 \G 有什么好处?”的底部。 部分 perlfaq6。它描述了如何使用 //gc 创建标记器(又名词法分析器)。
See the bottom of the "What good is \G in a regular expression?" section of perlfaq6. It describes how //gc can be used to create a tokeniser aka lexer.