[antlr-interest] Re: known number of repetitions..

Sriram Durbha cintyram at yahoo.com
Mon Oct 7 09:03:08 PDT 2002


hi,
  thank you very much for hte quick response.. 
actually 2 and 3 were solved pretty easily i just had
to go thru the documentatin once again.  

now i have more issues.. 

but first continuing with  
== 1 =========

your solution works.. 
but suppose there is a special case where the records
starting from record number 1001 are to be put into
anotehr set instead of issuing an error.. 
what can be done? in that case how do we return to the
same rule, reset our counter? 
one solution i have is to have conditinally recursive
rules.. 


some thing like this ..


rule1: 
       // count and match upto 1000 records..
       // if 1001th record is encountered 
       // go to  nextrule

nextrule : 
       // reset teh counter and go to rule1
 

i havent tried it but hope there is a way to do it
with predll(k).. 

==========================================

the new issue is as follows..
i am writing a translator from on elanguage to
another.. 

if you look at the source file , translating it by
hand is very easy, because almost line by line
translation is possible.. 
but there are certain exceptions wehre this is not
possible ;


also language constructs may span over multiple
lines.. 

eg: 
number a ,  // comment for a 
       b ,  // comment for b
       c    // comment for c

this has to be translated into 

a , (* comment for a *)
b , (* comment for b *)
c : real ; (* comment for c *)

on inspection writing a grammar for this looked like
an over kill especially when some how what ever i
write ends up throwing all kinds of tantrums ..

so i wrote a c++ class which handles it line by line 
1. tokenize based on '\n'
2. from each line look for first '//'
3. retain the comment 
4. translate the first part , append  comment 

how ever thsi strategy is very confusing for cases
wehre do .. while has to be translated into while or
for 

have to retain all lines in the loop which might
contain more loops.. 

so i want to develop mini translators [lex,parse,if
-walk ] for specific constructs and call them
dynamically from the line by line processor 
when ever necessary ;

an initial assessment of the code generated by antlr
for small grammars makes me feel that this is better,
however im not sure plese do chip in with more inputs 
and your own analysis of my strategy .. 

thank you
warm regards
ram

============== eom =================

--- lgcraymer <lgc at mail1.jpl.nasa.gov> wrote:
> --- In antlr-interest at y..., Sriram Durbha
> <cintyram at y...> wrote: 
> > == 1 =========
> >  this is with reference to writing a rule for
> numbers
> > 
> > in english : " a number can be any sequence of
> digits
> > upto a maximum of 6; so  0 - 999999 is a valid
> number
> > "
> > but 1234567 is not 
> > 
> > what is the best way to write such a rule?
> > 
> > also what to do if the known number is really
> large
> > like" a maximum of 1000 records makes a recset "
> > 
> > but anything more than 1000 is invalid ;
> > 
> 
> For answers to 2), and 3), check the FAQs at
> www.antlr.org.
> 
> For this one, the consideration is what to do if
> numbers are outside 
> the accepted range.  Do you really want to consider
> 1234567 to be 
> interpreted as two tokens, 123456 and 7?  Or do you
> want to accept 
> 1234567 as a single token and detect an error
> condition?  I suspect 
> that the second alternative is preferable, and that
> the same is true 
> for recset.
> 
> I'll assume that your grammar has a DIGIT rule:
> protected
> DIGIT
> 	:	'0'..'9'
> 	;
> 
> The first case is handled in LISP style (lots of
> irritating single 
> parentheses:
> 
> NUMBER
>         :
>         DIGIT
>         (    DIGIT
>              (     DIGIT
>                    (     DIGIT
>                          (     DIGIT
>                                (     DIGIT
>                                )?
>                          )?
>                     )?
>               )?
>         )?
>         ;
> 
> Yes, it's ugly, but it interprets a number as a
> sequence of from one 
> to six digits.
> 
> For the second case, you accept any number of digits
> 
> NUMBER
> returns [ int counter; ]
>      :
>      { counter = 1; }
>      DIGIT (DIGIT {counter++;)*
>      ;
> 
> and either add code to throw an error if counter > 6
> in NUMBER (then 
> you don't need the return value) or do validate the
> return value where 
> NUMBER is used and use the ANTLR error handling
> stuff to throw a 
> meaningful exception.
> 
> --Loring
> 

__________________________________________________
Do you Yahoo!?
Faith Hill - Exclusive Performances, Videos & More
http://faith.yahoo.com

 

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



More information about the antlr-interest mailing list