I browsed through all the documentation available in the treecc document and searched for a sample session with treecc but couldn't find.
I am now learning about treecc.
After going through the complete document I just picked the code in the appendix A and tried to build a sample parser. But the generated code is not compiling.
The grammar file is expr.y
--------------------------
#include <stdio.h>
#include "expr.h"
%}
%union {
_expression_ *node;
int inum;
double fnum;
}
%token INT FLOAT
%type <node> expr
%type <inum> INT
%type <fnum> FLOAT
%left '+' '-'
%left '*' '/'
%nonassoc UMINUS
%right '^'
%%
expr: INT { $$ = intnum_create($1); }
| FLOAT { $$ = floatnum_create($1); }
| '(' expr ')' { $$ = $2; }
| expr '+' expr { $$ = plus_create($1, $3); }
| expr '-' expr { $$ = minus_create($1, $3); }
| expr '*' expr { $$ = multiply_create($1, $3); }
| expr '/' expr { $$ = divide_create($1, $3); }
| expr '^' expr { $$ = power_create($1, $3); }
| '-' expr %prec UMINUS { $$ = negate_create($2); }
;
%%
int yyerror(char * msg)
{
fprintf(stderr, "expr : %s\n", msg);
return -1;
}
int main(void)
{
init();
yyparse();
return 0;
}
/* end of grammar file */
The lex file expr.l
-------------------
%{
#include <stdlib.h>
#include "expr.tab.h"
%}
%%
[0-9]+ {
yylval.inum = atoi(yytext);
return INT;
}
[0-9]+\.[0-9]+ {
yylval.fnum = atof(yytext);
return FLOAT;
}
[ \t] ;
. return *yytext;
/* end of lex file */
and the treecc file expr.tc
---------------------------
%enum type_code =
{
int_type,
float_type
}
%node _expression_ %abstract %typedef =
{
%nocreate type_code type = {int_type};
}
%node binary _expression_ %abstract =
{
_expression_ *expr1;
_expression_ *expr2;
}
%node unary _expression_ %abstract =
{
_expression_ *expr;
}
%node intnum _expression_ =
{
int num;
}
%node floatnum _expression_ =
{
float num;
}
%node plus binary
%node minus binary
%node multiply binary
%node divide binary
%node power binary
%node negate unary
%operation void infer_type(_expression_ *e)
infer_type(binary)
{
infer_type(e->expr1);
infer_type(e->expr2);
if(e->expr1->type == float_type || e->expr2->type == float_type)
{
e->type = float_type;
}
else
{
e->type = int_type;
}
}
infer_type(unary)
{
infer_type(e->expr);
e->type = e->expr->type;
}
infer_type(intnum)
{
e->type = int_type;
}
infer_type(floatnum)
{
e->type = float_type;
}
infer_type(power)
{
infer_type(e->expr1);
infer_type(e->expr2);
if(e->expr2->type != int_type)
{
error("second argument to `^' is not an integer");
}
e->type = e->expr1->type;
}
/* end of treecc file */
Now I am using following sequence of command,
$ treecc expr.tc
$ bison -d expr.y
$ flex expr.l
This sequence fine runs fine. But when I compile,
cc expr.c expr.tab.c lex.yy.c
This produces a lot of error, A few lines of error are,
c_skel.c:50: error: syntax error before "fixed_state__"
c_skel.c:50: warning: data definition has no type or storage class
c_skel.c: In function `yynodeinit':
c_skel.c:99: error: `YYNODESTATE' undeclared (first use in this function)
c_skel.c:99: error: (Each undeclared identifier is reported only once
c_skel.c:99: error: for each function it appears in.)
c_skel.c:99: error: `state__' undeclared (first use in this function)
c_skel.c: In function `yynodealloc':
c_skel.c:118: error: `YYNODESTATE' undeclared (first use in this function)
c_skel.c:118: error: `state__' undeclared (first use in this function)
c_skel.c: In function `yynodepush':
c_skel.c:171: error: `YYNODESTATE' undeclared (first use in this function)
c_skel.c:171: error: `state__' undeclared (first use in this function)
c_skel.c: In function `yynodepop':
c_skel.c:214: error: `YYNODESTATE' undeclared (first use in this function)
c_skel.c:214: error: `state__' undeclared (first use in this function)
c_skel.c: In function `yynodeclear':
c_skel.c:253: error: `YYNODESTATE' undeclared (first use in this function)
c_skel.c:253: error: `state__' undeclared (first use in this function)
expr.c: At top level:
expr.c:270: error: variable `expression_vt__' has initializer but incomplete type
expr.c:271: warning: excess elements in struct initializer
expr.c:271: warning: (near initialization for `expression_vt__')
expr.c:272: error: `expression_kind' undeclared here (not in a function)
expr.c:272: warning: excess elements in struct initializer
expr.c:272: warning: (near initialization for `expression_vt__')
expr.c:273: warning: excess elements in struct initializer
Please help it indicates that I am missing some file. What other things do I need to compile this.
Regards,
Pankaj
_______________________________________________ Developers mailing list [email protected] http://dotgnu.org/mailman/listinfo/developers
