Example #1
0
void parser_t::Statement(){
  parsetree.push(NT_Statement);
switch(scanner.next_token())
  {
  case T_eof:
    parsetree.drawepsilon();
    break;
  case T_m:
    cerr<<"m";
    Assignment();
    break;
  case T_label:
    cerr<<"L";
    Label();
    break;
  case T_goto:
    cerr<<"goto";
    Jump();
    break;
  case T_print:
    cerr<<"printf()";
    Print();
    break;
  default:
    syntax_error(NT_Statement);
    break;
  }
 parsetree.pop();
}
Example #2
0
void parser_t::jumpImouto()
{
	parsetree.push(NT_JumpImouto);
	switch(scanner.next_token())
	{
	case T_if:
		eat_token(T_if);
		outSave += "if(";
		outSave += expression();
		outSave += ")";
		break;
	case T_eof:
		parsetree.drawepsilon();
		break;
	case T_label:
	case T_goto:
	case T_m:
	case T_print:
		break;
	default:
		syntax_error(NT_JumpImouto);
		break;
	}
	parsetree.pop();
}
Example #3
0
//Here is an example
void parser_t::Start()
{
  //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_Start);

  switch( scanner.next_token() ) 
    {
    case T_plus:
      eat_token(T_plus);
      Start();
      break;
    case T_eof:
      parsetree.drawepsilon();
      break;
    default:
      syntax_error(NT_Start);
      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 #4
0
//Here is an example
void parser_t::Start()
{
	//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_Start);

	fprintf(stderr, "int main()\n");
	fprintf(stderr, "{\n");
	fprintf(stderr, "\tint m[101];\n");
	fprintf(stderr, "\n");


	switch( scanner.next_token() )
	{
		case T_label:
		case T_goto:
		case T_m:
		case T_print:
			Statements();
			break;
		case T_eof:
			parsetree.drawepsilon();
			break;
		default:
			syntax_error(NT_Start);
			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();
	fprintf(stderr, "}\n");
}
Example #5
0
void parser_t::LabelStatements()
{
	parsetree.push(NT_LabelStatements);

	switch(scanner.next_token())
	{
		case T_m:
		case T_print:
			LabelStatement();
			LabelStatements();
			break;
		case T_goto:
		case T_label:
		case T_eof:
			parsetree.drawepsilon();
			break;
		default:
			syntax_error(NT_LabelStatement);
			break;
	}


	parsetree.pop();

}
Example #6
0
void parser_t::Statements()
{
  parsetree.push(NT_Statements);
  if(scanner.next_token() != T_eof){
    Statement();
    Statements();
  }
  else
    parsetree.drawepsilon();
  parsetree.pop();
}
Example #7
0
void parser_t::Expression1(){
  parsetree.push(NT_Expression1);

//WRITEME: you will need to put the rest of the procedures here

  switch(scanner.next_token()){
  case T_eof:
    parsetree.drawepsilon();
    //eat_token(T_eof);
    break;
  case T_plus:
    //cerr<<"+";
    eat_token(T_plus);
    T();
    Expression1();
    break;
  case T_minus:
    //cerr<<"-";
     eat_token(T_minus);
     T();
     Expression1();
     break;
  case T_label:
    parsetree.drawepsilon();
    break;
  case T_goto:
    parsetree.drawepsilon();
    break;
  case T_if:
    parsetree.drawepsilon();
    break;
  case T_m:
    parsetree.drawepsilon();
    break;
  case T_print:
    parsetree.drawepsilon();
    break;
  case T_closesquare:
    parsetree.drawepsilon();
    break;
  case T_closeparen:
    parsetree.drawepsilon();
    break;
  default:
    syntax_error(NT_Expression1);
    break;

  }
  parsetree.pop();
}
Example #8
0
void parser_t::furuImouto(string& expString)
{
	parsetree.push(NT_MultDivImouto);
	switch(scanner.next_token())
	{
	case T_times:
		eat_token(T_times);
		expString += "*";
		exp(expString);
		furuImouto(expString);
		break;
	case T_divide:
		eat_token(T_divide);
		expString += "/";
		exp(expString);
		furuImouto(expString);
		break;
	case T_eof:
			parsetree.drawepsilon();
			break;
	case T_plus:
	case T_minus:
	case T_closeparen:
	case T_closesquare:
	case T_label:
	case T_goto:
	case T_m:
	case T_print:
		parsetree.drawepsilon();
		break;
	
	default:
		syntax_error(NT_MultDivImouto);
		break;
	}

	parsetree.pop();
}
Example #9
0
//WRITEME: you will need to put the rest of the procedures here
void parser_t::Statements()
{
    token_type watch = scanner.next_token();
    parsetree.push(NT_Statements);
    if((watch == T_label) || (watch == T_goto) || (watch == T_m)||(watch == T_print)){
        Statement();
        Statements();
    }else if(watch == T_eof){
        parsetree.drawepsilon();
    }else{
        syntax_error(NT_Statements);
    }
    parsetree.pop();
}
Example #10
0
void parser_t::StatementsPrime()
{
  parsetree.push(NT_StatementsPrime);
  switch(scanner.next_token())
    {
    case T_eof:
      parsetree.drawepsilon();
      break;
    default:
      Statement();
      StatementsPrime();
    }
  parsetree.pop();
}
Example #11
0
void parser_t::ExpressionP()
{
	//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_ExpressionPrime);

	switch( scanner.next_token() ) 
	{
		case T_label:
			parsetree.drawepsilon();
			break;
		case T_goto:
			parsetree.drawepsilon();
			break;
		case T_print:
			parsetree.drawepsilon();
			break;
		case T_m:
			parsetree.drawepsilon();
			break;
		case T_closeparen:
			parsetree.drawepsilon();
			break;
		case T_closesquare:
			parsetree.drawepsilon();
			break;
		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:
			syntax_error(NT_ExpressionPrime);
			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 #12
0
void parser_t::JumpIf(){
  parsetree.push(NT_JumpIf);
  if(scanner.next_token() == T_if){
    code_printf("if(");
    eat_token(T_if);
    Expression();
    code_printf(")\n\t\t");
  }
  else
    parsetree.drawepsilon();
  

  parsetree.pop();

}
Example #13
0
void parser_t::Power(){
  parsetree.push(NT_Power);
  switch(scanner.next_token()){
  case T_power:
    code_printf(", ");
    eat_token(T_power);
    Factor();
    code_printf(")");

    break;
  default:
    code_printf(", 1)");
    parsetree.drawepsilon();
  }
  parsetree.pop();

}
Example #14
0
void parser_t::Statement()
{
	//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_Statement);
	switch( scanner.next_token() ) 
	{
		case T_m:
			eat_token(T_m);
			Assignment();
			break;

		case T_label:
			Label();
			//eat_token(T_label);

			scanner.next_token();
			break;

		case T_goto:
			//eat_token(T_goto);
			Jump();
			scanner.next_token();
			break;

//COME BACK TO FIX WHAT PRINT prints out ~!~!~!~!~!~!~!~!~!~!~!~!~!~~!!~~!!~!
		case T_print:
			//eat_token(T_print);
			Print();
			scanner.next_token();
			break;
		case T_eof:
			parsetree.drawepsilon();
			break;
		default:
			syntax_error(NT_Statement);
			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
	//scanner.next_token();
	parsetree.pop();
}
Example #15
0
void parser_t::RTerm(){
  parsetree.push(NT_RTerm);
  switch(scanner.next_token()){
  case T_times:
    code_printf(" * ");
    eat_token(T_times);
    Term();
    break;
  case T_divide:
    code_printf(" / ");
    eat_token(T_divide);
    Term();
    break;
  default:
    parsetree.drawepsilon();  
  }
  parsetree.pop();
}
Example #16
0
void parser_t::RExpression(){
  parsetree.push(NT_RExpression);
  switch(scanner.next_token()){
  case T_plus:
    code_printf(" + ");
    eat_token(T_plus);
    Expression();
    break;
  case T_minus:
    code_printf(" - ");
    eat_token(T_minus);
    Expression();
    break;
  default:
    parsetree.drawepsilon();
  }
  parsetree.pop();    
}
Example #17
0
void parser_t::Q(){
  parsetree.push(NT_Q);
  switch(scanner.next_token()){
  case T_openparen:
    // cerr<<"(";
    eat_token(T_openparen);
    Expression();
    if(scanner.next_token()==T_closeparen){
      //cerr<<")";
      eat_token(T_closeparen);
    }
    else
      syntax_error(NT_Q);
    break;
  case T_m:
    //cerr<<"m";
    eat_token(T_m);
    if(scanner.next_token()==T_opensquare)
      {//cerr<<"[";
      eat_token(T_opensquare);
      Expression();
    }
    else
      syntax_error(NT_Q);
  if(scanner.next_token() ==T_closesquare)
    {//cerr<<"]";
      eat_token(T_closesquare);

    }
  else
    syntax_error(NT_Q);
  break;
 case T_num:
   eat_token(T_num);
   break;
 case T_eof:
   parsetree.drawepsilon();
   break;
 default:
   syntax_error(NT_Q);
   break;
  }
parsetree.pop();
}
Example #18
0
void parser_t::Exp(){

	parsetree.push(NT_Exp);

	switch (scanner.next_token()){

		case T_openparen:
			eat_token(T_openparen);
			cerr<<" ( ";
			Expression();
			//scanner.next_token();
			eat_token(T_closeparen);
			cerr<< " ) ";
			break;

		case T_m:
			eat_token(T_m);
			cerr<<"m";
			scanner.next_token();
			eat_token(T_opensquare);
			cerr<<"[ ";
			Expression();
			//scanner.next_token();
			eat_token(T_closesquare);
			cerr<<" ]";
			break;

		case T_num:
			eat_token(T_num);
			cerr<<scanner.get_num();
			break;

		case T_eof:
			parsetree.drawepsilon();
			break;

		default:
			syntax_error(NT_Exp);
			break;
	}


	parsetree.pop();
}
Example #19
0
void parser_t::TermPrime()
{
	parsetree.push(NT_TermPrime);

	switch(scanner.next_token())
	{
		case T_times:
			eat_token(T_times);
			current = current + "*";
			// printf("%s\n", current.c_str());
			Power();
			TermPrime();
			break;

		case T_divide:
			eat_token(T_divide);
			current = current + "/";
			Power();
			TermPrime();
			break;

		//Follow Set of TermPrime
		case T_plus:
		case T_minus:
		case T_closesquare:
		case T_closeparen:
		case T_eof:
		case T_label:
		case T_goto:
		case T_m:
		case T_print:
			parsetree.drawepsilon();
			break;

		// Otherwise throw syntax error
		default:
			syntax_error(NT_TermPrime);
			break;
	}

	parsetree.pop();
}
Example #20
0
int parser_t::List(int v)
{
	switch( scanner.next_token() )
	{
		case T_semicolon:
			eat_token(T_semicolon);
			fprintf(stderr, "%d\n", v);
			v = -1;
			break;
		default:
			break;
	}	

	switch( scanner.next_token() )
	{
		case T_eof:
			parsetree.drawepsilon();
			return v;
		default:
			break;
	}	


	parsetree.push(NT_List);
	switch( scanner.next_token() )
	{
		case T_num:
			v = List(Rel(v));
			break;
		case T_openparen:
			v = List(Rel(v));
			break;
		default:
			syntax_error(NT_List);
			break;
	}
	parsetree.pop();

	return v;
}
Example #21
0
std::string parser_t::Expression_(std::string firstExpression)
{
	std::string result("");
	std::string secondExpression("");
	std::ostringstream stream;
	parsetree.push(NT_Expression_);

	switch(scanner.next_token())
	{
		case T_plus:
			eat_token(T_plus);
			secondExpression = Expression();
			stream << firstExpression << " + " << secondExpression;
			result = stream.str();
			break;
		case T_minus:
			eat_token(T_minus);
			secondExpression = Expression();
			stream << firstExpression << " - " << secondExpression;
			result = stream.str();
			break;
		case T_times:
		case T_divide:
		case T_power:
			// Times and divide and power
			result = Expression__(firstExpression);
			break;
		default:
			result = firstExpression;
			parsetree.drawepsilon();
			// Follow set
	}

	parsetree.pop();

	string s(result);

	return s;
}
Example #22
0
void parser_t::ExpressionPrime()
{
	parsetree.push(NT_ExpPrime);

	switch(scanner.next_token())
	{
		case T_plus:
			eat_token(T_plus);
			current = current + "+ ";
			Term();
			ExpressionPrime();
			break;
		case T_minus:
			eat_token(T_minus);
			current = current + "-";
			Term();
			ExpressionPrime();
			break;

		// All included in the follow set of ExpPrime
		case T_closesquare:
		case T_closeparen:
		case T_eof:
		case T_label:
		case T_goto:
		case T_m:
		case T_print:
			parsetree.drawepsilon();
			break;

		// Otherwise throw syntax error
		default:
			syntax_error(NT_ExpPrime);
			break;
	}

	parsetree.pop();
}
Example #23
0
//Here is an example
void parser_t::Start()
{
	//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
	outSave = "";
	maxArray = 0;
	cerr << "#include <stdio.h>\n#include <math.h>\nint main(void)\n{\nint m[101];\n";
	
	parsetree.push(NT_Start);

	switch( scanner.next_token() )
	{
		case T_plus:
			eat_token(T_plus);
			Start();
			break;
	case T_label:
	case T_goto:
	case T_m:
	case T_print:
		statements();
		break;
		case T_eof:
			parsetree.drawepsilon();
			break;
		default:
			syntax_error(NT_Start);
			break;
	}
	cerr << outSave;
	//statements();
	//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();
	cerr << "}\n";
}
Example #24
0
void parser_t::TermPrime()
{
  parsetree.push(NT_TermPrime);
  switch(scanner.next_token())
    {
      case T_times:
	eat_token(T_times);
	Factor();
	TermPrime();
	break;
      case T_divide:
	eat_token(T_divide);
	Factor();
	TermPrime();
	break;
      case T_eof:
	parsetree.drawepsilon();
	break;
      default:
	syntax_error(NT_TermPrime);
    }
  parsetree.pop();
}
Example #25
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();
}
Example #26
0
string parser_t::AnotherForward(string s)
{
    token_type watch = scanner.next_token();
    parsetree.push(NT_AnotherForward);
    switch(watch){
        case T_if:
            eat_token(T_if);
            s = Expression(s);
            break;
        case T_eof:
        case T_label:
        case T_goto:
        case T_m:
        case T_print:
            parsetree.drawepsilon();
            break;
        default:
            syntax_error(NT_AnotherForward);
            break;
    }
    parsetree.pop();
    return s;
}
Example #27
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();
}
Example #28
0
//Here is an example
void parser_t::Start()
{
	//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
	c_code_pre = "#include <math.h>\n#include <stdio.h>\n";//" int pow(int *num, count) { 	int i; int result = num[0]; for(i=1; i<count; i++) { result = pow(result, num[i]); } return result; }";

	c_code_main = "int main() {\n  int m[101];\n ";
	parsetree.push(NT_Start);

	switch( scanner.next_token() ) 
	{
		case T_label:
		case T_goto:
		case T_m:
		case T_print:
			Statements();
			break;
		case T_eof:
			parsetree.drawepsilon();
			break;
		default:
			syntax_error(NT_Start);
			break;
	}

	//Now that the parsing is done, print out the code to stderr
	// c_code_main = current;
	c_code_main = c_code_main + "return 0; }";
	fprintf(stderr, "%s\n%s", c_code_pre.c_str(), c_code_main.c_str());

	//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::ExpressionPrime()
{
  parsetree.push(NT_ExpressionPrime);
  switch(scanner.next_token())
    {
      case T_plus:
	eat_token(T_plus);
	Term();
	ExpressionPrime();
	break;
      case T_minus:
	eat_token(T_minus);
	Term();
	ExpressionPrime();
	break;
      case T_eof:
	parsetree.drawepsilon();
	break;
      default:
	syntax_error(NT_ExpressionPrime);
	break;
    }
  parsetree.pop();
}
Example #30
0
void parser_t::Secondjump(){
  parsetree.push(NT_Secondjump);
  switch(scanner.next_token())
    {
          case T_eof:
	     parsetree.drawepsilon();
	    // break;
	    //eat_token(T_eof);
	    break;
    case T_if:
      eat_token(T_if);
      //cerr<<"if";
      Expression();
      break;
    case T_label:
      parsetree.drawepsilon();
      break;
    case T_goto:
      parsetree.drawepsilon();
      break;
    case T_m:
      parsetree.drawepsilon();
      break;
    case T_print:
      parsetree.drawepsilon();
      break;
    case T_closesquare:
      parsetree.drawepsilon();
      break;
    case T_closeparen:
      parsetree.drawepsilon();
      break;
    default:
      syntax_error(NT_Secondjump);
      break;
    }
  parsetree.pop();
}