2019-05-11 21:19:15 -07:00
|
|
|
%{
|
|
|
|
|
|
|
|
#include <string>
|
2019-05-11 22:34:26 -07:00
|
|
|
#include <iostream>
|
2019-05-12 12:53:04 -07:00
|
|
|
#include <set>
|
2019-05-11 22:34:26 -07:00
|
|
|
#include "parser.hpp"
|
2019-05-11 21:19:15 -07:00
|
|
|
|
|
|
|
%}
|
|
|
|
|
2019-05-12 23:02:48 -07:00
|
|
|
%token TEOF
|
2019-05-11 21:19:15 -07:00
|
|
|
%token INDENT
|
|
|
|
%token DEDENT
|
|
|
|
%token NEWLINE
|
|
|
|
%token AND
|
|
|
|
%token BREAK
|
|
|
|
%token DEF
|
|
|
|
%token ELIF
|
|
|
|
%token ELSE
|
|
|
|
%token FOR
|
|
|
|
%token IF
|
|
|
|
%token NOT
|
|
|
|
%token OR
|
|
|
|
%token RETURN
|
|
|
|
%token WHILE
|
|
|
|
%token BOOLEAN
|
2019-05-11 22:34:26 -07:00
|
|
|
%token FLOAT
|
|
|
|
%token INTEGER
|
2019-05-11 21:19:15 -07:00
|
|
|
%token IDENTIFIER
|
|
|
|
%token ASSIGN
|
|
|
|
%token PLUS
|
|
|
|
%token MINUS
|
|
|
|
%token TIMES
|
|
|
|
%token DIVIDEDBY
|
|
|
|
%token EQ
|
|
|
|
%token NEQ
|
|
|
|
%token GT
|
|
|
|
%token GTE
|
|
|
|
%token LT
|
|
|
|
%token LTE
|
|
|
|
%token LPAREN
|
|
|
|
%token RPAREN
|
|
|
|
%token COMMA
|
|
|
|
%token COLON
|
|
|
|
|
2019-05-12 19:01:25 -07:00
|
|
|
%define api.value.type { std::string* }
|
2019-05-11 21:19:15 -07:00
|
|
|
%define api.pure full
|
|
|
|
%define api.push-pull push
|
2019-05-11 22:34:26 -07:00
|
|
|
%start program
|
|
|
|
|
|
|
|
%{
|
|
|
|
|
2019-05-12 19:01:25 -07:00
|
|
|
template <typename T, typename std::enable_if<std::is_convertible<T, const char*>::value, T>::type* = nullptr>
|
|
|
|
void concat(std::string* s, T vs) {
|
|
|
|
s->append(vs);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename std::enable_if<std::is_convertible<T, std::string*>::value, T>::type* = nullptr>
|
|
|
|
void concat(std::string* s, T vs) {
|
|
|
|
s->append(*vs);
|
|
|
|
delete vs;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename ... Ts>
|
|
|
|
void concat(std::string* s, T v, Ts ... vs) {
|
|
|
|
concat(s, v);
|
|
|
|
concat(s, vs...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ... Ts>
|
|
|
|
std::string* concatenate(Ts ... vs) {
|
|
|
|
std::string* temp = new std::string();
|
|
|
|
concat(temp, vs...);
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
2019-05-11 22:34:26 -07:00
|
|
|
void yyerror(const char* s) {
|
|
|
|
std::cout << s << std::endl;
|
|
|
|
}
|
|
|
|
|
2019-05-12 12:53:04 -07:00
|
|
|
std::set<std::string> varset;
|
|
|
|
|
2019-05-12 23:02:48 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-05-11 22:34:26 -07:00
|
|
|
%}
|
2019-05-11 21:19:15 -07:00
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
program
|
2019-05-12 23:02:48 -07:00
|
|
|
: stmts { print_standard($1); } TEOF
|
2019-05-12 12:53:04 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
stmts
|
2019-05-12 19:01:25 -07:00
|
|
|
: stmts stmt { $$ = concatenate($1, $2); }
|
|
|
|
| stmt { $$ = $1; }
|
2019-05-11 21:19:15 -07:00
|
|
|
;
|
|
|
|
|
2019-05-13 17:32:27 -07:00
|
|
|
newlines
|
|
|
|
: newlines NEWLINE
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
2019-05-11 22:34:26 -07:00
|
|
|
stmt
|
2019-05-13 17:32:27 -07:00
|
|
|
: expr newlines { $$ = concatenate($1, ";"); }
|
|
|
|
| if newlines { $$ = $1; }
|
|
|
|
| while newlines { $$ = $1; }
|
|
|
|
| BREAK newlines { $$ = new std::string("break;"); }
|
2019-05-12 12:53:04 -07:00
|
|
|
;
|
2019-05-11 22:34:26 -07:00
|
|
|
|
|
|
|
expr
|
2019-05-12 19:01:25 -07:00
|
|
|
: assign { $$ = $1; }
|
|
|
|
| eq { $$ = $1; }
|
2019-05-12 12:53:04 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
while
|
2019-05-12 19:01:25 -07:00
|
|
|
: WHILE expr COLON NEWLINE block { $$ = concatenate("while(", $2, ")", $5); }
|
2019-05-12 12:53:04 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
if
|
2019-05-12 19:01:25 -07:00
|
|
|
: IF expr COLON NEWLINE block elses { $$ = concatenate("if(", $2, ")", $5, $6); }
|
|
|
|
;
|
|
|
|
|
|
|
|
elses
|
|
|
|
: %empty { $$ = new std::string(""); }
|
|
|
|
| elif elses { $$ = concatenate($1, $2); }
|
|
|
|
| else { $$ = $1; }
|
2019-05-12 12:53:04 -07:00
|
|
|
;
|
|
|
|
|
2019-05-12 19:01:25 -07:00
|
|
|
elif
|
|
|
|
: ELIF expr COLON NEWLINE block { $$ = concatenate("else if(", $2, ")", $5); }
|
|
|
|
;
|
|
|
|
|
|
|
|
else
|
|
|
|
: ELSE COLON NEWLINE block { $$ = concatenate("else", $4); }
|
|
|
|
;
|
2019-05-12 12:53:04 -07:00
|
|
|
|
|
|
|
block
|
2019-05-12 19:01:25 -07:00
|
|
|
: INDENT stmts DEDENT { $$ = concatenate("{", $2, "}"); }
|
2019-05-11 21:19:15 -07:00
|
|
|
;
|
|
|
|
|
2019-05-11 22:34:26 -07:00
|
|
|
assign
|
2019-05-12 19:01:25 -07:00
|
|
|
: IDENTIFIER ASSIGN expr { varset.insert(*$1); $$ = concatenate($1, "=", $3); }
|
2019-05-11 21:19:15 -07:00
|
|
|
;
|
|
|
|
|
2019-05-12 12:53:04 -07:00
|
|
|
eq
|
2019-05-12 19:01:25 -07:00
|
|
|
: eq EQ rel { $$ = concatenate($1, "==", $3); }
|
|
|
|
| eq NEQ rel { $$ = concatenate($1, "!=", $3); }
|
|
|
|
| rel { $$ = $1; }
|
2019-05-12 12:53:04 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
rel
|
2019-05-12 19:01:25 -07:00
|
|
|
: rel LT sum { $$ = concatenate($1, "<", $3); }
|
|
|
|
| rel LTE sum { $$ = concatenate($1, "<=", $3); }
|
|
|
|
| rel GT sum { $$ = concatenate($1, ">", $3); }
|
|
|
|
| rel GTE sum { $$ = concatenate($1, ">=", $3); }
|
|
|
|
| sum { $$ = $1; }
|
2019-05-12 12:53:04 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
sum
|
2019-05-12 19:01:25 -07:00
|
|
|
: sum PLUS factor { $$ = concatenate($1, "+", $3); }
|
|
|
|
| sum MINUS factor { $$ = concatenate($1, "-", $3); }
|
|
|
|
| factor { $$ = $1; }
|
2019-05-12 12:53:04 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
factor
|
2019-05-12 19:01:25 -07:00
|
|
|
: factor TIMES term { $$ = concatenate($1, "*", $3); }
|
|
|
|
| factor DIVIDEDBY term { $$ = concatenate($1, "/", $3); }
|
|
|
|
| term { $$ = $1; }
|
2019-05-12 12:53:04 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
term
|
2019-05-12 19:01:25 -07:00
|
|
|
: IDENTIFIER { $$ = $1; }
|
|
|
|
| FLOAT { $$ = $1; }
|
|
|
|
| INTEGER { $$ = $1; }
|
|
|
|
| BOOLEAN { $$ = $1; }
|
|
|
|
| LPAREN expr RPAREN { $$ = $2; }
|
2019-05-12 12:53:04 -07:00
|
|
|
;
|
|
|
|
|
2019-05-11 21:19:15 -07:00
|
|
|
%%
|