program.g 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. /* $Id$ */
  6. /* PROGRAM PARSER */
  7. /* The presence of typedef declarations renders it impossible to
  8. make a context-free grammar of C. Consequently we need
  9. context-sensitive parsing techniques, the simplest one being
  10. a subtle cooperation between the parser and the lexical scanner.
  11. The lexical scanner has to know whether to return IDENTIFIER
  12. or TYPE_IDENTIFIER for a given tag, and it obtains this information
  13. from the definition list, as constructed by the parser.
  14. The present grammar is essentially LL(2), and is processed by
  15. a parser generator which accepts LL(1) with tie breaking rules
  16. in C, of the form %if(cond) and %while(cond). To solve the LL(1)
  17. ambiguities, the lexical scanner does a one symbol look-ahead.
  18. This symbol, however, cannot always be correctly assessed, since
  19. the present symbol may cause a change in the definition list
  20. which causes the identification of the look-ahead symbol to be
  21. invalidated.
  22. The lexical scanner relies on the parser (or its routines) to
  23. detect this situation and then update the look-ahead symbol.
  24. An alternative approach would be to reassess the look-ahead symbol
  25. in the lexical scanner when it is promoted to dot symbol. This
  26. would be more beautiful but less correct, since then for a short
  27. while there would be a discrepancy between the look-ahead symbol
  28. and the definition list; I think it would nevertheless work in
  29. correct programs.
  30. A third solution would be to enter the identifier as soon as it
  31. is found; its storage class is then known, although its full type
  32. isn't. We would have to fill that in afterwards.
  33. At block exit the situation is even worse. Upon reading the
  34. closing brace, the names declared inside the function are cleared
  35. from the name list. This action may expose a type identifier that
  36. is the same as the identifier in the look-ahead symbol. This
  37. situation certainly invalidates the third solution, and casts
  38. doubts upon the second.
  39. */
  40. %lexical LLlex;
  41. %start C_program, program;
  42. %start If_expr, control_if_expression;
  43. {
  44. #include "lint.h"
  45. #include "nopp.h"
  46. #include "arith.h"
  47. #include "LLlex.h"
  48. #include "idf.h"
  49. #include "label.h"
  50. #include "type.h"
  51. #include "declar.h"
  52. #include "decspecs.h"
  53. #include "code.h"
  54. #include "expr.h"
  55. #include "def.h"
  56. #ifdef LINT
  57. #include "l_lint.h"
  58. #endif /* LINT */
  59. #ifndef NOPP
  60. extern arith ifval;
  61. #endif /* NOPP */
  62. extern error();
  63. }
  64. control_if_expression
  65. {
  66. struct expr *exprX;
  67. }
  68. :
  69. constant_expression(&exprX)
  70. {
  71. #ifndef NOPP
  72. register struct expr *expr = exprX;
  73. if (expr->ex_flags & EX_SIZEOF)
  74. expr_error(expr,
  75. "sizeof not allowed in preprocessor");
  76. ifval = expr->VL_VALUE;
  77. free_expression(expr);
  78. #endif /* NOPP */
  79. }
  80. ;
  81. /* 10 */
  82. program:
  83. [%persistent external_definition]*
  84. {unstack_world();}
  85. ;
  86. /* A C identifier definition is remarkable in that it formulates
  87. the declaration in a way different from most other languages:
  88. e.g., rather than defining x as a pointer-to-integer, it defines
  89. *x as an integer and lets the compiler deduce that x is actually
  90. pointer-to-integer. This has profound consequences, both for the
  91. structure of an identifier definition and for the compiler.
  92. A definition starts with a decl_specifiers, which contains things
  93. like
  94. typedef int
  95. which is implicitly repeated for every definition in the list, and
  96. then for each identifier a declarator is given, of the form
  97. *a()
  98. or so. The decl_specifiers is kept in a struct decspecs, to be
  99. used again and again, while the declarator is stored in a struct
  100. declarator, only to be passed to declare_idf together with the
  101. struct decspecs.
  102. */
  103. external_definition
  104. {
  105. struct decspecs Ds;
  106. struct declarator Dc;
  107. }
  108. :
  109. {
  110. Ds = null_decspecs;
  111. Dc = null_declarator;
  112. }
  113. ext_decl_specifiers(&Ds)
  114. [
  115. declarator(&Dc)
  116. {
  117. declare_idf(&Ds, &Dc, level);
  118. #ifdef LINT
  119. lint_ext_def(Dc.dc_idf, Ds.ds_sc);
  120. #endif /* LINT */
  121. }
  122. [
  123. function(&Ds, &Dc)
  124. |
  125. non_function(&Ds, &Dc)
  126. ]
  127. |
  128. ';'
  129. ]
  130. {remove_declarator(&Dc);}
  131. |
  132. asm_statement /* top level, would you believe */
  133. ;
  134. ext_decl_specifiers(struct decspecs *ds;) :
  135. %if (DOT != IDENTIFIER || AHEAD == IDENTIFIER) /* the thin ice in R.M. 11.1 */
  136. decl_specifiers(ds)
  137. |
  138. empty
  139. {do_decspecs(ds);}
  140. ;
  141. non_function(register struct decspecs *ds; register struct declarator *dc;)
  142. :
  143. {reject_params(dc);}
  144. [
  145. initializer(dc->dc_idf, ds->ds_sc)
  146. |
  147. { code_declaration(dc->dc_idf, (struct expr *) 0, level, ds->ds_sc); }
  148. ]
  149. {
  150. #ifdef LINT
  151. lint_non_function_decl(ds, dc);
  152. #endif /* LINT */
  153. }
  154. [
  155. ','
  156. init_declarator(ds)
  157. ]*
  158. ';'
  159. ;
  160. /* 10.1 */
  161. function(struct decspecs *ds; struct declarator *dc;)
  162. {
  163. arith fbytes;
  164. }
  165. :
  166. { register struct idf *idf = dc->dc_idf;
  167. #ifdef LINT
  168. lint_start_function();
  169. #endif /* LINT */
  170. init_idf(idf);
  171. stack_level(); /* L_FORMAL1 declarations */
  172. declare_params(dc);
  173. begin_proc(ds, idf); /* sets global function info */
  174. stack_level(); /* L_FORMAL2 declarations */
  175. }
  176. declaration*
  177. {
  178. declare_formals(&fbytes);
  179. #ifdef LINT
  180. lint_formals();
  181. #endif /* LINT */
  182. }
  183. compound_statement
  184. {
  185. end_proc(fbytes);
  186. #ifdef LINT
  187. lint_implicit_return();
  188. #endif /* LINT */
  189. unstack_level(); /* L_FORMAL2 declarations */
  190. #ifdef LINT
  191. lint_end_formals();
  192. #endif /* LINT */
  193. unstack_level(); /* L_FORMAL1 declarations */
  194. #ifdef LINT
  195. lint_end_function();
  196. #endif /* LINT */
  197. }
  198. ;