Writing part of a compiler (written in c++) in Perl

。_饼干妹妹 提交于 2019-12-04 14:57:56

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.

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() and regfree() 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 called YACC for developing parsers. Both are time tested and generate tight, bug-free code. (GNU-ish environments call these programs flex and bison.)

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. :)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!