[antlr-interest] translating Java frontend code to C frontend code
    Marco Trudel 
    marco at mtsystems.ch
       
    Wed Aug 26 02:01:57 PDT 2009
    
    
  
Dear all
I translated the "ANSI C grammar for ANTLR v3" from the grammar list 
(http://antlr.org/grammar/list) from Java to C.
The original is here: http://antlr.org/grammar/1153358328744/C.g
My new C one can be found here: http://mtsystems.ch/tmp/C.g
Diff: http://mtsystems.ch/tmp/grammar-diff.txt
Since I'm not at all familiar with the internals of the Java or C 
frontends, I don't know how good a job I did. Specifically, I have three 
questions/concerns:
1. I think my translation is not fully correct since I manually have to 
add a "!= NULL" check in the created CParser.c. This change:
  			{
-			if ($declaration.size()>0&&$declaration::isTypedef) {
-				$Symbols::types.add($IDENTIFIER.text);
-				//System.out.println("define type "+$IDENTIFIER.text);
+			if($declaration->size($declaration) > 0 && $declaration::isTypedef) {
+				$Symbols::types->put($Symbols::types, $IDENTIFIER.text->chars, 1, 
NULL);
+				//fprintf(stderr, "define type \%s\n", $IDENTIFIER.text->chars);
  			}
  			}
creates this code:
if(ctx->SCOPE_STACK(declaration)->size(ctx->SCOPE_STACK(declaration)) > 
0 && 	(SCOPE_TOP(declaration))->isTypedef) ...
And I have to manually adapt it to (additional "SCOPE_TOP(declaration) 
!= NULL"):
if(ctx->SCOPE_STACK(declaration)->size(ctx->SCOPE_STACK(declaration)) > 
0 && SCOPE_TOP(declaration) != NULL && 
(SCOPE_TOP(declaration))->isTypedef) ...
2.
The Java implementation uses a set. Since the C frontend doesn't have 
this data structure, I'm using a hash table (pANTLR3_HASH_TABLE). But I 
think this is ok unless I missed something obvious.
3.
I'm not yet freeing the allocated hash tables. Where/how could I do 
this? Or will it be done automatically on
	psr->free(psr);
	tstream->free(tstream);
	lxr->free(lxr);
	input->close(input);
I would be very happy about feedback on this three points. If someone 
with deeper insights than me would take a look at the whole change, that 
would also be great.
Thanks
Marco
    
    
More information about the antlr-interest
mailing list