ycaa的计算器,开始写ACTION

主函数

 

#include  " memory.h "
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;
}

 

整数字

 

#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  
#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;
}

 

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  

 

挑了一个,乘法的例子

 

#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

 

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;

}

 

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

 

Composite.c

#include  " Composite.h "

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);
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的横打

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值