Example #1
0
string parser_t::Jump(string s)
{
    token_type watch = scanner.next_token();
    parsetree.push(NT_Jump);
    if(watch == T_goto){
        eat_token(T_goto);
        s = Forward(s);
    }else{
        syntax_error(NT_Jump);
    }
    watch = scanner.next_token();
    switch(watch){
        case T_eof:
        case T_label:
        case T_goto:
        case T_m:
        case T_print:
            break;
        default:
            syntax_error(NT_Jump);
            break;
    }
    parsetree.pop();
    return s;
}
Example #2
0
string parser_t::Assignment(string s)
{
    token_type watch = scanner.next_token();
    parsetree.push(NT_Assignment);
    if(watch == T_m){
        s.append("m");
        eat_token(T_m);
        s.append("[");
        eat_token(T_opensquare);
        s = Expression(s);
        s.append("]");
        eat_token(T_closesquare);
        s.append("=");
        eat_token(T_equals);
        s = Expression(s);
    }else{
        syntax_error(NT_Print);
    }
    watch = scanner.next_token();
    switch(watch){
        case T_eof:
        case T_label:
        case T_goto:
        case T_m:
        case T_print:
            break;
        default:
            syntax_error(NT_Print);
            break;
    }
    parsetree.pop();
    return s;
}
Example #3
0
string parser_t::Label(string s)
{
    token_type watch = scanner.next_token();
    parsetree.push(NT_Label);
    if(watch == T_label){
        s.append("L");
        eat_token(T_label);
        eat_token(T_num);
        char buf[100];
        snprintf(buf, 100, "%d", scanner.nextNumber);
        s.append(buf);
        s.append(":");
        eat_token(T_colon);
    }else{
        syntax_error(NT_Label);
    }
    watch = scanner.next_token();
    switch(watch){
        case T_eof:
        case T_label:
        case T_goto:
        case T_m:
        case T_print:
            break;
        default:
            syntax_error(NT_Label);
            break;
    }
    parsetree.pop();
    return s;
}
Example #4
0
void parser_t::Derivestate(){
  parsetree.push(NT_Derivestate);
  switch(scanner.next_token())
    {
    case T_label:
      Statement();
      Derivestate();
      break;
    case T_goto:
      Statement();
      Derivestate();
      break;
    case T_m:
      Statement();
      Derivestate();
      break;
    case T_print:
      Statement();
      Derivestate();
      break;
    case T_eof:
       eat_token(scanner.next_token());
       // parsetree.drawepsilon();
      //   cout<<"done"<<endl;
      break;
    default:
      syntax_error(NT_Derivestate);
	break;
    }
  parsetree.pop();


}
Example #5
0
void parser_t::FactorP(){
	int temp = scanner.get_num();
	parsetree.push(NT_FactorP);
	switch(scanner.next_token()){
		case T_power:

			eat_token(T_power);
			Exp();

			FactorP();
			break;

		case T_eof:
			parsetree.drawepsilon();
			break;

		default:
			parsetree.drawepsilon();
			// syntax_error(NT_ExpressionP);
			break;

	}
	parsetree.pop();

}
Example #6
0
void parser_t::Assignment()
{
  parsetree.push(NT_Assignment);
  if(scanner.next_token() == T_m)
    {
      eat_token(T_m);
    }
  else{syntax_error(NT_Assignment);}
  if(scanner.next_token()==T_opensquare)
    {
      eat_token(T_opensquare);
    }
  else{syntax_error(NT_Assignment);}
  Expression();
if(scanner.next_token() == T_closesquare)
  {
    eat_token(T_closesquare);
  }
 else{syntax_error(NT_Assignment);}
if(scanner.next_token() == T_equals)
  {
    eat_token(T_equals);
  }
 else{syntax_error(NT_Assignment);}
 Expression();
 parsetree.pop();

}
Example #7
0
void parser_t::Factor(){
  token_type token;
  parsetree.push(NT_Factor);
  code_printf("intPow(");
  switch(token = scanner.next_token()){
  case T_openparen:
    eat_token(T_openparen);
    code_printf("(");
    Expression();
    code_printf(")");
    eat_token(T_closeparen);
    break;
  case T_m:
    code_printf("m[");
    eat_token(T_m);
    eat_token(T_opensquare);
    Expression();
    eat_token(T_closesquare);
    code_printf("]");
    break;
  case T_num:
    code_printf("%d",scanner.get_num());
    eat_token(T_num);
    break;
  default:
    syntax_error(NT_Factor);
    /*
    fprintf(stderr, "%d Invalid factor: %s\n", scanner.get_line(), token_to_string(token));
    assert(0);
    */
  }
  Power();
  parsetree.pop();
}
Example #8
0
void parser_t::Jump(){
	int temp;
	parsetree.push(NT_Jump);
	eat_token(T_goto);
	//cerr<<"goto ";
	scanner.next_token();
	temp = 	scanner.get_num();
	eat_token(T_num);

	scanner.next_token();
	if(scanner.next_token()== T_if){
		eat_token(T_if);
	//	cerr<<"if( ";
		//scanner.next_token();
		Expression();
		cerr<<" );\n";
		cerr<<"goto L";
		cerr<<temp;
		cerr<<"; \n";
		}
	else{
		cerr<<"goto L";
		cerr<<temp;
		cerr<<"; \n";
		}
	parsetree.pop();
}
Example #9
0
void parser_t::Assignment(){

	parsetree.push(NT_Assignment);

	switch( scanner.next_token() ) 
	{
		case T_opensquare:
			cerr<<"m";
			eat_token(T_opensquare);
			cerr<<"[ ";
			Expression();
			scanner.next_token();
			eat_token(T_closesquare);
			cerr<<" ] ";
			scanner.next_token();
			eat_token(T_equals);
			cerr<<" = ";
			Expression();
			cerr<<"; \n";
			scanner.next_token();
			break;

		// case T_eof:
		// 	parsetree.drawepsilon();
		// 	break;
		default:
			syntax_error(NT_Assignment);
			break;
	}

	//now that we are done with List, we can pop it from the data
	//stucture that is tracking it for drawing the parse tree
	parsetree.pop();

}
Example #10
0
void parser_t::Jump()
{
	//push this non-terminal onto the parse tree.
	//the parsetree class is just for drawing the finished
	//parse tree, and should in should have no effect the actual
	//parsing of the data
	parsetree.push(NT_Jump);

	if(scanner.next_token() == T_goto)
	{
		eat_token(T_goto);
		if(scanner.next_token() == T_num)
		{
			eat_token(T_num);
			int labelNum = scanner.numberList[numberListSpot];
			numberListSpot++;
			JumpP(labelNum);
		}
		else
			syntax_error(NT_Jump);

	}
	else
		syntax_error(NT_Jump);

	//now that we are done with List, we can pop it from the data
	//stucture that is tracking it for drawing the parse tree
	parsetree.pop();
}
Example #11
0
void parser_t::Statement()
{
    string s;
    token_type watch = scanner.next_token();
    parsetree.push(NT_Statement);
    if(watch == T_label){
        s = Label(s);
    }else if(watch == T_goto){
        s = Jump(s);
    }else if(watch == T_m){
        s = Assignment(s);
    }else if(watch == T_print){
        s = Print(s);
    }else{
        syntax_error(NT_Statement);
    }
    cerr<<s<<";"<<endl;
    watch = scanner.next_token();
    switch(watch){
        case T_eof:
        case T_label:
        case T_goto:
        case T_m:
        case T_print:
            break;
        default:
            syntax_error(NT_Statement);
            break;
    }
    parsetree.pop();
}
Example #12
0
void parser_t::numend(string& expString)
{
	parsetree.push(NT_NumEnd);
	switch(scanner.next_token())
	{
	case T_openparen:
		//cout <<"here";
		eat_token(T_openparen);
		expString += "(";
		//cout<< "ga";
		expString += expression();
		//cout <<"wtfffff";
		eat_token(T_closeparen);
		expString += ")";
		break;
	case T_m:
		eat_token(T_m);
		eat_token(T_opensquare);
		expString += "m[";
		expString += expression();
		eat_token(T_closesquare);
		expString += "]";
		break;
	case T_num:
		//cout <<"num found";
		eat_token(T_num);
		expString += scanner.numString();
		break;
	default:
			syntax_error(NT_NumEnd);
			break;
	}
	parsetree.pop();
}
Example #13
0
void parser_t::Factor()
{
	//push this non-terminal onto the parse tree.
	//the parsetree class is just for drawing the finished
	//parse tree, and should in should have no effect the actual
	//parsing of the data
	parsetree.push(NT_Factor);

	switch( scanner.next_token() ) 
	{
		case T_num:
			eat_token(T_num);
			if(scanner.next_token()==T_power)
			{
				powBuffer=scanner.numberList[numberListSpot];
			}
			else
				cerr << scanner.numberList[numberListSpot];
			numberListSpot++;
			break;
		case T_m:
			eat_token(T_m);
			cerr << "m";
			if (scanner.next_token() == T_opensquare)
			{
				eat_token(T_opensquare);
				cerr << "[";
				Expression();
				if(scanner.next_token() == T_closesquare)
				{
					eat_token(T_closesquare);
					cerr << "]";
				}
				else
					syntax_error(NT_Factor);
			}
			else
				syntax_error(NT_Factor);
			break;
		case T_openparen:
			eat_token(T_openparen);
			cerr << "(";
			Expression();
			if (scanner.next_token() == T_closeparen)
			{
				eat_token(T_closeparen);
				cerr << ")";
			}
			else
				syntax_error(NT_Factor);
			break;
		default:
			syntax_error(NT_Factor);
			break;
	}

	//now that we are done with List, we can pop it from the data
	//stucture that is tracking it for drawing the parse tree
	parsetree.pop();
}
Example #14
0
void parser_t::Factor()
{
	parsetree.push(NT_Factor);

	if(scanner.next_token() == T_num) 
	{
		current = current + scanner.get_token_value();
		// printf("%s\n", current.c_str());
		eat_token(T_num);
	}
	else if(scanner.next_token() == T_m) 
	{
		eat_token(T_m);
		eat_token(T_opensquare);
		current = current + "m[";
		Expression();

		eat_token(T_closesquare);
		current = current + "]";
	}
	else { syntax_error(NT_Factor); }

	parsetree.pop();

}
Example #15
0
void parser_t::Jump()
{
	parsetree.push(NT_Jump);

	if(scanner.next_token() == T_goto)
	{
		eat_token(T_goto);
		scanner.next_token();
		std::string tmp =  scanner.get_token_value();
		eat_token(T_num);

		//If there's an if statement, handle it it.
		if(scanner.next_token() == T_if) {
			eat_token(T_if);
			current = current + "if(";
			Expression();
			current = current + " != 0) { ";
			current = current + "goto L" + tmp + "; }\n ";
		}
		else {
			current = current + " goto L" + tmp + ";\n ";
		}
	}
	else { syntax_error(NT_Jump); }

	parsetree.pop();
}
Example #16
0
void parser_t::Label() 
{
	parsetree.push(NT_Label);

	if(scanner.next_token() == T_label) 
	{
		eat_token(T_label);
		current = c_code_main;
		scanner.next_token();
		current = current + "\n L" + scanner.get_token_value() + ":\n ";
				// printf("current:\n%s\n", current.c_str());

		eat_token(T_num);
		eat_token(T_colon);
		LabelStatements();
		// printf("current:\n%s\n", current.c_str());

   
		c_code_main = current;
		current = c_code_main;
		printf("%s\n", c_code_main.c_str());

	}
	else { syntax_error(NT_Label); }
	parsetree.pop();
}
Example #17
0
	bool parse(scanner_t& scanner, skip_t& skip_p) const
	{
		scanner.save();
		if (!stub.parse(scanner, skip_p))
			scanner.rollback();

		return true;
	}
