[antlr-interest] problem with tree rewrite :(

Heinrich du Toit heinrich at silasvalley.com
Fri Sep 19 01:53:44 PDT 2008


Hi

I'm trying to write a C parser that generates a tree from the code for 
specializer pre-processing and diagnostics needs.
I'm new to antlr so be patient please.

I've attached my current grammer file.
It's very similar to the example C.g actually.
The main difference is that I can't load in header files and therefore I 
can't be sure which IDENTIFIERS are type names and which aren't.
In general the idea is not to check syntax. Rather just to accept 
everything that can possibly be correct.
And in the end identify the important things.. functions, expressions, 
statements ect.

Inside I have these rules currently:
-------------------------------
// syntactically this is the same as declaration* I think..
// yet somehow it just doesn't budge like that ?!?!
declarator_list
 
    :( typedef_statement |
      ((decl_specifiers init_declarator_list? ';')=>
        decl_specifiers init_declarator_list? ';' )
     )*;
   
    //: (declaration)*;
   
declaration_normal
    :((decl_specifiers init_declarator_list? ';')=>
        decl_specifiers init_declarator_list? ';'
            -> ^(DECLARATION decl_specifiers init_declarator_list 
';'));       

//note: currnt decl_specifiers1 is a fix for the grammar here :(
// in theory we can throw this out maybe somehow..
// typedef is still a problem
declaration
    : typedef_statement
   
    | ((decl_specifiers init_declarator_list? ';')=>
        decl_specifiers
         init_declarator_list? ';' -> ^(DECLARATION decl_specifiers 
init_declarator_list ';'));   
---------------------------

if I change declarator_list to:
 : (typedef_statement | declaration_normal ) *;

which technically only adds the rewrite rule.. things goes horribly wrong.
When it parses and there is a list like this:
int a;
int b;
int c;
only int c ends up in the tree, all the other declarations simply 
dissappear?



Preferably I want to alter my parser so that it can accept C++ type 
expressions..
Where variables can be declared inside the expression.
But I seem to run into hundreds of problems just trying todo that.
Also my expreience is that ANTLRWorks1.2 is very unstable and becomes 
slow after a while if you give it complex stuff.

I'm also not sure about the whole LL* parsing thing?
Aint the parser suppose to look ahead (branch) on multiple rules if 
unsure and then throw out those that are wrong? (That's what bison/yacc 
did anyways)
Then why do I get warnings saying the input 'IDENTIFIER' can be matched 
on 2 rules and therefore the one is thrown out?
Am I suppose to switch on backtracking for this?
Btw. if you take the C.g example and say "output=AST; ASTLabelType= 
pANTLR3_BASE_TREE;" in options the generated C code doesn't compile for 
me :(

thanks
-H













-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: Ctree.g
Url: http://www.antlr.org/pipermail/antlr-interest/attachments/20080919/0d1aa0a7/attachment.pl 


More information about the antlr-interest mailing list