Add string pasting support to the gramamr.
[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
2ab9671f 38 | STRING of string
0a24e44d
JW
39 | RETURN
40 | PLUS | MINUS | STAR | SLASH | PERCENT | LSH | RSH | LOGOR | LOGAND | BITAND | BITXOR | BITOR | BITNOT | BANG
41 | ASSIGN | PLUSEQ | MINUSEQ | STAREQ | SLASHEQ | PERCENTEQ | LSHEQ | RSHEQ | BITANDEQ | BITXOREQ | BITOREQ
42 | EQ | NEQ | LT | LE | GT | GE
43 | IF | ELSE | WHILE | FOR | CONTINUE | BREAK
44 | LBRACE | RBRACE
45 | LPAREN | RPAREN
46 | UNARY | ASNOP (* dummy *)
2ab9671f 47 | EXTERN | VAR | INT | TSTRING | QUESTION | COLON | COMMA | STRUCT | NULL | LBRACKET | RBRACKET | ARROW | DOT | NEW
5c79bb68 48 | PLUSPLUS | MINUSMINUS
0a24e44d
JW
49
50%nonterm
51 program of A.program
1144856b 52 | programx of A.program
0a24e44d 53 | stms of A.stm list
a644da89 54 | stringcat of string
0a24e44d
JW
55 | stm of A.stm
56 | simp of A.stm
57 | return of A.stm
58 | exp of A.exp
6ade8b0a 59 | explist of A.exp list
0a24e44d 60 | control of A.stm
1144856b 61 | asnop of A.oper
0a24e44d
JW
62 | block of A.stm list
63 | simpoption of A.stm option
64 | elseoption of A.stm list option
6ade8b0a 65 | idents of A.ident list
5c79bb68 66 | vtype of T.vtype
1144856b
JW
67 | decls of A.program
68 | extdecl of A.ident * A.function
5c79bb68
JW
69 | paramlist of T.variable list
70 | param of T.variable
71 | typedecl of T.ident * T.typedef
72 | memberlist of (T.ident * T.vtype) list
73 | member of (T.ident * T.vtype)
1144856b 74 | function of A.ident * A.function
5c79bb68
JW
75 | vardecl of T.variable list
76 | vardecls of T.variable list
0a24e44d
JW
77
78%verbose (* print summary of errors *)
79%pos int (* positions *)
80%start program
81%eop EOF
82%noshift EOF
83
5c79bb68 84%name L5
0a24e44d 85
5c79bb68 86%right QUESTION COLON
0a24e44d
JW
87%left LOGOR
88%left LOGAND
89%left BITOR
90%left BITXOR
91%left BITAND
92%left EQ NEQ
93%left LT LE GT GE
94%left LSH RSH
95%left PLUS MINUS
96%left STAR SLASH PERCENT
97%right UNARY
1144856b 98%left LPAREN LBRACKET ARROW DOT
0a24e44d
JW
99
100%%
101
1144856b
JW
102program : programx (programx)
103
104programx : decls (decls)
105 | programx function (AUP.append_function programx function)
6ade8b0a 106
5c79bb68 107vtype : INT (T.Int)
2ab9671f 108 | TSTRING (T.String)
5c79bb68
JW
109 | IDENT (T.Typedef IDENT)
110 | vtype STAR (T.Pointer vtype)
1144856b 111 | vtype LBRACKET RBRACKET
5c79bb68 112 (T.Array vtype)
6ade8b0a 113
1144856b
JW
114decls : (Symbol.empty, Symbol.empty)
115 | typedecl decls (AUP.append_typedef decls typedecl)
116 | extdecl decls (AUP.append_function decls extdecl)
6ade8b0a
JW
117
118extdecl : EXTERN vtype IDENT LPAREN RPAREN SEMI
1144856b 119 (IDENT, markfunction (A.Extern (vtype, []), (EXTERNleft, SEMIright)))
6ade8b0a 120 | EXTERN vtype IDENT LPAREN paramlist RPAREN SEMI
1144856b 121 (IDENT, markfunction (A.Extern (vtype, paramlist), (EXTERNleft, SEMIright)))
6ade8b0a
JW
122
123paramlist : param COMMA paramlist (param :: paramlist)
124 | param ([param])
125
126param : IDENT COLON vtype (IDENT, vtype)
127
1144856b 128typedecl : STRUCT IDENT LBRACE RBRACE SEMI
5c79bb68 129 (IDENT, marktypedef (T.Struct ([]), (STRUCTleft, SEMIright)))
1144856b 130 | STRUCT IDENT LBRACE memberlist RBRACE SEMI
5c79bb68 131 (IDENT, marktypedef (T.Struct (memberlist), (STRUCTleft, SEMIright)))
1144856b
JW
132
133memberlist : member memberlist (member :: memberlist)
134 | member ([member])
135
136member : IDENT COLON vtype SEMI (IDENT, vtype)
6ade8b0a 137
1144856b
JW
138function : vtype IDENT LPAREN paramlist RPAREN LBRACE vardecls stms RBRACE
139 (IDENT, markfunction (A.Function (vtype, paramlist, vardecls, stms), (vtypeleft, RBRACEright)))
140 | vtype IDENT LPAREN RPAREN LBRACE vardecls stms RBRACE
141 (IDENT, markfunction (A.Function (vtype, [], vardecls, stms), (vtypeleft, RBRACEright)))
6ade8b0a
JW
142
143vardecls : ([])
144 | vardecl vardecls (vardecl @ vardecls)
145
146vardecl : VAR idents COLON vtype SEMI
147 (map (fn x => (x, vtype)) idents)
148
149idents : IDENT ([IDENT])
150 | IDENT COMMA idents (IDENT :: idents)
0a24e44d
JW
151
152stms : ([])
153 | stm stms (stm :: stms)
154
6ade8b0a
JW
155stm : simp SEMI (simp)
156 | control (control)
157 | SEMI (A.Nop)
0a24e44d 158
1144856b
JW
159simp : exp ASSIGN exp %prec ASNOP
160 (A.Assign(exp1, exp2))
161 | exp asnop exp %prec ASNOP
162 (A.AsnOp(asnop, exp1, exp2))
5c79bb68
JW
163 | exp PLUSPLUS %prec ASNOP
164 (A.AsnOp(A.PLUS, exp, A.ConstExp(0w1)))
165 | exp MINUSMINUS %prec ASNOP
166 (A.AsnOp(A.MINUS, exp, A.ConstExp(0w1)))
1144856b 167 | exp (markstm (A.Effect (exp), (expleft, expright)))
0a24e44d
JW
168
169control : IF LPAREN exp RPAREN block elseoption
170 (markstm ((A.If (exp, block, elseoption)), (IFleft, elseoptionright)))
171 | WHILE LPAREN exp RPAREN block
172 (markstm ((A.While (exp, block)), (WHILEleft, blockright)))
173 | FOR LPAREN simpoption SEMI exp SEMI simpoption RPAREN block
174 (markstm ((A.For (simpoption1, exp, simpoption2, block)), (FORleft, blockright)))
175 | CONTINUE SEMI (markstm ((A.Continue), (CONTINUEleft, SEMIright)))
176 | BREAK SEMI (markstm ((A.Break), (BREAKleft, SEMIright)))
177 | RETURN exp SEMI (markstm ((A.Return exp), (RETURNleft, SEMIright)))
178
179elseoption : ELSE block (SOME block)
180 | (NONE)
181
182simpoption : (NONE)
183 | simp (SOME simp)
184
185block : stm ([stm])
186 | LBRACE stms RBRACE (stms)
187
a644da89
JW
188stringcat : STRING (STRING)
189 | STRING stringcat (STRING ^ stringcat)
190
0a24e44d
JW
191exp : LPAREN exp RPAREN (exp)
192 | INTNUM (mark (A.ConstExp(INTNUM),(INTNUMleft,INTNUMright)))
a644da89 193 | stringcat (mark (A.StringExp(stringcat),(stringcatleft,stringcatright)))
0a24e44d 194 | IDENT (mark (A.Var(IDENT), (IDENTleft,IDENTright)))
e63d3705 195 | LBRACKET vtype RBRACKET exp %prec UNARY (mark (A.Cast (vtype, exp), (LBRACKETleft, expright)))
1144856b
JW
196 | exp DOT IDENT (mark (A.Member(exp, IDENT), (expleft, IDENTright)))
197 | exp ARROW IDENT (mark (A.DerefMember(exp, IDENT), (expleft, IDENTright)))
198 | STAR exp %prec UNARY (mark (A.Dereference(exp), (STARleft, expright)))
199 | exp LBRACKET exp RBRACKET
200 (mark (A.ArrIndex(exp1, exp2), (exp1left, exp2right)))
0a24e44d
JW
201 | exp PLUS exp (mark (A.OpExp (A.PLUS, [exp1,exp2]), (exp1left,exp2right)))
202 | exp MINUS exp (mark (A.OpExp (A.MINUS, [exp1,exp2]), (exp1left,exp2right)))
203 | exp STAR exp (mark (A.OpExp (A.TIMES, [exp1,exp2]), (exp1left,exp2right)))
204 | exp SLASH exp (mark (A.OpExp (A.DIVIDEDBY, [exp1,exp2]), (exp1left,exp2right)))
205 | exp PERCENT exp (mark (A.OpExp (A.MODULO, [exp1,exp2]), (exp1left,exp2right)))
206 | exp LSH exp (mark (A.OpExp (A.LSH, [exp1,exp2]), (exp1left,exp2right)))
207 | exp RSH exp (mark (A.OpExp (A.RSH, [exp1,exp2]), (exp1left,exp2right)))
208 | exp LOGOR exp (mark (A.OpExp (A.LOGOR, [exp1,exp2]), (exp1left,exp2right)))
209 | exp LOGAND exp (mark (A.OpExp (A.LOGAND, [exp1,exp2]), (exp1left,exp2right)))
210 | exp BITOR exp (mark (A.OpExp (A.BITOR, [exp1,exp2]), (exp1left,exp2right)))
211 | exp BITAND exp (mark (A.OpExp (A.BITAND, [exp1,exp2]), (exp1left,exp2right)))
212 | exp BITXOR exp (mark (A.OpExp (A.BITXOR, [exp1,exp2]), (exp1left,exp2right)))
213 | exp EQ exp (mark (A.OpExp (A.EQ, [exp1,exp2]), (exp1left,exp2right)))
214 | exp NEQ exp (mark (A.OpExp (A.NEQ, [exp1,exp2]), (exp1left,exp2right)))
215 | exp LT exp (mark (A.OpExp (A.LT, [exp1,exp2]), (exp1left,exp2right)))
216 | exp LE exp (mark (A.OpExp (A.LE, [exp1,exp2]), (exp1left,exp2right)))
217 | exp GT exp (mark (A.OpExp (A.GT, [exp1,exp2]), (exp1left,exp2right)))
218 | exp GE exp (mark (A.OpExp (A.GE, [exp1,exp2]), (exp1left,exp2right)))
1144856b 219 | NULL (mark (A.Null, (NULLleft, NULLright)))
6ade8b0a 220 | IDENT LPAREN RPAREN (mark (A.FuncCall(IDENT, []), (IDENTleft, RPARENright)))
6ade8b0a
JW
221 | IDENT LPAREN explist RPAREN
222 (mark (A.FuncCall(IDENT, explist), (IDENTleft, RPARENright)))
1144856b
JW
223 | NEW LPAREN vtype RPAREN
224 (mark (A.New (vtype), (NEWleft, RPARENright)))
225 | NEW LPAREN vtype LBRACKET exp RBRACKET RPAREN
226 (mark (A.NewArr (vtype, exp), (NEWleft, RPARENright)))
0a24e44d
JW
227 | MINUS exp %prec UNARY (mark (A.OpExp (A.NEGATIVE, [exp]), (MINUSleft,expright)))
228 | BITNOT exp %prec UNARY (mark (A.OpExp (A.BITNOT, [exp]), (BITNOTleft,expright)))
229 | BANG exp %prec UNARY (mark (A.OpExp (A.BANG, [exp]), (BANGleft,expright)))
5c79bb68
JW
230 | exp QUESTION exp COLON exp
231 (mark (A.Conditional (exp1, exp2, exp3), (exp1left, exp3right)))
0a24e44d 232
6ade8b0a
JW
233explist : exp ([exp])
234 | exp COMMA explist (exp :: explist)
235
1144856b
JW
236asnop : PLUSEQ (A.PLUS)
237 | MINUSEQ (A.MINUS)
238 | STAREQ (A.TIMES)
239 | SLASHEQ (A.DIVIDEDBY)
240 | PERCENTEQ (A.MODULO)
241 | LSHEQ (A.LSH)
242 | RSHEQ (A.RSH)
243 | BITOREQ (A.BITOR)
244 | BITANDEQ (A.BITAND)
245 | BITXOREQ (A.BITXOR)
This page took 0.114846 seconds and 4 git commands to generate.