Exemplo n.º 1
0
/// Just like txAlfaBravoCharlie, but adds digits from
/// the stack to expose the entire alphabet.
void txAlphaNNN(MorseToken) {
  uint8_t n(0);
  uint8_t k(symbolStackSize());
  if (0 < k) {
    n += s(0).m2i();
    popSymbolStack(MorseToken());
  }
  if (1 < k) {
    n += 10 * s(1).m2i();
    popSymbolStack(MorseToken());
  }
  if (0 <= n && n <= ('Z' - 'A')) {
    char letterName[1 + sizeof "NOVEMBER"];
    const char* p(alphaBravoCharlie);
    size_t i(0);
    while (*p) {
      if (*p == ' ') {
	if (n) {
	  --n;
	} else {
	  break;
	}
      } else if (!n) {
	letterName[i++] = *p;
      }
      ++p;
    }
    txString(letterName);
  } else {
    txError();
  }
}
Exemplo n.º 2
0
void gradeNext(MorseToken) {
   if (1 < symbolStackSize()) {
      if (s(1) == s(0)) {
         txString("Y");
         popSymbolStack(MorseToken());
         popSymbolStack(MorseToken());
      } else {
         txString("N");
         popSymbolStack(MorseToken());
         txSymbolStackTop(MorseToken());
         popSymbolStack(MorseToken());
      }
   } else {
      txError();
   }
}
Exemplo n.º 3
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