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(); }
//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); code_printf("#include <stdio.h>\n"); code_printf("inline int intPow(int num, int e) {\n"); code_printf("\tint res = 1;\n"); code_printf("\tif(e>=0){\n"); code_printf("\t\tfor(;e>0;e--)res = res*num;\n"); code_printf("\t\treturn res;\n"); code_printf("\t}\n"); code_printf("\tif(num == 1) return 1;\n"); /* if e is negative 1^-a is 1*/ code_printf("\treturn 0;\n"); /* if e is negative floor(1 ^-a) is 0*/ code_printf("}\n"); code_printf("int main(int argc, char *argv[]){\n"); code_printf("\tint m[101];\n"); // code_printf("\tint m[101];\n"); Statements(); code_printf("\treturn 0;\n"); code_printf("}\n"); parsetree.pop(); }
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; }
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; }
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(); }
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(); }
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; }
void parser_t::Statement() { parsetree.push(NT_Statement); code_printf("\t"); switch(scanner.next_token()){ case T_label: Label(); break; case T_goto: Jump(); code_printf(";\n"); break; case T_m: Assignment(); code_printf(";\n"); break; case T_print: Print(); code_printf(";\n"); break; default: syntax_error(NT_Start); break; } parsetree.pop(); }
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(); }
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(); }
void parser_t::Statement() { parsetree.push(NT_Statement); current = c_code_main; // printf("%s\n", current.c_str()); switch(scanner.next_token()) { case T_label: Label(); break; case T_goto: Jump(); break; case T_m: Assignment(); break; case T_print: Print(); break; default: syntax_error(NT_Statement); break; } c_code_main = current; // printf("%s\n", c_code_main.c_str()); parsetree.pop(); }
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(); }
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(); }
void parser_t::Print(){ parsetree.push(NT_Print); eat_token(T_print); code_printf("printf(\"%%d\\n\", "); Expression(); code_printf(")"); parsetree.pop(); }
void parser_t::Term() { parsetree.push(NT_Term); ExponentialTerm(); TermRight(); parsetree.pop(); }
void parser_t::Expression() { parsetree.push(NT_Expression); Term(); ExpressionRight(); parsetree.pop(); }
void parser_t::TermP() { //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_TermPrime); switch( scanner.next_token() ) { case T_label: parsetree.drawepsilon(); break; case T_m: parsetree.drawepsilon(); break; case T_closeparen: parsetree.drawepsilon(); break; case T_goto: parsetree.drawepsilon(); break; case T_print: parsetree.drawepsilon(); break; case T_closesquare: parsetree.drawepsilon(); break; case T_plus: parsetree.drawepsilon(); break; case T_minus: parsetree.drawepsilon(); break; case T_times: eat_token(T_times); cerr << "*"; Power(); TermP(); break; case T_divide: eat_token(T_divide); cerr << "/"; Power(); TermP(); break; case T_eof: parsetree.drawepsilon(); break; default: syntax_error(NT_TermPrime); 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(); }
void parser_t::Power() { parsetree.push(NT_Power); Parenthesis(); PowerPrime(); parsetree.pop(); }
void parser_t::Term() { parsetree.push(NT_Term); Power(); TermPrime(); parsetree.pop(); }
void parser_t::Statements(){ parsetree.push(NT_Statements); Statement(); MoreStatement(); parsetree.pop(); }
bool parser_t::furu(string& expString) { parsetree.push(NT_MultDiv); bool rr = exp(expString); furuImouto(expString); parsetree.pop(); return rr; }
// Print -> "print" Expression void parser_t::Print() { parsetree.push(NT_Print); eat_token(T_print); fprintf(stderr, "\tprintf(\"%%d\\n\", %s);", Expression().c_str()); parsetree.pop(); }
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(); }
void parser_t::Print() { parsetree.push(NT_Print); if(scanner.next_token()==T_print) {eat_token(T_print);} else{syntax_error(NT_Print);} Expression(); parsetree.pop(); }
void parser_t::Term(){ parsetree.push(NT_Term); scanner.next_token(); Factor(); TermP(); parsetree.pop(); }
bool parser_t::exp(string& expString) { parsetree.push(NT_Exp); numend(expString); bool rr = expImouto(expString); parsetree.pop(); return rr; }
void parser_t::print() { parsetree.push(NT_Print); eat_token(T_print); outSave += "printf(\"%d\","; string ss = ""; outSave += expression(); outSave += ")"; parsetree.pop(); }
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(); }
void parser_t::Factor(){ parsetree.push(NT_Factor); scanner.next_token(); Exp(); FactorP(); parsetree.pop(); }
void parser_t::Jump(){ int number; parsetree.push(NT_Jump); eat_token(T_goto); eat_token(T_num); number = scanner.get_num(); JumpIf(); code_printf("goto my_label%d",number); parsetree.pop(); }