program.g 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. *
  5. * Author: Ceriel J.H. Jacobs
  6. */
  7. /* O V E R A L L S T R U C T U R E */
  8. /* stripped down version of the one in the Modula-2 compiler */
  9. /* $Id$ */
  10. /*
  11. The grammar as given by Wirth is already almost LL(1); the
  12. main problem is that the full form of a qualified designator
  13. may be:
  14. [ module_ident '.' ]* IDENT [ '.' field_ident ]*
  15. which is quite confusing to an LL(1) parser. Rather than
  16. resorting to context-sensitive techniques, I have decided
  17. to render this as:
  18. IDENT [ '.' IDENT ]*
  19. on the grounds that it is quite natural to consider the first
  20. IDENT to be the name of the object and regard the others as
  21. field identifiers.
  22. */
  23. {
  24. #include "main.h"
  25. #include "idf.h"
  26. #include "f_info.h"
  27. #include "LLlex.h"
  28. #include <alloc.h>
  29. struct lnk *
  30. new_lnk()
  31. {
  32. static struct lnk *p;
  33. static int cnt;
  34. if (cnt-- <= 0) {
  35. p = (struct lnk *)Malloc(50*sizeof(struct lnk));
  36. cnt = 49;
  37. }
  38. p->lnk_next = 0;
  39. p->lnk_imp = 0;
  40. return p++;
  41. }
  42. }
  43. %lexical LLlex;
  44. %start CompUnit, CompilationUnit;
  45. %start DefModule, DefinitionModule;
  46. ModuleDeclaration :
  47. MODULE IDENT priority ';' import((struct lnk **) 0)* export?
  48. block IDENT
  49. ;
  50. priority:
  51. [
  52. '[' ConstExpression ']'
  53. |
  54. /* empty */
  55. ]
  56. ;
  57. export :
  58. EXPORT
  59. [
  60. QUALIFIED
  61. |
  62. /* empty */
  63. ]
  64. IdentList ';'
  65. ;
  66. import(register struct lnk **p;)
  67. {
  68. register struct idf *fromid = 0;
  69. struct idf *id;
  70. }
  71. :
  72. { if (p) while (*p) p = &((*p)->lnk_next); }
  73. [ FROM
  74. identifier(&id) { fromid = id;
  75. if (p) {
  76. if (AddToList(fromid->id_text, ".def")) {
  77. *p = new_lnk();
  78. (*p)->lnk_imp = fromid;
  79. }
  80. }
  81. }
  82. ]?
  83. IMPORT
  84. identifier(&id) { if (! fromid && p) {
  85. if (AddToList(id->id_text, ".def")) {
  86. *p = new_lnk();
  87. (*p)->lnk_imp = id;
  88. p = &((*p)->lnk_next);
  89. }
  90. }
  91. }
  92. [
  93. ',' identifier(&id)
  94. { if (! fromid && p) {
  95. if (AddToList(id->id_text, ".def")) {
  96. *p = new_lnk();
  97. (*p)->lnk_imp = id;
  98. p = &((*p)->lnk_next);
  99. }
  100. }
  101. }
  102. ]*
  103. ';'
  104. /*
  105. When parsing a global module, this is the place where we must
  106. read already compiled definition modules.
  107. If the FROM clause is present, the identifier in it is a module
  108. name, otherwise the names in the import list are module names.
  109. */
  110. ;
  111. DefinitionModule
  112. {
  113. struct idf *id;
  114. extern char *strrchr();
  115. }
  116. :
  117. DEFINITION
  118. MODULE identifier(&id)
  119. { if (! ForeignFlag) {
  120. AddToList(id->id_text, ".mod");
  121. }
  122. if (! id->id_type) {
  123. id->id_type = DEFINITION;
  124. }
  125. else if (id->id_type != IMPLEMENTATION) {
  126. error("multiple declaration for module %s",
  127. id->id_text);
  128. }
  129. if (! id->id_dir) {
  130. id->id_dir = WorkingDir;
  131. }
  132. else if (strcmp(id->id_dir, WorkingDir)) {
  133. Gerror("definition and implementation of module %s reside in different directories", id->id_text);
  134. }
  135. id->id_def = strrchr(FileName, '/');
  136. if (! id->id_def) id->id_def = FileName;
  137. else (id->id_def)++;
  138. CurrentArg->a_idf = id;
  139. }
  140. ';'
  141. import(&(id->id_defimports))*
  142. [
  143. export
  144. |
  145. /* empty */
  146. ]
  147. definition* END IDENT
  148. '.'
  149. ;
  150. definition :
  151. CONST [ %persistent ConstantDeclaration ';' ]*
  152. |
  153. TYPE
  154. [ %persistent
  155. IDENT
  156. [ '=' type
  157. | /* empty */
  158. /*
  159. Here, the exported type has a hidden implementation.
  160. The export is said to be opaque.
  161. It is restricted to pointer types.
  162. */
  163. ]
  164. ';'
  165. ]*
  166. |
  167. VAR [ %persistent VariableDeclaration ';' ]*
  168. |
  169. ProcedureHeading
  170. ';'
  171. ;
  172. ProgramModule
  173. {
  174. struct idf *id;
  175. }
  176. :
  177. MODULE
  178. identifier(&id) { if (! id->id_type) {
  179. id->id_type = state;
  180. }
  181. else if (id->id_type != DEFINITION ||
  182. state != IMPLEMENTATION) {
  183. error("multiple declaration for module %s",
  184. id->id_text);
  185. }
  186. if (! id->id_dir) {
  187. id->id_dir = WorkingDir;
  188. }
  189. else if (strcmp(id->id_dir, WorkingDir)) {
  190. Gerror("definition and implementation of module %s reside in different directories", id->id_text);
  191. }
  192. CurrentArg->a_idf = id;
  193. }
  194. priority
  195. ';' import(&(id->id_modimports))*
  196. block IDENT
  197. '.'
  198. ;
  199. Module:
  200. DEFINITION
  201. { fatal("Definition module in .mod file"); }
  202. | %default
  203. [
  204. IMPLEMENTATION { state = IMPLEMENTATION; }
  205. |
  206. /* empty */ { state = PROGRAM; }
  207. ]
  208. ProgramModule
  209. ;
  210. CompilationUnit:
  211. Module
  212. ;
  213. identifier(struct idf **id;):
  214. IDENT
  215. { extern char idfbuf[];
  216. *id = str2idf(idfbuf, 1);
  217. }
  218. ;