Browse Source

Fix else ifs

master
Danila Fedorin 3 years ago
parent
commit
626fd1629b
  1. 4
      Makefile
  2. 9
      parser.y
  3. 34
      tree.cpp
  4. 1
      tree.hpp

4
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

9
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

34
tree.cpp

@ -20,29 +20,29 @@ template <>
std::string label<binop>(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>(unop op) {
}
template <typename T>
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) {

1
tree.hpp

@ -1,6 +1,7 @@
#pragma once
#include <memory>
#include <vector>
#include <string>
typedef std::vector<std::string> dot;

Loading…
Cancel
Save