aboutsummaryrefslogtreecommitdiffstats
path: root/c_parser/src
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-02-12 20:49:21 +0000
committerYann Herklotz <ymherklotz@gmail.com>2017-02-12 20:49:21 +0000
commit3b0a7ae09d2f9770bfae9f5eed6a6667c9a3c1ca (patch)
treeeece0681cab7486bbb1543197335eab1d29dae46 /c_parser/src
parentbd2b74569393c2a12f39fad6a3ad11eef2a367b3 (diff)
downloadCompiler-3b0a7ae09d2f9770bfae9f5eed6a6667c9a3c1ca.tar.gz
Compiler-3b0a7ae09d2f9770bfae9f5eed6a6667c9a3c1ca.zip
Saving files to make changes
Diffstat (limited to 'c_parser/src')
-rw-r--r--c_parser/src/#c_parser.y#53
-rw-r--r--c_parser/src/c_lexer.flex6
-rw-r--r--c_parser/src/c_parser.y30
-rw-r--r--c_parser/src/parser_main.cpp8
4 files changed, 24 insertions, 73 deletions
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;
}