-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrm.grammar
67 lines (56 loc) · 1.85 KB
/
rm.grammar
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
Package rm;
Helpers
alpha = [['a'..'z']+['A'..'Z']];
num = ['0'..'9'];
alnum = alpha|num;
exponent = 'e'|'E';
Tokens
plussym = '+';
minussym = '-';
timessym = '*';
idivsym = '\';
rdivsym = '/';
imodsym = '%';
equalsym = '=';
noteqsym = '!=';
lesssym = '<';
lseqsym = '<=';
greqsym = '>=';
grtrsym = '>';
lparsym = '(';
rparsym = ')';
questionsym = '?';
commasym = ',';
semicolonsym = ';';
endsym = '.';
letsym = 'LET';
ifsym = 'IF';
thensym = 'THEN';
elsesym = 'ELSE';
intdenotation = num+;
realdenotation = num* '.' num+ (exponent '-'? num+)?;
booldenotation = 'false' | 'true';
chardenotation = 39 [[32..0xffff]-39] 39;
ident = alpha alnum*;
comment = ('--' [[32..0xffff]+9]*);
blank = (' ' | 13 | 10 | 9);
Ignored Tokens
comment, blank;
Productions
prog = {def} def prog | {comp} comp prog | {end} endsym;
def = {let} letsym ident parlst equalsym expr semicolonsym;
comp = {expr} expr questionsym;
parlst = {nonempty} lparsym pars rparsym | {empty} ;
pars = {multi} pars commasym ident | {single} ident;
expr = {ifthen} ifsym relexpr thensym [then]:expr elsesym [else]:expr | {rel} relexpr;
relexpr = {rel} [left]:smplexpr relop [right]:smplexpr | {simple} smplexpr;
smplexpr = {term} term | {add} smplexpr addop term;
term = {factor} addopfactor | {mul} term mulop addopfactor;
addopfactor = {addop} addop factor | {factor} factor;
factor = {expr} lparsym expr rparsym | {int} intdenotation | {real} realdenotation | {bool} booldenotation
| {char} chardenotation | {call} ident arglst;
arglst = {nonempty} lparsym args rparsym | {empty} ;
args = {multi} args commasym expr | {single} expr;
addop = {plus} plussym | {minus} minussym;
mulop = {times} timessym | {idiv} idivsym | {rdiv} rdivsym | {imod} imodsym;
relop = {equal} equalsym | {noteq} noteqsym | {less} lesssym | {lseq} lseqsym | {greq} greqsym | {grtr} grtrsym;