xref: /minix3/external/bsd/flex/dist/tests/test-rescan-r/scanner.l (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*357f1050SThomas Veerman /*
2*357f1050SThomas Veerman  * This file is part of flex.
3*357f1050SThomas Veerman  *
4*357f1050SThomas Veerman  * Redistribution and use in source and binary forms, with or without
5*357f1050SThomas Veerman  * modification, are permitted provided that the following conditions
6*357f1050SThomas Veerman  * are met:
7*357f1050SThomas Veerman  *
8*357f1050SThomas Veerman  * 1. Redistributions of source code must retain the above copyright
9*357f1050SThomas Veerman  *    notice, this list of conditions and the following disclaimer.
10*357f1050SThomas Veerman  * 2. Redistributions in binary form must reproduce the above copyright
11*357f1050SThomas Veerman  *    notice, this list of conditions and the following disclaimer in the
12*357f1050SThomas Veerman  *    documentation and/or other materials provided with the distribution.
13*357f1050SThomas Veerman  *
14*357f1050SThomas Veerman  * Neither the name of the University nor the names of its contributors
15*357f1050SThomas Veerman  * may be used to endorse or promote products derived from this software
16*357f1050SThomas Veerman  * without specific prior written permission.
17*357f1050SThomas Veerman  *
18*357f1050SThomas Veerman  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19*357f1050SThomas Veerman  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20*357f1050SThomas Veerman  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21*357f1050SThomas Veerman  * PURPOSE.
22*357f1050SThomas Veerman  */
23*357f1050SThomas Veerman 
24*357f1050SThomas Veerman %{
25*357f1050SThomas Veerman /* A template scanner file to build "scanner.c". */
26*357f1050SThomas Veerman #include <stdio.h>
27*357f1050SThomas Veerman #include <stdlib.h>
28*357f1050SThomas Veerman %}
29*357f1050SThomas Veerman 
30*357f1050SThomas Veerman %option 8bit outfile="scanner.c" prefix="test"
31*357f1050SThomas Veerman %option nounput nomain noyywrap reentrant
32*357f1050SThomas Veerman %option warn stack never-interactive
33*357f1050SThomas Veerman %x STATE_1
34*357f1050SThomas Veerman 
35*357f1050SThomas Veerman %%
36*357f1050SThomas Veerman 
37*357f1050SThomas Veerman <INITIAL>{
38*357f1050SThomas Veerman 0              yy_push_state (STATE_1, yyscanner);
39*357f1050SThomas Veerman .|\n           return 1;
40*357f1050SThomas Veerman }
41*357f1050SThomas Veerman <STATE_1>{
42*357f1050SThomas Veerman 1              yy_pop_state(yyscanner);
43*357f1050SThomas Veerman .|\n           return yy_top_state(yyscanner) + 1;
44*357f1050SThomas Veerman }
45*357f1050SThomas Veerman 
46*357f1050SThomas Veerman %%
47*357f1050SThomas Veerman 
48*357f1050SThomas Veerman int
49*357f1050SThomas Veerman main (int argc, char* const argv[])
50*357f1050SThomas Veerman {
51*357f1050SThomas Veerman     FILE* fp;
52*357f1050SThomas Veerman     int i;
53*357f1050SThomas Veerman     yyscan_t  yyscanner;
54*357f1050SThomas Veerman 
55*357f1050SThomas Veerman     if ((fp = fopen(argv[1],"r")) == NULL){
56*357f1050SThomas Veerman         perror("Failed to open input file.");
57*357f1050SThomas Veerman         return 1;
58*357f1050SThomas Veerman     }
59*357f1050SThomas Veerman 
60*357f1050SThomas Veerman     printf("Test 1: Reusing same scanner.\n");
61*357f1050SThomas Veerman     yylex_init( &yyscanner );
62*357f1050SThomas Veerman     yyset_out ( stdout, yyscanner);
63*357f1050SThomas Veerman 
64*357f1050SThomas Veerman     for (i=0; i <  4; ++i){
65*357f1050SThomas Veerman 
66*357f1050SThomas Veerman         rewind(fp);
67*357f1050SThomas Veerman         yyset_in  ( fp, yyscanner);
68*357f1050SThomas Veerman 
69*357f1050SThomas Veerman         while( yylex(yyscanner) )
70*357f1050SThomas Veerman             ;
71*357f1050SThomas Veerman     }
72*357f1050SThomas Veerman     yylex_destroy( yyscanner );
73*357f1050SThomas Veerman     printf("Test 1 OK\n\n");
74*357f1050SThomas Veerman 
75*357f1050SThomas Veerman     printf("Test 2: Rescanning with new scanner each time.\n");
76*357f1050SThomas Veerman 
77*357f1050SThomas Veerman     memset(&yyscanner,0,sizeof(yyscanner)); // Just to be clean about it.
78*357f1050SThomas Veerman 
79*357f1050SThomas Veerman     for (i=0; i < 4; ++i){
80*357f1050SThomas Veerman         yyscan_t  s;
81*357f1050SThomas Veerman         yylex_init( &s );
82*357f1050SThomas Veerman         yyset_out ( stdout, s);
83*357f1050SThomas Veerman         rewind(fp);
84*357f1050SThomas Veerman         yyset_in  ( fp, s);
85*357f1050SThomas Veerman 
86*357f1050SThomas Veerman         while( yylex(s) )
87*357f1050SThomas Veerman             ;
88*357f1050SThomas Veerman         yylex_destroy( s );
89*357f1050SThomas Veerman     }
90*357f1050SThomas Veerman     printf("Test 2 OK\n\n");
91*357f1050SThomas Veerman 
92*357f1050SThomas Veerman 
93*357f1050SThomas Veerman     printf("TEST RETURNING OK.\n");
94*357f1050SThomas Veerman     return 0;
95*357f1050SThomas Veerman }
96