diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-02-15 12:28:20 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-02-15 12:28:20 +0000 |
commit | 2864f0483c9c7dc8b57184348a1b2a8ab3656ddd (patch) | |
tree | e42a867e821c2f2f9b57af73433e2714702b0641 /c_parser | |
parent | d2377ef22035ed93cbab1ae577e6f17f115891f7 (diff) | |
download | Compiler-2864f0483c9c7dc8b57184348a1b2a8ab3656ddd.tar.gz Compiler-2864f0483c9c7dc8b57184348a1b2a8ab3656ddd.zip |
Finished parser for declarations
Diffstat (limited to 'c_parser')
-rw-r--r-- | c_parser/include/ast_top.hpp | 4 | ||||
-rw-r--r-- | c_parser/src/c_lexer.flex | 8 | ||||
-rw-r--r-- | c_parser/src/c_parser.y | 30 | ||||
-rw-r--r-- | c_parser/src/parser_main.cpp | 2 |
4 files changed, 31 insertions, 13 deletions
diff --git a/c_parser/include/ast_top.hpp b/c_parser/include/ast_top.hpp index cafae3a..ff1cffa 100644 --- a/c_parser/include/ast_top.hpp +++ b/c_parser/include/ast_top.hpp @@ -7,13 +7,13 @@ class ast_Top { public: - void print_vec() { + void print() { for(size_t i = 0; i < ast_vec.size(); ++i) { ast_vec[i]->print(); } } - void push_back(const ast_Base *stmnt) { + void push(const ast_Base *stmnt) { ast_vec.push_back(stmnt); } private: diff --git a/c_parser/src/c_lexer.flex b/c_parser/src/c_lexer.flex index 96f6f26..c2b0d52 100644 --- a/c_parser/src/c_lexer.flex +++ b/c_parser/src/c_lexer.flex @@ -36,11 +36,17 @@ ALL . %% -{KEYWORD} { yylval.string = new std::string(yytext); return T_KEYWORD; } +typedef|extern|static|auto|register { return T_STRG_SPEC; } +void|char|short|int|long|float|double|signed|unsigned { return T_TYPE_SPEC; } +const|volatile { return T_TYPE_QUAL; } {IDENTIFIER} { yylval.string = new std::string(yytext); return T_IDENTIFIER; } ; { return T_SC; } += { return T_EQ; } +, { return T_CMA; } + +({HEXCONSTANT}|{OCTALCONSTANT}|{DECIMALCONSTANT}){INTEGERSUFFIX}? { return T_INT_CONST; } {WHITESPACE} { ; } diff --git a/c_parser/src/c_parser.y b/c_parser/src/c_parser.y index ae80446..038f7dd 100644 --- a/c_parser/src/c_parser.y +++ b/c_parser/src/c_parser.y @@ -19,11 +19,11 @@ void yyerror(const char *); std::string *string; } -%token T_KEYWORD T_IDENTIFIER T_SC //T_CONSTANT T_OPERATOR T_LCBRACKET T_RCBRACKET +%token T_TYPE_SPEC T_TYPE_QUAL T_STRG_SPEC T_IDENTIFIER T_SC T_CMA T_EQ T_INT_CONST -%type <stmnt> STMNT DCLRTN STMNT_LIST // COMP_STMNT STMNT_LIST_STMNT SLCT_STMNT ITR_STMNT JMP_STMNT +%type <stmnt> STMNT DCLRTN DCLRTN_SPEC DCLRTN_SPEC_T INIT_DCLRTR INIT_DCLRTR_LIST DCLRTR INITIALIZER STMNT_LIST // COMP_STMNT STMNT_LIST_STMNT SLCT_STMNT ITR_STMNT JMP_STMNT // %type <number> // T_CONSTANT -%type <string> T_KEYWORD T_IDENTIFIER //T_OPERATOR +%type <string> T_IDENTIFIER //T_OPERATOR %start ROOT @@ -31,12 +31,24 @@ void yyerror(const char *); ROOT : STMNT_LIST { ; } -STMNT_LIST : STMNT { g_root->push_back($1); } - | STMNT_LIST STMNT { g_root->push_back($2); } - -STMNT : DCLRTN { $$ = $1; } - -DCLRTN : T_KEYWORD T_IDENTIFIER T_SC { $$ = new ast_Declaration(*$2); } +STMNT_LIST : STMNT { ; } + | STMNT_LIST STMNT { ; } + | DCLRTN { ; } + +STMNT : DCLRTN { ; } + +DCLRTN : DCLRTN_SPEC INIT_DCLRTR_LIST T_SC { $$ = $2; } +DCLRTN_SPEC : DCLRTN_SPEC_T { ; } + | DCLRTN_SPEC_T DCLRTN_SPEC { ; } +DCLRTN_SPEC_T : T_TYPE_SPEC { ; } + | T_TYPE_QUAL { ; } + | T_STRG_SPEC { ; } +INIT_DCLRTR_LIST : INIT_DCLRTR { g_root->push($1); } + | INIT_DCLRTR_LIST T_CMA INIT_DCLRTR { g_root->push($3); } +INIT_DCLRTR : DCLRTR { ; } + | DCLRTR T_EQ INITIALIZER { ; } +DCLRTR : T_IDENTIFIER {$$ = new ast_Declaration(*$1); } +INITIALIZER : T_INT_CONST { ; } %% diff --git a/c_parser/src/parser_main.cpp b/c_parser/src/parser_main.cpp index 53209e3..02f3b3f 100644 --- a/c_parser/src/parser_main.cpp +++ b/c_parser/src/parser_main.cpp @@ -5,7 +5,7 @@ int main(int argc, char *argv[]) { ast_Top *ast = parseAST(); - ast->print_vec(); + ast->print(); std::cout << std::endl; |