1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#header <<
/* Only requirements are that the user define what an AST is; it must have
* right, down pointers and a token field. After that, the user can put it
* what he/she wants
*/
typedef struct _node {
struct _node *right, *down;
int token;
char text[50];
} SORAST;
#ifdef __STDC__
#include "stdlib.h"
#else
#include "malloc.h"
#endif
>>
<<
#define Plus 1
#define Mult 2
#define Var 3
/* define error routines here or include sorcerer.c */
#include "errsupport.c"
/* We use our own node construction routine here rather than #[...] */
SORAST *
#ifdef __STDC__
node(int tok, char *s)
#else
node(tok, s)
int tok;
char *s;
#endif
{
SORAST *p;
p = (SORAST *) calloc(1, sizeof(SORAST));
if ( p == NULL ) {fprintf(stderr, "out of mem\n"); exit(-1);}
p->token = tok;
strcpy(p->text, s);
return p;
}
main()
{
SORAST *a,*b,*c,*d,*e,*f;
STreeParser myparser;
STreeParserInit(&myparser);
/* tree is ( + ( * d a ) ( * a b ) ) == "d*a + a*b" */
c = node(Mult,""); c->down = node(Var,"d"); c->down->right = node(Var,"a");
b = node(Mult,""); b->down = node(Var,"a"); b->down->right = node(Var,"b");
a = node(Plus,""); a->down = c; a->down->right = b;
/* gen code */
reg(&myparser, &a);
}
>>
reg : #( Plus reg reg ) <<printf("\tadd\n");>>
| #( Mult reg reg ) <<printf("\tmult\n");>>
| load
;
load: f:Var <<printf("\tpush %s\n", f->text);>>
;
|