aboutsummaryrefslogtreecommitdiffstats
path: root/c_parser
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
parent850f46eeb6de286d4b079373104af87e3aca781c (diff)
downloadCompiler-744f11aad18ce8d184e69d129dcf636f8be9b302.tar.gz
Compiler-744f11aad18ce8d184e69d129dcf636f8be9b302.zip
Creating if statement and added test case for it
Diffstat (limited to 'c_parser')
-rw-r--r--c_parser/src/c_lexer.flex8
-rw-r--r--c_parser/src/c_parser.y20
-rw-r--r--c_parser/test/in/07.c3
-rw-r--r--c_parser/test/out/07.diff.txt7
-rw-r--r--c_parser/test/out/07.stderr.txt1
-rw-r--r--c_parser/test/out/07.stdout.xml0
-rw-r--r--c_parser/test/ref/07.stdout.xml6
7 files changed, 40 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)
diff --git a/c_parser/test/in/07.c b/c_parser/test/in/07.c
new file mode 100644
index 0000000..baa3ba5
--- /dev/null
+++ b/c_parser/test/in/07.c
@@ -0,0 +1,3 @@
+if(x == y) {
+ int z;
+}
diff --git a/c_parser/test/out/07.diff.txt b/c_parser/test/out/07.diff.txt
new file mode 100644
index 0000000..292a596
--- /dev/null
+++ b/c_parser/test/out/07.diff.txt
@@ -0,0 +1,7 @@
+1,6d0
+< <?xml version="1.0"?>
+< <Program>
+< <Scope>
+< <Variable id="z" />
+< </Scope>
+< </Program>
diff --git a/c_parser/test/out/07.stderr.txt b/c_parser/test/out/07.stderr.txt
new file mode 100644
index 0000000..6200509
--- /dev/null
+++ b/c_parser/test/out/07.stderr.txt
@@ -0,0 +1 @@
+Parse error : syntax error
diff --git a/c_parser/test/out/07.stdout.xml b/c_parser/test/out/07.stdout.xml
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/c_parser/test/out/07.stdout.xml
diff --git a/c_parser/test/ref/07.stdout.xml b/c_parser/test/ref/07.stdout.xml
new file mode 100644
index 0000000..baedd20
--- /dev/null
+++ b/c_parser/test/ref/07.stdout.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0"?>
+<Program>
+<Scope>
+<Variable id="z" />
+</Scope>
+</Program>