diff --git a/Makefile b/Makefile index e9b1151..2c04e82 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ all: scan scan: main.cpp scanner.cpp parser.cpp - g++ main.cpp scanner.cpp parser.cpp -o scan + g++ -std=c++11 main.cpp scanner.cpp parser.cpp -o scan scanner.cpp: scanner.l flex -o scanner.cpp scanner.l diff --git a/parser.y b/parser.y index 86a3147..ad92ce7 100644 --- a/parser.y +++ b/parser.y @@ -7,6 +7,7 @@ %} +%token TEOF %token INDENT %token DEDENT %token NEWLINE @@ -78,12 +79,27 @@ void yyerror(const char* s) { std::set varset; +void print_standard(std::string* str) { + std::cout << "#include " << std::endl; + std::cout << "int main() {" << std::endl; + for(auto& name : varset) { + std::cout << "double " << name << ";" << std::endl; + } + std::cout << " " << *str << std::endl; + for(auto& name : varset) { + std::cout << " std::cout << \"" << name << ": \" << " << name << "<< std::endl;" << std::endl; + } + std::cout << "}"; + + delete str; +} + %} %% program - : stmts { std::cout << *$1 << std::endl; $$ = $1; } + : stmts { print_standard($1); } TEOF ; stmts diff --git a/scanner.l b/scanner.l index 8227fbe..c5a9d6b 100644 --- a/scanner.l +++ b/scanner.l @@ -26,8 +26,7 @@ std::stack _indent_stack; yypstate* state = yypstate_new(); #define PUSH_TOKEN(i, str) do { \ - std::cout << i << ", " << str << std::endl; \ - YYSTYPE temp = str; \ + YYSTYPE temp = new std::string(str); \ int s = yypush_parse(state, i, &temp); \ if (s != YYPUSH_MORE) { \ yypstate_delete(state); \ @@ -125,6 +124,7 @@ yypstate* state = yypstate_new(); _indent_stack.pop(); PUSH_TOKEN(DEDENT, ""); } + PUSH_TOKEN(TEOF, ""); yyterminate(); } @@ -142,39 +142,39 @@ yypstate* state = yypstate_new(); "return" { PUSH_TOKEN(RETURN, ""); } "while" { PUSH_TOKEN(WHILE, ""); } -"True" { PUSH_TOKEN(BOOLEAN, "True"); } -"False" { PUSH_TOKEN(BOOLEAN, "True"); } +"True" { PUSH_TOKEN(BOOLEAN, "true"); } +"False" { PUSH_TOKEN(BOOLEAN, "false"); } [a-zA-Z_][a-zA-Z0-9_]* { - PUSH_TOKEN(IDENTIFIER, ""); + PUSH_TOKEN(IDENTIFIER, yytext); } -?[0-9]*"."[0-9]+ { - PUSH_TOKEN(FLOAT, ""); + PUSH_TOKEN(FLOAT, yytext); } -?[0-9]+ { - PUSH_TOKEN(INTEGER, ""); + PUSH_TOKEN(INTEGER, yytext); } -"=" { PUSH_TOKEN(ASSIGN, ""); } -"+" { PUSH_TOKEN(PLUS, ""); } -"-" { PUSH_TOKEN(MINUS, ""); } -"*" { PUSH_TOKEN(TIMES, ""); } -"/" { PUSH_TOKEN(DIVIDEDBY, ""); } +"=" { PUSH_TOKEN(ASSIGN, yytext); } +"+" { PUSH_TOKEN(PLUS, yytext); } +"-" { PUSH_TOKEN(MINUS, yytext); } +"*" { PUSH_TOKEN(TIMES, yytext); } +"/" { PUSH_TOKEN(DIVIDEDBY, yytext); } -"==" { PUSH_TOKEN(EQ, ""); } -"!=" { PUSH_TOKEN(NEQ, ""); } -">" { PUSH_TOKEN(GT, ""); } -">=" { PUSH_TOKEN(GTE, ""); } -"<" { PUSH_TOKEN(LT, ""); } -"<=" { PUSH_TOKEN(LTE, ""); } +"==" { PUSH_TOKEN(EQ, yytext); } +"!=" { PUSH_TOKEN(NEQ, yytext); } +">" { PUSH_TOKEN(GT, yytext); } +">=" { PUSH_TOKEN(GTE, yytext); } +"<" { PUSH_TOKEN(LT, yytext); } +"<=" { PUSH_TOKEN(LTE, yytext); } -"(" { PUSH_TOKEN(LPAREN, ""); } -")" { PUSH_TOKEN(RPAREN, ""); } +"(" { PUSH_TOKEN(LPAREN, yytext); } +")" { PUSH_TOKEN(RPAREN, yytext); } -"," { PUSH_TOKEN(COMMA, ""); } -":" { PUSH_TOKEN(COLON, ""); } +"," { PUSH_TOKEN(COMMA, yytext); } +":" { PUSH_TOKEN(COLON, yytext); } . { std::cerr << "Unrecognized token on line " << yylineno << ": "