1 import library.*; 2 3 COMPILER PrettyParva $CN 4 /* Parva level 1.5 grammar for examination - Coco/R for Java 5 PrettyPrinter actions 6 Java operator precedences 7 Supplied Parva Compiler matches this grammar (and has a few extensions) 8 P.D. Terry, Rhodes University, 2011 */ 9 10 public static boolean 11 indented = true; 12 13 CHARACTERS 14 lf = CHR(10) . 15 backslash = CHR(92) . 16 control = CHR(0) .. CHR(31) . 17 letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" . 18 digit = "0123456789" . 19 nonZeroDigit = "123456789" . 20 stringCh = ANY - '"' - control - backslash . 21 charCh = ANY - "'" - control - backslash . 22 printable = ANY - control . 23 24 TOKENS 25 identifier = letter { letter | digit | "_" { "_" } ( letter | digit ) } . 26 number = "0" | nonZeroDigit { digit } . 27 stringLit = '"' { stringCh | backslash printable } '"' . 28 charLit = "'" ( charCh | backslash printable ) "'" . 29 30 COMMENTS FROM "//" TO lf 31 COMMENTS FROM "/*" TO "*/" 32 33 IGNORE CHR(9) .. CHR(13) 34 35 PRODUCTIONS 36 PrettyParva (. String name; .) 37 = "void" (. CodeGen.append("void "); .) 38 Ident<out name> (. CodeGen.append(name); .) 39 "(" ")" (. CodeGen.append("()"); .) 40 Block . 41 42 Block 43 = "{" (. CodeGen.append(" {"); CodeGen.indent(); .) 44 { Statement<!indented> } 45 "}" (. CodeGen.exdentNewLine(); 46 CodeGen.append("}"); .) . 47 48 Statement<boolean indented> 49 = Block 50 | ";" (. CodeGen.append(";"); .) 51 | (. if (indented) CodeGen.indent(); 52 CodeGen.newLine(); .) 53 ( ConstDeclarations 54 | VarDeclarations 55 | AssignmentStatement 56 | IfStatement 57 | WhileStatement 58 | DoWhileStatement 59 | ForStatement 60 | BreakStatement 61 | HaltStatement 62 | ReadStatement 63 | ReadLineStatement 64 | WriteStatement 65 | WriteLineStatement 66 ) (. if (indented) CodeGen.exdent(); .) . 67 68 ConstDeclarations 69 = "const" (. CodeGen.append("const "); CodeGen.indentNewLine(); .) 70 OneConst 71 { WEAK "," (. CodeGen.append(", "); CodeGen.newLine(); .) 72 OneConst 73 } WEAK ";" (. CodeGen.append(";"); CodeGen.exdent(); .) . 74 75 OneConst (. String name; .) 76 = Ident<out name> (. CodeGen.append(name); .) 77 "=" (. CodeGen.append(" = "); .) 78 Constant . 79 80 Constant (. String value; .) 81 = IntConst<out value> (. CodeGen.append(value); .) 82 | CharConst<out value> (. CodeGen.append(value); .) 83 | "true" (. CodeGen.append("true"); .) 84 | "false" (. CodeGen.append("false"); .) 85 | "null" (. CodeGen.append("null"); .) . 86 87 VarDeclarations 88 = Type (. CodeGen.indentNewLine(); .) 89 OneVar 90 { WEAK "," (. CodeGen.append(", "); .) 91 OneVar 92 } 93 WEAK ";" (. CodeGen.append(";"); CodeGen.exdent(); .) . 94 95 Type 96 = BasicType 97 [ "[]" (. CodeGen.append("[]"); .) 98 ] . 99 100 BasicType 101 = "int" (. CodeGen.append("int"); .) 102 | "bool" (. CodeGen.append("bool"); .) 103 | "char" (. CodeGen.append("char"); .) . 104 105 OneVar (. String name; .) 106 = Ident<out name> (. CodeGen.append(name); .) 107 [ "=" (. CodeGen.append(" = "); .) 108 Expression 109 ] . 110 111 AssignmentStatement 112 = Assignment WEAK ";" (. CodeGen.append(";"); .) . 113 114 Assignment 115 = Designator 116 ( AssignOp Expression 117 | "++" (. CodeGen.append("++"); .) 118 | "--" (. CodeGen.append("--"); .) 119 ) 120 | "++" (. CodeGen.append("++"); .) 121 Designator 122 | "--" (. CodeGen.append("--"); .) 123 Designator . 124 125 Designator (. String name; .) 126 = Ident<out name> (. CodeGen.append(name); .) 127 [ "[" (. CodeGen.append("["); .) 128 Expression 129 "]" (. CodeGen.append("]"); .) 130 ] . 131 132 IfStatement 133 = "if" (. CodeGen.append("if "); .) 134 "(" (. CodeGen.append("("); .) 135 Condition 136 ")" (. CodeGen.append(")"); .) 137 Statement<indented> 138 { "elsif" "(" (. CodeGen.newLine(); CodeGen.append("elsif ("); .) 139 Condition 140 ")" (. CodeGen.append(")"); .) 141 Statement<indented> 142 } 143 [ "else" (. CodeGen.newLine(); CodeGen.append("else"); .) 144 Statement<indented> 145 ] . 146 147 WhileStatement 148 = "while" (. CodeGen.append("while "); .) 149 "(" (. CodeGen.append("("); .) 150 Condition 151 ")" (. CodeGen.append(")"); .) 152 Statement<indented> . 153 154 DoWhileStatement 155 = "do" (. CodeGen.append("do"); .) 156 Statement<indented> 157 WEAK "while" (. CodeGen.newLine(); CodeGen.append("while "); .) 158 "(" (. CodeGen.append("("); .) 159 Condition 160 ")" (. CodeGen.append(")"); .) 161 WEAK ";" (. CodeGen.append(";"); .) . 162 163 ForStatement 164 = "for" (. CodeGen.append("for "); .) 165 ForControl 166 Statement<indented> . 167 168 ForControl (. String name; .) 169 = "(" (. CodeGen.append("("); .) 170 [ [ BasicType (. CodeGen.append(" "); .) 171 ] 172 Ident<out name> (. CodeGen.append(name); .) 173 "=" (. CodeGen.append(" = "); .) 174 Expression 175 ] ";" (. CodeGen.append("; "); .) 176 [ Condition ] ";" (. CodeGen.append("; "); .) 177 [ Assignment ] (. CodeGen.append(")"); .) 178 ")" 179 | Ident<out name> (. CodeGen.append(name); .) 180 "=" (. CodeGen.append(" = "); .) 181 Expression 182 ( "to" (. CodeGen.append(" to "); .) 183 | "downto" (. CodeGen.append(" downto "); .) 184 ) 185 Expression . 186 187 BreakStatement 188 = "break" (. CodeGen.append("break"); .) 189 WEAK ";" (. CodeGen.append(";"); .) . 190 191 HaltStatement 192 = "halt" (. CodeGen.append("halt"); .) 193 WEAK ";" (. CodeGen.append(";"); .) . 194 195 ReadStatement 196 = "read" (. CodeGen.append("read"); .) 197 "(" (. CodeGen.append("("); .) 198 ReadElement 199 { WEAK "," (. CodeGen.append(", "); .) 200 ReadElement 201 } 202 ")" (. CodeGen.append(")"); .) 203 WEAK ";" (. CodeGen.append(";"); .) . 204 205 ReadLineStatement 206 = "readLine" (. CodeGen.append("readLine"); .) 207 "(" (. CodeGen.append("("); .) 208 [ ReadElement 209 { WEAK "," (. CodeGen.append(", "); .) 210 ReadElement 211 } 212 ] 213 ")" (. CodeGen.append(")"); .) 214 WEAK ";" (. CodeGen.append(";"); .) . 215 216 ReadElement (. String str; .) 217 = ( StringConst<out str> (. CodeGen.append(str); .) 218 | Designator 219 ) . 220 221 WriteStatement 222 = "write" (. CodeGen.append("write"); .) 223 "(" (. CodeGen.append("("); .) 224 WriteElement 225 { WEAK "," (. CodeGen.append(", "); .) 226 WriteElement 227 } 228 ")" (. CodeGen.append(")"); .) 229 WEAK ";" (. CodeGen.append(";"); .) . 230 231 WriteLineStatement 232 = "writeLine" (. CodeGen.append("writeLine"); .) 233 "(" (. CodeGen.append("("); .) 234 [ WriteElement 235 { WEAK "," (. CodeGen.append(", "); .) 236 WriteElement 237 } 238 ] 239 ")" (. CodeGen.append(")"); .) 240 WEAK ";" (. CodeGen.append(";"); .) . 241 242 WriteElement (. String str; .) 243 = ( StringConst<out str> (. CodeGen.append(str); .) 244 | Expression 245 ) . 246 247 Condition 248 = Expression . 249 250 Expression 251 = AndExp 252 { "||" (. CodeGen.append(" || "); .) 253 AndExp 254 } . 255 256 AndExp 257 = EqlExp 258 { "&&" (. CodeGen.append(" && "); .) 259 EqlExp 260 } . 261 262 EqlExp 263 = RelExp 264 { EqualOp 265 RelExp 266 } . 267 268 RelExp 269 = AddExp 270 [ RelOp 271 AddExp 272 ] . 273 274 AddExp 275 = MultExp 276 { AddOp 277 MultExp 278 } . 279 280 MultExp 281 = Factor 282 { MulOp 283 Factor 284 } . 285 286 Factor 287 = Primary 288 | "+" (. CodeGen.append(" +"); .) 289 Factor 290 | "-" (. CodeGen.append(" -"); .) 291 Factor 292 | "!" (. CodeGen.append("!"); .) 293 Factor . 294 295 Primary 296 = Designator 297 | Constant 298 | "new" (. CodeGen.append("new "); .) 299 BasicType 300 "[" (. CodeGen.append("["); .) 301 Expression 302 "]" (. CodeGen.append("]"); .) 303 | "(" (. CodeGen.append("("); .) 304 ( "char" ")" (. CodeGen.append("char)"); .) 305 Factor 306 | "int" ")" (. CodeGen.append("int)"); .) 307 Factor 308 | Expression 309 ")" (. CodeGen.append(")"); .) 310 ) . 311 312 AddOp 313 = "+" (. CodeGen.append(" + "); .) 314 | "-" (. CodeGen.append(" - "); .) . 315 316 MulOp 317 = "*" (. CodeGen.append(" * "); .) 318 | "/" (. CodeGen.append(" / "); .) 319 | "%" (. CodeGen.append(" % "); .) . 320 321 EqualOp 322 = "==" (. CodeGen.append(" == "); .) 323 | "!=" (. CodeGen.append(" != "); .) . 324 325 RelOp 326 = "<" (. CodeGen.append(" < "); .) 327 | "<=" (. CodeGen.append(" <= "); .) 328 | ">" (. CodeGen.append(" > "); .) 329 | ">=" (. CodeGen.append(" >= "); .) . 330 331 AssignOp 332 = "=" (. CodeGen.append(" = "); .) . 333 334 Ident<out String name> 335 = identifier (. name = token.val; .) . 336 337 StringConst<out String name> 338 = stringLit (. name = token.val; .) . 339 340 CharConst<out String name> 341 = charLit (. name = token.val; .) . 342 343 IntConst<out String name> 344 = number (. name = token.val; .) . 345 346 END PrettyParva. 1 // Code Generation for Parva level 1 pretty-printers (Java version) 2 // P.D. Terry, Rhodes University, 2011 3 4 package PrettyParva; 5 6 import library.*; 7 import java.util.*; 8 9 class CodeGen { 10 public static OutFile output = null; 11 12 static int indentation = 0; 13 14 public static void openOutput(String fileName) { 15 // Opens output file from specified fileName 16 output = new OutFile(fileName); 17 if (output.openError()) { 18 System.out.println("cannot open " + fileName); 19 System.exit(1); 20 } 21 } 22 23 public static void closeOutput() { 24 // Closes output file 25 output.close(); 26 } 27 28 public static void append(String str) { 29 // Appends str to output file 30 output.write(str); 31 } 32 33 public static void newLine() { 34 // Writes line mark to output file but leaves indentation as before 35 output.writeLine(); 36 appendLeadingSpaces(); 37 } 38 39 static void appendLeadingSpaces() { 40 // Appends the spaces needed at the start of each line of output before a statement 41 for (int j = 1; j <= indentation; j++) output.write(' '); 42 } 43 44 public static void indentNewLine() { 45 // Writes line mark to output file and prepares to indent further lines 46 // by two spaces more than before 47 indentation += 2; 48 newLine(); 49 } 50 51 public static void exdentNewLine() { 52 // Writes line mark to output file and prepares to indent further lines 53 // by two spaces less 54 indentation -= 2; 55 newLine(); 56 } 57 58 public static void indent() { 59 // Increments indentation level by 2 60 indentation += 2; 61 } 62 63 public static void exdent() { 64 // Decrements indentation level by 2 65 indentation -= 2; 66 } 67 } // CodeGen 1 COMPILER Mikra $CN 2 /* Mikra level 1.5 grammar 3 P.D. Terry, Rhodes University, 2011 */ 4 5 CHARACTERS 6 lf = CHR(10) . 7 backslash = CHR(92) . 8 control = CHR(0) .. CHR(31) . 9 letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" . 10 digit = "0123456789" . 11 nonZeroDigit = "123456789" . 12 stringCh = ANY - '"' - control - backslash . 13 charCh = ANY - "'" - control - backslash . 14 printable = ANY - control . 15 16 TOKENS 17 identifier = letter { letter | digit | "_" { "_" } ( letter | digit ) } . 18 number = "0" | nonZeroDigit { digit } . 19 stringLit = '"' { stringCh | backslash printable } '"' . 20 charLit = "'" ( charCh | backslash printable ) "'" . 21 22 COMMENTS FROM "(*" TO "*)" 23 24 IGNORE CHR(9) .. CHR(13) 25 26 PRODUCTIONS 27 Mikra 28 = "program" Ident ";" 29 Block Ident "." . 30 31 Block 32 = { ConstDeclarations | VarDeclarations } 33 "begin" StatementSequence "end" . 34 35 StatementSequence 36 = Statement { ";" Statement } . 37 38 Statement 39 = SYNC 40 [ AssignmentStatement | IfStatement 41 | WhileStatement | HaltStatement 42 | ReadStatement | WriteStatement 43 | ForStatement | RepeatStatement 44 | LoopStatement | ExitStatement 45 | IncOrDecStatement 46 ] . 47 48 ConstDeclarations 49 = "const" OneConst { "," OneConst } ";" . 50 51 OneConst 52 = Ident "=" Constant . 53 54 Constant 55 = IntConst | CharConst | "true" | "false" | "null" . 56 57 VarDeclarations 58 = "var" VarList ";" { VarList ";" } . 59 60 VarList 61 = Ident { "," Ident } ":" Type . 62 63 Type 64 = [ "array" "of" ] BasicType . 65 66 BasicType 67 = "int" | "bool" | "char" . 68 69 AssignmentStatement 70 = Designator AssignOp Expression . 71 72 Designator 73 = Ident [ "[" Expression "]" ] . 74 75 IfStatement 76 = "if" Condition "then" StatementSequence 77 { "elsif" Condition "then" StatementSequence } 78 [ "else" StatementSequence ] 79 "end" . 80 81 WhileStatement 82 = "while" Condition "do" StatementSequence "end" . 83 84 RepeatStatement 85 = "repeat" StatementSequence "until" Condition . 86 87 LoopStatement 88 = "loop" StatementSequence "end" . 89 90 ForStatement 91 = "for" Ident ":=" Expression ( "to" | "downto" ) Expression "do" StatementSequence "end" . 92 93 HaltStatement 94 = "halt" . 95 96 ExitStatement 97 = "exit" . 98 99 IncOrDecStatement 100 = ( "inc" | "dec" ) "(" Designator ")" . 101 102 ReadStatement 103 = "read" ReadList 104 | "readLn" [ ReadList ] . 105 106 ReadList 107 = "(" ReadElement { "," ReadElement } ")" . 108 109 ReadElement 110 = StringConst | Designator . 111 112 WriteStatement 113 = "write" WriteList 114 | "writeLn" [ WriteList ] . 115 116 WriteList = "(" WriteElement { "," WriteElement } ")" . 117 118 WriteElement 119 = StringConst | Expression . 120 121 Condition 122 = Expression . 123 124 Expression = AddExp [ RelOp AddExp ] . 125 126 AddExp 127 = [ "+" | "-" ] MulExp { AddOp MulExp } . 128 129 MulExp 130 = Factor { MulOp Factor } . 131 132 Factor 133 = Designator | Constant 134 | "new" BasicType "[" Expression "]" 135 | "char" "(" Expression ")" 136 | "int" "(" Expression ")" 137 | "(" Expression ")" 138 | NotOp Factor . 139 140 NotOp 141 = "not" . 142 143 MulOp 144 = "*" | "/" | "mod" | "and" . 145 146 AddOp 147 = "+" | "-" | "or" . 148 149 RelOp 150 = "=" | "<>" | "<" | "<=" | ">" | ">=" . 151 152 AssignOp 153 = ":=" . 154 155 Ident = identifier . 156 157 StringConst = stringLit . 158 159 CharConst = charLit . 160 161 IntConst = number . 162 163 END Mikra.