コード例 #1
0
struct varNode* decl()
{
	struct varNode* varList;
	varList = ((struct varNode *) malloc(sizeof(struct varNode)));
	
		ttype = getToken();
		if (ttype == ID)
		{	
			symAdd(varList);
		
			ttype = getToken();
			if (ttype == COMMA)
			{	
				varList->next = decl();
				return varList;
			} 
			else
			{	
				//ungetToken();
				return varList;
			}
		} 
		else
		{	
			return NULL;
		}
	
	//return varList;
}
コード例 #2
0
ファイル: compiler-1.c プロジェクト: beckabec/Compiler
struct id_listNode* id_list()
{
	struct id_listNode* idList;
	
	idList = make_id_list();

	ttype = getToken();
	if (ttype == ID)
	{	
		idList->id = (char*) malloc(tokenLength+1);
		strcpy(idList->id, token);
		symAdd(idList->id);
		
		ttype = getToken();
		if (ttype == COMMA)
		{
			idList->id_list = id_list();
			return idList;
			
		} 
		else
		{	
			ungetToken();
			return idList;
		}
	} 
	else
	{	
		return NULL;
	}
}
コード例 #3
0
ファイル: nfsd.c プロジェクト: netdebug/vxnfsd
void
nfs_start(char *pathtab_name)
{
	FILE           *fp;
	char            path[PATH_MAX];
	int             id;

	if ((nfsd_task_id = taskSpawn("tNfsd", nfsd_task_priority,
				      nfsd_task_options, nfsd_task_stacksize,
				      nfsd, 0)) != ERROR &&
	    (mountd_task_id = taskSpawn("tMountd", mountd_task_priority,
				 mountd_task_options, mountd_task_stacksize,
					mountd, 0)) != ERROR) {
		nfsd_debug("nfs_start: nfsd and mountd started OK\n");

		if (!pathtab_name)
			return;

		nfsd_pathtab = symTblCreate(8, FALSE, memSysPartId);

		if (!nfsd_pathtab) {
			nfsd_debug("nfs_start: can't create nfsd_pathtab\n");
			return;
		}
		if ((fp = fopen(pathtab_name, "r")) == 0) {
			symTblDelete(nfsd_pathtab);
			nfsd_debug("nfs_start: can't open pathtab file <%s>\n",
				   pathtab_name);
			return;
		}
		while (fgets(nfsd_buffer, sizeof(nfsd_buffer), fp)) {
			sscanf(nfsd_buffer, "%s %d", path, &id);

			path_to_all_lower(path);

			if (symAdd(nfsd_pathtab, path, (char *) id,
				   (SYM_TYPE) NFSD_MSDOS_TYPE, 0) == ERROR) {
				symTblDelete(nfsd_pathtab);
				nfsd_debug("nfs_start: can't add path <%s>\n",
					   path);
				return;
			}
			nfsd_debug("nfs_start: added path <%s> id %x\n",
				   path, id);
		}

		nfsd_debug("nfs_start: %d paths added to pathtab\n",
			   nfsd_pathtab->nsymbols);
	} else {
		nfsd_debug("nfs_start: didn't work\n");

		if (nfsd_task_id != ERROR)
			taskDelete(nfsd_task_id);
	}
}
コード例 #4
0
ファイル: parser.c プロジェクト: dario23/tush
/**
 * Pattern = <Name>
 */
static ast* parserPattern (parserCtx* ctx) {
    ast* node;

    //todo accept only [\w\d-]

    if (see_kind(ctx, tokenNormal)) {
        sym* symbol = symAdd(ctx->scope, ctx->current.buffer);
        accept(ctx);

        node = astCreateSymbol(symbol);

    } else {
        expected(ctx, "function argument");
        node = astCreateInvalid();
    }

    return node;
}
コード例 #5
0
ファイル: asm.c プロジェクト: ccckmit/Nand2Tetris
void pass1(string inFile) {
  printf("============= PASS1 ================\n");
  char line[100]="";
  FILE *fp = fopen(inFile, "r");
  int address = 0;
  while (fgets(line, sizeof(line), fp)) {
    char *code = parse(line);
    if (strlen(code)==0) continue;
    printf("%02d:%s\n", address, code);
    if (code[0] == '(') {
      char label[100];
      sscanf(code, "(%[^)])", label);
      symAdd(label, address, symTable, &symTop);
    } else {
      address ++;
    }
  }
  fclose(fp);
}
コード例 #6
0
struct primaryNode* primary(struct primaryNode* prim)
{
	struct varNode* op;
	//struct primaryNode* pNode;
	//pNode = make_primary();
	
