test.sor
来自「SRI international 发布的OAA框架软件」· SOR 代码 · 共 70 行
SOR
70 行
#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);>>
;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?