[antlr-interest] ANTLR3 Capturing System.out.println

Loring Craymer lgcraymer at yahoo.com
Thu Mar 29 07:05:33 PDT 2007


Kailey--

You want to use semantic predicates here, not
keywords, for names.  That is, try

println
    :
    {LA(1).getText().equals("System") &&
LA(3).getText().equals("println"} => ID
    DOT ID
    LPAREN
    outputExpression
    RPAREN
    ;

Still better is to use a symbol table for functions to
be translated (still using sempreds for
identification).

--Loring



--- Kailey Joanette <kaileyjo at bigbluebubble.com>
wrote:

> I need to make sure System.out.println and similar
> library calls can be converted.  So I need to put a
> rule in that will work to do so.  Right now I have
> on in the "statement" rule of the java 1.5 grammar. 
> It looks like this:
> 
> statement
> 	: block
>     | 'assert' expression (':' expression)? ';'
>     | 'if' parExpression statement ('else'
> statement)?
>     | 'for' '(' forControl ')' statement
>     | 'while' parExpression statement
>     | 'do' statement 'while' parExpression ';'
>     | 'try' block
>       catches 'finally' block
>     |	'try' block
>       catches
>     |	'try' block
>       'finally' block
>     | 'switch' parExpression '{'
> switchBlockStatementGroups '}'
>     | 'synchronized' parExpression block
>     | 'return' expression? ';'
>     | 'throw' expression ';'
>     | 'break' Identifier? ';'
>     | 'continue' Identifier? ';'
>     | ';'
>     | statementExpression ';'
>     | Identifier ':' statement
>     | println ';'
>     | currentTime ';'
> 	;
> 	
> currentTime
> :	'System' '.' 'currentTimeMillis' '(' ')' ->
> propagate( value={"GETUPTIMEMS()"} )
> ;
> 	
> println
> @init
> {
> 	outputString="";
> }
> @after
> {
> 	outputSymbols.clear();
> }
> :	 'System' '.' 'out' '.' 'println' '('
> outputExpression ')'
> 	// This is the end of the output string...so deal
> 		-> { outputSymbols.size() > 0 }? print(
> string={outputString}, symbols={new ArrayList(
> outputSymbols ) } )
> 		-> printNoSym( string={outputString} )
> ;
> 
> +===================
> 
> But doing that makes things break when it reaches
> OTHER System calls.  Says there are no viable
> alternative.
> 
> 



 
____________________________________________________________________________________
Get your own web address.  
Have a HUGE year through Yahoo! Small Business.
http://smallbusiness.yahoo.com/domains/?p=BESTDEAL


More information about the antlr-interest mailing list