-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
143 lines (122 loc) · 2.46 KB
/
parser.y
File metadata and controls
143 lines (122 loc) · 2.46 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
%{
#include <stdio.h>
#include <stdlib.h>
#include "ast.h"
void yyerror(const char *s);
int yylex(void);
ASTNode* ast_root;
%}
%union {
int intval;
char* strval;
ASTNode* node;
}
%code requires {
typedef struct ASTNode ASTNode;
typedef struct ASTNodeList ASTNodeList;
}
%token <strval> IDENTIFIER
%token <intval> INTEGER
%token PUBLIC CLASS STATIC VOID MAIN STRING INT
%token SYSTEM OUT PRINTLN WHILE
%token LPAREN RPAREN LBRACE RBRACE SEMICOLON ASSIGN PLUS COMMA DOT LBRACKET RBRACKET
%token LT GT LE GE EQ NE
%type <node> program class_decl class_body method_decl stmt_list stmt expr
%left LT GT LE GE EQ NE
%left PLUS
%start program
%%
program:
class_decl
{
ast_root = create_program_node($1);
}
;
class_decl:
PUBLIC CLASS IDENTIFIER LBRACE class_body RBRACE
{
$$ = create_class_node($3, $5);
}
;
class_body:
method_decl
{
$$ = $1;
}
;
method_decl:
PUBLIC STATIC VOID MAIN LPAREN STRING LBRACKET RBRACKET IDENTIFIER RPAREN LBRACE stmt_list RBRACE
{
$$ = create_method_node("main", $12);
}
;
stmt_list:
/* empty */
{
$$ = NULL;
}
| stmt_list stmt
{
$$ = append_statement($1, $2);
}
;
stmt:
INT IDENTIFIER SEMICOLON
{
$$ = create_var_decl_node($2);
}
| IDENTIFIER ASSIGN expr SEMICOLON
{
$$ = create_assign_node($1, $3);
}
| SYSTEM DOT OUT DOT PRINTLN LPAREN IDENTIFIER RPAREN SEMICOLON
{
$$ = create_print_node($7);
}
| WHILE LPAREN expr RPAREN LBRACE stmt_list RBRACE
{
$$ = create_while_node($3, $6);
}
;
expr:
INTEGER
{
$$ = create_int_node($1);
}
| IDENTIFIER
{
$$ = create_var_node($1);
}
| expr PLUS expr
{
$$ = create_binop_node('+', $1, $3);
}
| expr LT expr
{
$$ = create_binop_node('<', $1, $3);
}
| expr GT expr
{
$$ = create_binop_node('>', $1, $3);
}
| expr LE expr
{
$$ = create_binop_node('l', $1, $3); // 'l' for less than or equal
}
| expr GE expr
{
$$ = create_binop_node('g', $1, $3); // 'g' for greater than or equal
}
| expr EQ expr
{
$$ = create_binop_node('e', $1, $3); // 'e' for equals
}
| expr NE expr
{
$$ = create_binop_node('n', $1, $3); // 'n' for not equals
}
;
%%
void yyerror(const char *s) {
fprintf(stderr, "Error: %s\n", s);
}