예제 #1
0
uint32_t* assemble(char*** tokens, uint32_t lines)
{
  uint32_t i;
  uint32_t* assembledInstrs = calloc(lines, 8);
  char* dataprocessing[12] = {"add", "sub", "rsb", "and", "eor", "orr", "mov", "tst", "teq", "cmp", "lsl", "andeq"};
  char* multiply[2] = {"mul", "mla"};
  char* singleDataTransfer[2] = {"ldr", "str"};
  char* branch[7] = {"beq", "bne", "bge", "blt", "bgt", "ble", "b"};
  node* symbolTable = createSymbolTable(tokens, lines);

  for (i = 0; i < lines; i++)
  {
    if(arrayContains(tokens[i][0], dataprocessing, 12))
      assembledInstrs[i] = data_processing(tokens[i]);
    else if(arrayContains(tokens[i][0], multiply, 2))
      assembledInstrs[i] = multiply_instr(tokens[i]);
    else if(arrayContains(tokens[i][0], singleDataTransfer, 2))
      assembledInstrs[i] = single_data_transfer(tokens[i], symbolTable);
    else if(arrayContains(tokens[i][0], branch, 7))
      assembledInstrs[i] = branch_instr(tokens[i], symbolTable);
    else
      printf("Unknown command");
  }
  return assembledInstrs;
}
예제 #2
0
파일: ucode.c 프로젝트: wan2land/mini-c
int insert(SymbolTable *table, char *id, Specifier spec, Qualifier qual, int offset, int width, int init)
{
    int symID;
    SymbolTable* children = NULL;

    if (qual == QUAL_FUNC) {
        children = createSymbolTable(id);
    } else {
        if ((symID = lookup(table, id)) != -1) {
            return symID;
        }
    }
    int top = table->count;

    table->rows[top].id = id;
    table->rows[top].spec = spec;
    table->rows[top].qual = qual;
    table->rows[top].offset = offset;
    table->rows[top].width = width;
    table->rows[top].init = init;
    table->rows[top].table = children;
    table->rows[top].base = table->base;
    table->count++;

    return top;
}
예제 #3
0
int solve(FILE *f) {
	SymbolTable *table = NULL;
	int i;
	
	table = createSymbolTable(makeDictionaryEntry, compareDictionaryEntry);
	getTableData(f, table);
	findWords(table);

	dropSymbolTable(table);
	return 0;
}
예제 #4
0
int main()
{
  char choice;
  char name[80];
  int number;
  int found;
  SymbolTable book;
  int i;
  /* initialize phone book */
  book = createSymbolTable(&makePhoneBook, &comparePhone);

  do{
    choice = getMenu();
    switch(choice){

    case '1':
      printf("Enter the name: ");
      scanf("%[^\n]", name);
      myfflush();
      printf("Enter the number: ");
      scanf("%d", &number);
      myfflush();
      addEntry(&number, &name, &book);
      printf("%d\n", book.total);
      break;

    case '2':
      printf("Enter the number you want to find: ");
      scanf("%d", &number);
      myfflush();
      found = getEntryValue(&number, &book);
      if(found == -1) printf("Not found\n");
      else{
	displayTitle();
	displayEntry(book.entries[found]);
      }      
      break;

    case '3':
      displayTitle();
      for(i = 0; i < book.current; i++){
	displayEntry(book.entries[i]);
      }
      break;

    case '4':
      dropSymbolTable(&book);
      break;
      
    default: printf("Invalid choice\n");
    }
  }while(choice != EXIT);
}
예제 #5
0
int
commandInit()
{
    if (gCommandState.commandStateInitialized) {
        return -1;
    }
    gCommandState.symbolTable = createSymbolTable();
    if (gCommandState.symbolTable == NULL) {
        return -1;
    }
    gCommandState.commandStateInitialized = true;
    return 0;
}
예제 #6
0
int solve(FILE *f) {
	SymbolTable *table = NULL;
	int i;
	
	table = createSymbolTable(makeDomainEntry, compareDomainEntry);
	getTableData(f, table);
	do {
		findWords(table);
		printf("Do you want to continue?\n");
	} while(getContinueRequest() != 'N');

	dropSymbolTable(table);
	return 0;
}
예제 #7
0
파일: ucode.c 프로젝트: wan2land/mini-c
SymbolTable* initSymbolTable()
{
    return createSymbolTable(NULL);
}
예제 #8
0
void semantics(ASTnode* ASTroot){
	firstMatrix=1;
	if(ASTroot==NULL){
		return;
	}
	int z,noTraverse=0;
	ASTnode *rows,*l;
	token bufToken;
	SymbolTable *tmp;
	SymbolTableEntryNode *t;
	int p=0;
	if(sflag==0){
		//first time, main function so create a symbol table for main function
		sflag=1;
		S[0]=createSymbolTable(size, NULL, "MAIN");//parentTable of Main is NULL
		symbolStack=createSymbolStack();
		pushSymbolStack(S[0],symbolStack);
		p=1;
		counter++;
		}
		
	switch(ASTroot->label){
	//Symbol Table creates only in 1, 61, 64
	//Symbol table populates in  1:functionDef,31:declarationStmt
		case 1://make a new symbol table, this would be the scope unless an END is encountered, functionDef
			InsertSymbolTable(topSymbolStack(symbolStack), ASTroot);
			S[counter]=createSymbolTable(size, topSymbolStack(symbolStack),ASTroot->array[1]->t.lexeme);
			pushSymbolStack(S[counter],symbolStack);
			InsertSymbolTableFun(topSymbolStack(symbolStack), ASTroot);//for input and output arguments of function
			p=1;
			counter++;
			break;
		case 2://ifstmt
			S[counter]=createSymbolTable(size, topSymbolStack(symbolStack),"IF");
			pushSymbolStack(S[counter],symbolStack);
			p=1;
			counter++;
			break;
		case 3: noTraverse=1;
			if(strcmp(topSymbolStack(symbolStack)->symbolTableName,ASTroot->array[0]->t.lexeme)==0){//checking for Recursion
				
				semanticError(3,ASTroot->array[0]->t);
				return;
				}
			//check for input parameter list and function signature- input and output
			tmp=topSymbolStack(symbolStack);
			 while(tmp!=NULL){
			 	z=SearchHashTable(tmp, ASTroot->array[0]->t.lexeme);
				if(z!=-1)
					break;
				else tmp=tmp->parentTable;
				}
				if(tmp==NULL){
					semanticError(1,ASTroot->t);
					break;
				}//declaration of FunId is checked here itself
				t=findSymbolTableNode(tmp->table[z].next,ASTroot->array[0]->t.lexeme);
				if(t->type.fid.outputSize!=typeCounter){
					semanticError(5,ASTroot->array[0]->t);
					break;
				}
				else{
					for(z=0; z<=t->type.fid.outputSize; z++){
						if(t->type.fid.output[z]!=type[z]){
							semanticError(5,ASTroot->array[0]->t);
							noTraverse=1;
							break;//from for
						}					
				}
					typeCounter=-1;//successfully implemented.
				}
				l=ASTroot->array[1];
				for(z=0; z<=t->type.fid.inputSize; z++){
					if(l==NULL){
						semanticError(5,ASTroot->array[0]->t);//number of output parameters
						noTraverse=1;
						break;
						}
					if(t->type.fid.input[z]!=findTypeVar(l->array[0])){
						semanticError(14,ASTroot->array[0]->t);//type Mismatch
						noTraverse=1;
						break;
					}
					l=l->array[1];					
				}
			break;
		
		case 11://else stmt
			S[counter]=createSymbolTable(size, topSymbolStack(symbolStack),"ELSE");
			pushSymbolStack(S[counter],symbolStack);
			p=1;
			counter++;
			break;	
		case 26:if(ASTroot->array[0]->label==67){
				t=getSymbolTableNode(ASTroot->array[1]);
				t->type.id.initialized=1;
			}
		case 27: break;
		case 28: break; //it should not come
		case 29: break;
		case 30: break;
		case 31://declaration stmt
			InsertSymbolTable(topSymbolStack(symbolStack), ASTroot);
			return;
			break;
		case 51://Assignment
			noTraverse=1;
			typeCounter=-1;
			if(ASTroot->array[1]->label==3){//function call statement
			
				if(ASTroot->array[0]->label==54){//single list
					if(outputCheck1(ASTroot->array[0])==0)//send leaf directly
					return;
				//1- it should already have been declared, 2-if so, then it's type should be recorded
				}
				else{
				//send l
					if(outputCheck(ASTroot->array[0])==0)
						return;
				}
				semantics(ASTroot->array[1]);
			}
			if(ASTroot->array[1]->label==60){//size stmt
				//1- check if ID is declared, 2- What is the type of ID, 3- Compare with the return type
				if(!isDeclared(ASTroot->array[1]->array[0])){
					semanticError(1,ASTroot->array[1]->array[0]->t);
					return;
				}
				z=findType(ASTroot->array[1]->array[0],0);
				if(z==57){
					if(outputCheck(ASTroot->array[0])==0){//it will populate type of LHS if declared, else returns 0
						return;
						}
					else{//declared
						if(!(typeCounter==0&&type[0]==57))
							semanticError(6,ASTroot->array[0]->t);
							return;
						}
					}
				else if(z==58){
					if(outputCheck(ASTroot->array[0])==0){//it will populate type of LHS if declared, else returns 0
						return;
						}
					else{//declared
						if(!(typeCounter==1&&type[0]==55&&type[1]==55))
							semanticError(6,ASTroot->array[0]->t);
							return;
						}
						
					}
				else {
					semanticError(8,ASTroot->array[1]->array[0]->t);//Size contains other that String and Matrix
					}
				}
			if(ASTroot->array[1]->label==37){//Arithmetic Expression
				l=ASTroot->array[0];
				z=findType(l,1);
				if(l->label==54){
					if(z==57){
						if(ASTroot->array[1]->array[1]==NULL){
							if(ASTroot->array[1]->array[0]->array[1]==NULL){
								if(findTypeVar(ASTroot->array[1]->array[0]->array[0])==57){//initialization
									StringInit(ASTroot->array[0],ASTroot->array[1]->array[0]->array[0]->t.lexeme);
									return;				
							}
							}
						}
					}
					else if(z==58){//lhs is matrix
						firstMatrix=1;
						if(ASTroot->array[1]->array[1]==NULL){
							if(ASTroot->array[1]->array[0]->array[1]==NULL){
								if((ASTroot->array[1]->array[0]->array[0]->label)==44){//initialization
									MatrixInit(ASTroot->array[0],ASTroot->array[1]->array[0]->array[0]);			
									return;
								}
							}
						}
					}
										
				}
				if(z==-1)
					break;
				typeCounter++;
				type[typeCounter]=z;
				if((z=findTypeAE(ASTroot->array[1]))!=type[typeCounter]){
					bufToken.lineNumber=l->t.lineNumber;
					semanticError(10,bufToken);
					break;
				}
				//valid Arithmetic expression
				//debug();
				t= getSymbolTableNode(ASTroot->array[0]);
				t->type.id.initialized=1;
				typeCounter=-1;
			}
			break;
		case 52://go to case 54
		case 54: if(!isDeclared(ASTroot))
					semanticError(1,ASTroot->t);
			break;	
		case 75:// AND
		case 76:// OR
		case 77:// LT
		case 78:// LE
		case 79:// EQ
		case 80:// GT
		case 81:// GE
		case 82:// NE
		case 83:// NOTbooleanExpressionSemantics(ASTnode* BE)	
			noTraverse=1;
			if(booleanExpressionSemantics(ASTroot)==0){
				semanticError(10,bufToken);
			}	
		default: break;
			
	}//end of switch
	int i;
	if(noTraverse==0){
		for( i=0; i<ASTroot->arraySize; i++){
			if(ASTroot->array[i]!=NULL){
				semantics(ASTroot->array[i]);
			}
		}
	}
	
	if(p){
		//if popping SymbolTable is a function, then check if it's output parameter are accurately initialised or not
		tmp=popSymbolStack(symbolStack);
		if(strcmp(tmp->symbolTableName,"MAIN")!=0&&strcmp(tmp->symbolTableName,"IF")!=0&&strcmp(tmp->symbolTableName,"ELSE")!=0){//it is a function
			int i;
			for(i=0; i<tmp->outputParameter; i++){
				t=outputParameterInitCheck(tmp,tmp->outputParameterLexeme[i]);
				if(t->type.id.initialized!=1)
					{
						semanticError(13,ASTroot->array[1]->t);
						break;
					}
					}
		}
		}
	
	}//end of function
