[antlr-interest] C Target: what to use instead of CommonTree when writing tree grammars, antlr 3.0

Hardy, Stephen Stephen_Hardy at rabbit.com
Sat Jul 7 23:07:29 PDT 2007


Jim,

 

OK it seems to be working fine now that I used the correct AST node
type.

 

Regards,

SJH

 

________________________________

From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Hardy, Stephen
Sent: Saturday, July 07, 2007 10:51 PM
To: antlr-interest at antlr.org
Subject: Re: [antlr-interest] C Target: what to use instead of
CommonTreewhenwriting tree grammars, antlr 3.0

 

Jim,

 

sorry about the terseness of the report.  I'm developing on a diferent
machine than my email client, but I'm working on combining the two.

 

The error was when (C) compiling the code generated for the tree parser.
Without any actions, it (and the corresponding lexer/parser for the C
grammar) compiles fine, however when running against some sample C input
the combination, based on your sample code for the simple C tree walker,
I encounter some "unexpected node" errors in the tree walker.  This is
undoubtedly my fault, but unfortunately, it was not obvious just what
was wrong, since the default error messages do not relate back to the
original input.  So, to quickly work out where I had stuffed up, I added
some actions in the tree walker to try to find out how far it got.  This
is the point where I got stuck with the C compile error, when I was
trying to print the nodes it had encountered in the tree walk.

 

The fragment I provided was from the tree walker.  There is basically no
transformation from the corresponding grammar fragment, which is why it
was confusing.  Anyway, given that line of code, the output templates
are generating a function called type_id() as follows:

 

static void

type_id(pC2Tree ctx)

{

            CommonTree IDENTIFIER3;

            IDENTIFIER3 = NULL;

            { { IDENTIFIER3 = (CommonTree)LT(1);

                MATCHT(IDENTIFIER, &FOLLOW_IDENTIFIER_yaddayadda);

                { // my code here

                        Printf("%s ",
IDENTIFIER3->getText(IDENTIFIER3)->chars);

                }

            } }

            ...

}

 

And the compiler complains at the CommonTree typedef, since it doesn't
exist (my ignorance).  However the template does not generate a
CommonTree (or whatever) variable in other actions such as the
pseudo-tokens generated for tree nodes.

 

Yes, I saw the sample code you mentioned and indeed it works for things
other than tokens.  Anyhow, I'll look into it some more now that you've
provided some more information.  I take it I should be putting

 

ASTLabelType = pANTLR3_BASE_TREE;

 

so that variables will be created with the correct type.

 

Basically, I am trying to write a fairly simple translator which
converts ANSI C into a flavor of non-ANSI (more like K&R, but with
additional restrictions).  Also, the target machine memory model is
segmented, so I have to add FAR qualifiers to pointers, just like in the
good ol' DOS days where 640k was all anyone would ever need...  I
convinced my boss that antlr was such a cool tool I could get this done
in a week.  So bear with me.  I hope to donate the resulting C
translator back to the community as a real-world example of using the C
target.

 

Regards,

SJH

 

________________________________

From: Jim Idle [mailto:jimi at temporal-wave.com] 
Sent: Saturday, July 07, 2007 8:02 PM
To: Hardy, Stephen; antlr-interest at antlr.org
Subject: RE: [antlr-interest] C Target: what to use instead of
CommonTree whenwriting tree grammars, antlr 3.0

 

Tree walking and everything is all implemented (it better be or my own
in production parsers are imaginary ;-).

 

However, you don't say if this snippet is in your tree generating
parser, or your tree parser, nor supply the error messages?

 

Once again, the example projects should rescue you. While we could do
with a more comprehensive tree walking example, but in the tree parser
for the strangely named 'treeparser' example, you will find:

 

decl : ^(DECL type declarator)

 

       // label.start, label.start, label.text

       {

         printf("int \%s\n", $declarator.text->chars);

       }

     ;

 

The node types in the C runtime are always the built in ones as these
contain a pointer within them that should poitn to any super structure
you might add. SO you are always passing around pANTLR3_BASE_TREE,
within wich is a super pointer to pANTLR3_COMMON_TREE, within which is a
superpointer which you are free to point to your own even larger
structure. Because this is a bit of a pain in C, there is also a custom
void * pointer in the pANTLR3_COMMON_TREE which you can just hijack to
point to whatever you like (you must free it).

 

Perhaps there is some problem referencing .text on a TOKEN? For error
reports I need as much detail as possible if you could oblige :-)

 

Jim

 

 

 

From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Hardy, Stephen
Sent: Saturday, July 07, 2007 12:17 PM
To: antlr-interest at antlr.org
Subject: [antlr-interest] C Target: what to use instead of CommonTree
whenwriting tree grammars, antlr 3.0

 

Jim,

 

I can't get my head around this.  Have you implemented the necessary
infrastructure for using tree walkers with the C target?  I would like
to use templates, however they are not yet available for C.  So, I am
happy to use printf's and other hacks to get going.  The trouble is, I
can't relate the java examples in the book to how to do this in C, and
there are no examples.  For example, I am writing

 

type_id:

            : IDENTIFIER { printf("\%s ", $IDENTIFIER.text->chars); }

            ;

 

But it obviously isn't that simple since I get errors all over the
place.  Also, what is the correct ASTLabelType incantation to use in the
options section?  It isn't CommonTree, that's for sure.

 

Regards,

SJH

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070707/ddec36a4/attachment-0001.html 


More information about the antlr-interest mailing list