[antlr-interest] revised subject: tree matcher problem with validating semantic predicate

Tom Smith yotommy at gmail.com
Wed Aug 5 14:34:14 PDT 2009


I have narrowed down my problem.  The validating semantic predicate
properly throws a FailedPredicateException in a regular tree grammar,
but does not when filter=true.

I added the following TreeMatcher.g to the VecMath example from the
new book.  (I invoke this TreeMatcher before the Printer.)  Note that
the INT alternative has a validating semantic predicate that is
guaranteed to fail.  However when I run the program, no sign of the
FailedPredicateException.

Am I wrong to expect this?

Thanks,
Tom.

tree grammar TreeMatcher;

options {
	tokenVocab=VecMath;      // use token vocabulary from VecMath.g
	output = AST;
	ASTLabelType = CommonTree;
	filter = true;
}

topdown:	expr;

// START: expr
expr:   ^('+' expr expr)
    |   ^('*' expr expr)
    |   ^('.' expr expr)
    |   ^(VEC expr (expr)*)
    |   INT {false}?
    |   ID
    ;
// END: expr


On Tue, Aug 4, 2009 at 2:02 PM, Tom Smith<yotommy at gmail.com> wrote:
> I have a rule (in a tree pattern matcher) that uses a validating
> semantic predicate.  When the predicate returns false, I expected to
> catch a FailedPredicateException and take some custom action.
> However, the FailedPredicateException is not getting thrown.  Looking
> at the generated code, the problem appears to be that the exception
> only gets thrown conditionally (if state.backtracking==0).  For some
> reason that is beyond my meager understanding, state.backtracking = 1
> when the semantic predicate fails.
>
> Any clues for me?
>
> Thanks,
> Tom Smith.
>
> tree grammar Trans;
>
> options {
>        tokenVocab=SsipAST;
>        output=AST;
>        ASTLabelType=CommonTree;
>        filter = true;
> }
> ...
> transition
> scope {
>    ...
> }
> @init {
>    ...
> }
>        :       ^(TRANSITION s1=stateName s2=stateName arcBody[$s1.text])
>        { checkTransition($s1.text, $s2.text) }?
>        { System.err.println("Transition from " + $s1.text + " to " +
> $s2.text + " is OK"); }
>        ;
>        catch [FailedPredicateException fpe] {
>                String msg = String.format("In transition from \%s to \%s, liveness
> is not consistent", $s1.text, $s2.text);
>                throw new SemanticException(msg);
>        }
>


More information about the antlr-interest mailing list