This repository has been archived by the owner on Aug 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flex.y
137 lines (104 loc) · 2.35 KB
/
flex.y
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
%{
#include <stdio.h>
#include <stdlib.h>
void yyerror(char * string);
int symbols[100];
int valueOfSymbol(char symbol);
void updateSymbolWithValue(int value, char symbol);
%}
%union {
int integer;
char* text;
}
%start program
%token if_t else_t for_t read_t print_t range_t int_t string_t
%token assign semicolon open_brackets close_brackets colon
%token open_special_brackets close_special_brackets
%token less greater less_or_equal greater_or_equal equal not_equal
%token plus minus division multiply
%token quote next
%token identifier
%token constant
%token <integer> INTEGER
%token <text> STRING
%%
program :
| block program
;
block : declaration
| statement
;
statement : assignment
| control
| io_statement
;
assignment : type identifier assign expression semicolon
;
io_statement : read_t open_brackets identifier close_brackets semicolon
| print_t open_brackets expression close_brackets semicolon
;
control : conditional
| loop
;
conditional : if_t open_brackets expression close_brackets
open_special_brackets block close_special_brackets
;
loop : for_t open_brackets type identifier colon range close_brackets
open_special_brackets block close_special_brackets
;
range : identifier
| range_t open_brackets constant next constant close_brackets
;
declaration : type identifier assign expression
| type identifier
;
type : int_t
| string_t
;
expression : sign_atom
| expression operation atom
| open_brackets expression close_brackets
;
sign_atom : atom
| minus atom
;
atom : identifier
| constant
operation : low_operation
| high_operation
;
low_operation : plus
| minus
;
high_operation : division
| multiply
| relation
;
relation : equal
| not_equal
| greater_or_equal
| less_or_equal
| greater
| less
;
%%
int getSymbolIndex(char symbol) {
return (islower(symbol))?
symbol - 'a' + 26 :
symbol - 'A';
}
int valueOfSymbol(char symbol) {
return symbols[getSymbolIndex(symbol)];
}
void updateSymbolWithValue(int value, char symbol) {
symbols[getSymbolIndex(symbol)] = value;
}
void yyerror(char * string) {
printf("%s\n", string);
}
int main(void) {
for(int index = 0; index < 100; index++) {
symbols[index] = 0;
}
return yyparse();
}