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

body antlr-list at splitbody.com
Thu Jan 3 05:40:38 PST 2008


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')+ ;
-----------------


More information about the antlr-interest mailing list