Ejemplo n.º 1
0
/*
parse_statement

Checks the current token to be able to parse the correct statement

Parameters: none

Return: none
*/
void parse_statement(Tree &cst){
	cst.add_branch_node("statement");
	if(!(curr_token -> desc).compare("print")){
		parse_print(cst);
	}
	else if(!(curr_token -> type).compare("identifier")){
		parse_assingment(cst);
	}
	else if(types.find(curr_token -> desc) != types.end()){
		parse_var_decl(cst);
	}
	else if(!(curr_token -> desc).compare("while")){
		parse_while(cst);
	}
	else if(!(curr_token -> desc).compare("if")){
		parse_if(cst);
	}
	else if(!(curr_token -> type).compare("open_block")){
		parse_block(cst);
	}
	else{
		std::cout << "Parse Error on line " << curr_token -> line_number
			<< ".\nExpected statement, found " << curr_token -> desc
			<< "\nStatements begin with:\n\tprint\n\tidentifier -- [a-z]\n\t"
			<< "int, string, boolean\n\twhile\n\tif\n\t{"
			<< "\n\nCompilation unsuccessful" << std::endl;
		exit(EXIT_FAILURE);
	}
	cst.kill_all_children();
}
Ejemplo n.º 2
0
stmt_t parse_comp_stmt(tokenizer_t t) {
    char * filename = cur_tok(t).filename;
    int line = cur_tok(t).line_num;
    eat_it(t, TOK_LBRACE);
    stmt_list_t c = mk_stmt_list();
    var_decl_list_t d = parse_var_decl(t);
    stmt_t x;
    while(cur_tok(t).kind != TOK_RBRACE) {
        x = parse_stmt(t);
        stmt_list_add(c, x);
    }
    eat_it(t, TOK_RBRACE);
    return mk_stmt_compound(filename, line, d, c);
}