⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sub.c

📁 一个非常小的C语言编译器!!! !!! !!! !!!!
💻 C
📖 第 1 页 / 共 2 页
字号:
//=====================================================////   StringT, Token, SymbolT Implementation  "sub.C"   ////=====================================================//#include "sub.h"#include "arm.h"#include "mips.h"//                                               Tan Watanabe, UEC//========== StringT Class Implementation ==========////---- Globals and statics ----//  String  str_null;            // Null string ("").  StringP StringT::first;      // First   StringT instance.  StringP StringT::current;    // Current StringT instance.//--------------------//void StringT::Initiation()     // Initiate the string block list.{  first    = this;  current  = first;  str_null = Append(""); } //---- Initiation end ----////--------------------//String StringT:: Append(String pStr) // Append pStr to the current block.{                                    // If no room, allocate the next block.  StringP lNext;  String  lStr;  int     lLeng;    lLeng = strlen(pStr);  if ((current->top+lLeng) >= current->tail) { // There is no room.    lNext = new StringT;                       // Allocate the next block    current->nextBlk = lNext;    current = lNext;    if ((current->top+lLeng) >= current->tail) // pStr is too long.      return (NULL);             // Error  }  strcpy(current->top, pStr);    // Append the string to the tail  lStr = current->top;           //   of the current block.  current->top += (lLeng+1);     // Add length + 1(for '\0')  return lStr;} //---- Append end ====////========== Token Class Implementation ==========////---- Global variables ----//Tkn_kindT tkn_chKindT[384];  // Token kind guessed by 1st char.Token     tkn_curr;          // Current tokenString    tkn_kindName[] =    {"blank  ","spec   ","compoun","eol    ","eof    ",     "intC   ","key    ","id     ","var    ","param  ",     "func   ","type   ","label  ","oper   ","reg    ","mrg    " };//--------------------//void Token::Initiation()     // Initiate the token class.{  int i;    tknC   = TKN_eol;    leng   = 0;  value  = 0;  for (i = 0; i < TKN_lim; i++)     name[i] = ' ';  for (i = 0; i < 384; i++)           // Initiate by special char kind.    tkn_chKindT[i] = tknSpec;         // (tkn_chKindT[0..126] is unused)  for (i = '0'; i <= '9'; i++)        // Numeric constant header.    tkn_chKindT[i+128] = tknIntC;     // char is -128 ~ 127  for (i = 'A'; i <= 'Z'; i++)        // Identifier initial.    tkn_chKindT[i+128] = tknId;   for (i = 'a'; i <= 'z'; i++)        // Identifier initial.    tkn_chKindT[i+128] = tknId;   tkn_chKindT['_'+128] = tknId;       // GIAN 011107  tkn_chKindT[' '+128] = tknBlank;    // Blank  tkn_chKindT['='+128] = tknCompound; // Compound operator initial.  tkn_chKindT['>'+128] = tknCompound;  tkn_chKindT['<'+128] = tknCompound;  tkn_chKindT['!'+128] = tknCompound;  tkn_chKindT[TKN_eol+128] = tknEol;  // End of line.  tkn_chKindT[TKN_eof+128] = tknEof;  // End of file (EOF == -1).} //---- Initiation end ----////--------------------//void Token::Get()             // Get the next token from inFile.{  while ((tknC != TKN_eof)&&         ((tknC == ' ')||(tknC == TKN_tab)||(tknC == TKN_eol)))    tknC = NextC();              // Skip blanks, tabs, and EOL.  leng = 0;  kind = tkn_chKindT[tknC+128];  // Guess the token kind by 1st char.  if (kind == tknId) {           // Identifier.    do {      if (leng < TKN_lim)        // Cut out the identifier.        name[leng] = tknC;      leng++;      tknC = NextC();    } while ((tkn_chKindT[tknC+128] == tknId)||             (tkn_chKindT[tknC+128] == tknIntC));    if (leng > TKN_lim)          // Truncate if too long.      leng = TKN_lim;  }else if (kind == tknIntC) {   // Numeric constant.    value = 0;    do {                         // Cut out the integer constant.      value = value*10 + ((int)tknC - (int)'0');      if (leng < TKN_lim)        name[leng] = tknC;      leng++;      tknC = NextC();    } while (tkn_chKindT[tknC+128] == tknIntC);    if (leng > TKN_lim)          // Truncate if too long.      leng = TKN_lim;  }else {            // Special character or compound operator.    name[0] = tknC;    leng = 1;    tknC = NextC();    if (kind == tknCompound) {  // May be compound operator.      if (((name[0] == '=')&&((tknC == '=')||(tknC == '>'))) ||          ((name[0] == '!')&&(tknC == '=')) ||          ((name[0] == '<')&&(tknC == '=')) ||          ((name[0] == '<')&&(tknC == '<')) ||          ((name[0] == '>')&&(tknC == '=')) ||          ((name[0] == '>')&&(tknC == '>')) ||          ((name[0] == '-')&&(tknC == '>')) ) { // Compound operator.        name[1] = tknC;        leng = 2;        tknC = NextC();      }    }else if ((name[0] == '/')&&(tknC == '*')) {  // Comment start      tknC = NextC();      while ((tknC != TKN_eof)&&             ((tknC != '*')||((tknC = NextC()) != '/')))         tknC = NextC();      tknC = NextC();      Get();    }  }  name[leng] = '\0';            // Attach end_of_string indication.} //---- Get end ----////-------- Static Variables --------//char Token::tknC;           // Current token character.//========== SymbolTable Class Implementation ==========////-------- Global Variables --------//  StringT sym_string;       // String table containing symbol names.  SymbolP                   // Predefined symbols.    sym_eof,    sym_lpar,   sym_rpar,  sym_lbracket,sym_rbracket,    sym_lbrace, sym_rbrace, sym_comma, sym_scolon,  sym_period,    sym_arrow,  sym_int,    sym_ptr,   sym_void,    sym_extern,      sym_if ,    sym_else,   sym_while, sym_return,  sym_add ,    sym_sub,    sym_mult,   sym_div,   sym_assign,  sym_equal,    sym_ne,     sym_gt,     sym_ge,    sym_lt,      sym_le,    sym_or,     sym_and,    sym_shl,   sym_shr,     sym_mod;    //    sym_main  SymbolT sym_tbl;      // Pre-declared symbol table.  SymbolP sym_curr;     // Current symbol.  int     sym_tempVar;  // Generated temporal index.//--------------------//void SymbolT::Initiation()         // Initiate symbol table and string table.     // (initiate hash table, set predefined symbols, and so on.) {  char lEof[2];    for (int i = 0; i < SYM_hashLim; i++)    hash[i] = NULL;                     // Nullify the hash table.  sym_string.Initiation();              // Initiate the string table.  head         = this;                  // Set pointers.  tail         = head;  name         = str_null;  lEof[0]      = TKN_eof;               // End file code  lEof[1]      = '\0';                  // followed by end of string.  sym_eof      = Set(lEof,  tknEof);    // Set availabel special characters  sym_lpar     = Set("(",   tknSpec);   // in the symbol table.  sym_rpar     = Set(")",   tknSpec);  sym_lbracket = Set("[",   tknSpec);  sym_rbracket = Set("]",   tknSpec);  sym_lbrace   = Set("{",   tknSpec);  sym_rbrace   = Set("}",   tknSpec);  sym_comma    = Set(",",   tknSpec);  sym_scolon   = Set(";",   tknSpec);  sym_period   = Set(".",   tknSpec);  sym_int      = Set("int",    tknType);  // Set key words in the symbol table.  sym_void     = Set("void",   tknType);  sym_extern   = Set("extern", tknType);  sym_if       = Set("if",     tknKey);  sym_else     = Set("else",   tknKey);  sym_while    = Set("while",  tknKey);  sym_return   = Set("return", tknKey);  sym_add      = SetOp("+" ,tknOper, opAdd ); // Set operators in the   sym_sub      = SetOp("-" ,tknOper, opSub ); //   symbol table.  sym_mult     = SetOp("*" ,tknOper, opMult);  sym_div      = SetOp("/" ,tknOper, opDiv );  sym_assign   = SetOp("=" ,tknOper, opStore);  sym_equal    = SetOp("==",tknOper, opBrne);   sym_ne       = SetOp("!=",tknOper, opBreq);  sym_gt       = SetOp(">" ,tknOper, opBrle);  sym_ge       = SetOp(">=",tknOper, opBrlt);  sym_lt       = SetOp("<" ,tknOper, opBrge);  sym_le       = SetOp("<=",tknOper, opBrgt);  //  sym_main     = Set("main",tknFunc);  sym_int->SizeSet(4);} //---- Initiation end ----////--------------------//SymbolP SymbolT::SearchOrAdd( // Search given symbol in the symbol table                          // and add it if not found.  String    pString,      //   Name string of the symbol to be searched.  int       pLeng,        //   Character length of pString.  Tkn_kindT pKind)        //   Token kind of the symbol. For identifier,                          //   give tknId if it is not yet declared.                          // Return symbol table ptr of the symbol.{  int     lHash;  SymbolP lSymp, lPrev, lNew;  String  lName;    lHash = HashKey(pString, pLeng);  lSymp = hash[lHash];  lPrev = NULL;  while ((lSymp != NULL)&&strcmp(pString, lSymp->name)) {     lPrev = lSymp;                      // Not equal.    lSymp = lSymp->hashChain;           // Advance to the next symbol.  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -