2013-05-31 19:13:55 +02:00
|
|
|
%include {
|
|
|
|
|
|
2013-05-31 21:35:44 +02:00
|
|
|
#include <cstdio>
|
2013-05-31 19:13:55 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
2013-05-31 23:51:44 +02:00
|
|
|
#include "Token.h"
|
|
|
|
|
|
|
|
|
|
#include "AST/ASTElement.h"
|
2013-06-01 01:50:24 +02:00
|
|
|
#include "AST/AssignmentExpression.h"
|
2013-06-01 03:18:46 +02:00
|
|
|
#include "AST/BinOpExpression.h"
|
2013-06-01 14:29:18 +02:00
|
|
|
#include "AST/CodeGenVisitor.h"
|
2013-06-01 00:52:03 +02:00
|
|
|
#include "AST/ConstantExpression.h"
|
|
|
|
|
#include "AST/Expression.h"
|
2013-06-01 02:50:47 +02:00
|
|
|
#include "AST/ExpressionStatement.h"
|
2013-06-01 03:02:14 +02:00
|
|
|
#include "AST/ForStatement.h"
|
2013-06-01 03:10:16 +02:00
|
|
|
#include "AST/FunctionCallExpression.h"
|
2013-06-01 02:50:47 +02:00
|
|
|
#include "AST/FunctionDefinition.h"
|
2013-06-01 02:21:59 +02:00
|
|
|
#include "AST/IfStatement.h"
|
2013-06-01 15:48:28 +02:00
|
|
|
#include "AST/LoadExpression.h"
|
2013-06-01 00:38:20 +02:00
|
|
|
#include "AST/ParameterList.h"
|
2013-06-01 03:33:38 +02:00
|
|
|
#include "AST/PrintVisitor.h"
|
|
|
|
|
#include "AST/RandomForStatement.h"
|
2013-06-01 03:02:14 +02:00
|
|
|
#include "AST/RandomIfStatement.h"
|
2013-06-01 02:50:47 +02:00
|
|
|
#include "AST/ReturnStatement.h"
|
2013-06-01 02:27:01 +02:00
|
|
|
#include "AST/Scope.h"
|
2013-06-01 00:16:38 +02:00
|
|
|
#include "AST/Statement.h"
|
2013-06-01 01:53:15 +02:00
|
|
|
#include "AST/StatementList.h"
|
2013-06-01 00:16:38 +02:00
|
|
|
#include "AST/Type.h"
|
2013-06-01 02:10:29 +02:00
|
|
|
#include "AST/ValueList.h"
|
2013-06-01 00:38:20 +02:00
|
|
|
#include "AST/VariableDefinition.h"
|
2013-05-31 23:51:44 +02:00
|
|
|
|
2013-05-31 19:13:55 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-31 23:51:44 +02:00
|
|
|
%name scullyParser
|
|
|
|
|
|
|
|
|
|
%token_type {Token*}
|
2013-06-01 14:32:48 +02:00
|
|
|
%extra_argument{CodeGenVisitor* cv}
|
2013-05-31 19:13:55 +02:00
|
|
|
|
2013-05-31 22:20:31 +02:00
|
|
|
// whitespace and comments
|
|
|
|
|
%type T_WHITESPACE {int}
|
|
|
|
|
|
2013-05-31 21:04:20 +02:00
|
|
|
%right T_ASSIGN.
|
|
|
|
|
%left T_EQUALS.
|
|
|
|
|
%left T_LESS.
|
|
|
|
|
%left T_PLUS T_MINUS.
|
|
|
|
|
%left T_TIMES T_DIV.
|
2013-05-31 19:13:55 +02:00
|
|
|
|
|
|
|
|
%syntax_error {
|
2013-06-01 02:21:59 +02:00
|
|
|
std::cerr << "Syntax error!" << std::endl;
|
2013-05-31 23:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
%parse_failure {
|
|
|
|
|
std::cerr << "Parser Failed!" << std::endl;
|
2013-05-31 19:13:55 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-31 23:51:44 +02:00
|
|
|
%type program {int}
|
2013-06-01 14:32:48 +02:00
|
|
|
program ::= fundef(F). { PrintVisitor* pv = new PrintVisitor; F->accept(pv); F->accept(cv); delete pv; }
|
2013-06-01 21:10:05 +02:00
|
|
|
program ::= expr(E). { PrintVisitor* pv = new PrintVisitor; E->accept(pv); cv->JIT(E); delete pv; }
|
2013-05-31 21:04:20 +02:00
|
|
|
|
2013-06-01 02:50:47 +02:00
|
|
|
%type fundef {FunctionDefinition*}
|
2013-06-01 02:12:49 +02:00
|
|
|
fundef(A) ::= type(T) T_IDENTIFIER(ID) T_LPAREN params(P) T_RPAREN T_BEGIN statements(S) T_END.
|
2013-06-01 02:50:47 +02:00
|
|
|
{ A = new FunctionDefinition(T, ID->getText(), P, S); }
|
2013-05-31 21:04:20 +02:00
|
|
|
|
2013-06-01 16:22:01 +02:00
|
|
|
%type type {Type}
|
|
|
|
|
type(A) ::= T_BOOL. { A = Type::BOOL; }
|
|
|
|
|
type(A) ::= T_INT. { A = Type::INT; }
|
|
|
|
|
type(A) ::= T_VOID. { A = Type::VOID; }
|
2013-05-31 21:04:20 +02:00
|
|
|
|
2013-06-01 00:38:20 +02:00
|
|
|
%type params {ParameterList*}
|
2013-06-01 02:12:49 +02:00
|
|
|
params(A) ::= . { A = new ParameterList(); }
|
|
|
|
|
params(A) ::= type(T) T_IDENTIFIER(ID). { A = new ParameterList(); A->addParameter(T, ID->getText()); }
|
|
|
|
|
params(A) ::= params(B) T_COMMA type(T) T_IDENTIFIER(ID).
|
|
|
|
|
{ B->addParameter(T, ID->getText()); A = B; }
|
2013-05-31 21:04:20 +02:00
|
|
|
|
2013-06-01 02:21:59 +02:00
|
|
|
%type statement {Statement*}
|
2013-06-01 02:12:49 +02:00
|
|
|
statement(A) ::= T_IF T_LPAREN expr(E) T_RPAREN statement(S).
|
2013-06-01 02:21:59 +02:00
|
|
|
{ A = new IfStatement(E, S); }
|
2013-06-01 03:02:14 +02:00
|
|
|
statement(A) ::= T_RIF T_LPAREN expr(P) T_RPAREN statement(S).
|
|
|
|
|
{ A = new RandomIfStatement(P, S); }
|
2013-06-01 22:04:59 +02:00
|
|
|
statement(A) ::= T_FOR T_LPAREN statement(INIT) expr(COND) T_SEMICOLON statement(STEP) T_RPAREN statement(S).
|
2013-06-01 03:02:14 +02:00
|
|
|
{ A = new ForStatement(INIT, COND, STEP, S); }
|
2013-06-01 22:05:24 +02:00
|
|
|
statement(A) ::= T_RFOR T_LPAREN statement(INIT) expr(P) T_SEMICOLON statement(STEP) T_RPAREN statement(S).
|
2013-06-01 03:33:38 +02:00
|
|
|
{ A = new RandomForStatement(INIT, P, STEP, S); }
|
2013-06-02 08:31:45 +02:00
|
|
|
statement(A) ::= T_RETURN T_SEMICOLON. { A = new ReturnStatement(0); }
|
2013-06-01 02:50:47 +02:00
|
|
|
statement(A) ::= T_RETURN expr(E) T_SEMICOLON. { A = new ReturnStatement(E); }
|
2013-06-01 02:29:23 +02:00
|
|
|
statement(A) ::= T_BEGIN statements(S) T_END. { A = new Scope(S); }
|
2013-06-01 03:02:14 +02:00
|
|
|
statement(A) ::= vardef(V) T_SEMICOLON. { A = V; }
|
2013-06-01 02:50:47 +02:00
|
|
|
statement(A) ::= expr(E) T_SEMICOLON. { A = new ExpressionStatement(E); }
|
2013-05-31 21:04:20 +02:00
|
|
|
|
2013-06-01 01:53:15 +02:00
|
|
|
%type statements {StatementList*}
|
2013-06-01 02:12:49 +02:00
|
|
|
statements(A) ::= . { A = new StatementList(); }
|
|
|
|
|
statements(A) ::= statements(B) statement(C). { B->addStatement(C); A = B; }
|
2013-05-31 21:04:20 +02:00
|
|
|
|
2013-06-01 01:50:24 +02:00
|
|
|
%type expr {Expression*}
|
2013-06-01 15:48:28 +02:00
|
|
|
expr(A) ::= T_IDENTIFIER(ID). { A = new LoadExpression(ID->getText()); }
|
2013-06-01 02:12:49 +02:00
|
|
|
expr(A) ::= T_IDENTIFIER(ID) T_ASSIGN expr(E). { A = new AssignmentExpression(ID->getText(), E); }
|
2013-06-01 17:00:39 +02:00
|
|
|
expr(A) ::= expr(B) T_EQUALS expr(C). { A = new BinOpExpression(B, BinOp::EQUALS, C); }
|
|
|
|
|
expr(A) ::= expr(B) T_LESS expr(C). { A = new BinOpExpression(B, BinOp::LESS, C); }
|
|
|
|
|
expr(A) ::= expr(B) T_PLUS expr(C). { A = new BinOpExpression(B, BinOp::PLUS, C); }
|
|
|
|
|
expr(A) ::= expr(B) T_MINUS expr(C). { A = new BinOpExpression(B, BinOp::MINUS, C); }
|
|
|
|
|
expr(A) ::= expr(B) T_TIMES expr(C). { A = new BinOpExpression(B, BinOp::TIMES, C); }
|
|
|
|
|
expr(A) ::= expr(B) T_DIV expr(C). { A = new BinOpExpression(B, BinOp::DIV, C); }
|
2013-06-01 01:50:24 +02:00
|
|
|
expr(A) ::= T_CINT(I). { A = new ConstantExpression(I->getText()); }
|
2013-06-01 02:12:49 +02:00
|
|
|
expr(A) ::= T_TRUE. { A = new ConstantExpression("true"); }
|
2013-06-01 01:50:24 +02:00
|
|
|
expr(A) ::= T_FALSE. { A = new ConstantExpression("false"); }
|
2013-06-01 02:12:49 +02:00
|
|
|
expr(A) ::= T_IDENTIFIER(ID) T_LPAREN values(V) T_RPAREN.
|
2013-06-01 03:10:16 +02:00
|
|
|
{ A = new FunctionCallExpression(ID->getText(), V); }
|
2013-05-31 21:04:20 +02:00
|
|
|
|
2013-06-01 00:43:30 +02:00
|
|
|
%type vardef {VariableDefinition*}
|
2013-06-01 02:12:49 +02:00
|
|
|
vardef(A) ::= type(T) T_IDENTIFIER(ID). { A = new VariableDefinition(T, ID->getText()); }
|
2013-05-31 21:04:20 +02:00
|
|
|
|
2013-06-01 02:10:29 +02:00
|
|
|
%type values {ValueList*}
|
2013-06-01 02:12:49 +02:00
|
|
|
values(A) ::= . { A = new ValueList(); }
|
2013-06-01 02:10:29 +02:00
|
|
|
values(A) ::= expr(E). { A = new ValueList(); A->addValue(E); }
|
2013-06-01 02:12:49 +02:00
|
|
|
values(A) ::= values(B) T_COMMA expr(E). { B->addValue(E); A = B; }
|
2013-05-31 19:13:55 +02:00
|
|
|
|