2 * This file is part of flex.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 /* A template scanner file to build "scanner.c".
26 * The whole idea is to cause memory realloc by
27 * 1. pushing a lot on the condition stack, and
28 * 2. eating input greater than YY_BUF_SIZE
34 /* Insanely small read buffer. This pretty much guarantees at least one realloc. */
42 %option 8bit outfile="scanner.c" prefix="test"
43 %option nounput nomain noyywrap
44 %option warn stack nodefault reentrant
45 %option noyyalloc noyyrealloc noyyfree
52 "(" { printf("yy_push_state(parens)\n"); yy_push_state(parens,yyscanner); }
53 len=[0-9]+ { printf("About read token where %s\n",yytext); }
59 "(" { printf("yy_push_state(parens)\n"); yy_push_state(parens,yyscanner); }
60 ")" { printf("yy_pop_state()\n");yy_pop_state(yyscanner);}
66 /* total memory allocated */
67 static size_t total_mem=0;
69 /* track the amount of memory for ptr. */
75 static struct memsz * ptrs=0; /* Array of pairs. */
76 static int nptrs=0; /* Number of pairs in array. */
77 static int arrsz=0; /* Capacity of array. */
79 static void dump_mem(FILE* fp){
81 fprintf(fp,"\tptrs[%d] = {", nptrs);
82 for (i=0; i < arrsz; i++)
83 fprintf(fp," {%#lx,%ld},", (long)ptrs[i].p, (long)ptrs[i].sz);
88 void * yyalloc(yy_size_t n , void* yyscanner)
97 /* increase array size by 1 */
99 ptrs = (struct memsz*)realloc( ptrs, arrsz * sizeof(struct memsz));
104 /* find a null slot */
105 for(i=0; i < arrsz ; i++)
106 if (ptrs[i].p == 0) {
112 printf("yyflex_alloc(%8ld) total=%8ld return=%#10lx\n",(long)n,(long)total_mem,(long)p);
117 void * yyrealloc(void* p, yy_size_t n , void* yyscanner)
120 for (i=0; i < arrsz; i++)
121 if ( ptrs[i].p == p){
122 total_mem -= ptrs[i].sz;
124 ptrs[i].p = (void*)realloc(p,n);
127 printf("yyflex_realloc(%#10lx,%8ld) total=%8ld return=%8lx\n",
128 (long)p,(long)n,(long)total_mem,(long)ptrs[i].p);
133 fprintf(stderr,"ERROR: yyflex_realloc could not locate pointer %#lx.\n",(long)p);
138 void yyfree(void* p , void* yyscanner)
141 for (i=0; i < arrsz; i++)
142 if ( ptrs[i].p == p){
143 total_mem -= ptrs[i].sz;
148 printf("yyflex_free(%#10lx) total=%8ld\n",(long)p,(long)total_mem);
153 fprintf(stderr,"ERROR: yyflex_free could not locate pointer %#lx.\n",(long)p);
165 ptrs = (struct memsz*)calloc(1,sizeof(struct memsz));
168 yylex_init(&scanner);
169 yyset_in(stdin,scanner);
170 yyset_out(stdout,scanner);
172 yylex_destroy(scanner);
175 if ( nptrs > 0 || total_mem > 0){
176 fprintf(stderr,"Oops. Looks like a memory leak: nptrs=%d, unfreed memory=%ld\n",nptrs,(long)total_mem);
179 printf("TEST RETURNING OK.\n");