1// 2// Pascal Tree Super Grammar (symtab.g derives from this) 3// 4// Adapted from, 5// Pascal User Manual And Report (Second Edition-1978) 6// Kathleen Jensen - Niklaus Wirth 7// 8// By 9// 10// Hakki Dogusan dogusanh@tr-net.net.tr 11// 12// Then significantly enhanced by Piet Schoutteten 13// with some guidance by Terence Parr. Piet added tree 14// construction, and some tree walkers. 15// 16 17class PascalTreeParserSuper extends TreeParser; 18 19options { 20 importVocab = Pascal; 21 ASTLabelType = "PascalAST"; 22} 23 24program 25 : programHeading 26 block 27 ; 28 29programHeading 30 : #(PROGRAM IDENT identifierList) 31 | #(UNIT IDENT) 32 ; 33 34identifier 35 : IDENT 36 ; 37 38block 39 : ( labelDeclarationPart 40 | constantDefinitionPart 41 | typeDefinitionPart 42 | variableDeclarationPart 43 | procedureAndFunctionDeclarationPart 44 | usesUnitsPart 45 | IMPLEMENTATION 46 )* 47 compoundStatement 48 ; 49 50usesUnitsPart 51 : #(USES identifierList) 52 ; 53 54labelDeclarationPart 55 : #(LABEL ( label )+) 56 ; 57 58label 59 : NUM_INT 60 ; 61 62constantDefinitionPart 63 : #(CONST ( constantDefinition )+ ) 64 ; 65 66constantDefinition 67 : #(EQUAL IDENT constant) 68 ; 69 70constant 71 : NUM_INT 72 | NUM_REAL 73 | #( PLUS 74 ( NUM_INT 75 | NUM_REAL 76 | IDENT 77 ) 78 ) 79 | #( MINUS 80 ( NUM_INT 81 | NUM_REAL 82 | IDENT 83 ) 84 ) 85 | IDENT 86 | STRING_LITERAL 87 | #(CHR (NUM_INT|NUM_REAL)) 88 ; 89 90string 91 : STRING_LITERAL 92 ; 93 94typeDefinitionPart 95 : #(TYPE ( typeDefinition )+) 96 ; 97 98typeDefinition 99 : #(TYPEDECL IDENT 100 ( type 101 | #(FUNCTION (formalParameterList)? resultType) 102 | #(PROCEDURE (formalParameterList)?) 103 ) 104 ) 105 ; 106 107type 108 : #(SCALARTYPE identifierList) 109 | #(DOTDOT constant constant) 110 | typeIdentifier 111 | structuredType 112 | #(POINTER typeIdentifier) 113 ; 114 115typeIdentifier 116 : IDENT 117 | CHAR 118 | BOOLEAN 119 | INTEGER 120 | REAL 121 | #( STRING 122 ( IDENT 123 | NUM_INT 124 | NUM_REAL 125 | 126 ) 127 ) 128 ; 129 130structuredType 131 : #(PACKED unpackedStructuredType) 132 | unpackedStructuredType 133 ; 134 135unpackedStructuredType 136 : arrayType 137 | recordType 138 | setType 139 | fileType 140 ; 141 142/** Note here that the syntactic diff between brackets disappears. 143 * If the brackets mean different things semantically, we need 144 * two different alternatives here. 145 */ 146arrayType 147 : #(ARRAY typeList type) 148 ; 149 150typeList 151 : #( TYPELIST ( type )+ ) 152 ; 153 154recordType 155 : #(RECORD fieldList) 156 ; 157 158fieldList 159 : #( FIELDLIST 160 ( fixedPart ( variantPart )? 161 | variantPart 162 ) 163 ) 164 ; 165 166fixedPart 167 : ( recordSection )+ 168 ; 169 170recordSection 171 : #(FIELD identifierList type) 172 ; 173 174variantPart 175 : #( CASE tag ( variant )+ ) 176 ; 177 178tag 179 : #(VARIANT_TAG identifier typeIdentifier) 180 | #(VARIANT_TAG_NO_ID typeIdentifier) 181 ; 182 183variant 184 : #(VARIANT_CASE constList fieldList) 185 ; 186 187setType 188 : #(SET type) 189 ; 190 191fileType 192 : #(FILE (type)?) 193 ; 194 195/** Yields a list of VARDECL-rooted subtrees with VAR at the overall root */ 196variableDeclarationPart 197 : #( VAR ( variableDeclaration )+ ) 198 ; 199 200variableDeclaration 201 : #(VARDECL identifierList type) 202 ; 203 204procedureAndFunctionDeclarationPart 205 : procedureOrFunctionDeclaration 206 ; 207 208procedureOrFunctionDeclaration 209 : procedureDeclaration 210 | functionDeclaration 211 ; 212 213procedureDeclaration 214 : #(PROCEDURE IDENT (formalParameterList)? block ) 215 ; 216 217formalParameterList 218 : #(ARGDECLS ( formalParameterSection )+) 219 ; 220 221formalParameterSection 222 : parameterGroup 223 | #(VAR parameterGroup) 224 | #(FUNCTION parameterGroup) 225 | #(PROCEDURE parameterGroup) 226 ; 227 228parameterGroup 229 : #(ARGDECL identifierList typeIdentifier) 230 ; 231 232identifierList 233 : #(IDLIST (IDENT)+) 234 ; 235 236constList 237 : #(CONSTLIST ( constant )+) 238 ; 239 240functionDeclaration 241 : #(FUNCTION IDENT (formalParameterList)? resultType block) 242 ; 243 244resultType 245 : typeIdentifier 246 ; 247 248statement 249 : #(COLON label unlabelledStatement) 250 | unlabelledStatement 251 ; 252 253unlabelledStatement 254 : simpleStatement 255 | structuredStatement 256 ; 257 258simpleStatement 259 : assignmentStatement 260 | procedureStatement 261 | gotoStatement 262 ; 263 264assignmentStatement 265 : #(ASSIGN variable expression) 266 ; 267 268/** A variable is an id with a suffix and can look like: 269 * id 270 * id[expr,...] 271 * id.id 272 * id.id[expr,...] 273 * id^ 274 * id^.id 275 * id^.id[expr,...] 276 * ... 277 * 278 * LL has a really hard time with this construct as it's naturally 279 * left-recursive. We have to turn into a simple loop rather than 280 * recursive loop, hence, the suffixes. I keep in the same rule 281 * for easy tree construction. 282 */ 283variable 284 : #(LBRACK variable (expression)+) 285 | #(LBRACK2 variable (expression)+) 286 | #(DOT variable IDENT) 287 | #(POINTER variable) 288 | #(AT IDENT) 289 | IDENT 290 ; 291 292expression 293 : #(EQUAL expression expression) 294 | #(NOT_EQUAL expression expression) 295 | #(LT expression expression) 296 | #(LE expression expression) 297 | #(GE expression expression) 298 | #(GT expression expression) 299 | #(IN expression expression) 300 | #(PLUS expression (expression)?) 301 | #(MINUS expression (expression)?) 302 | #(OR expression expression) 303 | #(STAR expression expression) 304 | #(SLASH expression expression) 305 | #(DIV expression expression) 306 | #(MOD expression expression) 307 | #(AND expression expression) 308 | #(NOT expression) 309 | variable 310 | functionDesignator 311 | set 312 | NUM_INT 313 | NUM_REAL 314 | #(CHR (NUM_INT|NUM_REAL)) 315 | string 316 | NIL 317 ; 318 319functionDesignator 320 : #(FUNC_CALL IDENT (parameterList)?) 321 ; 322 323parameterList 324 : #( ARGLIST (actualParameter)+ ) 325 ; 326 327set 328 : #(SET (element)*) 329 ; 330 331element 332 : #(DOTDOT expression expression) 333 | expression 334 ; 335 336procedureStatement 337 : #(PROC_CALL IDENT ( parameterList )?) 338 ; 339 340actualParameter 341 : expression 342 ; 343 344gotoStatement 345 : #(GOTO label) 346 ; 347 348structuredStatement 349 : compoundStatement 350 | conditionalStatement 351 | repetetiveStatement 352 | withStatement 353 ; 354 355compoundStatement 356 : statements 357 ; 358 359statements 360 : #(BLOCK (statement)*) 361 ; 362 363conditionalStatement 364 : ifStatement 365 | caseStatement 366 ; 367 368ifStatement 369 : #(IF expression statement (statement)?) 370 ; 371 372caseStatement //pspsps ??? 373 : #(CASE expression 374 ( caseListElement )+ 375 ( statements )? 376 ) 377 ; 378 379caseListElement 380 : #(COLON constList statement) 381 ; 382 383repetetiveStatement 384 : whileStatement 385 | repeatStatement 386 | forStatement 387 ; 388 389whileStatement 390 : #(WHILE expression statement) 391 ; 392 393repeatStatement 394 : #(REPEAT statements expression) 395 ; 396 397forStatement 398 : #(FOR IDENT forList statement) 399 ; 400 401forList 402 : #(TO initialValue finalValue) 403 | #(DOWNTO initialValue finalValue) 404 ; 405 406initialValue 407 : expression 408 ; 409 410finalValue 411 : expression 412 ; 413 414withStatement 415 : #(WITH recordVariableList statement) 416 ; 417 418recordVariableList 419 : ( variable )+ 420 ; 421