[antlr-interest] C-Target $label.text / toString malfunction

Tobias Pape Das.Linux at gmx.de
Sat Mar 31 14:05:10 PDT 2007


Hi gavin
Am 2007-03-31 um 22:35 schrieb Gavin Lambert:

> At 04:25 1/04/2007, Tobias Pape wrote:
> >unaryPattern returns [ pASTPattern p ]
> >   : s=unarySelector
> >     { $p = (pASTPattern) ASTUnaryPattern_new((char *) $s.text-
> > >chars); }
> >   ;
> >
> >binaryPattern returns [ pASTPattern p  ]
> >   : s=binarySelector v=argument
> >     { $p = (pASTPattern) ASTBinaryPattern_new((char *) $s.text-
> > >chars, v.v); }
> >   ;
>
> I don't think you're allowed to upcast like that in the C-language  
> parsers.  At least not if 'ASTBinaryPattern' inherits from  
> ASTPattern, anyway, rather than simply being an ASTPattern with a  
> different constructor (in which case you probably don't need the  
> cast).  Inheritance is usually faked via containment and you're not  
> going to get the compiler's help to convert pointers, so unless the  
> structure is laid out in a very specific way then you're going to  
> end up with an invalid pointer.


leave my ASTs away ;)

yes, exactly, my AST* layout is special, its kinf of fake-OOP.
This will explain more to you:
for my Patterns, I got the rules as:
==============================================
pattern returns [ pASTPattern p ]
   : up=unaryPattern { $p = up.p; }
   | bp=binaryPattern { $p = bp.p;}
   | kp=keywordPattern { $p = (pASTPattern) kp.p; }
   ;

unaryPattern returns [ pASTPattern p ]
   : s=unarySelector
     { $p = (pASTPattern) ASTUnaryPattern_new((char *) $s.text- 
 >chars); }
   ;

binaryPattern returns [ pASTPattern p  ]
   : s=binarySelector v=argument
     { $p = (pASTPattern) ASTBinaryPattern_new((char *) $s.text- 
 >chars, v.v); }
   ;

keywordPattern returns [ pASTKeywordPattern p ]
   : (s=keyword v=argument {
   	$p = ASTKeywordPattern_new();
   	$p->add_argument($p,(char *)  $s.text->chars, v.v);
   } )+
   ;
  ==============================================

As you can see, "pattern" returns an pASTPattern p, but if
assigned is in the subrules as pASTBinaryPattern or pASTUnaryPattern,  
cc would complain,
when i assign "$p = up.b;" or "$p = bp.p;".
and, yes, the cast is intended, here the layouts:

==============================================
[from ASTPattern.h]


// ASTPattern
#define NODE_ASTPattern 6
#define ASTPATTERN_FORMAT \
		ASTNODE_FORMAT;\
		/*abstract */ p_string	(*get_selector)(pASTPattern self);\
		/*abstract */ pList		(*get_arguments)(pASTPattern self)


struct _ASTPattern {
  // ASTPattern
	ASTNODE_FORMAT;
	/*abstract */ p_string	(*get_selector)(pASTPattern self);
	/*abstract */ pList		(*get_arguments)(pASTPattern self);
};

==============================================
==============================================
[from ASTUnaryPattern.h]
//		ASTUnaryPattern
#define NODE_ASTUnaryPattern 7
#define ASTUNARYPATTERN_FORMAT \
			ASTPATTERN_FORMAT; \
			/* private */ char *		_selector


struct _ASTUnaryPattern {
	//		ASTUnaryPattern
			ASTPATTERN_FORMAT;
			/* private */ char *		_selector;
};
==============================================
==============================================
[from ASTBinaryPattern.h]
//		ASTBinaryPattern
#define NODE_ASTBinaryPattern 8
#define ASTBINARYPATTERN_FORMAT \
			ASTPATTERN_FORMAT;\
			/* private */ char *		_selector;\
	/* private */ struct _ASTVariable*	_parameter

struct _ASTBinaryPattern {
	//		ASTBinaryPattern
			ASTPATTERN_FORMAT;
			/* private */ char *		_selector;
			/* private */ struct _ASTVariable*	_parameter;
};
==============================================


as you can see, only the "visible" fields differ, and the constructors
are there for specific asignmenst.

A constructed ASTPattern would differ significantly (?) from any  
other AST*Pattern.
you know what I mean?

 >>These casts, fact, arent my problems at all. I'm merely messing  
with libantlr3c ;)<<
But thanks for your notice. If you want, I'll get you a copy of my
speciffic "C Class Layout" (without late binding, thoug)

thanks,
	-Tobias




-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 186 bytes
Desc: Signierter Teil der Nachricht
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20070331/4fabdfcb/attachment.bin 


More information about the antlr-interest mailing list