Seemingly finish the assignment.
This commit is contained in:
parent
3b2f6baff9
commit
4bc44f6d43
2
Makefile
2
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
|
||||
|
|
18
parser.y
18
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<std::string> varset;
|
||||
|
||||
void print_standard(std::string* str) {
|
||||
std::cout << "#include <iostream>" << 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
|
||||
|
|
44
scanner.l
44
scanner.l
|
@ -26,8 +26,7 @@ std::stack<int> _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 << ": "
|
||||
|
|
Loading…
Reference in New Issue
Block a user