| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- COMPILER Pascal
- (* J & W Pascal - not Turbo Pascal *)
- (* This grammar is not LL(1) *)
- CHARACTERS
- eol = CHR(13) .
- letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
- digit = "0123456789".
- noQuote1 = ANY - "'" - eol .
- IGNORE CHR(9) .. CHR(13)
- IGNORE CASE
- COMMENTS FROM "(*" TO "*)"
- COMMENTS FROM "{" TO "}"
- TOKENS
- identifier = letter { letter | digit } .
- integer = digit { digit } | digit { digit } CONTEXT ("..") .
- real = digit { digit } "." digit { digit }
- [ "E" ["+" | "-"] digit { digit } ]
- | digit { digit } "E" ["+" | "-"] digit { digit } .
- string = "'" { noQuote1 | "''" } "'" .
- PRODUCTIONS
- Pascal = "program" NewIdent [ ExternalFiles ] ";"
- Block "." .
- ExternalFiles = "(" NewIdentList ")" .
- Block = DeclarationPart StatementPart .
- DeclarationPart = LabelDeclarations ConstDefinitions
- TypeDefinitions VarDeclarations
- { ProcDeclarations } .
- (* -------------------------------------------------------------------- *)
- LabelDeclarations = [ "label" Labels ";" ] .
- Labels = Label { "," Label } .
- Label = UnsignedInt .
- (* -------------------------------------------------------------------- *)
- ConstDefinitions = [ "const" ConstDef { ConstDef } ] .
- ConstDef = NewIdent "=" Constant ";" .
- Constant = [ "+" | "-" ] ( UnsignedNumber | ConstIdent ) | String .
- UnsignedNumber = UnsignedInt | UnsignedReal .
- ConstIdent = identifier .
- (* -------------------------------------------------------------------- *)
- TypeDefinitions = [ "type" TypeDef { TypeDef } ] .
- TypeDef = NewIdent "=" Type ";" .
- Type = SimpleType | [ "packed" ] StructType | "^" TypeIdent .
- SimpleType = TypeIdent | EnumerationType | SubrangeType .
- TypeIdent = identifier .
- EnumerationType = "(" NewIdentList ")" .
- SubrangeType = Constant ".." Constant .
- StructType = ArrayType | RecordType | SetType | FileType .
- ArrayType = "array" "[" IndexList "]" "of" Type .
- IndexList = SimpleType { "," SimpleType } .
- RecordType = "record" FieldList "end" .
- SetType = "set" "of" SimpleType .
- FileType = "file" "of" Type .
- FieldList = [ ( fixedPart [ ";" VariantPart ] | VariantPart ) [ ";" ] ] .
- fixedPart = RecordSection { ";" RecordSection } .
- RecordSection = NewIdentList ":" Type .
- VariantPart = "case" VariantSelector "of" Variant { ";" Variant } .
- VariantSelector = [ NewIdent ":" ] TypeIdent .
- Variant = CaseLabelList ":" "(" FieldList ")" .
- (* -------------------------------------------------------------------- *)
- VarDeclarations = [ "var" VarDecl { VarDecl } ] .
- VarDecl = NewIdentList ":" Type ";" .
- (* -------------------------------------------------------------------- *)
- ProcDeclarations = ( ProcHeading | FuncHeading ) ";" Body ";" .
- ProcHeading = "procedure" NewIdent [ FormalParams ] .
- FuncHeading = "function" NewIdent [ FormalParams ] ReturnType .
- ReturnType = [ (* empty if forward referenced *) ":" TypeIdent ] .
- Body = Block | "forward" .
- FormalParams = "(" FormalSection { ";" FormalSection } ")" .
- FormalSection = [ "var" ] ParamGroup | ProcHeading | FuncHeading .
- ParamGroup = NewIdentList ":" ParamType .
- ParamType = TypeIdent
- | "array" "[" IndexSpecList "]" "of" ParamType
- | "packed" "array" "[" IndexSpec "]" "of" TypeIdent .
- IndexSpecList = IndexSpec { ";" IndexSpec } .
- IndexSpec = NewIdent ".." NewIdent ":" TypeIdent .
- (* -------------------------------------------------------------------- *)
- StatementPart = CompoundStatement .
- CompoundStatement = "begin" StatementSequence "end" .
- StatementSequence = Statement { ";" Statement } .
- Statement = [ Label ":" ]
- [ AssignmentOrCall
- | CompoundStatement
- | GotoStatement
- | WhileStatement
- | RepeatStatement
- | IfStatement
- | CaseStatement
- | ForStatement
- | WithStatement ] .
- AssignmentOrCall = Designator ( ":=" Expression | [ ActualParams ] ) .
- ActualParams = "(" ActualParameter { "," ActualParameter } ")" .
- ActualParameter = Expression [ FieldWidth (* only in i/o *) ] .
- FieldWidth = ":" IntegerExpression [ ":" IntegerExpression ] .
- GotoStatement = "goto" Label .
- WhileStatement = "while" BooleanExpression "do" Statement .
- RepeatStatement = "repeat" StatementSequence "until" BooleanExpression .
- IfStatement = "if" BooleanExpression "then" Statement
- [ "else" Statement ] .
- CaseStatement = "case" OrdinalExpression "of" CaseList "end" .
- CaseList = OneCase { ";" OneCase } [ ";" ] .
- OneCase = CaseLabelList ":" Statement .
- CaseLabelList = CaseLabel { "," CaseLabel } .
- CaseLabel = Constant .
- ForStatement = "for" ControlVariable ":=" OrdinalExpression
- ( "to" | "downto" ) OrdinalExpression "do" Statement .
- ControlVariable = identifier .
- WithStatement = "with" RecVarList "do" Statement .
- RecVarList = Designator { "," Designator } .
- (* -------------------------------------------------------------------- *)
- IntegerExpression = Expression .
- BooleanExpression = Expression .
- OrdinalExpression = Expression .
- Expression = SimpleExpression [ RelOp SimpleExpression ] .
- RelOp = "=" | "<" | ">" | "<=" | ">=" | "<>" | "in" .
- SimpleExpression = ( "+" Term | "-" Term | Term ) { AddOp Term } .
- AddOp = "+" | "-" | "or" .
- Term = Factor { MulOp Factor } .
- MulOp = "*" | "/" | "div" | "mod" | "and" .
- Factor = Designator [ ActualParams ]
- | UnsignedLiteral
- | SetConstructor
- | "(" Expression ")"
- | "not" Factor .
- Designator = identifier { "." identifier | "[" ExpList "]" | "^" } .
- ExpList = Expression { "," Expression } .
- UnsignedLiteral = UnsignedNumber | "nil" | String .
- SetConstructor = "[" Member { "," Member } "]" .
- Member = Expression [ ".." Expression ] .
- (* -------------------------------------------------------------------- *)
- NewIdentList = NewIdent { "," NewIdent } .
- NewIdent = identifier .
- UnsignedInt = integer .
- UnsignedReal = real .
- String = string .
- END Pascal.
|