Parsing, where can I learn about it
Asked Answered
C

12

18

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?

Contrived answered 29/8, 2008 at 6:57 Comment(0)
N
13

If you want to get "emotional" about the subject, pick up a copy of "The Dragon Book." It is usually the text in a compiler design course. It will definitely meet your need "learn more about lexical analysis and parsers" as well as a bunch of other fun stuff!

IMH(umble)O, save yourself an arm and/or leg and buy an older edition - it will fill your information desires.

Novitiate answered 29/8, 2008 at 7:15 Comment(3)
Matt, there're 3 edition, so please add ISBN for the one you're suggesting, or improve your comments with all books' ISBN's and say a word or two about each.Sawn
really @Ostati? I clearly state to save yourself some money and buy and older edition... or don't save money, and buy current.Novitiate
Matt, it took me a while to find out which version was which. But had your answer, which I upvoted btw, the ISBN....anyway, I go the book and started my journey. Thx.Sawn
C
8

Try ANLTR:

ANTLR, ANother Tool for Language Recognition, is a language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions containing actions in a variety of target languages.

There's a book for it also.

alt text

Cask answered 29/8, 2008 at 7:1 Comment(0)
P
5

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

Preshrunk answered 29/8, 2008 at 7:59 Comment(0)
E
2

I've recently been working with PLY which is an implementation of lex and yacc in Python. It's quite easy to get started with it and there are some simple examples in the documentation.

Parsing can quickly become a very technical topic and you'll find that you probably won't need to know all the details of the parsing algorithm if you're using a parser builder like PLY.

Excrement answered 29/8, 2008 at 6:59 Comment(0)
E
2

Lots of people have recommended books. For many these are much more useful in a structured environment with assignments and due dates and so forth. Even if not, having the material presented in a different way can help greatly.

(a) Have you considered going to a school with a decent CS curriculum?
(b) There are lots of online lectures, such as MIT's Open Courseware. Their EE/CS section has many courses that touch on parsing, though I can't see any on parsing per se. It's typically introduced as one of the first theory courses as language classification and automata is at the heart of much of CS theory.

Extend answered 24/10, 2008 at 19:1 Comment(1)
+1 for mit's ocw, I use it all the time for math. For some reason, going to class on MY schedual is so much better than getting up at 6:30.Cryobiology
D
1

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/

Desdamona answered 29/8, 2008 at 7:4 Comment(0)
V
1

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.

Veolaver answered 29/8, 2008 at 7:18 Comment(0)
H
1

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

Hurl answered 29/8, 2008 at 7:31 Comment(0)
C
1

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.

Colunga answered 18/9, 2008 at 21:40 Comment(0)
E
1

I found this site helpful:

Lex and YACC primer/HOWTO

The first time I used lex/yacc was for a relatively simple project. This tutorial was all I really needed. When I approached more complex projects later, the familiarity I had from this tutorial and a simple project allowed me to build something fancier.

Elspet answered 30/9, 2008 at 13:54 Comment(0)
N
1

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.

Nitrosyl answered 24/10, 2008 at 18:42 Comment(0)
W
1

Parsing Techniques - A Practical Guide By Dick Grune and Ceriel J.H. Jacobs

This book (freely available as PDF) gives an extensive overview of different parsing techniques/algorithms. If you really want to understand the different parsing algorithms, this IMO is a better reference than the Dragon Book (as Parsing Techniques focuses entirely on parsing, while the Dragon Book covers parsing only as one - although important - part of the compiler construction process).

Warmup answered 26/10, 2008 at 11:45 Comment(1)
I've fixed the link: the actual PDF (for the first edition) can be downloaded here: dickgrune.com/Books/PTAPG_1st_Edition/BookBody.pdf ; a new edition and more extensive edition of the book is now also available on AmazonWarmup

© 2022 - 2024 — McMap. All rights reserved.