I have a C header file that I have pre开发者_如何学JAVAcompiled using the gcc -E
flag, and am now trying to parse using Lex and Yacc; however, it is getting hung up on typedef'd variables.
for example:
typedef unsigned long ULONG;
ULONG i = 5;
will throw a syntax error at the second line's ULONG.
I have tried to redefine part of the grammar (found here) http://www.quut.com/c/ANSI-C-grammar-y.html, specifically under type_specifer
by replacing TYPE_NAME
with IDENTIFIER
, however that create multiple s/r and r/r errors that I am unable to fix.
Are there other approaches that you would recommend? Or a different approach at precompiling all together?
In the code you link too:
http://www.quut.com/c/ANSI-C-grammar-y.html,
look at the bottom:
You will see this function
int check_type(void)
{
/*
* pseudo code --- this is what it should check
*
* if (yytext == type_name)
* return TYPE_NAME;
*
* return IDENTIFIER;
*/
/*
* it actually will only return IDENTIFIER
*/
return IDENTIFIER;
}
You actually need to write the code that identifies weather a sequence (yytest, yytext+yylength] is an identifier or a TYPE_NAME. This means in the parser you need to build some structure as you parse the code so that this function can look up the identifier in the structure.
Pre-populate the structure with the default types char/int/short/long/float/double etc. The other types you will need to add as you parse the input.
精彩评论