Advertisement
alexarcan

Lab4 CT

Mar 17th, 2016
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.15 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<errno.h>
  4. #include<ctype.h>
  5. #include<stdarg.h>
  6. #include<string.h>
  7. #define SAFEALLOC(var,Type) if((var=(Type*)malloc(sizeof(Type)))==NULL) err("not enough memory");
  8. int line;
  9. const char *pCrtCh;
  10.  
  11. enum {ID, END, CT_INT,  ASSIGN, SEMICOLON, BREAK, CHAR, EQUAL};
  12. //   tokens  codes
  13. typedef struct _Token{
  14.   int code; // code (name)
  15.   union{
  16.     char *text; //  used for ID, CT_STRING(dynamically allocated )
  17.     long  int  i;  //  used   for CT_INT, CT_CHAR
  18.     double r;//  used for   CT_REAL
  19.   };
  20.   int line; // the input file line
  21.   struct _Token *next; // link to the next token
  22. }Token;
  23.  
  24. void err(const char *fmt,...)
  25. {
  26.   va_list va;
  27.   va_start(va,fmt);
  28.   fprintf(stderr,"error: ");
  29.   vfprintf(stderr,fmt,va);
  30.   fputc('\n',stderr);
  31.   va_end(va);
  32.   exit(-1);
  33. }
  34.  
  35. Token *lastToken, *tokens;
  36.  
  37. Token *addTk(int code)
  38. {
  39.   Token *tk;
  40.   SAFEALLOC(tk,Token);
  41.   tk->code=code;
  42.   tk->line=line;
  43.   tk->next=NULL;
  44.   if(lastToken){
  45.     lastToken->next=tk;    
  46.   }else{
  47.       tokens=tk;
  48.   }
  49.   lastToken=tk;
  50.   return tk;  
  51. }
  52.  
  53.  
  54. void tkerr (const Token *tk, const char *fmt,...)
  55. {
  56.   va_list va;
  57.   va_start(va,fmt);
  58.   fprintf(stderr,"error in line %d:", tk->line);
  59.   vfprintf(stderr,fmt,va);
  60.   fputc('\n', stderr);
  61.   va_end(va);
  62.   exit(-1);
  63. }
  64.  
  65. char* createString(const char *s1, const char *s2)
  66. {
  67.   char *result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-terminator
  68.   strcpy(result, s1);
  69.   strcat(result, s2);
  70.   return result;
  71. }
  72.  
  73. int getNextToken()
  74. {
  75.   int state=0, nCh;
  76.   char ch;
  77.   const char *pStartCh;
  78.   Token *tk;
  79.  
  80.   while(1)
  81.   {
  82.    ch=*pCrtCh;
  83.    printf("state: %d, character:%c\n",state,ch);
  84.    switch(state)
  85.    {
  86.      case 0:
  87.        if(isalpha(ch)||ch=='_')
  88.        {
  89.     pStartCh = pCrtCh;
  90.     pCrtCh++;
  91.     state=1;
  92.        }
  93.        else if(ch == '=')
  94.        {
  95.      pCrtCh++;
  96.      state=3;
  97.        }
  98.        else if(ch==' '||ch=='\r'||ch=='\t')
  99.        {
  100.      pCrtCh++; // consume the characterand remains in state 0
  101.        }
  102.        else if(ch=='\n')//handled separately in order to update the current line
  103.        {
  104.      line++;
  105.      pCrtCh++;
  106.        }
  107.        else if(ch==0) //the end of the input string
  108.        {
  109.      addTk(END);
  110.      return END;
  111.        }
  112.        else tkerr(addTk(END),"invalid character");
  113.        break;
  114.        
  115.      case 1:
  116.        if(isalnum(ch)||ch=='_') pCrtCh++;
  117.        else state=2;
  118.        break;
  119.        
  120.      case 2:
  121.        nCh=pCrtCh-pStartCh;//the id length
  122.        //keywords tests
  123.        if(nCh==5&&!memcmp(pStartCh,"break",5))tk=addTk(BREAK);
  124.        else if(nCh==4&&!memcmp(pStartCh,"char",4))tk=addTk(CHAR);
  125.        //...all keywords...
  126.        else //if no keyword, then it is an ID
  127.        {
  128.      tk=addTk(ID);
  129.      tk->text=createString(pStartCh,pCrtCh);
  130.        }
  131.        return tk->code;
  132.      
  133.      case 3:
  134.     if(ch=='=')
  135.     {  
  136.       pCrtCh++;
  137.       state=4;
  138.     }
  139.     else state = 5;
  140.     break;
  141.    
  142.      case 4:
  143.        addTk(EQUAL);
  144.        return EQUAL;
  145.        
  146.      case 5:
  147.        addTk(ASSIGN);
  148.        return ASSIGN;
  149.        
  150.    }  
  151.    
  152.   }
  153. }
  154.  
  155. int main(void)
  156. {
  157.  
  158.    
  159.   return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement