Esempio n. 1
0
Attribute* binary_operator( Attribute* attr1, Attribute* attr2, int op, int reverse, int rm1, int rm2, int lno) {
    if(attr1==NULL || attr2==NULL)
		die(lno, "NULL pointer for attr1 or attr2\n");
    if(reverse) {
        Attribute* tmp = attr1;
        attr1 = attr2;
        attr2 = tmp;
    }
    Attribute* result;
    switch (op) {
        case OP_ADD:
        case OP_SUB:
        case OP_MUL:
        case OP_DIV:
            result = math_operator(attr1, attr2, op, lno);break;
        case OP_GT:
        case OP_LT:
        case OP_GE:
        case OP_LE:
            result = relational_operator(attr1, attr2, op, lno);break;
        case OP_AND:
        case OP_OR:
            result = logic_operator(attr1, attr2, op, lno); break;
        case OP_EQ:
        case OP_NE:
            result = equal_operator(attr1, attr2, op, lno); break;
        default:
            die(lno, "binary_opertor: unsupported OP %d.\n", op);
    }
    if(rm1==FLAG_DESTROY_ATTR) destroy_attr(attr1);
    if(rm2==FLAG_DESTROY_ATTR) destroy_attr(attr2);

    return result;
}
Esempio n. 2
0
//main function
int main()
{
	initialize_Symbol_Table();
	
	remove("a4_1.out");
	
	char input[1000];
	
	token t;
	int i;
	
	//loop for rading lines of source code into input buffer	
	while(gets(input))
	{
		lexemeBegin = 0;
		
		while(input[lexemeBegin]!='\0'&&input[lexemeBegin]!='\n' &&input[lexemeBegin]!='\r')  //while end of line is not found
		{
			while(input[lexemeBegin]==' ' || input[lexemeBegin] == '\t')
				lexemeBegin++;
						
			//checking for keywords and identifiers
			t = identifier_keyword(input);
			if(t.tokenID!=NOTOK)
			{
				write_token(t);
				continue;
			}
			
			//checking for integer constants
			t = integer_constant(input);
			if(t.tokenID!=NOTOK)
			{
				write_token(t);
				continue;
			}
			
			//checking for floating point number constants
			t = float_constant(input);
			if(t.tokenID!=NOTOK)
			{
				write_token(t);
				continue;
			}

			//checking for arithmetic operators
			t = arithmetic_operator(input);
			if(t.tokenID!=NOTOK)
			{
				write_token(t);
				continue;
			}

			//checking for assignment operators
			t = assignment_operator(input);
			if(t.tokenID!=NOTOK)
			{
				write_token(t);
				continue;
			}

			//checking for relational operators
			t = relational_operator(input);
			if(t.tokenID!=NOTOK)
			{
				write_token(t);
				continue;
			}

			//checking for special symbols
			t = special_symbol(input);
			if(t.tokenID!=NOTOK)
			{
				write_token(t);
				continue;
			}
			
			//checking for comments
			comment(input);
			//checking erroneous lexeme
			error_condition(input);
		}
	}	
}