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

Mark Mandel mark.mandel at gmail.com
Wed Jun 20 20:13:31 PDT 2007


Just for interest's sake - I got it to work.

I had to do this:

tagContent
	:
	cfml
		(
		{ $tagScope::currentName.equals(input.LT(1).getText().toLowerCase().substring(2))
}?=> (endTag)
		)
	;
catch [FailedPredicateException fpe]
{
	retval.stop = input.LT(-1);
	retval.tree = (Object)adaptor.rulePostProcessing(root_0);
	adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop);
}

Otherwise, my rewrite rule would get a NullPointerException.

But this works *exactly* how I wanted it to, so I'm not going to change it.

Thanks for all the help Terrence.

Mark

On 6/21/07, Mark Mandel <mark.mandel at gmail.com> wrote:
> So I cleaned up the grammar a little:
>
> 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
>         :
>         cfml
>                 (
>                 { $tagScope::currentName.equals(input.LT(1).getText().toLowerCase().substring(2))
> }?=> (endTag)
>                 |
>                 )
>         ;
>
> endTag
>         :
>         END_TAG_OPEN^ END_TAG_CLOSE
>         ;
>
> Which solved my multiple rule path issues, but I am still getting a
> NoViableAltException on the test case of <a><c></a>
>
> I've attached the debug parse tree, and you can see that when it hits
> the predicate in tagContent, it doesn't skip back up to look at the
> endTag rule for the previous tag.
>
> Looks like I'm going to have to build the tree by hand... not sure
> what else to do.
>
> Thanks for the help,
>
> Mark
>
> On 6/20/07, Terence Parr <parrt at cs.usfca.edu> wrote:
> >
> > On Jun 20, 2007, at 3:42 PM, Mark Mandel wrote:
> >
> > > Nice to have you on Australia time :D
> > >
> > > With trying to do it with a predicate ? rule, I keep hitting a
> > > FailedPredicateException, as it looks at the </a>, and then goes
> > > 'well.. I have nothing at my end rule to match as well.. so umn.. I'm
> > > going to throw an error.
> >
> > Hmm...that should never fail, it should only direct the parse.
> > >
> > > I tried trying to match it to EOF, but that didn't manage to work -
> > > something like:
> > >
> > > agContent
> > >       :
> > >       tag* (
> > >               {
> > > $tagScope::currentName.equals(input.LT(1).getText().toLowerCase
> > > ().substring(2))
> > > }?
> > >               (
> > >                       endTag
> > >               )
> > >               |
> > >               EOF
> > >               )
> > >
> > > But I get noViableToken errors with that...
> >
> > Hmm...well, ( endTag) can be just endTag, but that's not it.  Should
> > be ( {...}? endTag | ).  Doesn't that work?
> >
> > Ter
> >
> >
>
>
> --
> E: mark.mandel at gmail.com
> W: www.compoundtheory.com
>
>


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


More information about the antlr-interest mailing list