[antlr-interest] first steps with a lexer/parser

向秦贤 fyaoxy at gmail.com
Thu Jan 3 18:35:00 PST 2008


Hi,
maybe you wanna check out that ON grammar at:
https://on.dev.java.net
You will see you could take little pain on typing:)

2008/1/3, body <antlr-list at splitbody.com>:
>
> hello,
>
> i am trying to deal with the messages that look like this:
>
> { a=1 b="2" c="t" d="stuff" e="one two" f={ g="three four" h={ i=5
> j="a ha" } } }
>
> below is my lexer/parser. it seems to work and emit proper-looking
> tree, but i want to run it by you, because it does not feel right.
>
> it seems like i should be using fragments somewhere, also i cannot
> figure out how to build a proper tree grammar out of it.
>
> any suggestions appreciated.
>
> thank you.
>
> -----------------
> grammar MsgString;
>
> options { output = AST; }
>
> tokens {
>         PAIR;
>         MSG;
>         STR_VAL;
>         INT_VAL;
> }
>
> start  :    msg NL? EOF -> ^(MSG msg) ;
>
> msg    :    '{' WS nameValuePairExpr* WS '}' -> ^(MSG nameValuePairExpr*)
> ;
>
> nameValuePairExpr
>        :    NAME '=' valueExpr WS? -> ^(PAIR NAME valueExpr) ;
>
> valueExpr
>        :    quotedString -> ^(STR_VAL quotedString)
>        |    INT -> ^(INT_VAL INT)
>        |    msg
>        ;
>
> quotedString
>        :    '"'! .* '"'!
>        ;
>
> INT    :    '0'..'9'+ ;
>
> NAME   :    ('a'..'z'|'A'..'Z'|'0'..'9')+ ;
>
> WS     :    ' '+ ;
>
> NL     :    ('\n'|'\r')+ ;
> -----------------
>



-- 
致敬
向秦贤
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080104/62449a53/attachment.html 


More information about the antlr-interest mailing list