[antlr-interest] Lists. Lexer or Parser?

Johannes Luber jaluber at gmx.de
Sat Sep 13 03:34:24 PDT 2008


Dave Pawson schrieb:
> 2008/9/13 Gavin Lambert <antlr at mirality.co.nz>:
>> At 01:58 13/09/2008, Dave Pawson wrote:
>>> Obvious next question, how to do it in the Parser please?
>> Just output whatever you want.  Like so:
>>
>> list : { Console.writeLine("<list>"); } item* {
>> Console.writeLine("</list>"); } ;
>> item : TEXT { Console.writeLine("<item>", $TEXT.text, "</item>"); } ;
> 
> That's the problem? There is no textual content for the lexer to
> trigger on for 'list'?
> I can define 'item', but not list?
> 
> The input is
> 
> blah
> blah
> 
> *list item 1
> * list content 2

There is one. I'd define a grammar like:

grammar Test;

tokens {
LIST='LIST';
STAR=''*';
}

CONTENT: ('a'..'z' | 'A'..'Z')+;
NEWLINE: '\r' | '\n';
WHITESPACE: ('\t' | ' ')+ {$channel = HIDDEN;};

start: line*;

line: content+ NEWLINE+
    | STAR LIST content+ NEWLINE+
    ;
> 
> etc.
> 
>>> I'm not that far in the Antlr book - seems like I do need a tree
>>> with a non-existant node to hang the list item children from.
>> Maybe you should read more of the book :)
> 
> I will, though it is discouraging that its content is 3.0 based.

3.1 has not many differences to 3.0, which are backwards-incompatible.
Look into the release notes for a list.

Johannes
> 
>> And you can do it like that if you want to (eg. if you want to generate an
>> AST you can manipulate or generate multiple kinds of output from), but if
>> your needs are simple then you might as well just do the output directly in
>> the parser.
> 
> 
> As I said, this is just me learning by example as I go.
> 
> regards
> 
> 
> 



More information about the antlr-interest mailing list