Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

300 lines
6.3 KiB

  1. // Copyright (c) 1993-1999 Microsoft Corporation
  2. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  3. The s switch has been introduced to allow yacc to generate extended tables
  4. for MIDL error recovery and reporting scheme. The following are the routines
  5. which take part in s switch processing:
  6. . EmitStateVsExpectedConstruct
  7. . EmitStateGotoTable
  8. . SSwitchInit
  9. . SSwitchExit
  10. The global int variable ssw is 0 if s switch is not specified, non-zero
  11. otherwise. This is set in ysetup.c If the sswitch is specified, the i switch
  12. is automatically enabled.
  13. ----------------------------------------------------------------------------*/
  14. #include <malloc.h>
  15. #include <stdlib.h>
  16. #include "y3.h"
  17. #include "y4.h"
  18. extern int ssw;
  19. FILE *tokxlathdl;/* token xlation file,token index vs value*/
  20. FILE *stgotohdl; /* state goto table file handle */
  21. FILE *stexhdl; /* state vs expected construct handle */
  22. short MaxStateVsTokenCount = 0;
  23. short MaxTokenVsStateCount = 0;
  24. short *pTokenVsStateCount;
  25. SSIZE_T MaxTokenValue = 0;
  26. short NStates = 0;
  27. int StateVsExpectedCount = 0;
  28. void
  29. wrstate( int i)
  30. {
  31. /* writes state i */
  32. register j0;
  33. SSIZE_T j1;
  34. register struct item *pp, *qq;
  35. register struct wset *u;
  36. if( foutput == NULL ) return;
  37. SSwitchInit();
  38. fprintf( foutput, "\nstate %d\n",i);
  39. ITMLOOP(i,pp,qq)
  40. {
  41. fprintf( foutput, "\t%s\n", writem(pp->pitem));
  42. EmitStateVsExpectedConstruct( i, pp->pitem );
  43. }
  44. if( tystate[i] == MUSTLOOKAHEAD )
  45. {
  46. /* print out empty productions in closure */
  47. WSLOOP( wsets+(pstate[i+1]-pstate[i]), u )
  48. {
  49. if( *(u->pitem) < 0 ) fprintf( foutput, "\t%s\n", writem(u->pitem) );
  50. }
  51. }
  52. /* check for state equal to another */
  53. TLOOP(j0)
  54. {
  55. if( (j1=temp1[j0]) != 0 )
  56. {
  57. fprintf( foutput, "\n\t%s ", symnam(j0) );
  58. if( j1>0 )
  59. {
  60. /* shift, error, or accept */
  61. if( j1 == ACCEPTCODE ) fprintf( foutput, "accept" );
  62. else if( j1 == ERRCODE ) fprintf( foutput, "error" );
  63. else fprintf( foutput, "shift %d", j1 );
  64. }
  65. else fprintf( foutput, "reduce %d",-j1 );
  66. }
  67. }
  68. /* output any s switch information */
  69. EmitStateGotoTable( i );
  70. /* output the final production */
  71. if( lastred ) fprintf( foutput, "\n\t. reduce %d\n\n", lastred );
  72. else fprintf( foutput, "\n\t. error\n\n" );
  73. /* now, output nonterminal actions */
  74. j1 = ntokens;
  75. for( j0 = 1; j0 <= nnonter; ++j0 )
  76. {
  77. if( temp1[++j1] )
  78. fprintf( foutput, "\t%s goto %d\n", symnam( j0+NTBASE), temp1[j1] );
  79. }
  80. }
  81. void
  82. wdef( char *s, int n )
  83. {
  84. /* output a definition of s to the value n */
  85. fprintf( ftable, "# define %s %d\n", s, n );
  86. }
  87. void
  88. EmitStateGotoTable(
  89. int i )
  90. {
  91. register int j0;
  92. short count = 0;
  93. #define TLOOP_0(i) for(i=0;i<=ntokens;++i)
  94. if( ssw )
  95. {
  96. NStates++;
  97. TLOOP_0( j0 )
  98. {
  99. if( (temp1[ j0 ] > 0 ) && (temp1[ j0 ] != ACCEPTCODE ) )
  100. count++;
  101. }
  102. if( count >= MaxStateVsTokenCount )
  103. MaxStateVsTokenCount = count;
  104. fprintf( stgotohdl, "%.4d : %.4d : ", i, count );
  105. TLOOP_0( j0 )
  106. {
  107. if( (temp1[ j0 ] > 0 ) && (temp1[ j0 ] != ACCEPTCODE ) )
  108. {
  109. fprintf( stgotohdl, " %.4d, %.4d", temp1[ j0 ], j0 );
  110. pTokenVsStateCount[ j0 ] += 1;
  111. if( pTokenVsStateCount[ j0 ] >= MaxTokenVsStateCount )
  112. MaxTokenVsStateCount = pTokenVsStateCount[ j0 ];
  113. }
  114. }
  115. fprintf( stgotohdl, "\n");
  116. }
  117. }
  118. void
  119. EmitStateVsExpectedConstruct(
  120. int state,
  121. SSIZE_T *pp )
  122. {
  123. SSIZE_T i,*p;
  124. // char *q;
  125. int flag = 0;
  126. int Count;
  127. if( ssw )
  128. {
  129. for( p=pp; *p>0 ; ++p ) ;
  130. p = prdptr[-*p];
  131. // fprintf( stexhdl, " %s", nontrst[ *p-NTBASE ].name );
  132. fprintf( stexhdl, " %.4d : ", state );
  133. Count = CountStateVsExpectedConstruct( state, pp );
  134. StateVsExpectedCount += Count;
  135. fprintf( stexhdl, " %.4d : ",Count );
  136. for(;;)
  137. {
  138. if( ++p==pp )
  139. {
  140. if( ( i = *p ) <= 0 )
  141. {
  142. fprintf( stexhdl, "\n" );
  143. return;
  144. }
  145. else
  146. fprintf( stexhdl, "%s\n", symnam(i) );
  147. }
  148. if( p >= pp ) return;
  149. }
  150. }
  151. }
  152. int
  153. CountStateVsExpectedConstruct(
  154. int state,
  155. SSIZE_T *pp )
  156. {
  157. SSIZE_T i,*p;
  158. int flag = 0;
  159. int Count = 0;
  160. if( ssw )
  161. {
  162. for( p=pp; *p>0 ; ++p ) ;
  163. p = prdptr[-*p];
  164. for(;;)
  165. {
  166. if( ++p==pp )
  167. {
  168. if( ( i = *p ) <= 0 )
  169. {
  170. return Count;
  171. }
  172. else
  173. ++Count;
  174. }
  175. if( p >= pp ) return Count;
  176. }
  177. }
  178. return Count; /* NOTREACHED */
  179. }
  180. void
  181. SSwitchInit()
  182. {
  183. static sswitch_inited = 0;
  184. int i = 0;
  185. if( ssw && ! sswitch_inited )
  186. {
  187. tokxlathdl = fopen( "extable.h1" , "w" );
  188. if ( NULL == tokxlathdl ) {error("Unable to open tokxlathdl" );exit(0);}
  189. /* output the token index vs the token value table */
  190. fprintf( tokxlathdl, "%d %d\n", ntokens+1, ACCEPTCODE );
  191. while( i <= ntokens )
  192. {
  193. fprintf( tokxlathdl , "%d ",
  194. tokset[ i ].value);
  195. if( tokset[ i ].value >= MaxTokenValue )
  196. MaxTokenValue = tokset[ i ].value;
  197. ++i;
  198. }
  199. fprintf(tokxlathdl, "\n");
  200. /* set up for the state vs expected construct */
  201. stexhdl = fopen( "extable.h2", "w" );
  202. if ( NULL == stexhdl ) error("Unable to open extable.h2");
  203. /* set up for state goto table */
  204. stgotohdl = fopen( "extable.h3", "w");
  205. if ( NULL == stgotohdl ) error("Unable to open extable.h3");
  206. /* set up state vs token count array */
  207. pTokenVsStateCount = calloc( 1, (ntokens+1 ) * sizeof(short) );
  208. if ( NULL == pTokenVsStateCount ) error("Out of memory");
  209. sswitch_inited = 1;
  210. }
  211. }
  212. void
  213. SSwitchExit( void )
  214. {
  215. int i;
  216. if( ssw )
  217. {
  218. /** print the token index vs goto count **/
  219. for( i = 0; i <= ntokens ; ++i )
  220. {
  221. fprintf( tokxlathdl , "%d ", pTokenVsStateCount[ i ]);
  222. }
  223. fprintf( tokxlathdl, "\n");
  224. fprintf( tokxlathdl, "%d %d %d %d %d \n",
  225. NStates,
  226. MaxTokenVsStateCount,
  227. MaxStateVsTokenCount,
  228. MaxTokenValue,
  229. StateVsExpectedCount );
  230. fclose( tokxlathdl );
  231. fclose( stexhdl );
  232. fclose( stgotohdl );
  233. }
  234. }