Example #18
0
//call this syntax error when you are trying to parse the
//non-terminal nt, but you fail to find a token that you need
//to make progress.  You should call this as soon as you can know
//there is a syntax_error. 
void parser_t::syntax_error(nonterm_type nt)
{
	printf("syntax error: found %s in parsing %s - line %d\n",
		token_to_string( scanner.next_token()),
		nonterm_to_string(nt),
		scanner.get_line() ); 
	exit(1); 
}
Example #19
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();
}
Example #20
0
	bool parse(scanner_t& scanner, skip_t& skip_p) const
	{
		scanner.save();
		if (lhs.parse(scanner, skip_p) ||
			rhs.parse(scanner, skip_p))
		{
			scanner.commit();
			return true;
		}
		scanner.rollback();
		return false;
	}
Example #21
0
	bool parse(scanner_t& scanner, skip_t&) const
	{
		for (int readChar = scanner.readAhead();
			 readChar != -1; readChar = scanner.readAhead())
		{
			if (!isSkipCharacter(readChar))
				break;

			scanner.skip(1);
		}

		return true;
	}
Example #22
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();
}
Example #23
0
void parser_t::Jump(){
  parsetree.push(NT_Jump);
  if(scanner.next_token()==T_goto)
    eat_token(T_goto);
else
  syntax_error(NT_Jump);
  if(scanner.next_token()==T_num){
    eat_token(T_num);
    Secondjump();
  }
  else
    syntax_error(NT_Jump); 
  parsetree.pop();
}
Example #24
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();
}
Example #25
0
	bool parse(scanner_t& scanner, skip_t& ) const
	{
		scanner.save();
		const int value = scanner.read();
		if (value != -1 &&
			traits_type::to_char_type(value) == character)
		{
			scanner.commit();
			return true;
		}
		
		scanner.rollback();
		return false;
	}