예제 #9
0
void opCount(int x)
{
    int i,j,k,flag=0;
    char tokGen[30]="";
    char ita[10];
    printf("\n\noperators:%d\n",countop);


    for(i=0;i<x;i++)
    {
      int c=checkId(&head,&tail,tok[i]);
        if(c==0)
         {
            for(j=0;j<noOfOperators;j++)
            {
              if(strcmp(op[j],tok[i])==0)
               {
                 for(k=0;k<noOfOperators;k++)
                   if(strcmp(op[k],tok[i+1])==0)
                   {
                       strcat(tokGen,op[j]);
                       strcat(tokGen,op[k]);
                       printToken(tokGen);
                       strcpy(tokGen,"");
                        flag==1;
                        i+=2;
                        break;
                   }

             }
            }
            if(flag==0)
              printToken(tok[i]);
         }
         else
         {
                         strcat(tokGen,"id,");
                        //strcat(tokGen,itoa(c,ita,10));
                         snprintf(ita,10,"%d",c);
                         strcat(tokGen,ita);
                         //strcat(tokGen,snprintf(ita,"%d",c));
                         printToken(tokGen);
                         strcpy(tokGen,"");
         }
      for(j=0;j<noOfKeyWords;j++)
        if(strcmp(key[j],tok[i])==0 && strcmp(tok[i+2],"(")!=0 && strcmp("if",tok[i])!=0)
         {
           if(strcmp(tok[i+2],";")==0 || strcmp(tok[i],"else")==0)
           {
                //printf("\n%s %s",tok[i],tok[i+1]);
                int c=checkId(&head,&tail,tok[i+1]);
                  if(c==0)
                      {

                         strcpy(attr[0],tok[i+1]);
                         strcpy(attr[1],tok[i]);
                         createSymbolTable(sl++,attr,&head,&tail);
                         strcat(tokGen,"id,");
                         //strcat(tokGen,itoa(sl-1,ita,10));
                         snprintf(ita,10,"%d",sl-1);
                         strcat(tokGen,ita);
                         printToken(tokGen);
                         strcpy(tokGen,"");

                      }
           }
          else
          {
             strcpy(type,tok[i]);
             while(strcmp(tok[++i],";")!=0 && strcmp(tok[i],"char")!=0 && strcmp(tok[i],"int")!=0 )
	       {
             if(strcmp(tok[i],"=")==0)
              {
                 // printf("\n%s %s",type,tok[i-1]);
                  int c=checkId(&head,&tail,tok[i-1]);
                  if(c==0)
                      {
                         strcpy(attr[0],tok[i-1]);
                         strcpy(attr[1],type);
                         createSymbolTable(sl++,attr,&head,&tail);
                         strcat(tokGen,"id,");
                         //strcat(tokGen,itoa(sl-1,ita,10));
                         snprintf(ita,10,"%d",sl-1);
                         strcat(tokGen,ita);
                         printToken(tokGen);
                         strcpy(tokGen,"");
                      }

              }
             else if(strcmp(tok[i],",")==0)
               {
                   //printf("\n%s %s",type,tok[i-1]);

                   int c=checkId(&head,&tail,tok[i-1]);
                  if(c==0)
                      {

                         strcpy(attr[0],tok[i-1]);
                         strcpy(attr[1],type);
                         createSymbolTable(sl++,attr,&head,&tail);
                            //strcat(tokGen,"<");
                         strcat(tokGen,"id,");
                         //strcat(tokGen,itoa(sl-1,ita,10));
                         snprintf(ita,10,"%d",sl-1);
                         strcat(tokGen,ita);
                         //strcat(tokGen,">");
                         //strcat(tokGen,"\0");
                         //printf("%s ",tokGen);
                         printToken(tokGen);
                         strcpy(tokGen,"");

                      }
                   strcat(tokGen,",");
                   printToken(tokGen);
                   strcpy(tokGen,"");

               }
	     }
          
           if(strcmp(tok[i-2],"=")!=0)
               {
                 //printf("\n%s %s",type,tok[i-1]);
                  int c=checkId(&head,&tail,tok[i-1]);
                  if(c==0)
                      {
                         strcpy(attr[0],tok[i-1]);
                         strcpy(attr[1],type);
                         createSymbolTable(sl++,attr,&head,&tail);
                         strcat(tokGen,"id,");
                         //strcat(tokGen,itoa(sl-1,ita,10));
                         snprintf(ita,10,"%d",sl-1);
                         strcat(tokGen,ita);
                         printToken(tokGen);
                         strcpy(tokGen,"");
                      }

               }
           i--;
          }
        }
        else if(strcmp(key[j],tok[i])==0 && strcmp(tok[i+2],"(")==0 && strcmp("if",tok[i])!=0)
         {
               //printf("\n%s %s",tok[i],tok[i+1]);
                  int c=checkId(&head,&tail,tok[i+1]);
                  if(c==0)
                      {
                         strcpy(attr[0],tok[i+1]);
                         strcpy(attr[1],tok[i]);
                         createSymbolTable(sl++,attr,&head,&tail);
                         strcat(tokGen,"id,");
                         //strcat(tokGen,itoa(sl-1,ita,10));
                         snprintf(ita,10,"%d",sl-1);
                         strcat(tokGen,ita);
                         printToken(tokGen);
                         strcpy(tokGen,"");
                         i++;
                      }

         }

    }


}