Пример #1
0
struct D_t *parse_D(){
	struct D_t *d = malloc(sizeof(struct D_t));
	int m = nexttoken();
	if(m == M_DEF){
		eat_token();
		d->kind = D_DEF;
		m = nexttoken();
		if(m == M_ID){
			eat_token();
			strcpy(d->str,yytext);
			d->e = parse_E();
			m = nexttoken();
			if(m == M_SEMIC){
				eat_token();
			}else{
				syntax_error();
			}
		}else{
			syntax_error();
		}
	}else{
		syntax_error();
	}
	return d;
}
Пример #2
0
void parse(char *e)
{
	str = e;
	i = 0;
	parse_E();
	if (str[i] == '\0')
		return;
	error("\'+\' or '\\0\'", str[i]);
	return;
}
Пример #3
0
void parse_F()
{
	char c = str[i];
	if (isdigit(c)){
		i++;
		return;
	}
	if (c == '('){
		i++;
		parse_E();
		c = str[i];
		if (c == ')'){
			i++;
			return;
		}
		error("\')\'", c);
		return;
	}
	error("\'0-9\' or \'(\'", c);
	return;
}
Пример #4
0
struct E_t *parse_E(){
	struct E_t *e = malloc(sizeof(struct E_t));
	int m = nexttoken();
	if(m == M_ID){
		eat_token();		//IDは読み終わった
		e->kind = E_ID;
		strcpy(e->str,yytext);	
	}else if(m == M_NUM){	//NUMの場合
		eat_token();
		e->kind = E_NUM;
		strcpy(e->str,yytext);
	}else if(m == M_ADD){
		eat_token();
		e->kind = E_ADD;
		e->e_left = parse_E();
		e->e_right = parse_E();
	}else if(m == M_SUB){
		eat_token();
		e->kind = E_SUB;
		e->e_left = parse_E();
		e->e_right = parse_E();
	}else if(m == M_EQ){
		eat_token();
		e->kind = E_EQ;
		e->e_left = parse_E();
		e->e_right = parse_E();
	}else if(m == M_LESS){
		eat_token();
		e->kind = E_LESS;
		e->e_left = parse_E();
		e->e_right = parse_E();
	}else{
		syntax_error();
	}
	return e;
}
Пример #5
0
struct C_t *parse_C(){
	struct C_t *c = malloc(sizeof(struct C_t));
	int m = nexttoken();
	if(m == M_PRINT){
		eat_token();
		c->kind = C_PRINT;
		c->e = parse_E();
		m = nexttoken();
		if(m == M_SEMIC){
			eat_token();
		}else{
			syntax_error();
		}
	}else if(m == M_READ){
		eat_token();
		c->kind = C_READ;
		m = nexttoken();
		if(m == M_ID){
			eat_token();
			strcpy(c->str,yytext);
			m = nexttoken();
			if(m == M_SEMIC){
				eat_token();
			}else{
				syntax_error();
			}
		}else{
			syntax_error();
		}
	}else if(m == M_SET){
		eat_token();
		c->kind = C_SET;
		m = nexttoken();
		if(m == M_ID){
			eat_token();
			strcpy(c->str,yytext);
			c->e = parse_E();
			m = nexttoken();
			if(m == M_SEMIC){
				eat_token();
			}else{
				syntax_error();
			}
		}else{
			syntax_error();
		}
	}else if(m == M_IF){
		eat_token();
		c->kind = C_IF;
		c->e = parse_E();
		m = nexttoken();
		if(m == M_THEN){
			eat_token();
			c->b_left = parse_B();
			m = nexttoken();
			if(m == M_ELSE){
				eat_token();
				c->b_right = parse_B();
			}else{
				syntax_error();
			}
		}else{
			syntax_error();
		}
	}else if(m == M_WHILE){
		eat_token();
		c->kind = C_WHILE;
		c->e = parse_E();
		m = nexttoken();
		if(m == M_DO){
			eat_token();
			c->b = parse_B();
		}else{
			syntax_error();
		}
	}else{
		syntax_error();
	}
	return c;
}