主函数
#include
"
memory.h
"
int yyparse();
main() ... {
#if YYDEBUG
extern int yydebug;
yydebug = 1;
#endif
init_memory();
yyparse();
clean_memory();
}
int yyparse();
main() ... {
#if YYDEBUG
extern int yydebug;
yydebug = 1;
#endif
init_memory();
yyparse();
clean_memory();
}
控制内存
#include
"
memory.h
"
#include < stdio.h >
#include < stdlib.h >
#define MEMORY_MAX 65535
static int count;
static void * mem[MEMORY_MAX];
void init_memory()
... {
count = 0;
}
void * get_memory( int size)
... {
void* s =NULL;
if (size < MEMORY_MAX)...{
s = (void*)malloc(size);
mem[count] = s;
count++;
return s;
}
else...{
printf("Memory Overflow");
exit(-1);
}
}
void clean_memory()
... {
int i;
for(i=0; i<count; i++)...{
free(mem[i]);
}
count = 0;
}
#include < stdio.h >
#include < stdlib.h >
#define MEMORY_MAX 65535
static int count;
static void * mem[MEMORY_MAX];
void init_memory()
... {
count = 0;
}
void * get_memory( int size)
... {
void* s =NULL;
if (size < MEMORY_MAX)...{
s = (void*)malloc(size);
mem[count] = s;
count++;
return s;
}
else...{
printf("Memory Overflow");
exit(-1);
}
}
void clean_memory()
... {
int i;
for(i=0; i<count; i++)...{
free(mem[i]);
}
count = 0;
}
整数字
#ifndef INTERGER_H__
#define INTERGER_H__
#include " Node.h "
typedef struct Interger_t CInterger;
typedef struct ... {
int (*calc)(CInterger* this);
} Interger_VMT;
struct Interger_t ... {
CNode super;
int value ;
} ;
extern void Interger_Init(CInterger * , int );
extern int Interger_Calc(CInterger * );
#endif
#define INTERGER_H__
#include " Node.h "
typedef struct Interger_t CInterger;
typedef struct ... {
int (*calc)(CInterger* this);
} Interger_VMT;
struct Interger_t ... {
CNode super;
int value ;
} ;
extern void Interger_Init(CInterger * , int );
extern int Interger_Calc(CInterger * );
#endif
#include
"
Interger.h
"
static const Interger_VMT vmt = ... {Interger_Calc} ;
void Interger_Init(CInterger * this , int val)
... {
Node_Init((CNode*)this);
((CNode*)this)->vmt=(const Node_VMT*)&vmt;
this->value = val;
}
int Interger_Calc(CInterger * this )
... {
return this->value;
}
static const Interger_VMT vmt = ... {Interger_Calc} ;
void Interger_Init(CInterger * this , int val)
... {
Node_Init((CNode*)this);
((CNode*)this)->vmt=(const Node_VMT*)&vmt;
this->value = val;
}
int Interger_Calc(CInterger * this )
... {
return this->value;
}
NODE,可以说是最顶上的一个类把
#ifndef NODE_H__
#define NODE_H__
typedef struct Node_t CNode;
typedef struct ... {
int (*calc)(CNode* this);
} Node_VMT;
struct Node_t ... {
const Node_VMT *vmt;
} ;
extern void Node_Init(CNode * );
// extern int Node_Calc(CNode*);
#define Node_Calc(this)
(((CNode * )( this )) -> vmt -> calc((CNode * ) this ))
#endif
#define NODE_H__
typedef struct Node_t CNode;
typedef struct ... {
int (*calc)(CNode* this);
} Node_VMT;
struct Node_t ... {
const Node_VMT *vmt;
} ;
extern void Node_Init(CNode * );
// extern int Node_Calc(CNode*);
#define Node_Calc(this)
(((CNode * )( this )) -> vmt -> calc((CNode * ) this ))
#endif
挑了一个,乘法的例子
#ifndef MULT_H__
#define MULT_H__
#include " Composite.h "
typedef struct Mult_t CMULT;
typedef struct ... {
int (*calc)(CMULT*);
} MULT_VMT;
struct Mult_t ... {
CComposite super;
} ;
extern void Mult_Init(CMULT * this , CNode * left, CNode * right);
extern int Mult_Calc(CMULT * this );
#endif
#define MULT_H__
#include " Composite.h "
typedef struct Mult_t CMULT;
typedef struct ... {
int (*calc)(CMULT*);
} MULT_VMT;
struct Mult_t ... {
CComposite super;
} ;
extern void Mult_Init(CMULT * this , CNode * left, CNode * right);
extern int Mult_Calc(CMULT * this );
#endif
Mult.c
#include
"
Mult.h
"
#include " Node.h "
static const MULT_VMT vmt = ... {Mult_Calc} ;
void Mult_Init(CMULT * this , CNode * left, CNode * right)
... {
Composite_Init((CComposite*)this,left,right );
((CNode*)this)->vmt = (const Node_VMT*)&vmt;
}
int Mult_Calc(CMULT * this )
... {
int left_value ,right_value;
left_value = Node_Calc(((CComposite*)this)->left);
right_value = Node_Calc(((CComposite*)this)->right);
return left_value * right_value;
}
#include " Node.h "
static const MULT_VMT vmt = ... {Mult_Calc} ;
void Mult_Init(CMULT * this , CNode * left, CNode * right)
... {
Composite_Init((CComposite*)this,left,right );
((CNode*)this)->vmt = (const Node_VMT*)&vmt;
}
int Mult_Calc(CMULT * this )
... {
int left_value ,right_value;
left_value = Node_Calc(((CComposite*)this)->left);
right_value = Node_Calc(((CComposite*)this)->right);
return left_value * right_value;
}
Composite.h
#ifndef COMPOSITE_H__
#define COMPOSITE_H__
#include " Node.h "
typedef struct Composite_t CComposite;
struct Composite_t ... {
CNode super;
CNode *left;
CNode *right;
} ;
extern void Composite_Init(CComposite * this , CNode * left, CNode * right);
#endif
#define COMPOSITE_H__
#include " Node.h "
typedef struct Composite_t CComposite;
struct Composite_t ... {
CNode super;
CNode *left;
CNode *right;
} ;
extern void Composite_Init(CComposite * this , CNode * left, CNode * right);
#endif
Composite.c
#include
"
Composite.h
"
void Composite_Init(CComposite * this , CNode * left, CNode * right)
... {
Node_Init((CNode*)this);
this->left = left;
this->right = right;
}
void Composite_Init(CComposite * this , CNode * left, CNode * right)
... {
Node_Init((CNode*)this);
this->left = left;
this->right = right;
}
好了真正的主角登场
%{
#include "parser.h"
%}
%%
"+" {
return PLUS;
}
"-" {
return MINUS;
}
"/" {
return DIV;
}
"*" {
return MULT;
}
"(" {
return L_PAR;
}
")" {
return R_PAR;
}
[0-9]+ {
yylval.value = atoi(yytext);
return INTEGER;
}
"/n" {
return NL;
}
. ;
%%
%
...
{
#include "Node.h"
#include "Mult.h"
#include "Minus.h"
#include "Plus.h"
#include "div.h"
#include "Interger.h"
#include "memory.h"
%}
% token PLUS
% token MINUS
% token DIV
% token MULT
% token L_PAR
% token R_PAR
% token < value > INTEGER
% token NL
% type < node > calc
% type < node > expression
% type < node > term
% type < node > factor
% union ... {
int value;
CNode *node;
}
%%
calc
: calc expression NL ... { printf("%d ", Node_Calc($2)); }
| expression NL ... { printf("%d ", Node_Calc($1)); }
;
expression
: term ... {$$ = $1; }
| expression PLUS term
... {
CPlus* pPlus = (CPlus*)get_memory(sizeof(CPlus));
CPlus_Initialize(pPlus,$1,$3);
$$ = (CNode*)pPlus;
}
| expression MINUS term
... {
CMinus* pMinus = (CMinus*)get_memory(sizeof(CMinus));
CMinus_Initialize(pMinus,$1,$3);
$$ = (CNode*)pMinus;
}
;
term
: factor ... {$$ = $1; }
| term MULT factor
... {
CMULT* pMult = (CMULT*)get_memory(sizeof(CMULT));
Mult_Init(pMult, $1, $3);
$$ = (CNode*)pMult;
}
| term DIV factor
... {
CDIV* pDIV = (CDIV*)get_memory(sizeof(CDIV));
Div_Init(pDIV, $1, $3);
$$ = (CNode*)pDIV;
}
;
factor
: L_PAR expression R_PAR ... { $$ = $2; }
| INTEGER
... {
CInterger *pInterger = (CInterger*)get_memory(sizeof(CInterger));
Interger_Init(pInterger, $1);
$$ = (CNode*)pInterger;
}
| MINUS factor
... {
CMinus* pMinus = (CMinus*)get_memory(sizeof(CMinus));
CInterger *pInteger = (CInterger*)get_memory(sizeof(CInterger));
Interger_Init(pInteger, 0);
CMinus_Initialize(pMinus, (CNode*)pInteger, $2);
$$ = (CNode*)pMinus;
}
%%
int yyerror( char * str) ... {
printf("%s ", str);
}
#include "Node.h"
#include "Mult.h"
#include "Minus.h"
#include "Plus.h"
#include "div.h"
#include "Interger.h"
#include "memory.h"
%}
% token PLUS
% token MINUS
% token DIV
% token MULT
% token L_PAR
% token R_PAR
% token < value > INTEGER
% token NL
% type < node > calc
% type < node > expression
% type < node > term
% type < node > factor
% union ... {
int value;
CNode *node;
}
%%
calc
: calc expression NL ... { printf("%d ", Node_Calc($2)); }
| expression NL ... { printf("%d ", Node_Calc($1)); }
;
expression
: term ... {$$ = $1; }
| expression PLUS term
... {
CPlus* pPlus = (CPlus*)get_memory(sizeof(CPlus));
CPlus_Initialize(pPlus,$1,$3);
$$ = (CNode*)pPlus;
}
| expression MINUS term
... {
CMinus* pMinus = (CMinus*)get_memory(sizeof(CMinus));
CMinus_Initialize(pMinus,$1,$3);
$$ = (CNode*)pMinus;
}
;
term
: factor ... {$$ = $1; }
| term MULT factor
... {
CMULT* pMult = (CMULT*)get_memory(sizeof(CMULT));
Mult_Init(pMult, $1, $3);
$$ = (CNode*)pMult;
}
| term DIV factor
... {
CDIV* pDIV = (CDIV*)get_memory(sizeof(CDIV));
Div_Init(pDIV, $1, $3);
$$ = (CNode*)pDIV;
}
;
factor
: L_PAR expression R_PAR ... { $$ = $2; }
| INTEGER
... {
CInterger *pInterger = (CInterger*)get_memory(sizeof(CInterger));
Interger_Init(pInterger, $1);
$$ = (CNode*)pInterger;
}
| MINUS factor
... {
CMinus* pMinus = (CMinus*)get_memory(sizeof(CMinus));
CInterger *pInteger = (CInterger*)get_memory(sizeof(CInterger));
Interger_Init(pInteger, 0);
CMinus_Initialize(pMinus, (CNode*)pInteger, $2);
$$ = (CNode*)pMinus;
}
%%
int yyerror( char * str) ... {
printf("%s ", str);
}