-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.mly
69 lines (53 loc) · 1.14 KB
/
parser.mly
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
%{
open Codegen
let depth = ref 0
let depth_incr f = incr depth; f !depth
let depth_reset = depth := 0
let stack = ref (Stack.create ())
let () = Stack.push "END2" !stack
let push_var var = Stack.push var !stack
let pop_var () = Stack.pop !stack
%}
%token BEGIN END EOF
%token <string> ID
%token <string> STR
%token <string> REST
%token <int> LITERAL
%token GET PRINT EXTRACT
%token LEFTPAREN RIGHTPAREN
%token ASSIGN PIPE
%start program
%type <unit> program
%%
program:
| begin_stmt statements end_stmt EOF { raise End_of_file }
;
begin_stmt:
| BEGIN { generate_begin () }
;
end_stmt:
| END { generate_end () }
;
statements:
| { }
| statement statements { }
| statement pipe_statements { finish_pipe (pop_var ()) }
;
statement:
| ID ASSIGN GET lstr { push_var $1; generate_get $1 $4 }
| GET lstr { generate_get_pipe $2 }
| ID ASSIGN EXTRACT ID lstr lstr { generate_extract $1 $4 $5 $6 }
| PRINT ID { generate_print $2 }
;
pipe_statements:
| { }
| pipe_statement pipe_statements { }
| pipe_statement statements { }
;
pipe_statement:
| PIPE EXTRACT lstr lstr { generate_extract_pipe $3 $4 }
| PIPE PRINT { generate_print_pipe () }
;
lstr:
| STR { $1 }
;