Parsing, where can I learn about it

2019-01-22 08:10发布

I've been given a job of 'translating' one language into another. The source is too flexible (complex) for a simple line by line approach with regex. Where can I go to learn more about lexical analysis and parsers?

标签: parsing lex
12条回答
聊天终结者
2楼-- · 2019-01-22 08:42

Niklaus Wirth's book "Compiler Construction" (available as a free PDF) http://www.google.com/search?q=wirth+compiler+construction

查看更多
老娘就宠你
3楼-- · 2019-01-22 08:42

Yet another textbook to consider is Programming Language Pragmatics. I prefer it over the Dragon book, but YMMV.

If you're using Perl, yet another tool to consider is Parse::RecDescent.

If you just need to do this translation once and don't know anything about compiler technology, I would suggest that you get as far as you can with some fairly simplistic translations and then fix it up by hand. Yes, it is a lot of work. But it is less work than learning a complex subject and coding up the right solution for one job. That said, you should still learn the subject, but don't let not knowing it be a roadblock to finishing your current project.

查看更多
虎瘦雄心在
4楼-- · 2019-01-22 08:44

flex and bison are the new lex and yacc though. The syntax for BNF is often derided for being a bit obtuse. Some have moved to ANTLR and Ragel for this reason.

If you're not doing much translation, you may one to pull a one-off using multiline regexes with Perl or Ruby. Writing a compatible BNF grammar for an existing language is not a task to be taken lightly.

On the other hand, it is entirely possible to leverage any given language's .l and .y files if they are available as open source. Then, you could construct new code from an existing parse tree.

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-22 08:45

After taking (quite) a few compilers classes, I've used both The Dragon Book and C&T. I think C&T does a far better job of making compiler construction digestible. Not to take anything away from The Dragon Book, but I think C&T is a far more practical book.

Also, if you like writing in Java, I recommend using JFlex and BYACC/J for your lexing and parsing needs.

查看更多
Deceive 欺骗
6楼-- · 2019-01-22 08:48

Lexing/Parsing + typecheck + code generation is a great CS exercise I would recommend it to anyone wanting a solid basis, so I'm all for the Dragon Book

查看更多
姐就是有狂的资本
7楼-- · 2019-01-22 08:52

If you prefer Java based tools, the Java Compiler Compiler, JavaCC, is a nice parser/scanner. It's config file driven, and will generate java code that you can include in your program. I haven't used it a couple years though, so I'm not sure how the current version is. You can find out more here: https://javacc.dev.java.net/

查看更多
登录 后发表回答