Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 107 lines (87 sloc) 3.106 kb
acc8d774 »
2009-09-17 initial commit
1 %{
89a295f5 »
2009-09-18 Move last_token to yylval
2 #include "node.h"
5c13e200 » bhuztez
2010-10-27 now it's llvm 2.8 compatible
3 #include <cstdio>
4 #include <cstdlib>
89a295f5 »
2009-09-18 Move last_token to yylval
5 NBlock *programBlock; /* the top level root node of our final AST */
acc8d774 »
2009-09-17 initial commit
6
89a295f5 »
2009-09-18 Move last_token to yylval
7 extern int yylex();
4ca49749 » Sergiy Dubovik
2011-09-19 Fixed compilation error in main.cpp and added error message argument …
8 void yyerror(const char *s) { std::printf("Error: %s\n", s);std::exit(1); }
acc8d774 »
2009-09-17 initial commit
9 %}
10
24d4da45 »
2009-09-18 Commenting and cleanup
11 /* Represents the many different ways we can access our data */
acc8d774 »
2009-09-17 initial commit
12 %union {
13 Node *node;
14 NBlock *block;
15 NExpression *expr;
16 NStatement *stmt;
17 NIdentifier *ident;
18 NVariableDeclaration *var_decl;
19 std::vector<NVariableDeclaration*> *varvec;
20 std::vector<NExpression*> *exprvec;
89a295f5 »
2009-09-18 Move last_token to yylval
21 std::string *string;
acc8d774 »
2009-09-17 initial commit
22 int token;
23 }
24
24d4da45 »
2009-09-18 Commenting and cleanup
25 /* Define our terminal symbols (tokens). This should
26 match our tokens.l lex file. We also define the node type
27 they represent.
28 */
89a295f5 »
2009-09-18 Move last_token to yylval
29 %token <string> TIDENTIFIER TINTEGER TDOUBLE
acc8d774 »
2009-09-17 initial commit
30 %token <token> TCEQ TCNE TCLT TCLE TCGT TCGE TEQUAL
31 %token <token> TLPAREN TRPAREN TLBRACE TRBRACE TCOMMA TDOT
0a9a056b »
2009-09-17 Add binary operations
32 %token <token> TPLUS TMINUS TMUL TDIV
24d4da45 »
2009-09-18 Commenting and cleanup
33
34 /* Define the type of node our nonterminal symbols represent.
35 The types refer to the %union declaration above. Ex: when
36 we call an ident (defined by union type ident) we are really
37 calling an (NIdentifier*). It makes the compiler happy.
38 */
acc8d774 »
2009-09-17 initial commit
39 %type <ident> ident
40 %type <expr> numeric expr
41 %type <varvec> func_decl_args
42 %type <exprvec> call_args
43 %type <block> program stmts block
44 %type <stmt> stmt var_decl func_decl
45 %type <token> comparison
46
24d4da45 »
2009-09-18 Commenting and cleanup
47 /* Operator precedence for mathematical operators */
0a9a056b »
2009-09-17 Add binary operations
48 %left TPLUS TMINUS
49 %left TMUL TDIV
50
acc8d774 »
2009-09-17 initial commit
51 %start program
52
53 %%
54
55 program : stmts { programBlock = $1; }
56 ;
57
58 stmts : stmt { $$ = new NBlock(); $$->statements.push_back($<stmt>1); }
59 | stmts stmt { $1->statements.push_back($<stmt>2); }
60 ;
61
62 stmt : var_decl | func_decl
89a295f5 »
2009-09-18 Move last_token to yylval
63 | expr { $$ = new NExpressionStatement(*$1); }
acc8d774 »
2009-09-17 initial commit
64 ;
65
66 block : TLBRACE stmts TRBRACE { $$ = $2; }
67 | TLBRACE TRBRACE { $$ = new NBlock(); }
68 ;
69
70 var_decl : ident ident { $$ = new NVariableDeclaration(*$1, *$2); }
71 | ident ident TEQUAL expr { $$ = new NVariableDeclaration(*$1, *$2, $4); }
72 ;
73
74 func_decl : ident ident TLPAREN func_decl_args TRPAREN block
75 { $$ = new NFunctionDeclaration(*$1, *$2, *$4, *$6); delete $4; }
76 ;
77
0a9a056b »
2009-09-17 Add binary operations
78 func_decl_args : /*blank*/ { $$ = new VariableList(); }
79 | var_decl { $$ = new VariableList(); $$->push_back($<var_decl>1); }
acc8d774 »
2009-09-17 initial commit
80 | func_decl_args TCOMMA var_decl { $1->push_back($<var_decl>3); }
81 ;
82
89a295f5 »
2009-09-18 Move last_token to yylval
83 ident : TIDENTIFIER { $$ = new NIdentifier(*$1); delete $1; }
acc8d774 »
2009-09-17 initial commit
84 ;
85
89a295f5 »
2009-09-18 Move last_token to yylval
86 numeric : TINTEGER { $$ = new NInteger(atol($1->c_str())); delete $1; }
87 | TDOUBLE { $$ = new NDouble(atof($1->c_str())); delete $1; }
acc8d774 »
2009-09-17 initial commit
88 ;
89
90 expr : ident TEQUAL expr { $$ = new NAssignment(*$<ident>1, *$3); }
91 | ident TLPAREN call_args TRPAREN { $$ = new NMethodCall(*$1, *$3); delete $3; }
92 | ident { $<ident>$ = $1; }
93 | numeric
2dc19ccd »
2009-09-18 Clean up code
94 | expr comparison expr { $$ = new NBinaryOperator(*$1, $2, *$3); }
0a9a056b »
2009-09-17 Add binary operations
95 | TLPAREN expr TRPAREN { $$ = $2; }
acc8d774 »
2009-09-17 initial commit
96 ;
97
0a9a056b »
2009-09-17 Add binary operations
98 call_args : /*blank*/ { $$ = new ExpressionList(); }
99 | expr { $$ = new ExpressionList(); $$->push_back($1); }
acc8d774 »
2009-09-17 initial commit
100 | call_args TCOMMA expr { $1->push_back($3); }
101 ;
102
2dc19ccd »
2009-09-18 Clean up code
103 comparison : TCEQ | TCNE | TCLT | TCLE | TCGT | TCGE
104 | TPLUS | TMINUS | TMUL | TDIV
0a9a056b »
2009-09-17 Add binary operations
105 ;
acc8d774 »
2009-09-17 initial commit
106
107 %%
Something went wrong with that request. Please try again.