Beispiel #1
0
void parser_t::Label() {
	parsetree.push(NT_Label);

	scanner.eat_token(T_label);
	scanner.eat_token(T_num);
	scanner.eat_token(T_colon);
	cerr_buffer.push_back("L" + scanner.get_number() + ":\n");

	parsetree.pop();
}
Beispiel #2
0
void parser_t::Assignment() {
	parsetree.push(NT_Assignment);

	scanner.eat_token(T_m);
	scanner.eat_token(T_opensquare);
	cerr_buffer.push_back("m[");
	Expression();
	scanner.eat_token(T_closesquare);
	scanner.eat_token(T_equals);
	cerr_buffer.push_back("]=");
	Expression();
	cerr_buffer.push_back(";\n");

	parsetree.pop();
}
Beispiel #3
0
void parser_t::Print() {
	parsetree.push(NT_Print);

	scanner.eat_token(T_print);
	cerr_buffer.push_back("printf(\"%d\",");
	Expression();
	cerr_buffer.push_back(");\n");

	parsetree.pop();
}
Beispiel #4
0
void parser_t::Jump() {
	parsetree.push(NT_Jump);

	scanner.eat_token(T_goto);
	scanner.eat_token(T_num);
	cerr_buffer.push_back("goto L" + scanner.get_number() + ";\n");
	if (scanner.next_token() == T_if) {
		scanner.eat_token(T_if);
		// Insert if statement before goto
		string goto_str = cerr_buffer.back();
		cerr_buffer.pop_back();
		cerr_buffer.push_back("if (");
		Expression();
		cerr_buffer.push_back(") {\n");
		cerr_buffer.push_back(goto_str);
		cerr_buffer.push_back("}\n");
	}

	parsetree.pop();
}
Beispiel #5
0
void parser_t::BracketTerm() {
	parsetree.push(NT_BracketTerm);

	switch (scanner.next_token()) {
		case T_openparen:
			scanner.eat_token(T_openparen);
			cerr_buffer.push_back("(");
			Expression();
			scanner.eat_token(T_closeparen);
			cerr_buffer.push_back(")");
			break;
		case T_m:
			scanner.eat_token(T_m);
			scanner.eat_token(T_opensquare);
			cerr_buffer.push_back("m[");
			Expression();
			scanner.eat_token(T_closesquare);
			cerr_buffer.push_back("]");
			break;
		case T_num:
			scanner.eat_token(T_num);
			cerr_buffer.push_back(scanner.get_number());
			break;
		default:
			syntax_error(NT_BracketTerm);
	}

	parsetree.pop();
}
Beispiel #6
0
//WRITEME: you will need to put the rest of the procedures here
void parser_t::Statements() {
	parsetree.push(NT_Statements);

	Statement();
	if (scanner.next_token() != T_eof) {
		Statements();
	} else {
		scanner.eat_token(T_eof);
		parsetree.drawepsilon();
	}

	parsetree.pop();
}
Beispiel #7
0
void parser_t::TermRight() {
	parsetree.push(NT_TermRight);

	switch (scanner.next_token()) {
		case T_times:
			scanner.eat_token(T_times);
			cerr_buffer.push_back("*");
			ExponentialTerm();
			TermRight();
			break;
		case T_divide:
			scanner.eat_token(T_divide);
			cerr_buffer.push_back("/");
			ExponentialTerm();
			TermRight();
			break;
		default:
			parsetree.drawepsilon();
			break;
	}

	parsetree.pop();
}
Beispiel #8
0
void parser_t::ExpressionRight() {
	parsetree.push(NT_ExpressionRight);

	switch (scanner.next_token()) {
		case T_plus:
			scanner.eat_token(T_plus);
			cerr_buffer.push_back("+");
			Term();
			ExpressionRight();
			break;
		case T_minus:
			scanner.eat_token(T_minus);
			cerr_buffer.push_back("-");
			Term();
			ExpressionRight();
			break;
		default:
			parsetree.drawepsilon();
			break;
	}

	parsetree.pop();
}
Beispiel #9
0
void parser_t::ExponentialTerm() {
	parsetree.push(NT_ExponentialTerm);

	cerr_buffer.push_back("pow(");
	BracketTerm();
	if (scanner.next_token() == T_power) {
		scanner.eat_token(T_power);
		cerr_buffer.push_back(",");
		ExponentialTerm();
		cerr_buffer.push_back(")");
	} else {
		for (vector<string>::reverse_iterator itr = cerr_buffer.rbegin(); itr != cerr_buffer.rend(); itr++) {
			if (*itr == "pow(") {
				cerr_buffer.erase(--(itr.base()));
				break;
			}
		}
	}

	parsetree.pop();
}
Beispiel #10
0
//this function not only eats the token (moving the scanner forward one
//token), it also makes sure that token is drawn in the parse tree 
//properly by calling push and pop.
void parser_t::eat_token(token_type t)
{
	parsetree.push(t);
	scanner.eat_token(t);
	parsetree.pop();
}