(* Pascal P4 ATG for Coco/R *) (* Pascal-4 (1977) - N. Wirth's fourth Pascal implementation *) (* Features: Nested procedures/functions, empty procedures *) (* Tags: P4, PASCAL4, WIRTH *) COMPILER P4 IGNORE CASE CHARACTERS eol = CHR(13) . letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" . digit = "0123456789" . noQuote1 = ANY - "'" - eol . IGNORE CHR(9) .. CHR(13) COMMENTS FROM "(*" TO "*)" NESTED COMMENTS FROM "{" TO "}" TOKENS ident = letter { letter | digit } . pnumber = digit { digit } | digit { digit } CONTEXT ( ".." ) . preal = digit { digit } "." digit { digit } [ "E" [ "+" | "-" ] digit { digit } ] . pstring = "'" { noQuote1 | "''" } "'" . PRODUCTIONS P4 = "program" ident ";" Block "." . Block = [ UnitSection ] DeclPart [ Compound ] . Compound = "begin" Statements "end" . UnitSection = "unit" ident ";" { FileSection } "end" ident "." . FileSection = ident { ";" ident } . DeclPart = [ LabelSection ] [ ConstSection ] [ TypeSection ] [ VarSection ] { ProcDecl }. LabelSection = "label" Label { "," Label } ";" . ConstSection = "const" [ ConstDecl { ";" [ ConstDecl ] } ] . ConstDecl = ident "=" Value . Value = Expr . TypeSection = "type" [ TypeDecl { ";" [ TypeDecl ] } ] . TypeDecl = ident "=" Type . VarSection = "var" [ VarDecl { ";" [ VarDecl ] } ] . VarDecl = identlist ":" Type . ProcDecl = ( "procedure" | "function" ) ident [ FormalParams ] [ ":" Type ] ";" Block ";" . FormalParams = "(" [ FormalGroup { ";" FormalGroup } ] ")" . FormalGroup = [ "var" ] identlist ":" Type . identlist = ident { "," ident } . Type = SimpleType | StructType | PointerType | EnumType | TypeIdent . EnumType = "(" ident { "," ident } ")" . TypeIdent = ident { "." ident } [ ".." ( ident | [ "-" ] pnumber ) ] | [ "-" ] pnumber [ ".." ( ident | [ "-" ] pnumber ) ] . PointerType = "^" Type . SimpleType = integertype | realtype | charType | booleanType . integertype = "integer" . realtype = "real" . charType = "char" . booleanType = "boolean" . StructType = ArrayType | RecordType | SetType | ProcType . ArrayType = "array" "[" IndexList "]" "of" Type . IndexList = Index { "," Index } . Index = SimpleType | Value [ ".." Value ] . RecordType = "record" FieldList "end" . FieldList = [ Field { ";" [ Field ] } ] . Field = identlist ":" Type . SetType = "set" "of" Type . ProcType = "procedure" ParamList ";" { [ ConstDecl ";" TypeDecl ";" VarDecl ";" ProcDecl ] } ";" . Statements = [ Statement { ";" [ Statement ] } ] . IfStatement = "if" Expr "then" Statement { "else" Statement } . Statement = [ pnumber ":" ] ( IfStatement | CaseStmt | WhileStmt | RepeatStmt | ForStmt | "goto" Label | ident IdentTail | ReturnStmt | Compound | ProcDecl ) . (* Nested procedure *) CaseStmt = "case" Expr "of" [ CaseItem { ";" [ CaseItem ] } ] "end" . CaseItem = LabelList ":" Statement . LabelList = Label { "," Label } . Label = pnumber | ident . WhileStmt = "while" Expr "do" Statement . RepeatStmt = "repeat" Statements "until" Expr . ForStmt = "for" ident ":=" Expr ( "to" | "downto" ) Expr "do" Statement . IdentTail = [ Selector { Selector } ] [ ":=" Expr | "(" [ ParamList ] ")" ] . Selector = "." ident | "[" Expr { "," Expr } "]" | "^" . ParamList = Param { "," Param } . Param = Expr . ReturnStmt = "return" [ Expr ] . Expr = SimpleExpr [ Relationship SimpleExpr ] . Relationship = "=" | "#" | "<" | ">" | "<=" | ">=" | "<>" . SimpleExpr = ["+" | "-"] Term { ("+" | "-" | "or") Term } . Term = Factor { ("*" | "/" | "div" | "mod" | "&" | "and") Factor } . Factor = ["+" | "-"] ["not"] Primary . Primary = pnumber | preal | pstring | "(" Expr ")" | ident { Selector } [ "(" [ ParamList ] ")" ] . END P4.