Statements(c programming language)

 


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define NOTAPPLICABLE -1

#define IF "if"
#define WHILE "while"
#define ELIF "else if"
#define ELSE "else"
#define DO "do"
#define SWITCH "switch"
#define FOR "for"
#define RETURN "return"
#define CONTINUE "continue"
#define BREAK "break"
#define GTO "goto"
#define CASE "case"


enum StatementType
{
IFCON, /*if*/
ELIFCON,  /*else if*/
ECON,     /*else*/
WLOOPCON,
DOLOOPCON,
    SWCON,
SWCASE,
FLOOPCON,
RET,
CONT,
BRK,
LABEL,
LAST_STATEMENT
};

struct C_node
{
int priority;
    enum StatementType OpernType;
    int compound_level;
    int label_no;
struct C_node* next;
};

struct C_node *head=NULL;
struct C_node *tail = NULL;

/*  Label part    */
int Label = 1;
int getNextLabel()
{
return Label++;
}
int getCurrentLabel()
{
return Label;
}
static int size = 0;
void insert_pll(int pr,enum StatementType opType,int compound_level, char IsLabel)
{
if(head == NULL)
{
head = (struct C_node *) malloc(sizeof(struct C_node));
tail = head;
head->next =NULL;
}
else
{
struct C_node *temp = (struct C_node *) malloc(sizeof(struct C_node));
tail->next = temp;
tail = temp;
tail->next = NULL;
}
tail->priority = pr;
tail->OpernType = opType;
tail->compound_level = compound_level;
// printf("compound level in ll :%d %d\n", tail->compound_level, compound_level );
if(IsLabel == TRUE)
{     
  tail->label_no = getNextLabel();
//  printf("%d\n", tail->label_no );
    }
else if(IsLabel == FALSE)
{
 
   tail->label_no = getCurrentLabel();
// printf("%d\n", tail->label_no );
    }
else if(IsLabel == NOTAPPLICABLE)
  tail->label_no = NOTAPPLICABLE;
size++;
return;
}


void DecodeNode(struct C_node * node)
{
switch(node->OpernType)
{
case IFCON:
  printf("Node: priority %d OpernType IFCON label_no %d\n",node->priority,node->label_no);
break;
case WLOOPCON:
    printf("Node: priority %d OpernType WLOOPCON label_no %d\n",node->priority,node->label_no);
break;
case FLOOPCON:
    printf("Node: priority %d OpernType FLOOPCON label_no %d\n",node->priority,node->label_no);
break;
}
printf("\n");
return;
}
void traverse_pll()
{
int i;
struct C_node* tr = head;
for (i=0; i<size;i++)
{
DecodeNode(tr);
tr = tr->next;
}
}
int is_space(char *str)
{
int i=0;
while(*(str+i) == ' ' || *(str+i) == '\t' || *(str+i) == '\v')
  i++;
  
return i;
}
int is_space_with_nl(char *str) // with new line
{
int i=0;
while(*(str+i) == ' ' || *(str+i) == '\t' || *(str+i) == '\v' || *(str+i) == '\n')
  i++;
  
return i;
}

