[antlr-interest] Parsing length-prefixed strings...

Douglas Mayle dmayle at dmayle.com
Thu Sep 28 06:18:44 PDT 2006


Thanks for the reference, for anyone else who might be interested, the
project sources can be found at http://www.padproj.org

Unfortunately, PADS is distributed under a non-commercial license, which
makes it all but unusable for me.  

In addition, I have ANTLR already in use in my program, so I wanted to
reuse the libraries that I already had in use.  For anyone else having
the same problem, I solved it with formal parameters and a predicate,
like below:

options { language="Cpp";}
class myLexer extends Lexer;

SEMICOLON: ';'
INT: ('0'..'9')+ ;
LETTER: 'A'..'Z' | 'a'..'z';

class myParser extends Parser;

string: SEMICOLON! iCount:INT letter[ atof(iCount->getText().c_str()) ];

letter[int iCount]
    : { iCount > 1}? LETTER letter[iCount-1]
    | LETTER
    ;

Douglas Mayle


On Wed, Sep 27, 2006 at 09:46:05AM -0700, Terence Parr wrote:
> Hi, PADS by Kathleen Fisher is better suited to parsing this kind of  
> data:
> 
> http://portal.acm.org/citation.cfm? 
> id=1065046&dl=ACM&coll=&CFID=15151515&CFTOKEN=6184618
> 
> Terence
> On Sep 27, 2006, at 9:14 AM, Douglas Mayle wrote:
> 
> >I was hoping someone here might be able to point me in the right
> >direction.  I've read most of the FAQ, and the document, and I still
> >haven't got an idea where to head.  In the language I'm parsing a  
> >have a
> >a token stream that includes integers and characters.  However, my
> >string literals, rather than being bounded on both sides, have an
> >opening character, a two digit length, and then that many  
> >characters.  I
> >can't figure out how to match this fixed length stream with a  
> >parser.  A
> >string might look like:
> >
> >;05Hello
> >
> >or:
> >
> >;06World!
> >
> >So I might have
> >
> >print;05Helloprint;06World!
> >
> >Now, this isn't the actual language, but the principal is the same, I
> >can't capture the string with the lexer, and I know I have to do it  
> >with
> >the parser, but I'm not sure how...
> >
> >Douglas Mayle
> >
> >
> 


More information about the antlr-interest mailing list