aboutsummaryrefslogtreecommitdiffstats
path: root/c_parser/src/c_parser.y
blob: 7d36fc64d1a8f12d11e582a394c26cbd4f838b1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
%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_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 { $$ = $1; }
	| 	STMNT_LIST STMNT { $$ = $2; }

STMNT : DCLRTN { $$ = $1; }

DCLRTN : T_KEYWORD T_IDENTIFIER T_SC { $$ = new Declaration(*$2); }

%%

const Expression *g_root; // Definition of variable (to match declaration earlier)

const Expression *parseAST() {
    g_root = 0;
    yyparse();
    return g_root;
}