From 3b0a7ae09d2f9770bfae9f5eed6a6667c9a3c1ca Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sun, 12 Feb 2017 20:49:21 +0000 Subject: Saving files to make changes --- c_parser/src/#c_parser.y# | 53 -------------------------------------------- c_parser/src/c_lexer.flex | 6 +++-- c_parser/src/c_parser.y | 30 +++++++++++-------------- c_parser/src/parser_main.cpp | 8 ++++++- 4 files changed, 24 insertions(+), 73 deletions(-) delete mode 100644 c_parser/src/#c_parser.y# (limited to 'c_parser/src') diff --git a/c_parser/src/#c_parser.y# b/c_parser/src/#c_parser.y# deleted file mode 100644 index 75bfead..0000000 --- a/c_parser/src/#c_parser.y# +++ /dev/null @@ -1,53 +0,0 @@ -%code requires{ - -#include "ast.hpp" -extern const Expression *g_root; // A way of getting the AST out - -//! This is to fix problems when generating C++ -// We are declaring the functions provided by Flex, so -// that Bison generated code can call them. -int yylex(void); -void yyerror(const char *); - -} - -// Represents the value associated with any kind of -// AST node. -%union{ - const Expression *expr; - double number; - std::string *string; -} - -%token T_KEYWORD T_IDENTIFIER //T_CONSTANT T_OPERATOR T_LCBRACKET T_RCBRACKET - -%type STMNT_LIST STMNT COMP_STMNT EXPR_STMNT SLCT_STMNT ITR_STMNT JMP_STMNT -%type T_CONSTANT -%type T_KEYWORD T_IDENTIFIER T_OPERATOR - -%start ROOT - -%% - -ROOT : STMNT_LIST { g_root = $1; } - -STMNT_LIST : STMNT - | STMNT_LIST STMNT - -STMNT : COMP_STMNT - | EXPR_STMNT - | SLCT_STMNT - | ITR_STMNT - | JMP_STMNT - -COMP_STMNT : STMNT_LIST - -%% - -const Expression *g_root; // Definition of variable (to match declaration earlier) - -const Expression *parseAST() { - g_root = 0; - yyparse(); - return g_root; -} diff --git a/c_parser/src/c_lexer.flex b/c_parser/src/c_lexer.flex index 1c10345..49997ed 100644 --- a/c_parser/src/c_lexer.flex +++ b/c_parser/src/c_lexer.flex @@ -36,9 +36,11 @@ ALL . %% -{KEYWORD} { yylval.string = new std::string(yytext); return T_KEYWORD; } +{KEYWORD} { yylval.string = new std::string(yytext); return T_KEYWORD; } -{IDENTIFIER} { yylval.string = new std::string(yytext); return T_IDENTIFIER; } +{IDENTIFIER} { yylval.string = new std::string(yytext); return T_IDENTIFIER; } + +; { return T_SC; } {OPERATOR} { yylval.string = new std::string(yytext); return T_OPERATOR; } diff --git a/c_parser/src/c_parser.y b/c_parser/src/c_parser.y index 25659b5..7d36fc6 100644 --- a/c_parser/src/c_parser.y +++ b/c_parser/src/c_parser.y @@ -15,32 +15,28 @@ void yyerror(const char *); // AST node. %union{ const Expression *expr; - double number; + // double number; std::string *string; } - -%token T_KEYWORD T_IDENTIFIER T_CONSTANT T_OPERATOR T_LCBRACKET T_RCBRACKET - -%type STMNT_LIST STMNT COMP_STMNT EXPR_STMNT SLCT_STMNT ITR_STMNT JMP_STMNT -%type T_CONSTANT -%type T_KEYWORD T_IDENTIFIER T_OPERATOR - + +%token T_KEYWORD T_IDENTIFIER T_SC //T_CONSTANT T_OPERATOR T_LCBRACKET T_RCBRACKET + +%type STMNT_LIST STMNT DCLRTN // COMP_STMNT EXPR_STMNT SLCT_STMNT ITR_STMNT JMP_STMNT +// %type // T_CONSTANT +%type T_KEYWORD T_IDENTIFIER //T_OPERATOR + %start ROOT - + %% ROOT : STMNT_LIST { g_root = $1; } -STMNT_LIST : STMNT - | STMNT_LIST STMNT +STMNT_LIST : STMNT { $$ = $1; } + | STMNT_LIST STMNT { $$ = $2; } -STMNT : COMP_STMNT - | EXPR_STMNT - | SLCT_STMNT - | ITR_STMNT - | JMP_STMNT +STMNT : DCLRTN { $$ = $1; } -COMP_STMNT : STMNT_LIST +DCLRTN : T_KEYWORD T_IDENTIFIER T_SC { $$ = new Declaration(*$2); } %% diff --git a/c_parser/src/parser_main.cpp b/c_parser/src/parser_main.cpp index 8bf67bf..fe80cb7 100644 --- a/c_parser/src/parser_main.cpp +++ b/c_parser/src/parser_main.cpp @@ -1,5 +1,11 @@ #include "ast.hpp" +#include + int main(int argc, char *argv[]) { - return 0; + + const Expression *ast = parseAST(); + ast->print(); + + return 0; } -- cgit