program.g 4.8 KB

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