diff --git a/Makefile b/Makefile index 0a9b5b2..59f9f2a 100644 --- a/Makefile +++ b/Makefile @@ -4,13 +4,13 @@ parser.cpp parser.hpp: parser.y bison -d -o parser.cpp parser.y tree.o: tree.cpp - g++ -g -c -o tree.o tree.cpp + g++ -std=c++11 -g -c -o tree.o tree.cpp scanner.cpp: scanner.l flex -o scanner.cpp scanner.l parse: main.cpp parser.cpp scanner.cpp tree.o - g++ -g main.cpp parser.cpp scanner.cpp tree.o -o parse + g++ -std=c++11 -g main.cpp parser.cpp scanner.cpp tree.o -o parse clean: rm -f parse scanner.cpp parser.cpp parser.hpp tree.o diff --git a/parser.y b/parser.y index bbb1c3c..c4f1630 100644 --- a/parser.y +++ b/parser.y @@ -134,17 +134,18 @@ condition ; if_statement - : IF condition COLON NEWLINE block elif_blocks else_block { $$ = new stmt_if($2, $5, $7); delete $6; } + : IF condition COLON NEWLINE block elif_blocks { $$ = new stmt_if($2, $5, $6); } ; elif_blocks : %empty { $$ = nullptr; } - | elif_blocks ELIF condition COLON NEWLINE block { $$ = new stmt_if($3, $6); delete $6; } + | else_block { $$ = $1; } + | ELIF condition COLON NEWLINE block elif_blocks { $$ = new stmt_if($2, $5, $6); } ; else_block - : %empty { $$ = nullptr; } - | ELSE COLON NEWLINE block { $$ = $4; } + : ELSE COLON NEWLINE block { $$ = $4; } + ; while_statement diff --git a/tree.cpp b/tree.cpp index bc9bf0f..ad7da2a 100644 --- a/tree.cpp +++ b/tree.cpp @@ -20,29 +20,29 @@ template <> std::string label(binop op) { switch(op) { case binop::plus: - return "+"; + return "PLUS"; case binop::minus: - return "-"; + return "MINUS"; case binop::times: - return "*"; + return "TIMES"; case binop::divide: - return "/"; + return "DIVIDEBY"; case binop::land: - return "&&"; + return "AND"; case binop::lor: - return "||"; + return "OR"; case binop::eq: - return "=="; + return "EQ"; case binop::neq: - return "!="; + return "NEQ"; case binop::lt: - return "<"; + return "LT"; case binop::lte: - return "<="; + return "LTE"; case binop::gt: - return ">"; + return "GT"; case binop::gte: - return ">="; + return "GTE"; default: return ""; } @@ -59,20 +59,20 @@ std::string label(unop op) { } template -std::string wrap_label(T v) { - return std::string(" [label=\"") + label(v) + "\"]"; +std::string wrap_label(T v, bool box = false) { + return std::string(" [label=\"") + label(v) + "\" " + (box ? "shape=\"box\"" : "") + "]"; } void expr_id::print_dot(dot& into, std::string prefix) { - into.push_back(prefix + wrap_label(id)); + into.push_back(prefix + wrap_label(std::string("identifier: ") + id, true)); } void expr_int::print_dot(dot& into, std::string prefix) { - into.push_back(prefix + wrap_label(val)); + into.push_back(prefix + wrap_label(std::string("integer: ") + label(val), true)); } void expr_float::print_dot(dot& into, std::string prefix) { - into.push_back(prefix + wrap_label(val)); + into.push_back(prefix + wrap_label(std::string("float: ") + label(val), true)); } void expr_binop::print_dot(dot& into, std::string prefix) { diff --git a/tree.hpp b/tree.hpp index 0a22fc1..e1c4eec 100644 --- a/tree.hpp +++ b/tree.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#include typedef std::vector dot;