diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-02-12 20:49:21 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-02-12 20:49:21 +0000 |
commit | 3b0a7ae09d2f9770bfae9f5eed6a6667c9a3c1ca (patch) | |
tree | eece0681cab7486bbb1543197335eab1d29dae46 /c_parser | |
parent | bd2b74569393c2a12f39fad6a3ad11eef2a367b3 (diff) | |
download | Compiler-3b0a7ae09d2f9770bfae9f5eed6a6667c9a3c1ca.tar.gz Compiler-3b0a7ae09d2f9770bfae9f5eed6a6667c9a3c1ca.zip |
Saving files to make changes
Diffstat (limited to 'c_parser')
-rw-r--r-- | c_parser/include/ast.hpp | 3 | ||||
-rw-r--r-- | c_parser/include/ast_declaration.hpp | 17 | ||||
-rw-r--r-- | c_parser/include/ast_declaration.hpp~ | 11 | ||||
-rw-r--r-- | c_parser/include/ast_expression.hpp | 14 | ||||
-rw-r--r-- | c_parser/include/ast_expression.hpp~ | 11 | ||||
-rw-r--r-- | c_parser/src/#c_parser.y# | 53 | ||||
-rw-r--r-- | c_parser/src/c_lexer.flex | 6 | ||||
-rw-r--r-- | c_parser/src/c_parser.y | 30 | ||||
-rw-r--r-- | c_parser/src/parser_main.cpp | 8 |
9 files changed, 80 insertions, 73 deletions
diff --git a/c_parser/include/ast.hpp b/c_parser/include/ast.hpp index 05ed1e4..834ad44 100644 --- a/c_parser/include/ast.hpp +++ b/c_parser/include/ast.hpp @@ -1,6 +1,9 @@ #ifndef AST_HPP #define AST_HPP +#include "ast_expression.hpp" +#include "ast_declaration.hpp" +extern const Expression *parseAST(); #endif diff --git a/c_parser/include/ast_declaration.hpp b/c_parser/include/ast_declaration.hpp new file mode 100644 index 0000000..cce68b6 --- /dev/null +++ b/c_parser/include/ast_declaration.hpp @@ -0,0 +1,17 @@ +#ifndef AST_DECLARATION_HPP +#define AST_DECLARATION_HPP + +#include "ast.hpp" + +class Declaration : public Expression { +private: + const std::string id; +public: + Declaration(const std::string& _id) : id(_id) {} + + virtual void print() const override { + std::cout << id; + } +}; + +#endif diff --git a/c_parser/include/ast_declaration.hpp~ b/c_parser/include/ast_declaration.hpp~ new file mode 100644 index 0000000..ba786fc --- /dev/null +++ b/c_parser/include/ast_declaration.hpp~ @@ -0,0 +1,11 @@ +#ifndef AST_DECLARATION_HPP +#define AST_DECLARATION_HPP + +#include "ast.hpp" + +class Declaration : public Expression { +private: + const +}; + +#endif diff --git a/c_parser/include/ast_expression.hpp b/c_parser/include/ast_expression.hpp new file mode 100644 index 0000000..493781e --- /dev/null +++ b/c_parser/include/ast_expression.hpp @@ -0,0 +1,14 @@ +#ifndef AST_EXPRESSION_HPP +#define AST_EXPRESSION_HPP + +#include <string> +#include <iostream> + +class Expression { +public: + virtual ~Expression() {} + + virtual void print() const = 0; +}; + +#endif diff --git a/c_parser/include/ast_expression.hpp~ b/c_parser/include/ast_expression.hpp~ new file mode 100644 index 0000000..7bd9814 --- /dev/null +++ b/c_parser/include/ast_expression.hpp~ @@ -0,0 +1,11 @@ +#ifndef AST_EXPRESSION_HPP +#define AST_EXPRESSION_HPP + +class Expression { +public: + virtual ~Expression() {} + + virtual void print() const = 0; +}; + +#endif 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 <expr> STMNT_LIST STMNT COMP_STMNT EXPR_STMNT SLCT_STMNT ITR_STMNT JMP_STMNT -%type <number> T_CONSTANT -%type <string> 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 <expr> STMNT_LIST STMNT COMP_STMNT EXPR_STMNT SLCT_STMNT ITR_STMNT JMP_STMNT -%type <number> T_CONSTANT -%type <string> T_KEYWORD T_IDENTIFIER T_OPERATOR - + +%token T_KEYWORD T_IDENTIFIER T_SC //T_CONSTANT T_OPERATOR T_LCBRACKET T_RCBRACKET + +%type <expr> STMNT_LIST STMNT DCLRTN // COMP_STMNT EXPR_STMNT SLCT_STMNT ITR_STMNT JMP_STMNT +// %type <number> // T_CONSTANT +%type <string> 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 <iostream> + int main(int argc, char *argv[]) { - return 0; + + const Expression *ast = parseAST(); + ast->print(); + + return 0; } |