2019-05-14 12:00:45 -07:00
|
|
|
%{
|
|
|
|
#include <iostream>
|
|
|
|
#include <set>
|
|
|
|
|
2019-05-20 11:12:53 -07:00
|
|
|
#include "tree.hpp"
|
2019-05-14 12:00:45 -07:00
|
|
|
#include "parser.hpp"
|
|
|
|
|
|
|
|
extern int yylex();
|
|
|
|
void yyerror(YYLTYPE* loc, const char* err);
|
|
|
|
std::string* translate_boolean_str(std::string* boolean_str);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here, target_program is a string that will hold the target program being
|
|
|
|
* generated, and symbols is a simple symbol table.
|
|
|
|
*/
|
2019-05-20 11:12:53 -07:00
|
|
|
stmt_ptr target_program;
|
2019-05-14 12:00:45 -07:00
|
|
|
%}
|
|
|
|
|
|
|
|
/* Enable location tracking. */
|
|
|
|
%locations
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All program constructs will be represented as strings, specifically as
|
|
|
|
* their corresponding C/C++ translation.
|
|
|
|
*/
|
2019-05-20 11:12:53 -07:00
|
|
|
%union {
|
|
|
|
expr_ptr ex;
|
|
|
|
stmt_ptr st;
|
|
|
|
std::string* str;
|
|
|
|
}
|
2019-05-14 12:00:45 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Because the lexer can generate more than one token at a time (i.e. DEDENT
|
|
|
|
* tokens), we'll use a push parser.
|
|
|
|
*/
|
|
|
|
%define api.pure full
|
|
|
|
%define api.push-pull push
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These are all of the terminals in our grammar, i.e. the syntactic
|
|
|
|
* categories that can be recognized by the lexer.
|
|
|
|
*/
|
|
|
|
%token IDENTIFIER
|
|
|
|
%token FLOAT INTEGER BOOLEAN
|
|
|
|
%token INDENT DEDENT NEWLINE
|
|
|
|
%token AND BREAK DEF ELIF ELSE FOR IF NOT OR RETURN WHILE
|
|
|
|
%token ASSIGN PLUS MINUS TIMES DIVIDEDBY
|
|
|
|
%token EQ NEQ GT GTE LT LTE
|
|
|
|
%token LPAREN RPAREN COMMA COLON
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here, we're defining the precedence of the operators. The ones that appear
|
|
|
|
* later have higher precedence. All of the operators are left-associative
|
|
|
|
* except the "not" operator, which is right-associative.
|
|
|
|
*/
|
|
|
|
%left OR
|
|
|
|
%left AND
|
|
|
|
%left PLUS MINUS
|
|
|
|
%left TIMES DIVIDEDBY
|
|
|
|
%left EQ NEQ GT GTE LT LTE
|
|
|
|
%right NOT
|
|
|
|
|
2019-05-20 11:12:53 -07:00
|
|
|
%type <str> IDENTIFIER FLOAT INTEGER BOOLEAN
|
|
|
|
%type <st> program statements statement if_statement while_statement break_statement block elif_blocks else_block
|
|
|
|
%type <ex> assign_statement primary_expression negated_expression expression condition
|
|
|
|
|
2019-05-14 12:00:45 -07:00
|
|
|
/* This is our goal/start symbol. */
|
|
|
|
%start program
|
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Each of the CFG rules below recognizes a particular program construct in
|
|
|
|
* Python and creates a new string containing the corresponding C/C++
|
|
|
|
* translation. Since we're allocating strings as we go, we also free them
|
|
|
|
* as we no longer need them. Specifically, each string is freed after it is
|
|
|
|
* combined into a larger string.
|
|
|
|
*/
|
|
|
|
program
|
|
|
|
: statements { target_program = $1; }
|
|
|
|
;
|
|
|
|
|
|
|
|
statements
|
2019-05-20 11:12:53 -07:00
|
|
|
: statement { $$ = new stmt_block(); ((stmt_block*)$$)->children.push_back($1); }
|
|
|
|
| statements statement { $$ = $1; ((stmt_block*)$$)->children.push_back($2); }
|
2019-05-14 12:00:45 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
statement
|
2019-05-20 11:12:53 -07:00
|
|
|
: assign_statement { $$ = new stmt_expr($1); }
|
2019-05-14 12:00:45 -07:00
|
|
|
| if_statement { $$ = $1; }
|
|
|
|
| while_statement { $$ = $1; }
|
|
|
|
| break_statement { $$ = $1; }
|
|
|
|
;
|
|
|
|
|
|
|
|
primary_expression
|
2019-05-20 11:12:53 -07:00
|
|
|
: IDENTIFIER { $$ = new expr_id(*$1); delete $1; }
|
|
|
|
| FLOAT { $$ = new expr_float(std::stod(*$1)); delete $1; }
|
|
|
|
| INTEGER { $$ = new expr_int(std::stoi(*$1)); delete $1; }
|
2019-05-27 21:57:41 -07:00
|
|
|
| BOOLEAN { $$ = new expr_bool(*$1 == "True"); delete $1; }
|
2019-05-20 11:12:53 -07:00
|
|
|
| LPAREN expression RPAREN { $$ = $2; }
|
2019-05-14 12:00:45 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
negated_expression
|
2019-05-20 11:12:53 -07:00
|
|
|
: NOT primary_expression { $$ = new expr_unop(unop::lnot, $2); }
|
2019-05-14 12:00:45 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
expression
|
|
|
|
: primary_expression { $$ = $1; }
|
|
|
|
| negated_expression { $$ = $1; }
|
2019-05-20 11:12:53 -07:00
|
|
|
| expression PLUS expression { $$ = new expr_binop(binop::plus, $1, $3); }
|
|
|
|
| expression MINUS expression { $$ = new expr_binop(binop::minus, $1, $3); }
|
|
|
|
| expression TIMES expression { $$ = new expr_binop(binop::times, $1, $3); }
|
|
|
|
| expression DIVIDEDBY expression { $$ = new expr_binop(binop::divide, $1, $3); }
|
|
|
|
| expression EQ expression { $$ = new expr_binop(binop::eq, $1, $3); }
|
|
|
|
| expression NEQ expression { $$ = new expr_binop(binop::neq, $1, $3); }
|
|
|
|
| expression GT expression { $$ = new expr_binop(binop::gt, $1, $3); }
|
|
|
|
| expression GTE expression { $$ = new expr_binop(binop::gte, $1, $3); }
|
|
|
|
| expression LT expression { $$ = new expr_binop(binop::lt, $1, $3); }
|
|
|
|
| expression LTE expression { $$ = new expr_binop(binop::lte, $1, $3); }
|
2019-05-14 12:00:45 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
assign_statement
|
2019-05-20 11:12:53 -07:00
|
|
|
: IDENTIFIER ASSIGN expression NEWLINE { $$ = new expr_assign(*$1, $3); delete $1; }
|
2019-05-14 12:00:45 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
block
|
2019-05-20 11:12:53 -07:00
|
|
|
: INDENT statements DEDENT { $$ = $2; }
|
2019-05-14 12:00:45 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
condition
|
|
|
|
: expression { $$ = $1; }
|
2019-05-20 11:12:53 -07:00
|
|
|
| condition AND condition { $$ = new expr_binop(binop::land, $1, $3); }
|
|
|
|
| condition OR condition { $$ = new expr_binop(binop::lor, $1, $3); }
|
2019-05-14 12:00:45 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
if_statement
|
2019-05-27 21:51:39 -07:00
|
|
|
: IF condition COLON NEWLINE block elif_blocks { $$ = new stmt_if($2, $5, $6); }
|
2019-05-14 12:00:45 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
elif_blocks
|
2019-05-20 11:12:53 -07:00
|
|
|
: %empty { $$ = nullptr; }
|
2019-05-27 21:51:39 -07:00
|
|
|
| else_block { $$ = $1; }
|
|
|
|
| ELIF condition COLON NEWLINE block elif_blocks { $$ = new stmt_if($2, $5, $6); }
|
2019-05-14 12:00:45 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
else_block
|
2019-05-27 21:51:39 -07:00
|
|
|
: ELSE COLON NEWLINE block { $$ = $4; }
|
|
|
|
;
|
2019-05-14 12:00:45 -07:00
|
|
|
|
|
|
|
|
|
|
|
while_statement
|
2019-05-20 11:12:53 -07:00
|
|
|
: WHILE condition COLON NEWLINE block { $$ = new stmt_while($2, $5); }
|
2019-05-14 12:00:45 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
break_statement
|
2019-05-20 11:12:53 -07:00
|
|
|
: BREAK NEWLINE { $$ = new stmt_break(); }
|
2019-05-14 12:00:45 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
void yyerror(YYLTYPE* loc, const char* err) {
|
|
|
|
std::cerr << "Error (line " << loc->first_line << "): " << err << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function translates a Python boolean value into the corresponding
|
|
|
|
* C++ boolean value.
|
|
|
|
*/
|
|
|
|
std::string* translate_boolean_str(std::string* boolean_str) {
|
|
|
|
if (*boolean_str == "True") {
|
|
|
|
return new std::string("true");
|
|
|
|
} else {
|
|
|
|
return new std::string("false");
|
|
|
|
}
|
|
|
|
}
|