[antlr-interest] validating semantic predicates

Mark Volkmann r.mark.volkmann at gmail.com
Fri Nov 30 09:20:26 PST 2007


On 11/30/07, Harald Mueller <harald_m_mueller at gmx.de> wrote:
> Hi -
>
> You are already in a tree-parser, aren't you?

Yes.

> So some syntax parser has produced your trees (I don't assume you create the trees manually in some other way).

Right.

> That syntax parser can then - in its tree-building rules (-> ...) - create trees which actually DO have terminating tokens - there is some documentation on such artificial tokens in the Wiki and also - I think - in the ANTLR book.

Genius! Thanks!

I think I'm almost there, but it still stops processing after it
completes the catch block.

Here's the AST I'm processing.

 (SCRIPT (ADD 10 END) (ADD 20 END) (PRINT END) (SUBTRACT 90 END) (IF
POSITIVE (PRINT END)) (ADD 10 END) (IF NEGATIVE (PRINT END)) (IF (< 0)
(ADD 100 END)) (IF (< 40) (SUBTRACT 10 END)) (IF (> 30) (SUBTRACT 20
END)) (PRINT END))

Everything works up to the first IF. At that point the value is 10 +
20 - 90 which is -60 so the first if fails since the value isn't
positive. Here's the rule with the catch.

statement
  : simpleStatement
  | ^('IF' c=condition { $c.result }? simpleStatement)
  ;
  catch[FailedPredicateException fpe] {
    System.err.println("recoving and continuing");
    consumeUntil(input, END);
    input.consume();
  }

I get the println output, but the ADD that follows isn't executed. I
know because the action for ADD prints out something every time it
runs. Any idea what processing stops after the catch executes?

> Hope this helps ...
> Regards
> Harald
>
> -------- Original-Nachricht --------
> > Datum: Fri, 30 Nov 2007 09:37:19 -0600
> > Von: "Mark Volkmann" <r.mark.volkmann at gmail.com>
> > An: antlr-interest at antlr.org
> > Betreff: [antlr-interest] validating semantic predicates
>
> > I'm trying to determine how to use validating semantic predicates in a
> > rule that has no terminating token. Here's an example of the approach
> > I've tried.
> >
> > statement
> >   : simpleStatement
> >   | ^('if' c=condition { $c.result }? simpleStatement)
> >   ;
> >   catch[FailedPredicateException fpe] {
> >     consumeUntil(input, STMT_TERMINATOR);
> >     input.consume();
> >   }
> >
> > The problem is that simpleStatements have no terminating token. They
> > are a keyword optionally foIlowed by a number. I know whether to
> > expect the number based on the keyword. There's no such thing as
> > STMT_TERMINATOR in my grammar.
> >
> > I think in order to continue parsing when the condition evaluates to
> > false, I have to consume all the tokens that make up the
> > simpleStatement being skipped. How can I consume those tokens?
> >
> > --
> > R. Mark Volkmann
> > Object Computing, Inc.
>
> --
> Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
> Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
>


-- 
R. Mark Volkmann
Object Computing, Inc.


More information about the antlr-interest mailing list