Skip to main content
gamedev.net
🔒 Locked

Parser Generator

Started by Lode Mar 25, 2007 at 12:10 PM 2 replies 450+ views
Original Post
Lode
Lode
I've always wondered what a parser generator or compiler compiler is used for? If it's a compiler then I guess it can compile your code, but why would you want to design a sort of language first, then let a compiler compiler compile a compiler for your language, and then compile the application, if there are already tons of languages available? But what are they used for then? What's an example of a useful application of one? I always wanted to know :)
Ravuya
Ravuya
I'm using lex and yacc for an assignment right now (actually, alex and happy).

The lexer generator builds the code to tokenize your source code based on your regular expressions and other rules; the parser generator builds a parser (generally LALR(1)) for your language's context-free grammar. Run yacc and lex on your files, and they crank out some C/C++/Haskell/Java source code that you can just call from your actual compiler.

I write the grammar for my "toy language" into yacc and it builds a parser for that language and outputs a syntax tree, which I can then use to evaluate or to generate machine code.

Since lexer and parser generation is boring, you tend to get boredom-induced errors when you write them by hand, which is a perfect task for a soulless, unthinking machine. [grin]

Check out the IBM Linux games tutorial for an example of a yacc and lex source file that's used for an in-game scripting language.
ToohrVyk
ToohrVyk
The question is not really "why do you need a parser generator", because the answer is "to create a parser for a language for which there is none". The actual question is "which languages don't already have a compiler?" and the answer here is: a lot. Every time someone creates a prototype language (happens on a weekly basis in academia) a parser generator is a simple way of generating a parser without having to spend days on writing it yourself.

jfclavette
jfclavette
I participated in a programming competition yesterday. I wrote a small parser that could handle logical expression of the form (((a v b) ^ (c x d)) <-> e), take some value binding in and tells you wether the expression is true or not.

I did it by hand since I didn't think using a compiler compiler would be useful for the small scope. However, the other team did it slightly faster than me using javacc.

Bottom line, ad-hoc languages are useful for some applications, compiler compilers are a good way to build an interpreter quickly.
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.