void char_read_statement(char *str,int *k)
{
static int compound_level = 1;
int pr = compound_level;
static int enable_do =0;
if(strncmp(str,RETURN,6) == 0 )
{
insert_pll(pr,RET, compound_level, TRUE);
*k += *k+6;
}
else if(strncmp(str,CONTINUE,8) == 0)
{
insert_pll(pr,CONT, compound_level, TRUE);
*k += *k+8;
}
else if(strncmp(str,BREAK,5) == 0)
{
insert_pll(pr,BRK, compound_level, TRUE);
*k += *k+5;
}
    else if(strncmp(str,DO,2) == 0)
{
    enable_do = 1;
// insert_pll(pr,DOLOOPCON, compound_level, TRUE);
    int keyword_size = sizeof(DO) -1;
    int j=is_space_with_nl(str+keyword_size);
if(*(str+keyword_size+j) != '{')
{
printf("error: expected ( \n");
exit(0);
}
*k += *k+keyword_size+j;
}
else if(strncmp(str,IF,2) == 0 )
{
int keyword_size = sizeof(IF) -1;
int j=is_space(str+keyword_size);
if(*(str+keyword_size+j) != '(')
{
printf("error: expected ( \n");
exit(0);
}
/**** expression read part****/
int f = j+1+keyword_size; // 2+1, 1 for reading ')'
while(*(str + f) != ')')
{
if(*(str + f) == '\n' || *(str + f) == '\0' || *(str + f) == EOF)
{
printf("error: expected ) \n");
exit(0);
}
f++;
}
char *expr = (char*) calloc(f - (j+keyword_size+1),sizeof(char));
strncpy(expr,str+j+keyword_size+1, f-(j+keyword_size+1));
printf("%s\n", expr);
// expr call part
free(expr);
if(*(str+f) == ')')
{
if(*(str+f+1) == ' ' || *(str+f+1) == '\t' || *(str+f+1) == '\v' || *(str+f+1) == '{')
{
int pr = compound_level;
printf("compound %d\n", pr);
insert_pll(pr,IFCON, compound_level, TRUE);
*k += f-1;
return;
}
else
{
printf("error: if condition wrong !");
exit(0);
}
}
else
{
printf("error: if condition wrong !");
exit(0);
}
}
else if(strncmp(str,WHILE,5) == 0)
{
    enum StatementType t = WLOOPCON;
    if(enable_do)
    {
        t = DOLOOPCON;
        enable_do = 0;
    }
    
int keyword_size = sizeof(WHILE) -1;
int j=is_space(str+keyword_size);
if(*(str+keyword_size+j) != '(')
{
printf("error: expected ( \n");
exit(0);
}
/**** expression read part****/
int f = j+keyword_size+1; // 5+1, 1 for reading ')'
while(*(str + f) != ')')
{
if(*(str + f) == '\n' || *(str + f) == '\0' || *(str + f) == EOF)
{
printf("error: expected ) \n");
exit(0);
}
f++;
}
char *expr = (char*) calloc(f - (j+keyword_size+1),sizeof(char));
strncpy(expr,str+j+keyword_size+1, f-(j+keyword_size+1));
printf("%s\n", expr);
// expr call part
free(expr);
if(*(str+f) == ')')
{
if(*(str+f+1) == ' ' || *(str+f+1) == '\t' || *(str+f+1) == '\v' || *(str+f+1) == '{')
{
int pr = compound_level;
insert_pll(pr,t, compound_level, TRUE);
*k += f-1;
return;
}
else
{
printf("error: while condition wrong !");
exit(0);
}
}
else
{
printf("error: while condition wrong !");
exit(0);
}
}
else if(strncmp(str,SWITCH,6) == 0)
{
enable_switch = 1;
int keyword_size = sizeof(FOR) -1;
int j=is_space(str+keyword_size);
if(*(str+keyword_size+j) != '(')
{
printf("error: expected ( \n");
exit(0);
}
/**** expression read part****/
int f = j+keyword_size+j+1; // 5+1, 1 for reading ')'
while(*(str + f) != ')')
{
if(*(str + f) == '\n' || *(str + f) == '\0' || *(str + f) == EOF)
{
printf("error: expected ) \n");
exit(0);
}
f++;
}
*k += *k+f;
}
else if(strncmp(str,FOR,3) == 0)
{
int keyword_size = sizeof(FOR) -1;
int j=is_space(str+keyword_size);
if(*(str+keyword_size+j) != '(')
{
printf("error: expected ( \n");
exit(0);
}
/**** expression read part****/
int f = j+keyword_size+1; // 5+1, 1 for reading ')'
while(*(str + f) != ')')
{
if(*(str + f) == '\n' || *(str + f) == '\0' || *(str + f) == EOF)
{
printf("error: expected ) \n");
exit(0);
}
f++;
}
char *expr = (char*) calloc(f - (j+keyword_size+1),sizeof(char));
strncpy(expr,str+j+keyword_size+1, f-(j+keyword_size+1));
printf("%s\n", expr);
// expr call part
free(expr);
if(*(str+f) == ')')
{
if(*(str+f+1) == ' ' || *(str+f+1) == '\t' || *(str+f+1) == '\v' || *(str+f+1) =='{')
{
int pr = compound_level;
printf("compound %d", pr);
insert_pll(pr,FLOOPCON, compound_level, TRUE);
*k += f-1;
return;
}
else
    {
   printf("error: for condition wrong !");
   exit(0);
    }
}
else
{
printf("error: for condition wrong !");
exit(0);
}
}
else if(*str =='{')
{
compound_level++;
}
else if(*str == '}')
{
compound_level--;
}
}


void statement(char *tape)
{
int len=strlen(tape),i;
   for(i=0; i< len; )
   {
      char_read_statement(tape+i,&i);
      i++;
   }
traverse_pll();
}


int main()
{
char tape[] = "if( a ) { if(b) { if (a) { }  }  } for (d) { }";
statement(tape);
// system("pause");
}

Comments