[antlr-interest] Island Parsing - a different way, seems to work

David Holroyd dave at badgers-in-foil.co.uk
Mon Jun 18 03:16:44 PDT 2007


On Mon, Jun 18, 2007 at 05:23:18PM +1000, Mark Mandel wrote:
> Hey all,
> 
> I've been playing around with Island Parsing, and I think I've come up
> with a much simpler way of doing it other than the one that is in the
> wiki
> (http://www.antlr.org/wiki/display/ANTLR3/Island+Grammars+Under+Parser+Control)

Hey!  My page :)


> The code ended up looking pretty much like this:
> 
> startTag
> 	:
> 	(
> 	sto=START_TAG_OPEN stc=START_TAG_CLOSE	tc=tagContent
> 		(
> 		-> ^(CFTAG[$sto] START_TAG_CLOSE
> 						{
> 							parseScript(stc, tc)
> 						}
> 						  tagContent)
> 		)
> 	)
> 	;

To be honest, I suspect you are trying to solve a simpler problem than
the kind which the 'under parser control' approach is tackling.  i.e.
your lexer is able to unambiguously recognise START_TAG_OPEN without any
additional context.  You should absolutely use a simpler approach than
the one on that wiki page if you possibly can!  :)

In some languages (e.g. the regex island grammar example from the wiki
page) it is not possible for the lexer to work out what's going on
without knowledge of what came before.  Getting the parser to talk to
the lexer so that the parser can supply this context needs a whole lot
of additional wiring that ANTLR doesn't deliver by default at the
moment.  That's why the example code is so complicated.

Have a look at 'island-grammar' from the examples-v3 download for
another approach handling a language that's closer to yours (I think).


Hope that makes sense!
dave

-- 
http://david.holroyd.me.uk/


More information about the antlr-interest mailing list