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"
|
|
|
|
|
#include "AST/BinOp.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"
|
|
|
|
|
#include "AST/FunctionDefinition.h"
|
2013-06-01 02:21:59 +02:00
|
|
|
#include "AST/IfStatement.h"
|
2013-06-01 00:38:20 +02:00
|
|
|
#include "AST/ParameterList.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-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 02:50:47 +02:00
|
|
|
program ::= fundef(F). { std::cout << F << std::endl; }
|
2013-06-01 02:12:49 +02:00
|
|
|
program ::= expr(E). { std::cout << E << std::endl; }
|
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 00:16:38 +02:00
|
|
|
%type type {Type*}
|
2013-06-01 02:12:49 +02:00
|
|
|
type(A) ::= T_BOOL. { A = new Type("bool"); }
|
|
|
|
|
type(A) ::= T_INT. { A = new Type("int"); }
|
2013-06-01 00:16:38 +02:00
|
|
|
type(A) ::= T_STRING. { A = new Type("string"); }
|
2013-06-01 02:12:49 +02:00
|
|
|
type(A) ::= T_VOID. { A = new 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 02:12:49 +02:00
|
|
|
statement(A) ::= T_RIF T_LPAREN T_CINT(P) T_RPAREN statement(S).
|
2013-06-01 02:21:59 +02:00
|
|
|
{ A = 0; /* P S */ }
|
2013-05-31 21:04:20 +02:00
|
|
|
statement(A) ::= T_FOR T_LPAREN expr(INIT) T_SEMICOLON expr(COND) T_SEMICOLON expr(STEP) T_RPAREN statement(S).
|
2013-06-01 02:21:59 +02:00
|
|
|
{ A = 0; /* INIT COND STEP S */ }
|
2013-05-31 21:04:20 +02:00
|
|
|
statement(A) ::= T_RFOR T_LPAREN expr(INIT) T_SEMICOLON T_CINT(P) T_SEMICOLON expr(STEP) T_RPAREN statement(S).
|
2013-06-01 02:21:59 +02:00
|
|
|
{ A = 0; /* INIT P STEP S */ }
|
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 02:50:47 +02:00
|
|
|
statement(A) ::= vardef(V) T_SEMICOLON. { A = V; /* V */ }
|
|
|
|
|
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 02:12:49 +02:00
|
|
|
expr(A) ::= T_IDENTIFIER(ID) T_ASSIGN expr(E). { A = new AssignmentExpression(ID->getText(), E); }
|
|
|
|
|
expr(A) ::= expr(B) T_EQUALS expr(C). { A = new BinOp(B, "==", C); }
|
|
|
|
|
expr(A) ::= expr(B) T_LESS expr(C). { A = new BinOp(B, "<", C); }
|
|
|
|
|
expr(A) ::= expr(B) T_PLUS expr(C). { A = new BinOp(B, "+", C); }
|
|
|
|
|
expr(A) ::= expr(B) T_MINUS expr(C). { A = new BinOp(B, "-", C); }
|
|
|
|
|
expr(A) ::= expr(B) T_TIMES expr(C). { A = new BinOp(B, "*", C); }
|
|
|
|
|
expr(A) ::= expr(B) T_DIV expr(C). { A = new BinOp(B, "/", 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.
|
|
|
|
|
{ A = 0; /* ID 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
|
|
|
|