	if ((ttype == ID)|(ttype == NUM))
	{	
		if (ttype == ID)
		{	
			op = symSearch(token);

			if(op == NULL)
			{ 	
				prim->id = (char *) malloc(tokenLength+1);
				strcpy(prim->id,token);	//allocating space for ID
				symAdd(op);
			}
			else
			{
				prim->id = op->vID;
				prim->ival = op->vValue;
			}
			
			return prim;
		}
		else if (ttype == NUM)
		{
			prim->ival = atoi(token); //convert string to an integer
			return prim;
		}
		else
		{
			return NULL;
		}
	} 
	else
	{	
		return NULL;
	}
}
コード例 #7
0
ファイル: parser.c プロジェクト: dario23/tush
static ast* parseLet (parserCtx* ctx) {
    match(ctx, "let");

    sym* symbol = 0;

    if (see_kind(ctx, tokenNormal)) {
        /*Name collision*/
        if (symLookup(ctx->scope, ctx->current.buffer))
            /*Error, but use the given name anyway*/
            error(ctx)("Symbol named '%s' already declared\n", ctx->current.buffer);

        symbol = symAdd(ctx->scope, ctx->current.buffer);
        accept(ctx);

    } else
        expected(ctx, "variable name");

    match(ctx, "=");

    ast* init = parseExpr(ctx);

    return astCreateLet(symbol, init);
}
コード例 #8
0
struct assignNode* assignment() 

{
	
	struct assignNode* assign;
	

	struct varNode* var;
	struct varNode* op;
	int oper;
	
	var = symSearch(token);

	
	if(var != NULL) 
	
	{
		
		//printf("var: %s\n",var->vID);
	
	
		assign = make_assign(var);
	
		ttype = getToken();



		if(ttype == EQUAL) 
		
		{
				
			//printf("EQUAL: %d\n",ttype);	
			//assign = expr(assign);
	
			
			ttype = getToken();
			if (ttype == NUM)
			{	
				//printf("NUM: %d\n",ttype);
				op = make_var();
		
				if(symSearch(token) == NULL)
				{	
					symAdd(op);
				}
		
				assign->op1 = symSearch(token);
		
				/*assign->op1 = primary(assign->op1);

		ttype = getToken();
		if ((ttype == PLUS) | (ttype == MINUS) | (ttype == MULT) | (ttype == DIV))
		{	
			assign->oper = ttype; //op is set + - * /
			assign->op2 = primary(assign->op2);
		}
		
		ungetToken();
		return assign;*/
			} 
			else if (ttype == ID)
			{
		
				assign->op1 = symSearch(token);
				//printf("assign->op1->vValue %d\n", assign->op1->vValue);
		
					/*printf("ID: %d\n",ttype);

		assign->op1 = primary(assign->op1);
		printf("ASSIGN ID %s\n", assign->op1->id);
			
		ttype = getToken();
		printf("PLUS: %d\n",ttype);
		if ((ttype == PLUS) | (ttype == MINUS) | (ttype == MULT) | (ttype == DIV))
		{
			assign->oper = ttype;
				
			ttype = getToken();
			if(ttype == ID) 
				
			{
	printf("ID: %d\n",ttype);				

				assign->op2 = primary(assign->op2);
				return assign;									
			} 
				
			else
				
			{
					
				ungetToken();
				
			}
		
		} 
			
		else 
			
		{
				
			return NULL;
			
		}
*/		
	
	 		}
	 
 			ttype = getToken();
 			//printf("SIGN %d\n", ttype);
 			if ((ttype == PLUS) | (ttype == MINUS) | (ttype == MULT) | (ttype == DIV))
 			{
				assign->oper = ttype;
				
				ttype = getToken();
				if (ttype == NUM)
				{	
					//printf("NUM: %d\n",ttype);
					op = make_var();
		
					if(symSearch(token) == NULL)
					{
						symAdd(op);
					}
		
					assign->op2 = symSearch(token);
		
				/*assign->op1 = primary(assign->op1);

		ttype = getToken();
		if ((ttype == PLUS) | (ttype == MINUS) | (ttype == MULT) | (ttype == DIV))
		{	
			assign->oper = ttype; //op is set + - * /
			assign->op2 = primary(assign->op2);
		}
		
		ungetToken();
		return assign;*/
				} 
				else if (ttype == ID)
				{
		
					assign->op2 = symSearch(token);
					//printf("assign->op2->vValue %d\n", assign->op2->vValue);
		
				/*printf("ID: %d\n",ttype);

		assign->op1 = primary(assign->op1);
		printf("ASSIGN ID %s\n", assign->op1->id);
			
		ttype = getToken();
		printf("PLUS: %d\n",ttype);
		if ((ttype == PLUS) | (ttype == MINUS) | (ttype == MULT) | (ttype == DIV))
		{
			assign->oper = ttype;
				
			ttype = getToken();
			if(ttype == ID) 
				
			{
	printf("ID: %d\n",ttype);				

				assign->op2 = primary(assign->op2);
				return assign;									
			} 
				
			else
				
			{
					
				ungetToken();
				
			}
		
		} 
			
		else 
			
		{
				
			return NULL;
			
		}
*/		
	
	 			}
	 			return assign;
	 		}
	 		else
	 		{
				ungetToken();
				return assign;
			}


		
		} 
		
		else 
		
		{
			
			return NULL;
		}
	
	} 
	
	else 
	
	{
		
		return NULL;
	
	}

}
コード例 #9
0
struct conditionNode* condition()
{
	struct conditionNode* cNode;
	struct varNode* var;
	//struct varNode* op2;
	struct statementNode* tBranch;
	struct statementNode* fBranch;
	int op;

