[antlr-interest] Newbie question

Alexey Demakov demakov at ispras.ru
Fri Aug 19 00:16:58 PDT 2005


Hi

There is no such constructs in BNF notation used bu ANTLR,
but you can check this restriction and even more complex ones
using semantic predicates.

rule : ( occurence )* ;

matches 'occurence' zero or more times.

rule
{ int count = 0; } // counter
:
( { count < MAX }? // check additional stop condition
  occcurence 
  { count++; }     // increase counter
)*
// after loop has ended, check that counter is in range
{ MIN <= count && count <= MAX }?
;

MIN and MAX are user-defined variables or constants.
So, both your cases are covered.

{ count < MAX }? is semantic predicate checked 
before each 'occurence' match. If it is false, loop will stop.
Instead of 'count < MAX' there can be any expression.

{ MIN <= count && count <= MAX }? is semantic predicate checked after loop.
If it is false, SemanticException will be thrown.
So, it is other usage of semantic predicate.
Of course, 'MIN <= count && count <= MAX' can be any expression.

Best regards,
Alexey
---
ICQ: 740187



----- Original Message ----- 
From: "Yaron Kretchmer" <YKRETCHM at altera.com>
To: <antlr-interest at antlr.org>
Sent: Friday, August 19, 2005 10:39 AM
Subject: [antlr-interest] Newbie question


> Hi All.
> I've started playing around with Antlr, and was wondering if you can
> point me to where I can find documentation on the following:
> *) An example of a rule to match a regular expression where the number
> of occurrences is fixed , and not zero-or-more or one-or-more (say like
> [0|1]{4} in PERL)
> *) An example of a rule to match a regular expression where the number
> of occurrences is a variable that we've parsed from the input stream
> (like [0|1]{$length} in PERL.
> 
> Yaron




More information about the antlr-interest mailing list