Coco/R - Compiler-Compiler V1.53 Released by Pat Terry 17 September 2002 Source file: pascal_impl/p4_pascal.atg Listing: 1 (* Pascal P4 ATG for Coco/R *) 2 (* Pascal-4 (1977) - N. Wirth's fourth Pascal implementation *) 3 (* Features: Nested procedures/functions, empty procedures *) 4 5 (* Tags: P4, PASCAL4, WIRTH *) 6 7 COMPILER PascalP4 8 9 CHARACTERS 10 eol = CHR(13) . 11 letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" . 12 digit = "0123456789" . 13 IGNORE CHR(9) .. CHR(13) 14 COMMENTS FROM "(*" TO "*)" NESTED 15 16 TOKENS 17 ident = letter { letter | digit } . 18 19 PRODUCTIONS 20 21 Program = "program" ident ";" Block "." . 22 23 Block = [ UnitSection ] DeclPart Statements . 24 25 UnitSection = "unit" ident ";" { FileSection } "end" ident "." . 26 27 FileSection = ident { ";" ident } . 28 29 DeclPart = [ ConstSection ] [ TypeSection ] [ VarSection ] [ ProcDecl ]. 30 31 ConstSection = "const" ConstDeclList { ";" ConstDeclList } . 32 33 ConstDeclList = ident "=" Value { ";" ident "=" Value } . 34 35 TypeSection = "type" TypeDeclList { ";" TypeDeclList } . 36 37 TypeDeclList = ident "=" Type { ";" ident "=" Type } . 38 39 VarSection = "var" VarDeclList { ";" VarDeclList } . 40 41 VarDeclList = identlist ":" Type . 42 43 identlist = ident { "," ident } . 44 45 Type = SimpleType | StructType | PointerType | ArrayType | SetType | ProcType . 46 47 SimpleType = integertype | realtype | charType | booleanType . 48 49 integertype = "integer" . 50 realtype = "real" . 51 charType = "char" . 52 booleanType = "boolean" . 53 54 StructType = ArrayType | RecordType | SetType | ProcType . 55 56 ArrayType = "array" "[" IndexList "]" "of" Type . 57 58 IndexList = SimpleType { "," SimpleType } . 59 60 RecordType = "record" FieldList "end" . 61 62 FieldList = Field { ";" Field } . 63 64 Field = identlist ":" Type . 65 66 SetType = "set" "of" Type . 67 68 ProcType = "procedure" ParList ";" { [ ConstDecl ; TypeDecl ; VarDecl ; ProcDecl ] } ";" . ***** ^ invalid Factor ***** ^ invalid Factor ***** ^ invalid Factor 69 70 Statements = Statement { ";" Statement } . 71 72 Statement = IfStatement 73 | CaseStatement 74 | WhileStatement 75 | RepeatStatement 76 | ForStatement 77 | Assignment 78 | ProcCall 79 | ReturnStatement 80 | EmptyStatement 81 | ProcDecl ; (* Nested procedure *) ***** ^ invalid Factor 82 83 IfStatement = "if" Expr "then" Stmts { "else" Stmts } . ***** ^ invalid Factor 84 85 CaseStmt = "case" Expr of { CaseItem ";" } "end" . 86 87 CaseItem = LabelList ":" Stmts . 88 89 LabelList = Label { "," Label } . 90 91 Label = number . 92 93 WhileStmt = "while" Expr "do" Stmts . 94 95 RepeatStmt = "repeat" Stmts "until" Expr . 96 97 ForStmt = "for" ident ":=" Expr "to" Expr "do" Stmts 98 | "for" ident ":=" Expr "downto" Expr "do" Stmts . 99 100 Assignment = Var ":=" Expr . 101 102 ProcCall = ident "(" [ ParamList ] ")" . 103 104 ParamList = Param { "," Param } . 105 106 Param = Expr . 107 108 ReturnStmt = "return" [ Expr ] . 109 110 EmptyStatement = "" . 111 112 Expr = SimpleExpr [ Relationship SimpleExpr ] . 113 114 Relationship = "=" | "#" | "<" | ">" | "<=" | ">=" | "<>" . 115 116 SimpleExpr = ["+" | "-"] Term { ("+" | "-") Term } . 117 118 Term = Factor { ("*" | "/" | "div" | "mod" | "&") Factor } . 119 120 Factor = ["+" | "-"] ["not"] Primary . 121 122 Primary = number | ident | string | "(" Expr ")" | Primary "^" . 123 124 number = digit { digit } . 125 126 ident = letter { letter | digit } . 127 128 string = "'" { noQuote1 | "''" } "'" . 129 130 EndProgram = "." . 131 EOF 5 errors