2010年9月6日 星期一

LEX && YACC sample case pt1

早在 Lex & Yacc case study @ PLY 有提到 Lex && Yacc 的用法, 這邊用主要是透過 link-list 的方式把 Yacc 所建立的 Token 轉成 Node list 的方式存入,之後可以方便我們在內部做Scheduling 和 Mapping 的動作. tt.h

#ifndef TT_H
#define TT_H
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

enum TTType {
 TT_ADD =0,
 TT_SUB =1,
 TT_MUL =2,
 TT_DIV =3,
 TT_MOD =4,
 TT_SRC1 =5,
 TT_SRC2 =6,
 TT_DST =7,
};

typedef struct tt {
 int Id;
 char*  Nm;
 struct tt *Nxt;
} TT;

typedef struct list {
 int    Typ;
 int    OpId;
 struct TT *Parent;
 struct TT *Child;
 struct list *Nxt;
} LIST;


TT *set_TTNode(TT *p,int Id,char* Nm){

    TT *tPtr =  malloc(sizeof(TT));
    if( tPtr == NULL )
         return NULL;
   
     tPtr->Id = Id;
     tPtr->Nm = Nm;
     tPtr->Nxt = NULL;
     
    if( p == NULL ){
        p = tPtr;
    } else {
        tPtr->Nxt = p;
        p      = tPtr;
   }
 
return p;
}

TT *display_TTNode(TT *p){
   if(p!=NULL){
     printf("Id :: %3d,",p->Id);
     printf("Nm "" %3s\n",p->Nm);
   }
return p; 
}

TT *get_TTNode(TT *p,int Id){
    TT *tPtr = p;

    while(tPtr!=NULL){
      if( tPtr->Id == Id ){
           return tPtr; break;
      }
      tPtr = tPtr->Nxt;
   }

return NULL; 
}

LIST* set_Parent2List(LIST *l,int Id,char *Nm){
      
     if(l==NULL)
        return NULL;

     TT *tPtr  = (void *) l->Parent;
     tPtr      = set_TTNode(tPtr,Id,Nm);
     l->Parent = (void *)tPtr;

return l; 
}

LIST* set_List(LIST *l,int OpId,int Tp,char *src1,char *src2,char *dst){

    LIST *lPtr =  malloc(sizeof(LIST));
    if( lPtr == NULL )
         return NULL;
    
     lPtr      = set_Parent2List(lPtr,TT_SRC1,src1); 
     lPtr      = set_Parent2List(lPtr,TT_SRC2,src2); 
     lPtr->Typ = Tp;
     lPtr->OpId= OpId;
     lPtr->Nxt = NULL;

     if(l==NULL){
        l = lPtr;
     }else {
        lPtr->Nxt = l;  
        l         = lPtr;
 
    }

return l;
}

LIST* get_List(LIST *l,int OpId){

  LIST *lPtr = l;

  while(lPtr!=NULL){
        if(lPtr->OpId == OpId){ return lPtr; break; }
        lPtr = lPtr->Nxt;
  }

}
#endif
tt.c

#include "tt.h"

int main(){

int OpId =0;

LIST *lstr = NULL; //LIST list
TT   *tPtr = NULL; //TT   point
LIST *lPtr = NULL; //LIST point

lstr = set_List(lstr,OpId,TT_ADD,"a","b","c"); OpId++;
lstr = set_List(lstr,OpId,TT_ADD,"c","d","e"); OpId++;

if(lstr == NULL ){ printf("<E1> Initial List Error ...\n"); return -1; }
lPtr = get_List(lstr,0);

if(lPtr == NULL ){ printf("<E2> Get Ptr Error ...\n"); return -1; }
tPtr = (void *)lPtr->Parent;

if(tPtr == NULL ){ printf("<E3> Get Ptr->Parent Error ...\n"); return -1; }
tPtr = get_TTNode(tPtr,TT_SRC1);

if(tPtr == NULL ){ printf("<E4> Get Ptr->Parent->Src1 Error ...\n"); return -1; }
display_TTNode(tPtr);

return 0;
}
Results: Id :: 5,Nm a Refs: pointer 2 pointer memory map tPtr = (void *)lPtr->Parent;

沒有留言:

張貼留言