turbo_pascal3.atg 6.8 KB

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