Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commitfd31939b2f94f4d64a5a6d5d7cb84b925b778166 (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-11-11 16:42:57 +0200
Committer
 Syndamia <kamen@syndamia.com>
2023-11-11 16:42:57 +0200
Files3cd9fcdb92fa26f6dd839f3c04561bf53d6d4827
uni-projects-fd31939b2f94f4d64a5a6d5d7cb84b925b778166.tar
uni-projects-fd31939b2f94f4d64a5a6d5d7cb84b925b778166.tar.gz
uni-projects-fd31939b2f94f4d64a5a6d5d7cb84b925b778166.zip
Parent864e98585b287d68c1f8863850124adfdba7a5b8 (diff)
[CI/PA2] Added an almost working version
1 files changed, 37 insertions, 7 deletions
-rw-r--r--Year 3/Compilers, Introduction/coolc-spim/my-code/coursework/pa2/cool.y44
diff --git a/Year 3/Compilers, Introduction/coolc-spim/my-code/coursework/pa2/cool.y b/Year 3/Compilers, Introduction/coolc-spim/my-code/coursework/pa2/cool.y
index c5a4d7a..67dc206 100644
--- a/Year 3/Compilers, Introduction/coolc-spim/my-code/coursework/pa2/cool.y
+++ b/Year 3/Compilers, Introduction/coolc-spim/my-code/coursework/pa2/cool.y
@@ -125,7 +125,7 @@
/* DON'T CHANGE ANYTHING ABOVE THIS LINE, OR YOUR PARSER WONT WORK */
/**************************************************************************/
- /* %define parse.error detailed */
+ %define parse.error detailed
/* Complete the nonterminal list below, giving a type for the semantic
value of each non terminal. (See section 3.6 in the bison
@@ -141,27 +141,40 @@
%type <formal> formal
%type <formals> formal_list formal_list_nonempty
%type <expression> expression dispatch static_dispatch let lets math_expression bool_expression
- %type <expressions> expression_list expression_list_nonempty expression_block
+ %type <expressions> expression_list expression_list_nonempty expression_block expression_block_list
%type <case_> case_
%type <cases> case_list
/* Boolean boolean; */
/* Symbol symbol; */
/* Precedence declarations go here. */
-
+ %right ASSIGN
+ %left NOT
+ %nonassoc '<' '=' LE
+ %left '+' '-'
+ %left '*' '/'
+ %left ISVOID
+ %left '~'
+ %left '@'
+ %left '.'
+
%%
program: class_list { @$ = @1; ast_root = program($1); }
;
// [[class;]]+
- class_list: class ';' { $$ = single_Classes($1); parse_results = $$; }
+ class_list: class ';' { $$ = single_Classes($1); parse_results = $$; }
+ | error ';' class ';' { $$ = single_Classes($3); }
| class_list class ';' { $$ = append_Classes($1, single_Classes($2)); parse_results = $$; }
+ | class_list error ';' class ';' { $$ = append_Classes($1, single_Classes($4)); }
;
// class TYPE [inherits TYPE] { [[feature;]]* }
class: CLASS TYPEID '{' feature_list '}' { $$ = class_($2, idtable.add_string("Object"), $4, stringtable.add_string(curr_filename)); }
| CLASS TYPEID INHERITS TYPEID '{' feature_list '}' { $$ = class_($2, $4, $6, stringtable.add_string(curr_filename)); }
+ | error '{' feature_list '}' {}
+ | error '{' error '}' {}
;
// [[feature;]]*
@@ -170,7 +183,9 @@
;
feature_list_nonempty: feature ';' { $$ = single_Features($1); }
+ | error ';' { $$ = nil_Features(); }
| feature_list feature ';' { $$ = append_Features($1, single_Features($2)); }
+ | feature_list error ';' { $$ = $1; }
;
// ID([formal [[, formal]]*]): TYPE { expr }
@@ -182,10 +197,14 @@
// ID : TYPE [ <- expr ]
attribute: OBJECTID ':' TYPEID { $$ = attr($1, $3, no_expr()); }
| OBJECTID ':' TYPEID ASSIGN expression { $$ = attr($1, $3, $5); }
+ | error expression {}
;
// ID([formal [[, formal]]*]): TYPE { expr }
method: OBJECTID '(' formal_list ')' ':' TYPEID '{' expression '}' { $$ = method($1, $3, $6, $8); }
+ | OBJECTID '(' formal_list ')' ':' TYPEID '{' error '}' {}
+ | OBJECTID '(' error ')' ':' TYPEID '{' expression '}' {}
+ | OBJECTID '(' error ')' ':' TYPEID '{' error '}' {}
;
// [formal [[, formal]]*]
@@ -212,7 +231,7 @@
// while expr loop expr pool
| WHILE expression LOOP expression POOL { $$ = loop($2, $4); }
// { [[expr;]]+ }
- | '{' expression_block '}' { $$ = block($2); }
+ | expression_block { $$ = block($1); }
// let ID : TYPE [<- expr] [[, ID : TYPE [<- expr]]]* in expr
| let
// case expr of [[ID : TYPE => expr;]]+ esac
@@ -250,9 +269,12 @@
// | ID([expr [[, expr]]*])
dispatch: expression '.' OBJECTID '(' expression_list ')' { $$ = dispatch($1, $3, $5); }
| OBJECTID '(' expression_list ')' { $$ = dispatch(object(idtable.add_string("self")), $1, $3); }
+ | OBJECTID '(' error ')' {}
+ ;
// expr@TYPE.ID([expr [[,expr]]*])
static_dispatch: expression '@' TYPEID '.' OBJECTID '(' expression_list ')' { $$ = static_dispatch($1, $3, $5, $7); }
+ | expression '@' TYPEID '.' OBJECTID '(' error ')' {}
;
// [expr [[,expr]]*]
@@ -266,20 +288,28 @@
;
// [[expr;]]+
- expression_block: expression ';' { $$ = single_Expressions($1); }
- | expression_block expression ';' { $$ = append_Expressions($1, single_Expressions($2)); }
+ expression_block: '{' expression_block_list '}' { $$ = $2; }
+ | '{' error '}' {}
;
+ expression_block_list: expression ';' { $$ = single_Expressions($1); }
+ | error ';' { $$ = nil_Expressions(); }
+ | expression_block_list expression ';' { $$ = append_Expressions($1, single_Expressions($2)); }
+ | expression_block_list error ';' { $$ = $1; }
+ ;
+
// let ID : TYPE [<- expr] [[, ID : TYPE [<- expr]]]* in expr
// Special handling, since "let A:B, C:D in x" has to be transformed into "let A:B in let C:D in x"
let: LET OBJECTID ':' TYPEID IN expression { $$ = let($2, $4, no_expr(), $6); }
| LET OBJECTID ':' TYPEID ASSIGN expression IN expression { $$ = let($2, $4, $6, $8); }
| LET OBJECTID ':' TYPEID lets { $$ = let($2, $4, no_expr(), $5); }
| LET OBJECTID ':' TYPEID ASSIGN expression lets { $$ = let($2, $4, $6, $7); }
+ | LET OBJECTID ':' TYPEID ASSIGN error lets { $$ = $7; }
;
lets: ',' OBJECTID ':' TYPEID lets { $$ = let($2, $4, no_expr(), $5); }
| ',' OBJECTID ':' TYPEID ASSIGN expression lets { $$ = let($2, $4, $6, $7); }
+ | ',' OBJECTID ':' TYPEID ASSIGN error lets { $$ = $7; }
| ',' OBJECTID ':' TYPEID IN expression { $$ = let($2, $4, no_expr(), $6); }
| ',' OBJECTID ':' TYPEID ASSIGN expression IN expression { $$ = let($2, $4, $6, $8); }
;