[antlr-interest] syntactic predicates??

netminka netminka at netscape.net
Mon Jul 28 12:36:19 PDT 2003


I am working on a VB6 to C# translator. There is an intermiediary 
phase
where the VB6 is xlated to VB7; if that translation didn't work,
we won't be able to go to C#. That means if there is residual VB6 
code, it must be commented out. Information that the VB6 to VB7
phase didn't work is in a comment.

So I need to scan forward in a comment to determine if the 
code inside a conditional compile should be put into the comment.
In the example below, the comment follows the single quote. 

#If 0 Then
'UPGRADE_NOTE: #If #EndIf block was not upgraded because the  
expression 0 did not evaluate to True or was not evaluated. Click for 
more: 'ms-help://MS.VSCC/commoner/redir/redirect.htm?
keyword="vbup1035"'
	Do While Not frm.Parent Is Nothing
	iVal = 1400
        Set frm = frm.Parent
	Loop
#End If

The goal above is to include the code in Do...Loop in the comment
associated with the UPGRADE_NOTE. 

My understanding of antlr is that you can't scan forward in the input 
and then push back tokens into the input. You have to use syntactic
predicates for this functionality. I use a COMMENT_PREDICATE rule
to distinguish between comments and these special cases. 

To simplify my problem I'll show just the #if case.
// VB6 code will be part of the UPGRADE comment

COMMENT_PREDICATE
	: ("'UPGRADE_NOTE: #If #EndIf block was not upgraded"
	   ( ~'#')* "#End If")
	   => NO_UPGRADE_IFBLOCK
	   {$setType(NO_UPGRADE_IFBLOCK);}
	|	( SINGLE_QUOTE)
	    => COMMENT_LITERAL
	    {$setType(COMMENT_LITERAL);}
	;
//this recognizes everything between the
//upgrade note and the next conditional dir
protected
NO_UPGRADE_IFBLOCK
	:  ("'" "UPGRADE_NOTE: #If #EndIf block was not upgraded"
	   ( ~'#')*)
	;


The above two rule have the effect of scanning through to the closing 
#END If and then pushing the #End If back on the input so that 
everything that was under the comment becomes part of the comment. 

However, I can't get this rule above, because it is protected, to 
return a token. 
If I add a rule like this:
NO_UPGRADE_NOTE
	:	( SINGLE_QUOTE)
	=> COMMENT_LITERAL
	{$setType(COMMENT_LITERAL);}
	|	NO_UPGRADE_IFBLOCK 
	{$setType(COMMENT_LITERAL);}
	;
then the rule COMMENT_PREDICATE is not called when it should be. 

Any ideas on how to get this to work? Some of the stuff that's 
documented has just not worked for me. Declaring variables results
in compile errors in generated code, etc. Is there no way to scan 
forward on the input and backtrack????? That seems like pretty basic
functionality to me. 
thanks for any assistance. 

megan adams 



 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list