[antlr-interest] gunit use in ANTLR v4

Terence Parr parrt at cs.usfca.edu
Sun Jan 24 16:40:43 PST 2010


Hiya, been using Leon's gunit to test ANTLR's AST builder...works great!  I made a few improvements (for next v3 ANTLR release).  Here's a few samples:

grammarSpec:
    "parser grammar P; a : A;"
    -> (PARSER_GRAMMAR P (RULES (RULE a (BLOCK (ALT A)))))

    <<
    parser grammar P;
    options {k=2; output=AST;}
    scope S {int x}
    tokens { A; B='33'; }
    @header {foo}
    a : A;
    >>
    ->
    (PARSER_GRAMMAR P
    (OPTIONS (= k 2) (= output AST))
    (scope S {int x})
    (tokens { A (= B '33'))
    (@ header {foo})
    (RULES (RULE a (BLOCK (ALT A)))))

block:
	"( ^(A B) | ^(b C) )" -> (BLOCK (ALT ("^(" A B)) (ALT ("^(" b C)))
	
alternative:
	"x+=ID* -> $x*" ->
	    (ALT_REWRITE
		    (ALT (* (BLOCK (ALT (+= x ID)))))
            (-> (ALT (* (BLOCK (ALT x))))))

	"A -> ..." -> (ALT_REWRITE (ALT A) (-> ...))
	"A -> "	   -> (ALT_REWRITE (ALT A) (-> EPSILON))

element:
	"b+"		-> (+ (BLOCK (ALT b)))
	"(b)+"		-> (+ (BLOCK (ALT b)))
	"b?"  		-> (? (BLOCK (ALT b)))
	"(b)?"		-> (? (BLOCK (ALT b)))
	"(b)*"		-> (* (BLOCK (ALT b)))
	"b*"		-> (* (BLOCK (ALT b)))
	"'while'*"	-> (* (BLOCK (ALT 'while')))
	"'a'+"		-> (+ (BLOCK (ALT 'a')))
	"a[3]"		-> (a 3)
	"'a'..'z'+" -> (+ (BLOCK (ALT (.. 'a' 'z'))))

Pretty cool, eh?

Ter


More information about the antlr-interest mailing list