[antlr-interest] Reading a string of fixed size
Alexandre Hamez
alexandre.hamez at lip6.fr
Mon Aug 27 09:02:15 PDT 2007
Thanks for your interest. But the fact is that I want to create a
Token with exactly NUMBER characters. The following characters will
match for other token. It's not an error if there are characters
which follow. Its mean that I can have something like that:
CAMI_STRING (',' CAMI_STRING)*
( for newcomers: CAMI_STRING : NUMBER ':' STRING where the size of
STRING is given by NUMBER).
Moreover, as a strange side effect of the following code, newlines
make the parser completely mad:
> CAMI_STRING
> :
> NUMBER ':'
> {
> // Get the current position in stream
> int start = input.getCharPositionInLine();
> // Computing the position of the last character of the STRING to
> be read
> int end = start + Integer.parseInt($NUMBER.text) - 1;
> // Set the value of the returned value to STRING
> setText(input.substring(start,end));
> // Update the position in the stream
> input.seek(end+1);
> }
> ;
On 27 août 07, at 12:24, Edwards, Waverly wrote:
> I gave it another chance, not knowing you could used rules as
> variables. Here is something a little more terse CAMI_STRING2
> though I generate an error which I don't understand how to get rid
> of. It didn't happen in the CAMI_STRING version.
>
> The error:
> line 1:0 required (...)+ loop did not match anything at input
> '5:abcde'
>
>
> W.
>
> grammar TESTCAMISTRING;
>
> fragment
> Letter : 'a'..'z' | 'A'..'Z' ;
>
> fragment
> Digit : '0'..'9' ;
>
> fragment
> AlphaNum : Letter | Digit ;
>
> fragment
> Number : Digit+ ;
>
> fragment
> AlphaNumStr : Letter AlphaNum* ;
>
>
> prog : start+ ;
>
> start : CAMI_STRING2
> ;
> NEWLINE : ('\r\n'|'\r'|'\n') ;
>
>
> // works just fine but is not as efficient or terse as CAMI_STRING2
> CAMI_STRING : Digit+ ':' Letter AlphaNum*
> {
> String wholeStr,numStr,fixLenStr;
> int colonLoc, num;
> boolean validate;
>
> wholeStr = getText().toString();
> colonLoc = wholeStr.indexOf(":");
> numStr = wholeStr.substring(0, colonLoc);
> num = Integer.parseInt(numStr);
> fixLenStr = wholeStr.substring(colonLoc+1);
> validate = (num == fixLenStr.length() );
> if ( validate == true )
> System.out.println(numStr + "," + fixLenStr );
> else
> System.out.println("ERROR: num:Str length mismatch");
> };
>
>
> CAMI_STRING2 : Number ':' AlphaNumStr
> {
> int num = Integer.parseInt($Number.text);
> String fixLenStr = $AlphaNumStr.text;
> boolean validate = (num == fixLenStr.length() );
> if ( validate == true )
> System.out.println( num + "," + fixLenStr );
> else
> System.out.println("ERROR: num:Str length mismatch");
> };
>
>
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Edwards, Waverly
> Sent: Monday, August 27, 2007 5:53 AM
> To: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] Reading a string of fixed size
>
>
> Here is something that is functional.
>
> I'm a newbie at all of this, ANTLR and Regular expressions so don't
> take this as the Gospel. You'll want to check this out carefully.
>
> I didn't even know you could do this: Integer.parseInt($NUMBER.text)
>
> If you can, this code could be shorter and I will have learned
> something else useful.
>
>
> W.
>
>
>
> grammar TESTCAMISTRING;
>
> fragment
> Letter : 'a'..'z' | 'A'..'Z' ;
>
> fragment
> Digit : '0'..'9' ;
>
> fragment
> AlphaNum : Letter | Digit ;
>
> prog : start+ ;
>
> start : CAMI_STRING
> ;
> NEWLINE : ('\r\n'|'\r'|'\n') ;
>
>
> CAMI_STRING : Digit+ ':' Letter AlphaNum*
> {
> String wholeStr,numStr,fixLenStr;
> int colonLoc, num;
> boolean validate;
>
> wholeStr = getText().toString();
> colonLoc = wholeStr.indexOf(":");
> numStr = wholeStr.substring(0, colonLoc);
> num = Integer.parseInt(numStr);
> fixLenStr = wholeStr.substring(colonLoc+1);
> validate = (num == fixLenStr.length() );
> if ( validate == true )
> System.out.println(numStr + "," + fixLenStr);
> else
> System.out.println("ERROR: num:Str length mismatch");
> };
>
>
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Alexandre Hamez
> Sent: Monday, August 27, 2007 3:33 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Reading a string of fixed size
>
> Hi everybody,
>
> I need to read a string of a fixed size. For example : '5:abcde'.
> The size of the string to be read is given by the number before the
> ':'. So I've written the following:
>
> CAMI_STRING
> :
> NUMBER ':'
> {
> // Get the current position in stream
> int start = input.getCharPositionInLine();
> // Computing the position of the last character of the STRING to
> be read
> int end = start + Integer.parseInt($NUMBER.text) - 1;
> // Set the value of the returned value to STRING
> setText(input.substring(start,end));
> // Update the position in the stream
> input.seek(end+1);
> }
> ;
>
> NUMBER
> :
> '0'..'9'+
> ;
>
>
> It works quite well until the string to be read contains a number
> itself. Have you an idea on this problem? Thanks for you help!
>
>
> ----------------------------------------------------------------------
> --
> ---
> Alexandre Hamez LIP6 - MoVe / EPITA - LRDE
> LIP6: tel: +33 1 44 27 31 92 / Bureau 818
> 104 Avenue du Président Kennedy 75016 Paris http://www-src.lip6.fr/
> ~Alexandre.Hamez
>
>
>
------------------------------------------------------------------------
---
Alexandre Hamez LIP6 - MoVe / EPITA - LRDE
LIP6: tel: +33 1 44 27 31 92 / Bureau 818
104 Avenue du Président Kennedy 75016 Paris
http://www-src.lip6.fr/~Alexandre.Hamez
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2429 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20070827/242fbc5d/attachment-0001.bin
More information about the antlr-interest
mailing list