On Wed, Jan 12, 2011 at 5:02 PM, Alex Lujan <[email protected]> wrote:
> I am trying Antlr v3 for message recognition, which must comply to a custom
> define protocol.
>
> I find myself unable to express the following rule (written in a
> non-standard notation):
>
> data: count number{count.value}
>
> What I am expecting to find in the input is a numerical value (which Ive
> labeled count), followed by as many numerical values as the value of count.
>
> Examples of valid input:
>
> 3 10 12 15
> 2 6 9
>
> Examples of invalid input:
>
> 3 10 12
> 2 4 5 6
>
>
You could use a 'validating semantic predicate'. A small demo:
*grammar Test;*
*
*
*parse*
* : line+ EOF*
* ;*
*
*
*line*
* : n=number {int c = $n.value;} (number {--c;})* {c==0}? LineBreak*
* ;*
*
*
*number returns [int value]*
* : Int {$value = Integer.parseInt($Int.text);}*
* ;*
* *
*Int*
* : '0'..'9'+ *
* ;*
*
*
*LineBreak*
* : '\r'? '\n'*
* | '\r'*
* ;*
* *
*Space*
* : (' ' | '\t') {skip();}*
* ;*
Regards,
Bart.
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address
--
You received this message because you are subscribed to the Google Groups
"il-antlr-interest" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/il-antlr-interest?hl=en.