-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmylang.grm
77 lines (61 loc) · 1.64 KB
/
mylang.grm
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
open DataTypes;
open String;
open Char;
%%
%eop EOF
%noshift EOF
(* %pos declares the type of positions for terminals.
Each symbol has an associated 1left and 1right position.
*)
%pos int
%term ID of string
| STRLIT of string
| LBRACE
| RBRACE
| LPAREN
| RPAREN
| RW_PRINT
| RW_CLASS
| RW_VOID
| EOF
| SEMI
| COMMA
%start PROGRAM
%nonterm PROGRAM of mylang_program
| MEMBERDECLS of mylang_member_decls
| METHODDECLS of mylang_decl list
| METHODDECL of mylang_decl
| STMTS of stmt list
| STMT of stmt
| OPTIONALSEMI of stmt
| ARG of expr
| WRITELIST of stmt list
| EXPR of expr
%name MyLang
%keyword RW_CLASS RW_PRINT
%verbose
%%
PROGRAM : RW_CLASS ID LBRACE MEMBERDECLS RBRACE
(Program(id(ID), MEMBERDECLS))
MEMBERDECLS : METHODDECLS
(MemberDecls(METHODDECLS))
METHODDECLS : METHODDECL METHODDECLS
(METHODDECL::METHODDECLS)
| (*λ*)
(nil)
METHODDECL : RW_VOID ID LPAREN RPAREN LBRACE
STMTS
RBRACE OPTIONALSEMI (
Method(id(ID), STMTS, RW_VOID1left, RBRACE1right)
)
OPTIONALSEMI : SEMI
(NoneStmt)
| (*λ*)
(NoneStmt)
STMTS : STMT STMTS
(STMT::STMTS)
| STMT
([STMT])
STMT : RW_PRINT LPAREN STRLIT RPAREN SEMI
(Write(StrLit(STRLIT, STRLIT1left, STRLIT1right),
RW_PRINT1left, SEMI1right))