Ivan Mushketyk
Ivan Mushketyk

Reputation: 8285

Generate two parsers for a single DSL

I need to implement two tools for a single DSL: UI editor in Java and interpreter in C/C++. My first idea was to use ANTLR, since it can generate parsers for both Java and C/C++. But all ANTLR examples that I've seen contains some language-specific code or settings.

Is there any way to generate two parsers for a single DSL?

Does this even make sense to generate two parsers from a single grammar?

Is there any commonly used approaches for this problem?

Upvotes: 0

Views: 161

Answers (3)

Mike Lischke
Mike Lischke

Reputation: 53337

You certainly can use ANTLR. The language specific parts are actions or predicates. If you don't need them then you won't have any language specific stuff in the grammar. Btw. regardless of the parser generator you use (including yacc, bison etc.) you would always have language specific stuff in the grammar if you need that.

Upvotes: 0

rici
rici

Reputation: 241701

bison can produce C++ and Java parsers, at least according to the documentation (I've never used the Java interface, and only used the C++ interface once, but I'm told that they work). The grammar will not be a problem, but the actions will be, particularly since you're presumably doing different things in the two parsers, and not just using different languages. But you should be able to make every action a simple $$ = method($1, $2, ...); statement.

bison doesn't use the C(++) preprocessor (and it couldn't really, because it's common to put preprocessor directives into the bison input files), but you could use some other macro system -- I hesitate to recommend m4 but it would work if you know how to use it -- or a shell script to assemble the different input files.

The other possibility would be to just create an AST in the parser. You could use any parser generator, including Antlr or bison, to build the AST parser in C or C++, and then wrap the result for use with JNI for Java. If you use Antlr, you can produce an AST generator with very little language-specific code, so with a simple macro processor, you could build native AST parsers in both C++ and Java, I think. But that depends on your language being fairly simple.

I don't know if there is a "commonly used approach" for this problem, but it's certainly a problem that comes up pretty regularly; lots of grammars get shared between different projects, but from what I've seen, the most common approach is to cut-and-paste the grammar, and rewrite the actions. I've done the macro approach a couple of times, and it can be made to work but it's never as elegant as you'd like it to be.

Upvotes: 1

fghj
fghj

Reputation: 9394

You can try yacc and jacc. http://web.cecs.pdx.edu/~mpj/jacc/ http://dinosaur.compilertools.net/#yacc

They have very similar syntax, may be with help some hand maid preprocessing tool you can use one source file.

PS But why not write parser once in C++ and use it via JNI?

Upvotes: 0

Related Questions