2014-11-23 19:04:25 +01:00
|
|
|
%{
|
|
|
|
#include "Type.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2014-11-23 22:46:50 +01:00
|
|
|
#define YYERROR_VERBOSE
|
|
|
|
|
2014-11-23 19:04:25 +01:00
|
|
|
extern void emit(const char *, ...);
|
|
|
|
%}
|
|
|
|
|
|
|
|
%union {
|
|
|
|
Type typeval;
|
|
|
|
int intval;
|
|
|
|
double floatval;
|
|
|
|
char *strval;
|
|
|
|
}
|
|
|
|
|
|
|
|
%token MAIN
|
|
|
|
%token RETURN
|
|
|
|
%token ARTICLE TYPENAME STAR
|
|
|
|
%token VARDECL ASSIGN
|
|
|
|
%token PRINT INPUT
|
|
|
|
%token ASSERT_BEGIN ASSERT_END
|
|
|
|
%token LOOP_BEGIN LOOP_CONDITION
|
2014-11-23 19:24:10 +01:00
|
|
|
%token BRANCH_CONDITION BRANCH_BEGIN BRANCH_ELSE BRANCH_END CASE_END
|
2014-11-23 22:33:50 +01:00
|
|
|
%token COLON COMMA
|
2014-11-23 19:04:25 +01:00
|
|
|
%token FUNDECL PARAMS FUNCALL
|
|
|
|
%token ABORT
|
|
|
|
%token ID NUMBER FLOAT
|
|
|
|
|
|
|
|
%left OP_LT OP_GT OP_LTE OP_GTE
|
|
|
|
%left OP_PLUS OP_MINUS
|
|
|
|
%left OP_TIMES OP_DIV
|
|
|
|
%left OP_SHL OP_SHR
|
|
|
|
|
|
|
|
%nonassoc LOWER_THAN_ELSE
|
|
|
|
%nonassoc BRANCH_ELSE
|
|
|
|
|
|
|
|
%start program
|
|
|
|
|
|
|
|
%type<intval> NUMBER;
|
|
|
|
%type<floatval> FLOAT;
|
|
|
|
%type<strval> ID;
|
|
|
|
%type<typeval> TYPENAME;
|
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
program:
|
2014-11-23 22:43:42 +01:00
|
|
|
fun_decls main fun_decls | /* epsilon */
|
2014-11-23 19:04:25 +01:00
|
|
|
;
|
|
|
|
fun_decls:
|
|
|
|
/* epsilon */ | fun_decls fun_decl
|
|
|
|
;
|
|
|
|
fun_decl:
|
2014-11-23 22:43:51 +01:00
|
|
|
FUNDECL ID args COMMA statements
|
2014-11-23 19:04:25 +01:00
|
|
|
;
|
|
|
|
args:
|
|
|
|
/* epsilon */ | PARAMS arglist
|
|
|
|
;
|
|
|
|
arglist:
|
2014-11-23 23:45:29 +01:00
|
|
|
variable | variable arglist
|
2014-11-23 19:04:25 +01:00
|
|
|
;
|
|
|
|
main:
|
2014-11-23 20:45:44 +01:00
|
|
|
MAIN statements
|
2014-11-23 19:04:25 +01:00
|
|
|
;
|
|
|
|
statements:
|
2014-11-23 22:32:41 +01:00
|
|
|
/* epsilon */ | statements statement COMMA | statements assert_stmt
|
2014-11-23 20:48:23 +01:00
|
|
|
;
|
2014-11-23 19:04:25 +01:00
|
|
|
statement:
|
|
|
|
var_decl | assign_stmt | print_stmt | input_stmt | return_stmt |
|
2014-11-23 22:07:43 +01:00
|
|
|
loop_stmt | branch_stmt | fun_call | abort_stmt |
|
2014-11-23 19:29:48 +01:00
|
|
|
/* epsilon */
|
2014-11-23 19:04:25 +01:00
|
|
|
;
|
|
|
|
var_decl:
|
2014-11-23 20:47:18 +01:00
|
|
|
VARDECL variable COMMA pointer TYPENAME var_init
|
2014-11-23 19:04:25 +01:00
|
|
|
;
|
|
|
|
pointer:
|
|
|
|
/* epsilon */ | STAR
|
|
|
|
;
|
|
|
|
var_init:
|
2014-11-23 22:32:41 +01:00
|
|
|
/* epsilon */ | ASSIGN expression
|
2014-11-23 19:04:25 +01:00
|
|
|
;
|
|
|
|
numeric:
|
|
|
|
NUMBER | FLOAT
|
|
|
|
;
|
|
|
|
variable:
|
2014-11-23 20:46:58 +01:00
|
|
|
ID | ARTICLE ID | ARTICLE STAR ID | STAR ID
|
2014-11-23 19:04:25 +01:00
|
|
|
;
|
|
|
|
assign_stmt:
|
2014-11-23 20:47:52 +01:00
|
|
|
variable ASSIGN expression
|
2014-11-23 19:04:25 +01:00
|
|
|
;
|
|
|
|
print_stmt:
|
|
|
|
expression PRINT
|
|
|
|
;
|
|
|
|
input_stmt:
|
|
|
|
INPUT variable
|
|
|
|
;
|
|
|
|
return_stmt:
|
2014-11-23 19:12:48 +01:00
|
|
|
RETURN | RETURN expression
|
2014-11-23 19:04:25 +01:00
|
|
|
;
|
|
|
|
loop_stmt:
|
|
|
|
LOOP_BEGIN statements LOOP_CONDITION expression
|
|
|
|
;
|
|
|
|
branch_stmt:
|
|
|
|
BRANCH_CONDITION variable BRANCH_BEGIN
|
|
|
|
branch_body
|
|
|
|
BRANCH_END
|
|
|
|
;
|
|
|
|
branch_body:
|
|
|
|
cases %prec LOWER_THAN_ELSE |
|
2014-11-23 20:45:44 +01:00
|
|
|
cases BRANCH_ELSE COLON statements
|
2014-11-23 19:04:25 +01:00
|
|
|
;
|
|
|
|
cases:
|
2014-11-24 10:50:10 +01:00
|
|
|
case_stmt | case_stmt CASE_END cases
|
2014-11-23 19:04:25 +01:00
|
|
|
;
|
|
|
|
case_stmt:
|
2014-11-24 10:50:10 +01:00
|
|
|
semi_expression COLON statements
|
2014-11-23 19:04:25 +01:00
|
|
|
;
|
|
|
|
fun_call:
|
2014-11-23 19:21:12 +01:00
|
|
|
FUNCALL ID args
|
2014-11-23 19:04:25 +01:00
|
|
|
;
|
|
|
|
abort_stmt:
|
2014-11-23 19:20:10 +01:00
|
|
|
ABORT
|
2014-11-23 19:04:25 +01:00
|
|
|
;
|
|
|
|
assert_stmt:
|
2014-11-23 19:20:10 +01:00
|
|
|
ASSERT_BEGIN expression ASSERT_END
|
2014-11-23 19:04:25 +01:00
|
|
|
;
|
|
|
|
expression:
|
2014-11-24 00:14:27 +01:00
|
|
|
simple_expression |
|
2014-11-23 19:04:25 +01:00
|
|
|
expression OP_LT expression |
|
|
|
|
expression OP_GT expression |
|
|
|
|
expression OP_LTE expression |
|
|
|
|
expression OP_GTE expression |
|
|
|
|
expression OP_PLUS expression |
|
|
|
|
expression OP_MINUS expression |
|
|
|
|
expression OP_TIMES expression |
|
|
|
|
expression OP_DIV expression |
|
|
|
|
expression OP_SHL expression |
|
|
|
|
expression OP_SHR expression
|
|
|
|
;
|
2014-11-23 20:47:39 +01:00
|
|
|
semi_expression:
|
2014-11-24 00:14:27 +01:00
|
|
|
simple_expression |
|
2014-11-23 20:47:39 +01:00
|
|
|
OP_LT expression |
|
|
|
|
OP_GT expression |
|
|
|
|
OP_LTE expression |
|
|
|
|
OP_GTE expression |
|
|
|
|
OP_PLUS expression |
|
|
|
|
OP_MINUS expression |
|
|
|
|
OP_TIMES expression |
|
|
|
|
OP_DIV expression |
|
|
|
|
OP_SHL expression |
|
|
|
|
OP_SHR expression
|
|
|
|
;
|
2014-11-24 00:14:27 +01:00
|
|
|
simple_expression:
|
|
|
|
fun_call |
|
|
|
|
numeric |
|
|
|
|
variable
|
|
|
|
;
|
2014-11-23 19:04:25 +01:00
|
|
|
%%
|
|
|
|
|