[antlr-interest] Good practice for grammar with translated keywords

Olivier THIERRY olivier.thierry at gmail.com
Thu Mar 12 07:50:07 PDT 2009


Hi,

I need to write a grammar for which keywords will be translated in
english, french, spanish, ...
Then I use StringTemplate to transform this language to Groovy script.

For example I would have the following statement in english :

IF (i = 0) THEN

And the following in french :

SI (i = 0) ALORS

To do this I thought about writing :
- many lexer grammar for keywords (i.e. translated tokens), one lexer
grammar for each language
- one lexer grammar for not translated tokens
- one parser grammar that would import the not translated tokens lexer
grammar and one of the translated tokens lexer grammar.

Actually only the first lexer grammar is language specific, the other
ones are common.
But I can't find the right way to do this since tokens have to be
imported in parser grammar. So it means you will have a parser grammar
for each language.

I also thought about using or statements in keywords tokens
definition. Something like that : IF : 'IF' | 'SI';
But it means you could mix languages, something like : IF (i=0) ALORS

If anyone had the same need, how did he achieve this ?

I use antlr3, antlrworks and antlr3 maven plugin.

Thanks in advance for any help !

Olivier


More information about the antlr-interest mailing list