Example #26
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();
}
Example #27
0
void parser_t::TermP(){

	parsetree.push(NT_TermP);

	switch( scanner.next_token() ) 
	{
		case T_times:
			eat_token(T_times);
			cerr<<" * ";
			Factor();
			TermP();
			break;

		case T_divide:
			eat_token(T_divide);
			cerr<<" / ";
			Factor();
			TermP();
			break;

		case T_eof:
			parsetree.drawepsilon();
			break;

		default:
			parsetree.drawepsilon();
			// syntax_error(NT_ExpressionP);
			break;
	}

	//now that we are done with List, we can pop it from the data
	//stucture that is tracking it for drawing the parse tree
	parsetree.pop();

}
Example #28
0
void parser_t::ExpressionP(){

	parsetree.push(NT_ExpressionP);

	switch( scanner.next_token() ) 
	{
		case T_plus:
			eat_token(T_plus);
			cerr<<" + ";
			Term();
			ExpressionP();
			break;

		case T_minus:
			eat_token(T_minus);
			cerr<< " - ";
			Term();
			ExpressionP();
			break;

		case T_eof:
			parsetree.drawepsilon();
			break;

		default:
			parsetree.drawepsilon();
			// syntax_error(NT_ExpressionP);
			break;
	}

	//now that we are done with List, we can pop it from the data
	//stucture that is tracking it for drawing the parse tree
	parsetree.pop();

}
Example #29
0
void parser_t::MoreStatement(){
	parsetree.push(NT_MoreStatement);

	switch(scanner.next_token()){

		case T_label:
		case T_m:
		case T_goto:
		case T_print: 
			Statement();
			MoreStatement();
			break;


		case T_eof:
			parsetree.drawepsilon();
			break;

		default:
			syntax_error(NT_MoreStatement);
			break;

	}

	parsetree.pop();

}
Example #30
0
	bool parse(scanner_t& scanner, skip_t& skip_p) const
	{
		scanner.save();

		const int input = scanner.read();
		if (input != -1 &&
			traits_type::to_char_type(input) >= small &&
			traits_type::to_char_type(input) <= large)
		{
			scanner.commit();
			return true;
		}

		scanner.rollback();
		return false;
	}