[antlr-interest] Possible to conditionally skip token in rule?

Terence Parr parrt at cs.usfca.edu
Tue Jun 19 21:58:54 PDT 2007


On Jun 20, 2007, at 2:37 PM, Mark Mandel wrote:

> I may have a bad title on this post, but this is the situation I'm  
> working with.
>
> I'm writing a ColdFusion parser (still), and dealing with an xml like
> sytax, where a closing tag is not required.
>
> so that a statement like:
>
> <a> <b> </a>
>
> Is completely valid.
>
> Now, what I want to do in my grammar, is say 'if the name of the next
> closing tag doesn't match the name of the current open tag, ignore it,
> as it probably belongs to another tag'

Seems to me that this is what ANTLR does for single token insertion  
during automatic error recovery.  Interesting...is this unusual or  
normal?  If unusual, an error recovery approach can work.  If normal,  
a predicate route is probably good.

cfml : tag* ;

tag : START_TAG_OPEN {push($START_TAG_OPEN.text);}
	| START_TAG_CLOSE
	  {
		if ( $START_TAG_CLOSE.text same as stack top ) {
			t = pop();
			add t to tree
		}
		else { // mismatch (isolated <b> from above)
			add stack top to tree
			t = pop();
			add t to tree
		}
	  }
	;

something like that.

Just do your own tree building; don't use the recursive grammatical  
structure.  Well, this should work; not sure it's optimal.

Ter



More information about the antlr-interest mailing list