-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfloop.txt
87 lines (63 loc) · 2.38 KB
/
floop.txt
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
.syntax [ arglist arglistp assignst block floop breakst callarglist
callarglistp callexpr callst compare expr factor foreverst
ifst intst loopst pmain printlnst printst proc continuest
returnst st term ] floop
arglistp = ',' .id < ', int ' * > ;
arglist = '(' < '(int ' > .id < * > $(arglistp) ')' ':' < ')' > ;
intst = 'int' .id { 'int ' * ' = 0;' } ;
assignst = .id < '' * ' = ' > '<-' expr { ';' } ;
printst = 'print' (.id { 'printf("%d ",' * ');' }
| .string { 'printf("' * ');' }) ;
printlnst = 'println' (.id { 'printf("%d\n",' * ');' }
| .string { 'puts("' * ');' }) ;
ifst = 'if' < '' 'if (' >
expr { ') {' } .lm+
',' 'then' ':'
st ';' .lm- { '} else { ' } .lm+
st .lm- { '}' } ;
continuest = 'continue' { 'continue;' } ;
breakst = 'break' { 'f = 0;' } ;
loopst = 'loop' 'at' 'most' < '' 'for(int i = 0, f = 1; i < ' >
expr { ' && f; i++) {' } .lm+
'times' ':'
block
.lm- { '}' } ;
foreverst = 'forever' ':'
{ '{' } .lm+
{ 'int f = 1;' } < '' 'while(1 && f)' > { ' {' } .lm+
block
.lm- { '}' }
.lm- { '}' } ;
st = printlnst | printst | ifst | loopst | foreverst | continuest |
breakst | block | returnst | callst | assignst ;
block = 'begin'
$(intst ';' )
{ 'do {' } .lm+ st $(';' st) .lm- { '} while (0);' }
'end' ;
proc = 'def' .id < 'int ' * > arglist { ' {' } .lm+
$(.string)
block
.lm- { '}' } ;
pmain = 'main' { 'int main(void) {' } .lm+ block .lm- { '}' } ;
expr = factor $ (('+' < ' + ' > factor)
| ('-' < ' - ' > factor)) ;
term = callexpr
| (.id | .number) < * >
| '(' < '(' > expr ')' < ')' > ;
compare = term $ (('<' < ' < ' > term)
| ('>' < ' > ' > term)
| ('=' < ' == ' > term)
| ('>=' < ' >=' > term)
| ('<=' < ' <=' > term)) ;
factor = compare $ ('*' < ' * ' > compare) ;
callarglistp = ',' < ', ' > expr ;
callarglist = '(' < '(' > expr $(callarglistp) ')' < ')' > ;
callst = 'do' .id < '' * > callarglist { ';' } ;
returnst = 'return' < '' 'return ' > expr { ';' } ;
callexpr = 'do' .id < '' * > callarglist ;
floop = {
'#include <stdio.h>
#include <stdlib.h>
' }
$(proc | .string) pmain ;
.end