[antlr-interest] Lists. Lexer or Parser?

Dave Pawson dave.pawson at gmail.com
Sat Sep 13 06:00:49 PDT 2008


2008/9/13 Johannes Luber <jaluber at gmx.de>:

> grammar Test;
>
> tokens {
> STAR=''*';
> }
>
> CONTENT: ~NEWLINE+; // possibly ~(NEWLINE|WHITESPACE)+
> NEWLINE: '\r' | '\n';
> WHITESPACE: ('\t' | ' ')+ {$channel = HIDDEN;};
>
> start: line*;
>
> line: content+ NEWLINE+
>    | STAR content+ NEWLINE+
>    ;
>
> You can add actions which determine the beginning and the end of the list.

I can't, that's where I startedQ

Tweaking the above to let it compile I get

grammar Test;


tokens {
STAR='*';
}

WHITESPACE: ('\t' | ' ') ;
CONTENT: ~(NEWLINE)+;
NEWLINE: '\r' | '\n';



start: line* ;

line:  (c=CONTENT NEWLINE ) {
            System.out.println("<para>"+ $c.text +"</para>\n" );}|
     STAR c=CONTENT+ NEWLINE+ {
            System.out.println("<list>"+ $c.text );}   ;



Test input
content only
* LIST list content
* LIST list content more


The output is
<para>content only</para>

<para>* LIST list content</para>

<para>* LIST list content more</para>



regards

-- 
Dave Pawson
XSLT XSL-FO FAQ.
Docbook FAQ.
http://www.dpawson.co.uk


More information about the antlr-interest mailing list