[antlr-interest] embedding Java codes to grammar problem
Panayiotis
panayk at gmail.com
Mon Jun 4 04:46:31 PDT 2007
Fırat KÜÇÜK wrote:
> what about "while statement" situation?
>
> 2007/6/4, Panayiotis <panayk at gmail.com>:
>> I'm not sure I understand what you want to do, but would the
>> following work?
>>
>> sample_rule
>> : SOME_TOKEN bla_bla_rule ({1 == 1}? another_rule)?
>> ;
>>
>> This will parse 'SOME_TOKEN' followed by 'bla_bla_rule' and then,
>> depending on the truth value of the semantic predicate '1 == 1', it will
>> try to parse an optional 'another_rule'.
>> A related syntax is {1 == 1}?=>.
>>
>> Panayiotis
>>
>> Fırat KÜÇÜK wrote:
>> > any solution?
>> >
>> > 2007/6/1, Fırat KÜÇÜK <firatkucuk at gmail.com>:
>> >> hi,
>> >>
>> >> for instance my treeparser rule is
>> >>
>> >>
>> >> sample_rule
>> >> : ^(SOME_TOKEN
>> >> bla_bla_rule
>> >> { if (1 == 1) { }
>> >> another_rule_should_be_in_if_statement
>> >> { } }
>> >> )
>> >> ;
>> >>
>> >>
>> >> "another_rule_should_be_in_if_statement" rule should be in "if (1==1)
>> >> {}" statement.
>> >> What should i do?
>> >>
>> >> --
>> >> Öğr. Gör. Fırat KÜÇÜK
>> >> ADAMYO Distance Learning
>> >> SAKARYA University / TURKEY
>> >>
>>
I suppose:
sample_rule
: SOME_TOKEN bla_bla_rule ({1 == 1}? another_rule)*
;
or
sample_rule
: SOME_TOKEN bla_bla_rule another_rule ({1 == 1}? another_rule)*
;
(a)* means: apply (a) zero or more times (kleene closure).
(a)? means: apply (a) zero or one time(s), i.e.: (a | empty)
(a)+ means (a) one or more times, i.e.: (a) (a)*
So I expected the last one to be equivalent to:
sample_rule
: SOME_TOKEN bla_bla_rule ({1 == 1}? another_rule)+
;
But it doesn't seem to work the same way.
Maybe someone can recommend a good tutorial.
Panayiotis
More information about the antlr-interest
mailing list