[antlr-interest] Re: Antlr grammar to parse Java classfile?
Terence Parr
parrt at jguru.com
Wed Dec 5 12:26:17 PST 2001
On Wednesday, December 5, 2001, at 12:12 PM, Andreas Rueckert wrote:
>
>> Anyway, concerning the "match n times" thing. You're right...it would
>> be pretty useful. What syntax is appropriate and how to do you say
>> 0...n vs 1..n? Perhaps, for uniformity, we use my "element modifier
>> syntax (e.g., "INT<AST=INTNode>"):
>>
>> ids4 : ( ID )+<n=4> ; // weird looking thing
>>
>> ids4 : ( ID )+<4> ; // a little better ("n" would be the default
>> variable)
>>
>> ids2opt : ( ID )*<2> ; // 0..2 not 1..2
>
> But in all these cases, you used constants. Many fileformats come a
> counter,
> that defines, how long the following data block is. So you have to
> parse the
> counter 1st, and then match the next rule <counter> times.
Oh, right. Yeah not a problem at all except for syntax. Should it
assume non-int implies variable defined in C++, Sather, or Java
(whatever your output is)? I'm partial to keeping all non-grammatical
stuff in {...}. Perhaps this then:
( ID )+{n=blockCount;}
or something?
>> The implementation would be pretty simple I guess. Just defines a
>> counter like the (...)+ does and generates an error if you don't get n;
>> counter<=n would need to go in the while loop, but should be easy.
>
> Yup! The implementation should be trivial. But since we don't have that
> feature
> yet, I'm trying to come up with something, that works with the current
> Antlr.
> I thought about the semantic predicate again and that might work, too.
> Like
> =======================================
> rule
> {int counter=0}
> : counter=blockCount
> ( {counter>0}? blockEntry {counter--;} )*
> ;
> =======================================
> Do you think it would work? So that block entry is matched <counter>
> times?
Let's see. Well, it's close, but it wouldn't give you an error if you
have less than blockCount. You need that "did I get enough" check at
the end of the loop. Could do with a simple "validating" sem pred after
loop.
Ter
--
Chief Scientist & Co-founder, http://www.jguru.com
Creator, ANTLR Parser Generator: http://www.antlr.org
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list