pascal.atg 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. COMPILER Pascal
  2. IGNORE CASE
  3. (* J & W Pascal - not Turbo Pascal *)
  4. (* This grammar is not LL(1) *)
  5. CHARACTERS
  6. eol = CHR(13) .
  7. letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
  8. digit = "0123456789".
  9. noQuote1 = ANY - "'" - eol .
  10. IGNORE CHR(9) .. CHR(13)
  11. COMMENTS FROM "(*" TO "*)"
  12. COMMENTS FROM "{" TO "}"
  13. TOKENS
  14. identifier = letter { letter | digit } .
  15. integer = digit { digit } | digit { digit } CONTEXT ("..") .
  16. real = digit { digit } "." digit { digit }
  17. [ "E" ["+" | "-"] digit { digit } ]
  18. | digit { digit } "E" ["+" | "-"] digit { digit } .
  19. string = "'" { noQuote1 | "''" } "'" .
  20. PRODUCTIONS
  21. Pascal = "program" NewIdent [ ExternalFiles ] ";"
  22. Block "." .
  23. ExternalFiles = "(" NewIdentList ")" .
  24. Block = DeclarationPart StatementPart .
  25. DeclarationPart = LabelDeclarations ConstDefinitions
  26. TypeDefinitions VarDeclarations
  27. { ProcDeclarations } .
  28. (* -------------------------------------------------------------------- *)
  29. LabelDeclarations = [ "label" Labels ";" ] .
  30. Labels = Label { "," Label } .
  31. Label = UnsignedInt .
  32. (* -------------------------------------------------------------------- *)
  33. ConstDefinitions = [ "const" ConstDef { ConstDef } ] .
  34. ConstDef = NewIdent "=" Constant ";" .
  35. Constant = [ "+" | "-" ] ( UnsignedNumber | ConstIdent ) | String .
  36. UnsignedNumber = UnsignedInt | UnsignedReal .
  37. ConstIdent = identifier .
  38. (* -------------------------------------------------------------------- *)
  39. TypeDefinitions = [ "type" TypeDef { TypeDef } ] .
  40. TypeDef = NewIdent "=" Type ";" .
  41. Type = SimpleType | [ "packed" ] StructType | "^" TypeIdent .
  42. SimpleType = TypeIdent | EnumerationType | SubrangeType .
  43. TypeIdent = identifier .
  44. EnumerationType = "(" NewIdentList ")" .
  45. SubrangeType = Constant ".." Constant .
  46. StructType = ArrayType | RecordType | SetType | FileType .
  47. ArrayType = "array" "[" IndexList "]" "of" Type .
  48. IndexList = SimpleType { "," SimpleType } .
  49. RecordType = "record" FieldList "end" .
  50. SetType = "set" "of" SimpleType .
  51. FileType = "file" "of" Type .
  52. FieldList = [ ( fixedPart [ ";" VariantPart ] | VariantPart ) [ ";" ] ] .
  53. fixedPart = RecordSection { ";" RecordSection } .
  54. RecordSection = NewIdentList ":" Type .
  55. VariantPart = "case" VariantSelector "of" Variant { ";" Variant } .
  56. VariantSelector = [ NewIdent ":" ] TypeIdent .
  57. Variant = CaseLabelList ":" "(" FieldList ")" .
  58. (* -------------------------------------------------------------------- *)
  59. VarDeclarations = [ "var" VarDecl { VarDecl } ] .
  60. VarDecl = NewIdentList ":" Type ";" .
  61. (* -------------------------------------------------------------------- *)
  62. ProcDeclarations = ( ProcHeading | FuncHeading ) ";" Body ";" .
  63. ProcHeading = "procedure" NewIdent [ FormalParams ] .
  64. FuncHeading = "function" NewIdent [ FormalParams ] ReturnType .
  65. ReturnType = [ (* empty if forward referenced *) ":" TypeIdent ] .
  66. Body = Block | "forward" .
  67. FormalParams = "(" FormalSection { ";" FormalSection } ")" .
  68. FormalSection = [ "var" ] ParamGroup | ProcHeading | FuncHeading .
  69. ParamGroup = NewIdentList ":" ParamType .
  70. ParamType = TypeIdent
  71. | "array" "[" IndexSpecList "]" "of" ParamType
  72. | "packed" "array" "[" IndexSpec "]" "of" TypeIdent .
  73. IndexSpecList = IndexSpec { ";" IndexSpec } .
  74. IndexSpec = NewIdent ".." NewIdent ":" TypeIdent .
  75. (* -------------------------------------------------------------------- *)
  76. StatementPart = CompoundStatement .
  77. CompoundStatement = "begin" StatementSequence "end" .
  78. StatementSequence = Statement { ";" Statement } .
  79. Statement = [ Label ":" ]
  80. [ AssignmentOrCall
  81. | CompoundStatement
  82. | GotoStatement
  83. | WhileStatement
  84. | RepeatStatement
  85. | IfStatement
  86. | CaseStatement
  87. | ForStatement
  88. | WithStatement ] .
  89. AssignmentOrCall = Designator ( ":=" Expression | [ ActualParams ] ) .
  90. ActualParams = "(" ActualParameter { "," ActualParameter } ")" .
  91. ActualParameter = Expression [ FieldWidth (* only in i/o *) ] .
  92. FieldWidth = ":" IntegerExpression [ ":" IntegerExpression ] .
  93. GotoStatement = "goto" Label .
  94. WhileStatement = "while" BooleanExpression "do" Statement .
  95. RepeatStatement = "repeat" StatementSequence "until" BooleanExpression .
  96. IfStatement = "if" BooleanExpression "then" Statement
  97. [ "else" Statement ] .
  98. CaseStatement = "case" OrdinalExpression "of" CaseList "end" .
  99. CaseList = OneCase { ";" OneCase } [ ";" ] .
  100. OneCase = CaseLabelList ":" Statement .
  101. CaseLabelList = CaseLabel { "," CaseLabel } .
  102. CaseLabel = Constant .
  103. ForStatement = "for" ControlVariable ":=" OrdinalExpression
  104. ( "to" | "downto" ) OrdinalExpression "do" Statement .
  105. ControlVariable = identifier .
  106. WithStatement = "with" RecVarList "do" Statement .
  107. RecVarList = Designator { "," Designator } .
  108. (* -------------------------------------------------------------------- *)
  109. IntegerExpression = Expression .
  110. BooleanExpression = Expression .
  111. OrdinalExpression = Expression .
  112. Expression = SimpleExpression [ RelOp SimpleExpression ] .
  113. RelOp = "=" | "<" | ">" | "<=" | ">=" | "<>" | "in" .
  114. SimpleExpression = ( "+" Term | "-" Term | Term ) { AddOp Term } .
  115. AddOp = "+" | "-" | "or" .
  116. Term = Factor { MulOp Factor } .
  117. MulOp = "*" | "/" | "div" | "mod" | "and" .
  118. Factor = Designator [ ActualParams ]
  119. | UnsignedLiteral
  120. | SetConstructor
  121. | "(" Expression ")"
  122. | "not" Factor .
  123. Designator = identifier { "." identifier | "[" ExpList "]" | "^" } .
  124. ExpList = Expression { "," Expression } .
  125. UnsignedLiteral = UnsignedNumber | "nil" | String .
  126. SetConstructor = "[" Member { "," Member } "]" .
  127. Member = Expression [ ".." Expression ] .
  128. (* -------------------------------------------------------------------- *)
  129. NewIdentList = NewIdent { "," NewIdent } .
  130. NewIdent = identifier .
  131. UnsignedInt = integer .
  132. UnsignedReal = real .
  133. String = string .
  134. END Pascal.