Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Is there a "modern" bison and flex equivalent which does not require the JVM or .NET or similar "heavy" runtimes?

Thanks for comments, edit:

By modern, I mean for example Antlr and another one in Java I saw but forgot the name of. (I know that for instance Antlr has a C target, but I am toying with the idea of embedding the compiler generator inside my own program and Java would be very impractical for me.)

share|improve this question
    
Define "more modern". What features or capabilities are you looking for that lex and yacc don't provide? –  Mason Wheeler May 20 '12 at 16:26
    
Is there a reason it should not require the JVM or .NET? Any computer made in the last decade (or more) would typically run them and be fine. –  Rig May 20 '12 at 16:26
    
Like C++'s Boost.Spirit and Haskell's Parsec? Or do you explicitly want a language-neutral generator? –  chrisaycock May 20 '12 at 16:40
3  
Since when to lex and/or yacc require JVM or .NET??? –  Crazy Eddie May 20 '12 at 16:42
    
Lex and Yacc can do anything, but calling them easy to use, I wouldn't dare. I'd like to be able to embed the lexer in my program eventually, so I don't want to depend on JVM or something which may not be installed. –  Prof. Falken May 20 '12 at 16:47
show 7 more comments

2 Answers

up vote 7 down vote accepted

I recommend using the Lemon parser generator for parsing. Like Bison, it produces a parser in C. However, its grammar is less error-prone, the parsing interface is reentrant without global variables, and works by you giving tokens to the parser (instead of the parser calling you back to get tokens).

You don't really need a lexer generator (e.g. lex). It's usually easier to write it by hand.

You can check out some of my code to get an idea of how to parse a language using Lemon: language, lexer, grammar, parser.

share|improve this answer
    
+1 Lemon looks really interesting! I will wait a little more for more answers before accepting this, but this looks like the winner. –  Prof. Falken May 20 '12 at 19:58
add comment

A very different but quite capable (and easily embeddable) answer is LPEG.

At first glance is just a pattern matching library, like a more capable RegEx; but it allows you to define not only patterns but whole language grammars, attaching custom behaviour to any step.

On that page there's a sample that matches an arithmetic expression, and produces the final value. It could instead produce a parse graph, or directly emit code.

The LPEG Recipes page includes samples for whole Lua lexer and parsers, and also an ANSI C lexer.

share|improve this answer
    
Wow, thanks! Will go look at the ANSI C lexer now. –  Prof. Falken May 21 '12 at 18:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.