From ac811de4d35d98437ed19bd06ef553c2545755f9 Mon Sep 17 00:00:00 2001 From: hatsusato Date: Sun, 11 Sep 2016 15:45:38 +0900 Subject: [PATCH 1/6] Add statement-list action --- src/parser.y | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/parser.y b/src/parser.y index a655abc5..b82deb5f 100644 --- a/src/parser.y +++ b/src/parser.y @@ -214,12 +214,18 @@ parameter-declaration ; statement-list.opt -: %empty -| statement-list +: %empty { + $$ = NULL; +} +| statement-list { + $$ = $[statement-list]; +} ; statement-list -: statement statement-list.opt +: statement statement-list.opt { + $$ = cons($[statement], $[statement-list.opt]); +} ; statement From bdf553d6367243aecd45050e97d2a65ffbc65601 Mon Sep 17 00:00:00 2001 From: hatsusato Date: Sun, 11 Sep 2016 15:46:25 +0900 Subject: [PATCH 2/6] Add statement action --- src/parser.y | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/parser.y b/src/parser.y index b82deb5f..c32f6032 100644 --- a/src/parser.y +++ b/src/parser.y @@ -229,8 +229,12 @@ statement-list ; statement -: compound-statement -| jump-statement +: compound-statement { + $$ = $[compound-statement]; +} +| jump-statement { + $$ = $[jump-statement]; +} /* : labeled-statement */ /* | compound-statement */ /* | expression-statement */ From 747dc7c34890b567022c32f71da09132feea06bc Mon Sep 17 00:00:00 2001 From: hatsusato Date: Sun, 11 Sep 2016 15:49:21 +0900 Subject: [PATCH 3/6] Add compound-statement action --- src/ast.h | 3 ++- src/parser.y | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ast.h b/src/ast.h index 424a2620..3fbe110c 100644 --- a/src/ast.h +++ b/src/ast.h @@ -2,7 +2,8 @@ #define KMC_C90_COMPILER_AST_H enum AstTag { - AST_IDENTIFIER + AST_IDENTIFIER, + AST_COMPOUND_STATEMENT }; #endif /* KMC_C90_COMPILER_AST_H */ diff --git a/src/parser.y b/src/parser.y index c32f6032..cad78d6c 100644 --- a/src/parser.y +++ b/src/parser.y @@ -244,7 +244,11 @@ statement ; compound-statement -: '{' declaration-statement-list.opt statement-list.opt '}' +: '{' declaration-statement-list.opt statement-list.opt '}' { + $$ = cons(sexpr_make_ast(AST_COMPOUND_STATEMENT), + cons($[declaration-statement-list.opt], + $[statement-list.opt])); +} ; declaration-statement-list.opt From 78871da20901130272ed10d6e77c56ffff56b3e2 Mon Sep 17 00:00:00 2001 From: hatsusato Date: Sun, 11 Sep 2016 15:50:23 +0900 Subject: [PATCH 4/6] Add declaration-statement-list action --- src/parser.y | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/parser.y b/src/parser.y index cad78d6c..e9690422 100644 --- a/src/parser.y +++ b/src/parser.y @@ -252,12 +252,18 @@ compound-statement ; declaration-statement-list.opt -: %empty -| declaration-statement-list +: %empty { + $$ = NULL; +} +| declaration-statement-list { + $$ = $[declaration-statement-list]; +} ; declaration-statement-list -: declaration-statement declaration-statement-list.opt +: declaration-statement declaration-statement-list.opt { + $$ = cons($[declaration-statement], $[declaration-statement-list.opt]); +} ; declaration-statement From 02ba7033d7a9ab73e2b9636901d2b378d20f57af Mon Sep 17 00:00:00 2001 From: hatsusato Date: Sun, 11 Sep 2016 15:51:27 +0900 Subject: [PATCH 5/6] Add declaration-statement action --- src/parser.y | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/parser.y b/src/parser.y index e9690422..46cb8fa9 100644 --- a/src/parser.y +++ b/src/parser.y @@ -267,8 +267,12 @@ declaration-statement-list ; declaration-statement -: storage-class-specifier.opt init-declaration -| typedef-specifier declaration +: storage-class-specifier.opt init-declaration { + $$ = cons($[storage-class-specifier.opt], $[init-declaration]); +} +| typedef-specifier declaration { + $$ = cons($[typedef-specifier], $[declaration]); +} ; jump-statement From 23599db51a2f6fc85c4a32d158430b3acaecb5ee Mon Sep 17 00:00:00 2001 From: hatsusato Date: Sun, 11 Sep 2016 15:54:53 +0900 Subject: [PATCH 6/6] Add jump-statement action --- src/ast.h | 6 +++++- src/parser.y | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/ast.h b/src/ast.h index 3fbe110c..cb2b003f 100644 --- a/src/ast.h +++ b/src/ast.h @@ -3,7 +3,11 @@ enum AstTag { AST_IDENTIFIER, - AST_COMPOUND_STATEMENT + AST_COMPOUND_STATEMENT, + AST_GOTO_STATEMENT, + AST_CONTINUE_STATEMENT, + AST_BREAK_STATEMENT, + AST_RETURN_STATEMENT }; #endif /* KMC_C90_COMPILER_AST_H */ diff --git a/src/parser.y b/src/parser.y index 46cb8fa9..a85d28bc 100644 --- a/src/parser.y +++ b/src/parser.y @@ -276,10 +276,18 @@ declaration-statement ; jump-statement -: GOTO identifier ';' -| CONTINUE ';' -| BREAK ';' -| RETURN expression.opt ';' +: GOTO identifier ';' { + $$ = cons(sexpr_make_ast(AST_GOTO_STATEMENT), $[identifier]); +} +| CONTINUE ';' { + $$ = cons(sexpr_make_ast(AST_CONTINUE_STATEMENT), NULL); +} +| BREAK ';' { + $$ = cons(sexpr_make_ast(AST_BREAK_STATEMENT), NULL); +} +| RETURN expression.opt ';' { + $$ = cons(sexpr_make_ast(AST_RETURN_STATEMENT), $[expression.opt]); +} ; translation-unit.opt