[antlr-interest] Semantic Predicates inside syntactic ones?

Ric Klaren klaren at cs.utwente.nl
Tue Dec 9 02:55:06 PST 2003


Okay I know there's other ways to solve my specific problem but I got to
wonder... (This probably has been discussed somewhere earlier but I could
not find relevant posts the search in yahoogroups is pretty crummy)

The situation: I want to pick out certain assignment expressions in a tree
walker to generate different output code from my tool. The old generic rule:

   #( "=" lhs1:write_expr[output,false]      { output << "->setValue( "; }
          rhs1:write_expr[output,true] )
      output << " )";

Now I need to detect assignments to a specific typed variable:


Where the id is a clock typed variable and the INT/FLOAT is zero.
(clock reset in a process algebra)

I could of course generate different trees for this type of assignment (got
all the tools in my parser to do the trick there). But I want to keep
things as generic as possible since we will be targetting multiple
formalisms from our language.

So I wondered if I could do the following since it's not particularly
farfetched (omitted 0 check for brevity):

   ( #( "=" id:IDENTIFIER { isClock(#id->getSymbol() ) }? (INT|FLOAT) ) ) =>
      #( "=" IDENTIFIER ( INT | FLOAT ) )
      // generate clock reset code
|  #( "=" write_expr[output,false]     { output << "->setValue( "; }
          write_expr[output,true] )
      output << " )";
| ... ;

Well to answer my question no I cannot do this with vanilla antlr. The code
for the semantic predicate is inserted correctly but ANTLR does not assign
to the referenced label (e.g. no id = _t) Being an antlr hacker I noticed
pretty quick that I can enable this trick by uncommenting a few odd checks
over the codegenerators.

Aside note: Currently it's btw pretty stupid that antlr does not generate
warnings for labels on tokens in syntactic predicates.. one for the todo

Thoughts comments?


    ---- Ric Klaren ----- j.klaren at utwente.nl ----- +31 53 4893722  ----
 'And this 'rebooting' business? Give it a good kicking, do you?' 'Oh, no,
  of course, we ... that is ... well, yes, in fact,' said Ponder. 'Adrian
    goes round the back and ... er ... prods it with his foot. But in a
     technical way,' he added. --- From: Hogfather by Terry Pratchett.


Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 

More information about the antlr-interest mailing list