aboutsummaryrefslogtreecommitdiffstats
path: root/c_parser/src
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-02-17 22:12:38 +0000
committerYann Herklotz <ymherklotz@gmail.com>2017-02-17 22:12:38 +0000
commit744f11aad18ce8d184e69d129dcf636f8be9b302 (patch)
tree1d5fe8bd3be92ee4f01240eb96aa6f82a7bed044 /c_parser/src
parent850f46eeb6de286d4b079373104af87e3aca781c (diff)
downloadCompiler-744f11aad18ce8d184e69d129dcf636f8be9b302.tar.gz
Compiler-744f11aad18ce8d184e69d129dcf636f8be9b302.zip
Creating if statement and added test case for it
Diffstat (limited to 'c_parser/src')
-rw-r--r--c_parser/src/c_lexer.flex8
-rw-r--r--c_parser/src/c_parser.y20
2 files changed, 23 insertions, 5 deletions
diff --git a/c_parser/src/c_lexer.flex b/c_parser/src/c_lexer.flex
index 59a48e7..5723e38 100644
--- a/c_parser/src/c_lexer.flex
+++ b/c_parser/src/c_lexer.flex
@@ -44,11 +44,15 @@ const|volatile { return T_TYPE_QUAL; }
[;] { return T_SC; }
[=] { return T_EQ; }
+[=][=] { return T_EQUALITY; }
[,] { return T_CMA; }
[(] { return T_LRB; }
[)] { return T_RRB; }
-[{] { return T_LCB; }
-[}] { return T_RCB; }
+[{] { return T_LCB; }
+[}] { return T_RCB; }
+
+if { return T_IF; }
+else { return T_ELSE; }
({HEXCONSTANT}|{OCTALCONSTANT}|{DECIMALCONSTANT}){INTEGERSUFFIX}? { return T_INT_CONST; }
diff --git a/c_parser/src/c_parser.y b/c_parser/src/c_parser.y
index d8396a8..9c65dfd 100644
--- a/c_parser/src/c_parser.y
+++ b/c_parser/src/c_parser.y
@@ -20,13 +20,15 @@ void yyerror(const char *);
}
%token T_TYPE_SPEC T_TYPE_QUAL T_STRG_SPEC T_IDENTIFIER
-%token T_SC T_CMA T_EQ T_LRB T_RRB T_LCB T_RCB
+%token T_SC T_CMA T_EQ T_LRB T_RRB T_LCB T_RCB T_EQUALITY
%token T_INT_CONST
+%token T_IF T_ELSE
%type <stmnt> EXT_DEF EXT_DECLARATION
%type <stmnt> FUNC_DEF PARAMETER_LIST PARAMETER PARAM_DECLARATOR
%type <stmnt> DECLARATION_LIST DECLARATION DECLARATION_SPEC DECLARATION_SPEC_T INIT_DECLARATOR INIT_DECLARATOR_LIST DECLARATOR INITIALIZER
-%type <stmnt> STATEMENT_LIST STATEMENT COMPOUND_STATEMENT COMPOUND_STATEMENT_2
+%type <stmnt> STATEMENT_LIST STATEMENT COMPOUND_STATEMENT COMPOUND_STATEMENT_2 SELECTION_STATEMENT
+%type <stmnt> EXPRESSION EQUALITY_EXPRESSION
// %type <number> // T_CONSTANT
%type <string> T_IDENTIFIER //T_OPERATOR
@@ -101,6 +103,7 @@ STATEMENT_LIST : STATEMENT { $$ = new ast_StatementList($1); }
;
STATEMENT : COMPOUND_STATEMENT { $$ = $1; }
+ | SELECTION_STATEMENT { ; }
;
COMPOUND_STATEMENT : T_LCB COMPOUND_STATEMENT_2 { $$ = $2; }
@@ -108,12 +111,23 @@ COMPOUND_STATEMENT : T_LCB COMPOUND_STATEMENT_2 { $$ = $2; }
COMPOUND_STATEMENT_2 : T_RCB { $$ = new ast_CompoundStatement; }
| DECLARATION_LIST T_RCB { $$ = new ast_CompoundStatement($1); }
-| DECLARATION_LIST STATEMENT_LIST T_RCB { $$ = new ast_CompoundStatement($1, $2); }
+ | DECLARATION_LIST STATEMENT_LIST T_RCB { $$ = new ast_CompoundStatement($1, $2); }
| STATEMENT_LIST T_RCB { $$ = new ast_CompoundStatement($1); }
;
+SELECTION_STATEMENT : T_IF T_LRB EXPRESSION T_RRB STATEMENT SELECTION_STATEMENT_2 { ; }
+
+SELECTION_STATEMENT_2 : %empty { ; }
+ | T_ELSE STATEMENT { ; }
+
// Expressions
+EXPRESSION : EQUALITY_EXPRESSION { ; }
+;
+
+EQUALITY_EXPRESSION : T_IDENTIFIER T_EQUALITY T_IDENTIFIER { ; }
+;
+
%%
ast_Top *g_root; // Definition of variable (to match declaration earlier)