]> Joshua Wise's Git repositories - snipe.git/blame - parse/l5.grm
Rename output binary from l5c to snipe
[snipe.git] / parse / l5.grm
CommitLineData
5c79bb68
JW
1(* L5 Compiler
2 * L5 grammar
0a24e44d
JW
3 * Author: Kaustuv Chaudhuri <kaustuv+@cs.cmu.edu>
4 * Modified: Frank Pfenning <fp@cs.cmu.edu>
5 * Modified: Joshua Wise <jwise@andrew.cmu.edu>
6 * Modified: Chris Lu <czl@andrew.cmu.edu>
7 *)
8
9structure A = Ast
5c79bb68 10structure T = Type
1144856b
JW
11structure AU = AstUtils
12structure AUP = AstUtils.Program
0a24e44d
JW
13
14(* for simplicity, we only mark expressions, not statements *)
15
16(* mark e with region (left, right) in source file *)
17fun mark (e, (left, right)) = A.Marked (Mark.mark' (e, ParseState.ext (left, right)))
18fun markstm (e, (left, right)) = A.MarkedStm (Mark.mark' (e, ParseState.ext (left, right)))
1144856b 19fun markfunction (e, (left, right)) = A.MarkedFunction (Mark.mark' (e, ParseState.ext (left, right)))
5c79bb68 20fun marktypedef (e, (left, right)) = T.MarkedTypedef (Mark.mark' (e, ParseState.ext (left, right)))
0a24e44d
JW
21
22(* create lval from expression; here just an id *)
23(* generates error if not an identifier *)
24fun make_lval (A.Var(id)) ext = id
25 | make_lval (A.Marked(marked_exp)) ext =
26 make_lval (Mark.data marked_exp) (Mark.ext marked_exp)
27 | make_lval _ ext = ( ErrorMsg.error ext "not a variable" ;
28 Symbol.bogus )
29
0a24e44d 30%%
5c79bb68 31%header (functor L5LrValsFn (structure Token : TOKEN))
0a24e44d
JW
32
33%term
34 EOF
35 | SEMI
36 | INTNUM of Word32.word
37 | IDENT of Symbol.symbol
38 | RETURN
39 | PLUS | MINUS | STAR | SLASH | PERCENT | LSH | RSH | LOGOR | LOGAND | BITAND | BITXOR | BITOR | BITNOT | BANG
40 | ASSIGN | PLUSEQ | MINUSEQ | STAREQ | SLASHEQ | PERCENTEQ | LSHEQ | RSHEQ | BITANDEQ | BITXOREQ | BITOREQ
41 | EQ | NEQ | LT | LE | GT | GE
42 | IF | ELSE | WHILE | FOR | CONTINUE | BREAK
43 | LBRACE | RBRACE
44 | LPAREN | RPAREN
45 | UNARY | ASNOP (* dummy *)
5c79bb68
JW
46 | EXTERN | VAR | INT | QUESTION | COLON | COMMA | STRUCT | NULL | LBRACKET | RBRACKET | ARROW | DOT | NEW
47 | PLUSPLUS | MINUSMINUS
0a24e44d
JW
48
49%nonterm
50 program of A.program
1144856b 51 | programx of A.program
0a24e44d
JW
52 | stms of A.stm list
53 | stm of A.stm
54 | simp of A.stm
55 | return of A.stm
56 | exp of A.exp
6ade8b0a 57 | explist of A.exp list
0a24e44d 58 | control of A.stm
1144856b 59 | asnop of A.oper
0a24e44d
JW
60 | block of A.stm list
61 | simpoption of A.stm option
62 | elseoption of A.stm list option
6ade8b0a 63 | idents of A.ident list
5c79bb68 64 | vtype of T.vtype
1144856b
JW
65 | decls of A.program
66 | extdecl of A.ident * A.function
5c79bb68
JW
67 | paramlist of T.variable list
68 | param of T.variable
69 | typedecl of T.ident * T.typedef
70 | memberlist of (T.ident * T.vtype) list
71 | member of (T.ident * T.vtype)
1144856b 72 | function of A.ident * A.function
5c79bb68
JW
73 | vardecl of T.variable list
74 | vardecls of T.variable list
0a24e44d
JW
75
76%verbose (* print summary of errors *)
77%pos int (* positions *)
78%start program
79%eop EOF
80%noshift EOF
81
5c79bb68 82%name L5
0a24e44d 83
5c79bb68 84%right QUESTION COLON
0a24e44d
JW
85%left LOGOR
86%left LOGAND
87%left BITOR
88%left BITXOR
89%left BITAND
90%left EQ NEQ
91%left LT LE GT GE
92%left LSH RSH
93%left PLUS MINUS
94%left STAR SLASH PERCENT
95%right UNARY
1144856b 96%left LPAREN LBRACKET ARROW DOT
0a24e44d
JW
97
98%%
99
1144856b
JW
100program : programx (programx)
101
102programx : decls (decls)
103 | programx function (AUP.append_function programx function)
6ade8b0a 104
5c79bb68
JW
105vtype : INT (T.Int)
106 | IDENT (T.Typedef IDENT)
107 | vtype STAR (T.Pointer vtype)
1144856b 108 | vtype LBRACKET RBRACKET
5c79bb68 109 (T.Array vtype)
6ade8b0a 110
1144856b
JW
111decls : (Symbol.empty, Symbol.empty)
112 | typedecl decls (AUP.append_typedef decls typedecl)
113 | extdecl decls (AUP.append_function decls extdecl)
6ade8b0a
JW
114
115extdecl : EXTERN vtype IDENT LPAREN RPAREN SEMI
1144856b 116 (IDENT, markfunction (A.Extern (vtype, []), (EXTERNleft, SEMIright)))
6ade8b0a 117 | EXTERN vtype IDENT LPAREN paramlist RPAREN SEMI
1144856b 118 (IDENT, markfunction (A.Extern (vtype, paramlist), (EXTERNleft, SEMIright)))
6ade8b0a
JW
119
120paramlist : param COMMA paramlist (param :: paramlist)
121 | param ([param])
122
123param : IDENT COLON vtype (IDENT, vtype)
124
1144856b 125typedecl : STRUCT IDENT LBRACE RBRACE SEMI
5c79bb68 126 (IDENT, marktypedef (T.Struct ([]), (STRUCTleft, SEMIright)))
1144856b 127 | STRUCT IDENT LBRACE memberlist RBRACE SEMI
5c79bb68 128 (IDENT, marktypedef (T.Struct (memberlist), (STRUCTleft, SEMIright)))
1144856b
JW
129
130memberlist : member memberlist (member :: memberlist)
131 | member ([member])
132
133member : IDENT COLON vtype SEMI (IDENT, vtype)
6ade8b0a 134
1144856b
JW
135function : vtype IDENT LPAREN paramlist RPAREN LBRACE vardecls stms RBRACE
136 (IDENT, markfunction (A.Function (vtype, paramlist, vardecls, stms), (vtypeleft, RBRACEright)))
137 | vtype IDENT LPAREN RPAREN LBRACE vardecls stms RBRACE
138 (IDENT, markfunction (A.Function (vtype, [], vardecls, stms), (vtypeleft, RBRACEright)))
6ade8b0a
JW
139
140vardecls : ([])
141 | vardecl vardecls (vardecl @ vardecls)
142
143vardecl : VAR idents COLON vtype SEMI
144 (map (fn x => (x, vtype)) idents)
145
146idents : IDENT ([IDENT])
147 | IDENT COMMA idents (IDENT :: idents)
0a24e44d
JW
148
149stms : ([])
150 | stm stms (stm :: stms)
151
6ade8b0a
JW
152stm : simp SEMI (simp)
153 | control (control)
154 | SEMI (A.Nop)
0a24e44d 155
1144856b
JW
156simp : exp ASSIGN exp %prec ASNOP
157 (A.Assign(exp1, exp2))
158 | exp asnop exp %prec ASNOP
159 (A.AsnOp(asnop, exp1, exp2))
5c79bb68
JW
160 | exp PLUSPLUS %prec ASNOP
161 (A.AsnOp(A.PLUS, exp, A.ConstExp(0w1)))
162 | exp MINUSMINUS %prec ASNOP
163 (A.AsnOp(A.MINUS, exp, A.ConstExp(0w1)))
1144856b 164 | exp (markstm (A.Effect (exp), (expleft, expright)))
0a24e44d
JW
165
166control : IF LPAREN exp RPAREN block elseoption
167 (markstm ((A.If (exp, block, elseoption)), (IFleft, elseoptionright)))
168 | WHILE LPAREN exp RPAREN block
169 (markstm ((A.While (exp, block)), (WHILEleft, blockright)))
170 | FOR LPAREN simpoption SEMI exp SEMI simpoption RPAREN block
171 (markstm ((A.For (simpoption1, exp, simpoption2, block)), (FORleft, blockright)))
172 | CONTINUE SEMI (markstm ((A.Continue), (CONTINUEleft, SEMIright)))
173 | BREAK SEMI (markstm ((A.Break), (BREAKleft, SEMIright)))
174 | RETURN exp SEMI (markstm ((A.Return exp), (RETURNleft, SEMIright)))
175
176elseoption : ELSE block (SOME block)
177 | (NONE)
178
179simpoption : (NONE)
180 | simp (SOME simp)
181
182block : stm ([stm])
183 | LBRACE stms RBRACE (stms)
184
185exp : LPAREN exp RPAREN (exp)
186 | INTNUM (mark (A.ConstExp(INTNUM),(INTNUMleft,INTNUMright)))
187 | IDENT (mark (A.Var(IDENT), (IDENTleft,IDENTright)))
1144856b
JW
188 | exp DOT IDENT (mark (A.Member(exp, IDENT), (expleft, IDENTright)))
189 | exp ARROW IDENT (mark (A.DerefMember(exp, IDENT), (expleft, IDENTright)))
190 | STAR exp %prec UNARY (mark (A.Dereference(exp), (STARleft, expright)))
191 | exp LBRACKET exp RBRACKET
192 (mark (A.ArrIndex(exp1, exp2), (exp1left, exp2right)))
0a24e44d
JW
193 | exp PLUS exp (mark (A.OpExp (A.PLUS, [exp1,exp2]), (exp1left,exp2right)))
194 | exp MINUS exp (mark (A.OpExp (A.MINUS, [exp1,exp2]), (exp1left,exp2right)))
195 | exp STAR exp (mark (A.OpExp (A.TIMES, [exp1,exp2]), (exp1left,exp2right)))
196 | exp SLASH exp (mark (A.OpExp (A.DIVIDEDBY, [exp1,exp2]), (exp1left,exp2right)))
197 | exp PERCENT exp (mark (A.OpExp (A.MODULO, [exp1,exp2]), (exp1left,exp2right)))
198 | exp LSH exp (mark (A.OpExp (A.LSH, [exp1,exp2]), (exp1left,exp2right)))
199 | exp RSH exp (mark (A.OpExp (A.RSH, [exp1,exp2]), (exp1left,exp2right)))
200 | exp LOGOR exp (mark (A.OpExp (A.LOGOR, [exp1,exp2]), (exp1left,exp2right)))
201 | exp LOGAND exp (mark (A.OpExp (A.LOGAND, [exp1,exp2]), (exp1left,exp2right)))
202 | exp BITOR exp (mark (A.OpExp (A.BITOR, [exp1,exp2]), (exp1left,exp2right)))
203 | exp BITAND exp (mark (A.OpExp (A.BITAND, [exp1,exp2]), (exp1left,exp2right)))
204 | exp BITXOR exp (mark (A.OpExp (A.BITXOR, [exp1,exp2]), (exp1left,exp2right)))
205 | exp EQ exp (mark (A.OpExp (A.EQ, [exp1,exp2]), (exp1left,exp2right)))
206 | exp NEQ exp (mark (A.OpExp (A.NEQ, [exp1,exp2]), (exp1left,exp2right)))
207 | exp LT exp (mark (A.OpExp (A.LT, [exp1,exp2]), (exp1left,exp2right)))
208 | exp LE exp (mark (A.OpExp (A.LE, [exp1,exp2]), (exp1left,exp2right)))
209 | exp GT exp (mark (A.OpExp (A.GT, [exp1,exp2]), (exp1left,exp2right)))
210 | exp GE exp (mark (A.OpExp (A.GE, [exp1,exp2]), (exp1left,exp2right)))
1144856b 211 | NULL (mark (A.Null, (NULLleft, NULLright)))
6ade8b0a 212 | IDENT LPAREN RPAREN (mark (A.FuncCall(IDENT, []), (IDENTleft, RPARENright)))
6ade8b0a
JW
213 | IDENT LPAREN explist RPAREN
214 (mark (A.FuncCall(IDENT, explist), (IDENTleft, RPARENright)))
1144856b
JW
215 | NEW LPAREN vtype RPAREN
216 (mark (A.New (vtype), (NEWleft, RPARENright)))
217 | NEW LPAREN vtype LBRACKET exp RBRACKET RPAREN
218 (mark (A.NewArr (vtype, exp), (NEWleft, RPARENright)))
0a24e44d
JW
219 | MINUS exp %prec UNARY (mark (A.OpExp (A.NEGATIVE, [exp]), (MINUSleft,expright)))
220 | BITNOT exp %prec UNARY (mark (A.OpExp (A.BITNOT, [exp]), (BITNOTleft,expright)))
221 | BANG exp %prec UNARY (mark (A.OpExp (A.BANG, [exp]), (BANGleft,expright)))
5c79bb68
JW
222 | exp QUESTION exp COLON exp
223 (mark (A.Conditional (exp1, exp2, exp3), (exp1left, exp3right)))
0a24e44d 224
6ade8b0a
JW
225explist : exp ([exp])
226 | exp COMMA explist (exp :: explist)
227
1144856b
JW
228asnop : PLUSEQ (A.PLUS)
229 | MINUSEQ (A.MINUS)
230 | STAREQ (A.TIMES)
231 | SLASHEQ (A.DIVIDEDBY)
232 | PERCENTEQ (A.MODULO)
233 | LSHEQ (A.LSH)
234 | RSHEQ (A.RSH)
235 | BITOREQ (A.BITOR)
236 | BITANDEQ (A.BITAND)
237 | BITXOREQ (A.BITXOR)
This page took 0.050881 seconds and 4 git commands to generate.