(* 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 PascalP4 CHARACTERS eol = CHR(13) . letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" . digit = "0123456789" . IGNORE CHR(9) .. CHR(13) COMMENTS FROM "(*" TO "*)" NESTED TOKENS ident = letter { letter | digit } . PRODUCTIONS Program = "program" ident ";" Block "." . Block = [ UnitSection ] DeclPart Statements . UnitSection = "unit" ident ";" { FileSection } "end" ident "." . FileSection = ident { ";" ident } . DeclPart = [ ConstSection ] [ TypeSection ] [ VarSection ] [ ProcDecl ]. ConstSection = "const" ConstDeclList { ";" ConstDeclList } . ConstDeclList = ident "=" Value { ";" ident "=" Value } . TypeSection = "type" TypeDeclList { ";" TypeDeclList } . TypeDeclList = ident "=" Type { ";" ident "=" Type } . VarSection = "var" VarDeclList { ";" VarDeclList } . VarDeclList = identlist ":" Type . identlist = ident { "," ident } . Type = SimpleType | StructType | PointerType | ArrayType | SetType | ProcType . SimpleType = integertype | realtype | charType | booleanType . integertype = "integer" . realtype = "real" . charType = "char" . booleanType = "boolean" . StructType = ArrayType | RecordType | SetType | ProcType . ArrayType = "array" "[" IndexList "]" "of" Type . IndexList = SimpleType { "," SimpleType } . RecordType = "record" FieldList "end" . FieldList = Field { ";" Field } . Field = identlist ":" Type . SetType = "set" "of" Type . ProcType = "procedure" ParList ";" { [ ConstDecl ; TypeDecl ; VarDecl ; ProcDecl ] } ";" . Statements = Statement { ";" Statement } . Statement = IfStatement | CaseStatement | WhileStatement | RepeatStatement | ForStatement | Assignment | ProcCall | ReturnStatement | EmptyStatement | ProcDecl ; (* Nested procedure *) IfStatement = "if" Expr "then" Stmts { "else" Stmts } . CaseStmt = "case" Expr of { CaseItem ";" } "end" . CaseItem = LabelList ":" Stmts . LabelList = Label { "," Label } . Label = number . WhileStmt = "while" Expr "do" Stmts . RepeatStmt = "repeat" Stmts "until" Expr . ForStmt = "for" ident ":=" Expr "to" Expr "do" Stmts | "for" ident ":=" Expr "downto" Expr "do" Stmts . Assignment = Var ":=" Expr . ProcCall = ident "(" [ ParamList ] ")" . ParamList = Param { "," Param } . Param = Expr . ReturnStmt = "return" [ Expr ] . EmptyStatement = "" . Expr = SimpleExpr [ Relationship SimpleExpr ] . Relationship = "=" | "#" | "<" | ">" | "<=" | ">=" | "<>" . SimpleExpr = ["+" | "-"] Term { ("+" | "-") Term } . Term = Factor { ("*" | "/" | "div" | "mod" | "&") Factor } . Factor = ["+" | "-"] ["not"] Primary . Primary = number | ident | string | "(" Expr ")" | Primary "^" . number = digit { digit } . ident = letter { letter | digit } . string = "'" { noQuote1 | "''" } "'" . EndProgram = "." . EOF