[antlr-interest] Need help on constructing trees
Mo
m.axmed at gmail.com
Fri Jul 14 10:05:04 PDT 2006
Hi kototama,
assuming that you want your code to look like:
public foo (blah) {
method methodName(blah) {
}
}
then the following will build a tree for you
----
the lexer
LPREN : '(';
RPREN : ')';
LBRACE : '{';
RBRACE : '}';
COMMA : ',';
SEMI : ';';
IDENT
options {testLiterals=true;}
: ('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9')* ;
NUMBER options {testLiterals=true;}
: ('0'..'9')+ ('.' ('0'..'9')*)? | '.' ('0'..'9')+;
STRING
: '"' ( EscapeSequence | ~('\\'|'"') )* '"' ;
EscapeSequence
: '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\') ;
----------
the parser
public_section
:
"public"^ IDENT LBRACE! (method )* RBRACE!
;
method
: "method"^ IDENT LPREN arguments RPREN
LBRACE!
(var_decl)*
RBRACE!
;
atom
: INIT_VALUE
| IDENT
| STRING
;
arguments
: (atom (COMMA! atom)*)
;
On 7/14/06, kototama kototama <kototamo at gmail.com> wrote:
> Hi,
>
> Here is an extract of my grammar for a tiny programming language :
>
> public_section!: PUBLIC LBRACE v:var_decl_list m:method_def_list RBRACE
> {#public_section = #( [PUBLIC_SECTION, "public_section"],
> #([VAR_DECLS,"var_decls"], v) , #([METHOD_DEFS, "method_defs"], m) );}
> ;
>
> var_decl_list: (var_decl)*
> ;
>
> var_decl!: LET t:type i:id (ASSIGN v:value)? EOS
> { #var_decl = #([VAR_DECL, "var_decl"], t, i, #([INIT_VALUE, "value"],
> v)); }
> ;
>
> I would like to simplify the public_section like this :
>
> public_section!: PUBLIC LBRACE (v:var_decl)* m:method_def_list RBRACE
>
> to avoid the definition of var_decl_list. Is it possible ? I don't find the
> correct way to build the tree ("var_decls" contains only the last definition
> if I do so).
>
> Thanks in advance for your help!
>
More information about the antlr-interest
mailing list