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

Mark Mandel mark.mandel at gmail.com
Tue Jun 19 21:37:38 PDT 2007


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'

So that I have a tree that looks like

- <a>
- - <b>
- </a>

Is this even possible?

My grammar looks like (stripped out all the rewrite rules and other
parts for simplicity):

cfml
	:
	tag*
	;

tag
	:
		startTag
	;

startTag
scope tagScope;
	:
	(
	sto=START_TAG_OPEN
	{
		{$tagScope::currentName = $sto.text.toLowerCase().substring(1); }
	}
	stc=START_TAG_CLOSE
	tc=tagContent
	)
	;
	
tagContent
	:
	tag* (
		{ $tagScope::currentName.equals(input.LT(1).getText().toLowerCase().substring(2))
}?
		(
			endTag
		)
		)
	;
endTag
	:
	END_TAG_OPEN^ END_TAG_CLOSE
	;

But this fails, on the <a><c></a> example, as the predicate throws an
error as </a> doesn't match <c>.

What I want to happen is to have the end tag ignore </a> as the name
of the tag is not the same.

I hope I've made sense...

Thanks,

Mark

-- 
E: mark.mandel at gmail.com
W: www.compoundtheory.com


More information about the antlr-interest mailing list