Using Island grammars in ANTLR 4 parser

640 views
Skip to first unread message

Paul Golds

unread,
May 16, 2013, 3:07:08 AM5/16/13
to antlr-di...@googlegroups.com
I've recently been learning ANTLR4 from the Definitive Guide, and have started using it for a personal project but have hit a problem.

In essence my program is intended to be able to evaluate expressions marked with '{ .. }' embedded inside text.  For example the input 'The total of 2+3= {2+3}' would be parsed by my program and output similar to 'The total of 2+3= 5'.

This seems a natural use for an island grammar, with the default mode parsing text with '{' triggering a mode switch to an evaluate mode, and evaluate mode being an expression evaluator with '}' switching back to the default mode.

The problem is though that I also need a parser for this, and from reading other questions I see that importing multi-mode lexer grammars isn't supported as yet in ANTLR4.  This leaves me with three possible ways forward that I can see, and possibly more that I can't, but in my inexperience I don't really know which is better.

- Continue with ANTLR4, put the lexer rules for numbers and operators into the main grammar.  I think this would involve me needing to put whitespace onto a hidden channel so that things which look like expressions but aren't wrapped in {...} can be presented without breaking them with lots of spaces.

- Switch to ANTLR3 for now, develop the island grammars as I was planning, keeping an eye on ANTLR4 development to see when this feature will be available.

- Somehow be able to use my multi-mode grammar inside of an ANTLR4 parser without an import.  I have no idea if this is possible, or how to go about it, but if it is a possibility I'd appreciate some pointers towards how to do this as it seems the best solution for now.



George S Cowan

unread,
Jul 5, 2013, 3:54:40 PM7/5/13
to antlr-di...@googlegroups.com
I don't understand the problem with option 1 - "Continue with ANTLR4, put the lexer rules for numbers and operators into the main grammar". Why would the tokens from one lexing mode interfere with the tokens from the other?

George
Reply all
Reply to author
Forward
0 new messages