[antlr-interest] Building an AST Tree?

Johannes Luber jaluber at gmx.de
Thu Jul 24 14:10:46 PDT 2008


Setia, Namit schrieb:
> Thanks for the help.
> 
> Does anyone know of any good tutorials/examples on how to build an AST
> with c#?  When I get the top level tree, it's children are the
> individual characters of the string I passed in.  I'm not quite sure
> what the approach should be to get it to parse into an AST (Maybe my
> understanding is wrong, but I'm trying to get a tree like what you see
> in the antlrworks interpreter).
> 
> I've been trying to find some good examples for the latest version of
> antlr, but none of the csharp samples I could find work.
> 
> Antlr.Runtime.ICharStream str = new
> Antlr.Runtime.ANTLRStringStream("ap=function(\"hello\",4,5);");
> qss_grammarLexer qlex = new qss_grammarLexer(str);
> Antlr.Runtime.CommonTokenStream tokens = new
> Antlr.Runtime.CommonTokenStream(qlex);
> qss_grammarParser grammar = new qss_grammarParser(tokens);
> qss_grammarParser.qexprlist_return ret = grammar.qexprlist();
> CommonTree t = (CommonTree)ret.Tree;
> 
> --> t.Children is an array  of tokens [ {a},{p},{=},{f},{u},{n}....]  

Actually, your grammar below works as expected. You haven't put any 
rewrite rules into the grammar so get always a flat list. I annotate a 
few rules as examples. Those new all uppercase symbols are imaginary 
tokens defined in the tokens section.

tokens {
QEXPR_LIST;
etc.
}

Johannes
> 
> ------------------------------------------------------------------------
> ---
> 
> grammar qss_grammar;
> 
> options
> {
> 	language=CSharp2;
> 	output=AST;
> }
> 
> qexprlist
>   : ( qassignment_statement )* EOF
>   ;
qexprlist
   : ( qassignment_statement )* EOF -> ^(QEXPR_LIST qassignment_statement)
   ;

> 
> qassignment_statement
>   : qassignment SEMI { Console.Write("hello"); }
>   ;

qassignment_statement
   : qassignment SEMI { Console.Write("hello"); } -> qassignment
   ;

> 
> qassignment
>   : (qname ASSIGN )? qexpr
>   ;
qassignment
   : (qname ASSIGN )? qexpr -> ^(QASSIGNMENT ^(OPTIONAL (qname ASSIGN)?) 
qexpr)
   ;
> 
> qprimary_expr
>   : qint
>   | qvname
>   | (LPAREN qexpr RPAREN ) 
>   | qfunction
>   | qarray
>   | qstring
>   ;
> 
> qsign_expr
>   : qprimary_expr
>   ;
> 
> qmul_expr
>   : qsign_expr (( MUL | DIV | MOD ) qsign_expr)*
>   ;
>   
> qexpr
>   	:	 qmul_expr (( PLUS | MINUS ) qmul_expr)*
>   ;
> 
> 
> 
> PLUS	:	'+';
> MINUS	:	'-';
> DIV	:	'/';
> MUL	:	'*';
> MOD	:	'%';
> POW	:	'^';
> LETTER	:	'a'..'z'|'A'..'Z';
> DIGIT	:	'0'..'9';
> PER	:	'.';
> SEMI	:	';';
> ASSIGN	:	'=';
> EQ	:	'=''=';
> NQ	:	'!''=';
> LEQ	:	'<''=';
> GEQ	:	'>''=';
> LPAREN	:	'(';
> RPAREN	:	')';
> 
> qint	:	(DIGIT)*;
> qname	:	LETTER(LETTER|DIGIT)*;
> qstr	:	(LETTER|DIGIT)*;
> qstring	:	'"' (LETTER|DIGIT)* '"';
> qinputval:	(LETTER|DIGIT)*;
> qvname	:	(LETTER(LETTER|DIGIT)*)(':'qinputval('.'qinputval)*)?;
> qarray	:	'{' (qstring|qvname)(','(qstring|qvname))* '}';
> qfunction:	(LETTER(LETTER|DIGIT)*)'('
> qprimary_expr(','qprimary_expr)* ')';
> 
> 
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> 
> This message is intended only for the personal and confidential use of the designated recipient(s) named above.  If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited.  This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers.  Email transmission cannot be guaranteed to be secure or error-free.  Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such.  All information is subject to change without notice.
> 
> --------
> IRS Circular 230 Disclosure:
> Please be advised that any discussion of U.S. tax matters contained within this communication (including any attachments) is not intended or written to be used and cannot be used for the purpose of (i) avoiding U.S. tax related penalties or (ii) promoting, marketing or recommending to another party any transaction or matter addressed herein.
> 



More information about the antlr-interest mailing list