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

Kailey Joanette kaileyjo at bigbluebubble.com
Thu Mar 29 06:49:10 PDT 2007


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.



More information about the antlr-interest mailing list