Source code of Windows XP (NT5)
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.

305 lines
5.6 KiB

  1. /* SCCSWHAT( "@(#)grammar.y 1.4 89/05/09 21:22:03 " ) */
  2. /*****************************************************************************/
  3. /** Microsoft LAN Manager **/
  4. /** Copyright(c) Microsoft Corp., 1987-1990 **/
  5. /*****************************************************************************/
  6. %{
  7. /****************************************************************************
  8. *** local defines
  9. ***************************************************************************/
  10. #define pascal
  11. #define FARDATA
  12. #define NEARDATA
  13. #define FARCODE
  14. #define NEARCODE
  15. #define NEARSWAP
  16. #define PASCAL pascal
  17. #define CDECL
  18. #define VOID void
  19. #define CONST const
  20. #define GLOBAL
  21. #define YYSTYPE lextype_t
  22. #define YYNEAR NEARCODE
  23. #define YYPASCAL PASCAL
  24. #define YYPRINT printf
  25. #define YYSTATIC static
  26. #define YYLEX yylex
  27. #define YYPARSER yyparse
  28. #define MAXARRAY 1000
  29. #define CASE_BUFFER_SIZE 10000
  30. #define CASE_FN_FORMAT ("\nstatic void\ncase_fn_%.4d()")
  31. #define DISPATCH_ENTRY_FORMAT ("\n\t,case_fn_%.4d")
  32. #define DISPATCH_FIRST_ENTRY ("\n\t case_fn_%.4d")
  33. /****************************************************************************
  34. *** include files
  35. ***************************************************************************/
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <ctype.h>
  40. #include <assert.h>
  41. #include "lex.h"
  42. /****************************************************************************
  43. *** externals
  44. ***************************************************************************/
  45. extern int Incase;
  46. extern int ActionSensed;
  47. extern int yylex();
  48. extern int yyparse();
  49. /****************************************************************************
  50. *** local procs
  51. ***************************************************************************/
  52. void Init( void );
  53. void EmitCaseTableArray( void );
  54. void EmitDefaultCase( void );
  55. void EmitCaseBody( int );
  56. void RegisterCase( int );
  57. void BufferIt( char * pStr, int iLen );
  58. void ResetBuffer();
  59. void FlushBuffer();
  60. /****************************************************************************
  61. *** local data
  62. ***************************************************************************/
  63. unsigned long SavedIDCount = 0;
  64. unsigned long IDCount = 0;
  65. unsigned char CaseTable[ MAXARRAY ] = { 0 };
  66. int CaseNumber = 0;
  67. int MaxCaseNumber = 0;
  68. char * pBufStart;
  69. char * pBufCur;
  70. char * pBufEnd;
  71. %}
  72. %token ID
  73. %token NUMBER
  74. %token TOKEN_CASE
  75. %token TOKEN_CHAR
  76. %token TOKEN_END
  77. %token TOKEN_END_CASE
  78. %token TOKEN_MYACT
  79. %token TOKEN_START
  80. %type <yynumber> NUMBER
  81. %type <yycharval> TOKEN_CHAR
  82. %type <yystring> ID
  83. %%
  84. Template:
  85. OptionalJunk TOKEN_START
  86. {
  87. Init();
  88. }
  89. OptionalJunk Body OptionalJunk TOKEN_END OptionalJunk
  90. {
  91. EmitDefaultCase();
  92. EmitCaseTableArray();
  93. }
  94. ;
  95. Body:
  96. TOKEN_MYACT
  97. {
  98. ActionSensed++;
  99. ResetBuffer();
  100. }
  101. OptionalJunk CaseList
  102. {
  103. }
  104. ;
  105. CaseList:
  106. CaseList OneCase
  107. {
  108. }
  109. | OneCase
  110. {
  111. }
  112. ;
  113. OneCase:
  114. TOKEN_CASE TOKEN_CHAR NUMBER TOKEN_CHAR
  115. {
  116. Incase = 1;
  117. CaseNumber = $3;
  118. if($3 >= MAXARRAY)
  119. {
  120. fprintf(stderr, "Case Limit Reached : Contact Dov/Vibhas\n");
  121. return 1;
  122. }
  123. SavedIDCount = IDCount;
  124. }
  125. OptionalJunk TOKEN_END_CASE
  126. {
  127. if(SavedIDCount != IDCount)
  128. {
  129. RegisterCase( CaseNumber );
  130. EmitCaseBody( CaseNumber );
  131. }
  132. ResetBuffer();
  133. if(CaseNumber > MaxCaseNumber)
  134. MaxCaseNumber = CaseNumber;
  135. Incase = 0;
  136. }
  137. ;
  138. OptionalJunk:
  139. Junk
  140. {
  141. }
  142. | /* Empty */
  143. {
  144. }
  145. ;
  146. Junk:
  147. Junk JunkElement
  148. {
  149. }
  150. | JunkElement
  151. {
  152. }
  153. ;
  154. JunkElement:
  155. TOKEN_CHAR
  156. {
  157. if(!ActionSensed)
  158. fprintf(stdout, "%c", $1);
  159. else
  160. BufferIt( &$1, 1);
  161. }
  162. | ID
  163. {
  164. IDCount++;
  165. if(!ActionSensed)
  166. fprintf(stdout, "%s", $1);
  167. else
  168. BufferIt( $1, strlen($1) );
  169. }
  170. | NUMBER
  171. {
  172. if(!ActionSensed)
  173. fprintf(stdout, "%d", $1);
  174. else
  175. {
  176. char buffer[20];
  177. sprintf(buffer,"%d", $1 );
  178. BufferIt( buffer, strlen(buffer) );
  179. }
  180. }
  181. ;
  182. %%
  183. /*****************************************************************************
  184. * utility functions
  185. *****************************************************************************/
  186. YYSTATIC VOID FARCODE PASCAL
  187. yyerror(char *szError)
  188. {
  189. extern int Line;
  190. extern char LocalBuffer[];
  191. fprintf(stderr, "%s at Line %d near %s\n", szError, Line, LocalBuffer);
  192. }
  193. void
  194. Init()
  195. {
  196. pBufStart = pBufCur = malloc( CASE_BUFFER_SIZE );
  197. if( !pBufStart )
  198. {
  199. fprintf(stderr,"Out Of Memory\n");
  200. exit(1);
  201. }
  202. pBufEnd = pBufStart + CASE_BUFFER_SIZE;
  203. }
  204. void
  205. BufferIt(
  206. char * pStr,
  207. int iLen )
  208. {
  209. if( pBufCur + iLen > pBufEnd )
  210. {
  211. printf("ALERT iLen = %d\n", iLen );
  212. // assert( (pBufCur + iLen) <= pBufEnd );
  213. exit(1);
  214. }
  215. strncpy( pBufCur , pStr, iLen );
  216. pBufCur += iLen;
  217. *pBufCur = '\0';
  218. }
  219. void
  220. ResetBuffer()
  221. {
  222. pBufCur = pBufStart;
  223. *pBufCur= '\0';
  224. }
  225. void
  226. FlushBuffer()
  227. {
  228. fprintf(stdout, "%s", pBufStart);
  229. ResetBuffer();
  230. }
  231. void
  232. EmitCaseBody(
  233. int CaseNumber )
  234. {
  235. fprintf( stdout, CASE_FN_FORMAT, CaseNumber );
  236. FlushBuffer();
  237. fprintf( stdout, "}\n" );
  238. }
  239. void
  240. EmitCaseTableArray()
  241. {
  242. int i, iTemp;
  243. fprintf( stdout, "static void\t (*case_fn_array[])() = \n\t{" );
  244. fprintf( stdout,DISPATCH_FIRST_ENTRY, 0 );
  245. for( i = 1 ; i <= MaxCaseNumber ; ++i )
  246. {
  247. iTemp = CaseTable[ i ] ? i : 0;
  248. fprintf(stdout,DISPATCH_ENTRY_FORMAT, iTemp );
  249. }
  250. fprintf( stdout, "\n\t};\n" );
  251. fprintf( stdout, "\nstatic void\nyy_vc_init(){ pcase_fn_array = case_fn_array;\nyym_vc_max = %d;\n }\n" , MaxCaseNumber);
  252. }
  253. void
  254. EmitDefaultCase()
  255. {
  256. fprintf(stdout, "static void\ncase_fn_%.4d() {\n\t}\n\n", 0 );
  257. }
  258. void
  259. RegisterCase(
  260. int iCase )
  261. {
  262. CaseTable[ iCase ] = 1;
  263. }