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