| 1 |
(* a subset of Pascal, a la the appendix to the Dragon book *) |
| 2 |
|
| 3 |
%defs ( |
| 4 |
val cat = String.concat |
| 5 |
val catSp = String.concatWith " " |
| 6 |
val catNl = String.concatWith "\n" |
| 7 |
val catNlNl = String.concatWith "\n\n" |
| 8 |
val catCm = String.concatWith ", " |
| 9 |
val catSemi = String.concatWith "; " |
| 10 |
val catSemiNl = String.concatWith ";\n" |
| 11 |
); |
| 12 |
|
| 13 |
%tokens |
| 14 |
: KW_program ("program") |
| 15 |
| KW_var ("var") |
| 16 |
| KW_array ("array") |
| 17 |
| KW_of ("of") |
| 18 |
| KW_integer ("integer") |
| 19 |
| KW_real ("real") |
| 20 |
| KW_function ("function") |
| 21 |
| KW_procedure ("procedure") |
| 22 |
| KW_begin ("begin") |
| 23 |
| KW_end ("end") |
| 24 |
| KW_if ("if") |
| 25 |
| KW_then ("then") |
| 26 |
| KW_else ("else") |
| 27 |
| KW_while ("while") |
| 28 |
| KW_do ("do") |
| 29 |
| KW_not ("not") |
| 30 |
| ASSIGNOP (":=") |
| 31 |
| COMMA (",") |
| 32 |
| COLON (":") |
| 33 |
| SEMI (";") |
| 34 |
| DOT (".") |
| 35 |
| LSB ("[") |
| 36 |
| RSB ("]") |
| 37 |
| LP ("(") |
| 38 |
| RP (")") |
| 39 |
| MINUS ("-") |
| 40 |
| RELOP of string |
| 41 |
| ADDOP of string |
| 42 |
| MULOP of string |
| 43 |
| ID of string |
| 44 |
| INT of IntInf.int |
| 45 |
| REAL of Real.real |
| 46 |
; |
| 47 |
|
| 48 |
%keywords |
| 49 |
KW_program, |
| 50 |
KW_var, |
| 51 |
KW_array, |
| 52 |
KW_of, |
| 53 |
KW_integer, |
| 54 |
KW_real, |
| 55 |
KW_function, |
| 56 |
KW_procedure, |
| 57 |
KW_begin, |
| 58 |
KW_end, |
| 59 |
KW_if, |
| 60 |
KW_then, |
| 61 |
KW_else, |
| 62 |
KW_while, |
| 63 |
KW_do, |
| 64 |
KW_not |
| 65 |
; |
| 66 |
|
| 67 |
%prefer ID, ";", ",", "["; |
| 68 |
|
| 69 |
%change "," -> ";" | ";" -> "," ; |
| 70 |
|
| 71 |
%value ID("bogus"); |
| 72 |
|
| 73 |
program |
| 74 |
: "program" ID LP id_list RP SEMI |
| 75 |
(declaration)* |
| 76 |
(subprogram_declaration)* |
| 77 |
compound_statement |
| 78 |
DOT |
| 79 |
=> ( catNl [ |
| 80 |
cat ["program ", ID, "(", id_list, ");"], |
| 81 |
catNl SR1, catNlNl SR2, |
| 82 |
compound_statement ^ "." |
| 83 |
]) |
| 84 |
; |
| 85 |
|
| 86 |
id_list |
| 87 |
: ID (COMMA ID)* => ( catCm (ID::SR) ) |
| 88 |
; |
| 89 |
|
| 90 |
declaration |
| 91 |
: KW_var id_list_type SEMI => ( cat ["var ", id_list_type, ";"] ) |
| 92 |
; |
| 93 |
|
| 94 |
compound_type |
| 95 |
: standard_type |
| 96 |
| KW_array LSB INT DOT DOT INT RSB KW_of standard_type |
| 97 |
=> ( cat ["array [", IntInf.toString INT1, "..", IntInf.toString INT2, "] of ", standard_type] ) |
| 98 |
; |
| 99 |
|
| 100 |
standard_type |
| 101 |
: KW_integer => ( "integer" ) |
| 102 |
| KW_real => ( "real" ) |
| 103 |
; |
| 104 |
|
| 105 |
id_list_type |
| 106 |
: id_list COLON compound_type => ( catSp [id_list, ":", compound_type] ) |
| 107 |
; |
| 108 |
|
| 109 |
subprogram_declaration |
| 110 |
: subprogram_head (declaration)* compound_statement SEMI |
| 111 |
=> ( catNl [subprogram_head, catNl SR, compound_statement ^ ";"] ) |
| 112 |
; |
| 113 |
|
| 114 |
subprogram_head |
| 115 |
: KW_function ID arguments COLON standard_type SEMI |
| 116 |
=> ( cat ["function ", ID, arguments, " : ", standard_type, ";"] ) |
| 117 |
| KW_procedure ID arguments SEMI |
| 118 |
=> ( cat ["procedure ", ID, arguments, ";"] ) |
| 119 |
; |
| 120 |
|
| 121 |
arguments |
| 122 |
: LP parameter_list RP => ( "(" ^ parameter_list ^ ")" ) |
| 123 |
| => ( "" ) |
| 124 |
; |
| 125 |
|
| 126 |
parameter_list |
| 127 |
: id_list_type (SEMI id_list_type)* => ( catSemi (id_list_type::SR) ) |
| 128 |
; |
| 129 |
|
| 130 |
compound_statement |
| 131 |
: KW_begin (statement (SEMI statement)* => ( catSemiNl (statement::SR) ))? KW_end |
| 132 |
=> ( catNl ["begin", getOpt(SR, ""), "end"] ) |
| 133 |
; |
| 134 |
|
| 135 |
statement |
| 136 |
: variable ASSIGNOP exp |
| 137 |
=> ( cat [variable, " := ", exp] ) |
| 138 |
| procedure_statement |
| 139 |
| compound_statement |
| 140 |
| KW_if exp KW_then statement KW_else statement |
| 141 |
=> ( cat ["if ", exp, " then ", statement1, " else ", statement2] ) |
| 142 |
| KW_while exp KW_do statement |
| 143 |
=> ( cat ["while ", exp, " do ", statement] ) |
| 144 |
; |
| 145 |
|
| 146 |
variable |
| 147 |
: ID |
| 148 |
| ID LSB exp RSB => ( cat [ID, "[", exp, "]"] ) |
| 149 |
; |
| 150 |
|
| 151 |
procedure_statement |
| 152 |
: ID |
| 153 |
| ID LP exp (COMMA exp)* RP => ( cat [ID, "(", catCm (exp::SR), ")" ] ) |
| 154 |
; |
| 155 |
|
| 156 |
exp |
| 157 |
: simple_exp (RELOP simple_exp => ( RELOP ^ " " ^ simple_exp ))? |
| 158 |
=> ( catSp [simple_exp, getOpt(SR, "")] ) |
| 159 |
; |
| 160 |
|
| 161 |
simple_exp |
| 162 |
: signed_term (ADDOP signed_term => ( ADDOP ^ " " ^signed_term ))* |
| 163 |
=> ( catSp (signed_term::SR) ) |
| 164 |
; |
| 165 |
|
| 166 |
signed_term |
| 167 |
: MINUS term => ( "-" ^ term ) |
| 168 |
| term |
| 169 |
; |
| 170 |
|
| 171 |
term |
| 172 |
: factor (MULOP factor => ( MULOP ^ " " ^ factor ))* |
| 173 |
=> ( catSp (factor::SR) ) |
| 174 |
; |
| 175 |
|
| 176 |
factor |
| 177 |
: variable |
| 178 |
| ID LP exp (COMMA exp)* RP => ( cat [ID, "(", catCm (exp::SR), ")" ] ) |
| 179 |
| INT => ( IntInf.toString INT ) |
| 180 |
| REAL => ( Real.toString REAL ) |
| 181 |
| LP exp RP => ( "(" ^ exp ^ ")" ) |
| 182 |
| KW_not factor => ( "not " ^ factor ) |
| 183 |
; |