gnu-m2-grammar.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. Next: PIM and ISO library definitions, Previous: Contributing to GNU Modula-2, Up: Introduction [Contents][Index]
  2. 3 EBNF of GNU Modula-2
  3. This chapter contains the EBNF of GNU Modula-2. This grammar currently supports both PIM and ISO dialects. The rules here are automatically extracted from the crammer files in GNU Modula-2 and serve to document the syntax of the extensions described earlier and how they fit in with the base language.
  4. Note that the first six productions are built into the lexical analysis phase.
  5. Ident := is a builtin and checks for an identifier
  6. =:
  7. Integer := is a builtin and checks for an integer
  8. =:
  9. Real := is a builtin and checks for an real constant
  10. =:
  11. string := is a builtin and checks for an string constant
  12. =:
  13. FileUnit := ( DefinitionModule |
  14. ImplementationOrProgramModule )
  15. =:
  16. ProgramModule := 'MODULE' Ident [ Priority ] ';' {
  17. Import } Block Ident '.'
  18. =:
  19. ImplementationModule := 'IMPLEMENTATION' 'MODULE' Ident
  20. [ Priority ] ';' { Import
  21. } Block
  22. Ident '.'
  23. =:
  24. ImplementationOrProgramModule := ImplementationModule |
  25. ProgramModule
  26. =:
  27. Number := Integer | Real
  28. =:
  29. Qualident := Ident { '.' Ident }
  30. =:
  31. ConstantDeclaration := Ident '=' ConstExpression
  32. =:
  33. ConstExpression := SimpleConstExpr [ Relation SimpleConstExpr ]
  34. =:
  35. Relation := '=' | '#' | '<>' | '<' | '<=' |
  36. '>' | '>=' | 'IN'
  37. =:
  38. SimpleConstExpr := UnaryOrConstTerm { AddOperator
  39. ConstTerm }
  40. =:
  41. UnaryOrConstTerm := '+' ConstTerm |
  42. '-' ConstTerm |
  43. ConstTerm
  44. =:
  45. AddOperator := '+' | '-' | 'OR'
  46. =:
  47. ConstTerm := ConstFactor { MulOperator ConstFactor }
  48. =:
  49. MulOperator := '*' | '/' | 'DIV' | 'MOD' |
  50. 'REM' | 'AND' | '&'
  51. =:
  52. ConstFactor := Number | ConstString |
  53. ConstSetOrQualidentOrFunction |
  54. '(' ConstExpression ')' |
  55. 'NOT' ConstFactor |
  56. ConstAttribute
  57. =:
  58. ConstString := string
  59. =:
  60. ComponentElement := ConstExpression [ '..' ConstExpression ]
  61. =:
  62. ComponentValue := ComponentElement [ 'BY' ConstExpression ]
  63. =:
  64. ArraySetRecordValue := ComponentValue { ',' ComponentValue }
  65. =:
  66. Constructor := '{' [ ArraySetRecordValue ] '}'
  67. =:
  68. ConstSetOrQualidentOrFunction := Constructor |
  69. Qualident [ Constructor |
  70. ConstActualParameters ]
  71. =:
  72. ConstActualParameters := '(' [ ExpList ] ')'
  73. =:
  74. ConstAttribute := '__ATTRIBUTE__' '__BUILTIN__' '('
  75. '(' ConstAttributeExpression ')'
  76. ')'
  77. =:
  78. ConstAttributeExpression := Ident | '<' Qualident
  79. ',' Ident '>'
  80. =:
  81. ByteAlignment := '<*' AttributeExpression '*>'
  82. =:
  83. Alignment := [ ByteAlignment ]
  84. =:
  85. TypeDeclaration := Ident '=' Type Alignment
  86. =:
  87. Type := SimpleType | ArrayType | RecordType |
  88. SetType | PointerType | ProcedureType
  89. =:
  90. SimpleType := Qualident [ SubrangeType ] |
  91. Enumeration | SubrangeType
  92. =:
  93. Enumeration := '(' IdentList ')'
  94. =:
  95. IdentList := Ident { ',' Ident }
  96. =:
  97. SubrangeType := '[' ConstExpression '..' ConstExpression
  98. ']'
  99. =:
  100. ArrayType := 'ARRAY' SimpleType { ',' SimpleType }
  101. 'OF' Type
  102. =:
  103. RecordType := 'RECORD' [ DefaultRecordAttributes ]
  104. FieldListSequence 'END'
  105. =:
  106. DefaultRecordAttributes := '<*' AttributeExpression
  107. '*>'
  108. =:
  109. RecordFieldPragma := [ '<*' FieldPragmaExpression {
  110. ',' FieldPragmaExpression } '*>' ]
  111. =:
  112. FieldPragmaExpression := Ident [ '(' ConstExpression
  113. ')' ]
  114. =:
  115. AttributeExpression := Ident '(' ConstExpression ')'
  116. =:
  117. FieldListSequence := FieldListStatement { ';' FieldListStatement }
  118. =:
  119. FieldListStatement := [ FieldList ]
  120. =:
  121. FieldList := IdentList ':' Type RecordFieldPragma |
  122. 'CASE' CaseTag 'OF' Varient { '|' Varient }
  123. [ 'ELSE' FieldListSequence ] 'END'
  124. =:
  125. TagIdent := [ Ident ]
  126. =:
  127. CaseTag := TagIdent [ ':' Qualident ]
  128. =:
  129. Varient := [ VarientCaseLabelList ':' FieldListSequence ]
  130. =:
  131. VarientCaseLabelList := VarientCaseLabels { ',' VarientCaseLabels }
  132. =:
  133. VarientCaseLabels := ConstExpression [ '..' ConstExpression ]
  134. =:
  135. CaseLabelList := CaseLabels { ',' CaseLabels }
  136. =:
  137. CaseLabels := ConstExpression [ '..' ConstExpression ]
  138. =:
  139. SetType := ( 'SET' | 'PACKEDSET' ) 'OF' SimpleType
  140. =:
  141. PointerType := 'POINTER' 'TO' Type
  142. =:
  143. ProcedureType := 'PROCEDURE' [ FormalTypeList ]
  144. =:
  145. FormalTypeList := '(' ( ')' FormalReturn |
  146. ProcedureParameters ')' FormalReturn )
  147. =:
  148. FormalReturn := [ ':' OptReturnType ]
  149. =:
  150. OptReturnType := '[' Qualident ']' |
  151. Qualident
  152. =:
  153. ProcedureParameters := ProcedureParameter { ',' ProcedureParameter }
  154. =:
  155. ProcedureParameter := '...' | 'VAR' FormalType |
  156. FormalType
  157. =:
  158. VarIdent := Ident [ '[' ConstExpression ']' ]
  159. =:
  160. VariableDeclaration := VarIdentList ':' Type Alignment
  161. =:
  162. VarIdentList := VarIdent { ',' VarIdent }
  163. =:
  164. Designator := Qualident { SubDesignator }
  165. =:
  166. SubDesignator := '.' Ident | '[' ExpList ']' |
  167. '^'
  168. =:
  169. ExpList := Expression { ',' Expression }
  170. =:
  171. Expression := SimpleExpression [ Relation SimpleExpression ]
  172. =:
  173. SimpleExpression := [ '+' | '-' ] Term { AddOperator
  174. Term }
  175. =:
  176. Term := Factor { MulOperator Factor }
  177. =:
  178. Factor := Number | string | SetOrDesignatorOrFunction |
  179. '(' Expression ')' |
  180. 'NOT' Factor | ConstAttribute
  181. =:
  182. SetOrDesignatorOrFunction := ( Qualident [ Constructor |
  183. SimpleDes
  184. [ ActualParameters ] ] |
  185. Constructor )
  186. =:
  187. SimpleDes := { '.' Ident | '[' ExpList ']' |
  188. '^' }
  189. =:
  190. ActualParameters := '(' [ ExpList ] ')'
  191. =:
  192. Statement := [ AssignmentOrProcedureCall |
  193. IfStatement | CaseStatement |
  194. WhileStatement | RepeatStatement |
  195. LoopStatement | ForStatement |
  196. WithStatement | AsmStatement |
  197. 'EXIT' | 'RETURN' [ Expression ] |
  198. RetryStatement ]
  199. =:
  200. RetryStatement := 'RETRY'
  201. =:
  202. AssignmentOrProcedureCall := Designator ( ':=' Expression |
  203. ActualParameters |
  204. )
  205. =:
  206. StatementSequence := Statement { ';' Statement }
  207. =:
  208. IfStatement := 'IF' Expression 'THEN' StatementSequence
  209. { 'ELSIF' Expression 'THEN' StatementSequence }
  210. [ 'ELSE' StatementSequence ] 'END'
  211. =:
  212. CaseStatement := 'CASE' Expression 'OF' Case { '|'
  213. Case }
  214. [ 'ELSE' StatementSequence ] 'END'
  215. =:
  216. Case := [ CaseLabelList ':' StatementSequence ]
  217. =:
  218. WhileStatement := 'WHILE' Expression 'DO' StatementSequence
  219. 'END'
  220. =:
  221. RepeatStatement := 'REPEAT' StatementSequence 'UNTIL'
  222. Expression
  223. =:
  224. ForStatement := 'FOR' Ident ':=' Expression 'TO' Expression
  225. [ 'BY' ConstExpression ] 'DO' StatementSequence
  226. 'END'
  227. =:
  228. LoopStatement := 'LOOP' StatementSequence 'END'
  229. =:
  230. WithStatement := 'WITH' Designator 'DO' StatementSequence
  231. 'END'
  232. =:
  233. ProcedureDeclaration := ProcedureHeading ';' ( ProcedureBlock
  234. Ident
  235. )
  236. =:
  237. DefineBuiltinProcedure := [ '__ATTRIBUTE__' '__BUILTIN__'
  238. '(' '(' Ident ')' ')' |
  239. '__INLINE__' ]
  240. =:
  241. ProcedureHeading := 'PROCEDURE' DefineBuiltinProcedure
  242. ( Ident [ FormalParameters ] AttributeNoReturn )
  243. =:
  244. AttributeNoReturn := [ '<*' Ident '*>' ]
  245. =:
  246. AttributeUnused := [ '<*' Ident '*>' ]
  247. =:
  248. Builtin := [ '__BUILTIN__' | '__INLINE__' ]
  249. =:
  250. DefProcedureHeading := 'PROCEDURE' Builtin ( Ident
  251. [ DefFormalParameters ]
  252. AttributeNoReturn )
  253. =:
  254. ProcedureBlock := { Declaration } [ 'BEGIN' BlockBody ]
  255. 'END'
  256. =:
  257. Block := { Declaration } InitialBlock FinalBlock
  258. 'END'
  259. =:
  260. InitialBlock := [ 'BEGIN' BlockBody ]
  261. =:
  262. FinalBlock := [ 'FINALLY' BlockBody ]
  263. =:
  264. BlockBody := NormalPart [ 'EXCEPT' ExceptionalPart ]
  265. =:
  266. NormalPart := StatementSequence
  267. =:
  268. ExceptionalPart := StatementSequence
  269. =:
  270. Declaration := 'CONST' { ConstantDeclaration ';' } |
  271. 'TYPE' { TypeDeclaration ';' } |
  272. 'VAR' { VariableDeclaration ';' } |
  273. ProcedureDeclaration ';' |
  274. ModuleDeclaration ';'
  275. =:
  276. DefFormalParameters := '(' [ DefMultiFPSection ] ')'
  277. FormalReturn
  278. =:
  279. DefMultiFPSection := DefExtendedFP |
  280. FPSection [ ';' DefMultiFPSection ]
  281. =:
  282. FormalParameters := '(' [ MultiFPSection ] ')' FormalReturn
  283. =:
  284. MultiFPSection := ExtendedFP | FPSection [ ';' MultiFPSection ]
  285. =:
  286. FPSection := NonVarFPSection | VarFPSection
  287. =:
  288. DefExtendedFP := DefOptArg | '...'
  289. =:
  290. ExtendedFP := OptArg | '...'
  291. =:
  292. VarFPSection := 'VAR' IdentList ':' FormalType [ AttributeUnused ]
  293. =:
  294. NonVarFPSection := IdentList ':' FormalType [ AttributeUnused ]
  295. =:
  296. OptArg := '[' Ident ':' FormalType [ '=' ConstExpression ]
  297. ']'
  298. =:
  299. DefOptArg := '[' Ident ':' FormalType '=' ConstExpression
  300. ']'
  301. =:
  302. FormalType := { 'ARRAY' 'OF' } Qualident
  303. =:
  304. ModuleDeclaration := 'MODULE' Ident [ Priority ] ';'
  305. { Import } [ Export ] Block
  306. Ident
  307. =:
  308. Priority := '[' ConstExpression ']'
  309. =:
  310. Export := 'EXPORT' ( 'QUALIFIED' IdentList |
  311. 'UNQUALIFIED' IdentList |
  312. IdentList ) ';'
  313. =:
  314. Import := 'FROM' Ident 'IMPORT' IdentList ';' |
  315. 'IMPORT' IdentList ';'
  316. =:
  317. DefinitionModule := 'DEFINITION' 'MODULE' [ 'FOR' string
  318. ] Ident
  319. ';' { Import } [ Export ] {
  320. Definition } 'END' Ident '.'
  321. =:
  322. Definition := 'CONST' { ConstantDeclaration ';' } |
  323. 'TYPE' { Ident ( ';' | '=' Type Alignment
  324. ';' ) } |
  325. 'VAR' { VariableDeclaration ';' } |
  326. DefProcedureHeading ';'
  327. =:
  328. AsmStatement := 'ASM' [ 'VOLATILE' ] '(' AsmOperands
  329. ')'
  330. =:
  331. NamedOperand := '[' Ident ']'
  332. =:
  333. AsmOperandName := [ NamedOperand ]
  334. =:
  335. AsmOperands := string [ ':' AsmList [ ':' AsmList [
  336. ':' TrashList ] ] ]
  337. =:
  338. AsmList := [ AsmElement ] { ',' AsmElement }
  339. =:
  340. AsmElement := AsmOperandName string '(' Expression
  341. ')'
  342. =:
  343. TrashList := [ string ] { ',' string }
  344. =:
  345. Next: PIM and ISO library definitions, Previous: Contributing to GNU Modula-2, Up: Introduction [Contents][Index]