[antlr-interest] Nested parser, island grammar and token ambiguity
Jean-Christophe Bach
jeanchristophe.bach at inria.fr
Wed Jul 28 08:05:48 PDT 2010
Hello antlr users,
I am trying to use antlr3 for our new parser (last one was a complex and ugly
parser with antlr v2, and it is not very maintenable), but I have few problems.
Maybe you will be able to help me.
I have a "HostParser" and few other "subparsers" called when specific
constructions are detected. Another subparser or the HostParser can be called be
from a subparser. For that, I looked at the official island grammar example, and
it seems to work well with simple cases.
It is something like that :
in Host :
myRule : ... t=KEYWORD0 ... -> ... ;
KEYWORD0 : '%keyword0'
{
SubLanguage1Lexer lexer = new SubLanguage1Lexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
SubLanguage1Parser parser = new SubLanguage1Parser(tokens);
SubLanguage1Parser.keyword0_return res = parser.keyword0();
}
;
And there is the same thing in other parsers/lexers if needed.
It seems OK, but in one of my SubLanguages, I use this and I need to have
different behaviours depending on the situation after the same character. And
here, I am stuck. The difference between '-> {' and '{' is ok, but between two
'{' is not easy.
SubLanguage1, ok :
rule1 : ... ARROWLBRACE ... -> ... ;
rule2 : ... LBRACE ... -> ... ;
ARROWLBRACE : '->' '{'
{
...
HostParser.hostrule1_return res = parser.hostrule1();
...
}
;
LBRACE : '{'
{
...
HostParser.hostrule2_return res = parser.hostrule2();
...
}
;
But there are other cases where a simple LBRACE needs a third Java treatment.
How can I do that ?
Is there any way to do something like that ? :
rule1 : ... ARROW LBRACE1 ... -> ... ;
rule2 : ... LBRACE2 ... -> ... ;
rule3 : ... LBRACE3 ... -> ... ;
LBRACE1 : '{' {<Java code #1>} ;
LBRACE2 : '{' {<Java code #2>} ;
LBRACE3 : '{' {<Java code #3>} ;
I hope my explanation was clear enough. If you have any idea, do not hesitate to
make a suggestion :)
Regards,
JC
More information about the antlr-interest
mailing list