	cNode = make_condition();

	ttype = getToken();
	//printf("ttype %d ln 1272\n", ttype);
	if ((ttype == ID)||(ttype == NUM))
	{
		//ungetToken(); //ungetToken since it still be parsed
		
		if(ttype == NUM)
		{
			var = make_var();
		
			if(symSearch(token) == NULL)
			{	
				symAdd(var);
			}
		
			cNode->op1 = symSearch(token);
		}
		else if (ttype == ID)
		{
			cNode->op1 = symSearch(token); //left operand of a condition is a primary
		}

		ttype = getToken();
		//printf("ttype %d ln 1273\n", ttype);
		if ((ttype == GREATER)||(ttype == GTEQ)||(ttype == LESS)
			||(ttype == NOTEQUAL)||(ttype == LTEQ))
		{
			cNode->operator = ttype; //relop is set to >, <, etc.
		
			ttype = getToken();
			//printf("ttype %d ln 1280\n", ttype);
			if ((ttype == ID)|(ttype == NUM))
			{
				//ungetToken(); //ungetToken since it still be parsed
				//cNode->op2 = symSearch(token); //right operand of a condition is a primary			
				
				if(ttype == NUM)
				{
					var = make_var();
		
					if(symSearch(token) == NULL)
					{	
						symAdd(var);
					}
		
					cNode->op2 = symSearch(token);
				}
				else if (ttype == ID)
				{
					cNode->op2 = symSearch(token); //left operand of a condition is a primary
				}
				
				return cNode;

			}
			else
			{
				return NULL;
			}
		}
		else
		{
			return NULL;
		}
	}
	else
	{
		return NULL;
	}
}
コード例 #10
0
struct assignNode* expr(struct assignNode* assign)
{
	struct varNode* op;
	struct varNode* oper2;
	int oper;

	ttype = getToken();
	if (ttype == NUM)
	{	
		//printf("NUM: %d\n",ttype);
		op = make_var();
		
		if(symSearch(token) == NULL)
		{
			symAdd(op);
		}
		
		assign->op1 = symSearch(token);
		
		/*assign->op1 = primary(assign->op1);

		ttype = getToken();
		if ((ttype == PLUS) | (ttype == MINUS) | (ttype == MULT) | (ttype == DIV))
		{	
			assign->oper = ttype; //op is set + - * /
			assign->op2 = primary(assign->op2);
		}
		
		ungetToken();
		return assign;*/
	} 
	else if (ttype == ID)
	{
		
		assign->op1 = symSearch(token);
		
		/*printf("ID: %d\n",ttype);

		assign->op1 = primary(assign->op1);
		printf("ASSIGN ID %s\n", assign->op1->id);
			
		ttype = getToken();
		printf("PLUS: %d\n",ttype);
		if ((ttype == PLUS) | (ttype == MINUS) | (ttype == MULT) | (ttype == DIV))
		{
			assign->oper = ttype;
				
			ttype = getToken();
			if(ttype == ID) 
				
			{
	printf("ID: %d\n",ttype);				

				assign->op2 = primary(assign->op2);
				return assign;									
			} 
				
			else
				
			{
					
				ungetToken();
				
			}
		
		} 
			
		else 
			
		{
				
			return NULL;
			
		}
*/		
	
	 }
	 
	 ttype = getToken();
	 if ((ttype == PLUS) | (ttype == MINUS) | (ttype == MULT) | (ttype == DIV))
	 {
		assign->oper = ttype;
	 }
		
	ttype = getToken();
	if (ttype == NUM)
	{	
		//printf("NUM: %d\n",ttype);
		op = make_var();
		
		if(symSearch(token) == NULL)
		{
			symAdd(op);
		}
		
		assign->op1 = symSearch(token);
		
		/*assign->op1 = primary(assign->op1);

		ttype = getToken();
		if ((ttype == PLUS) | (ttype == MINUS) | (ttype == MULT) | (ttype == DIV))
		{	
			assign->oper = ttype; //op is set + - * /
			assign->op2 = primary(assign->op2);
		}
		
		ungetToken();
		return assign;*/
	} 
	else if (ttype == ID)
	{
		
		assign->op2 = symSearch(token);
		
		/*printf("ID: %d\n",ttype);

		assign->op1 = primary(assign->op1);
		printf("ASSIGN ID %s\n", assign->op1->id);
			
		ttype = getToken();
		printf("PLUS: %d\n",ttype);
		if ((ttype == PLUS) | (ttype == MINUS) | (ttype == MULT) | (ttype == DIV))
		{
			assign->oper = ttype;
				
			ttype = getToken();
			if(ttype == ID) 
				
			{
	printf("ID: %d\n",ttype);				

				assign->op2 = primary(assign->op2);
				return assign;									
			} 
				
			else
				
			{
					
				ungetToken();
				
			}
		
		} 
			
		else 
			
		{
				
			return NULL;
			
		}
*/		
	
	 }
	 return assign;
}