xref: /minix3/external/bsd/flex/dist/tests/test-string-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 #include "config.h"
29*357f1050SThomas Veerman 
30*357f1050SThomas Veerman #define NUMBER 200
31*357f1050SThomas Veerman #define WORD   201
32*357f1050SThomas Veerman 
33*357f1050SThomas Veerman %}
34*357f1050SThomas Veerman 
35*357f1050SThomas Veerman %option 8bit outfile="scanner.c" prefix="test"
36*357f1050SThomas Veerman %option nounput nomain nodefault noyywrap
37*357f1050SThomas Veerman %option warn reentrant
38*357f1050SThomas Veerman 
39*357f1050SThomas Veerman 
40*357f1050SThomas Veerman %%
41*357f1050SThomas Veerman 
42*357f1050SThomas Veerman [[:space:]]+   { }
43*357f1050SThomas Veerman [[:digit:]]+   { printf("NUMBER "); fflush(stdout);}
44*357f1050SThomas Veerman [[:alpha:]]+   { printf("WORD "); fflush(stdout);}
45*357f1050SThomas Veerman .              {
46*357f1050SThomas Veerman     fprintf(stderr,"*** Error: Unrecognized character '%c' while scanning.\n",
47*357f1050SThomas Veerman          yytext[0]);
48*357f1050SThomas Veerman     yyterminate();
49*357f1050SThomas Veerman     }
50*357f1050SThomas Veerman 
51*357f1050SThomas Veerman <<EOF>>  { printf("<<EOF>>\n"); yyterminate();}
52*357f1050SThomas Veerman 
53*357f1050SThomas Veerman %%
54*357f1050SThomas Veerman 
55*357f1050SThomas Veerman 
56*357f1050SThomas Veerman #define INPUT_STRING_1  "1234 foo bar"
57*357f1050SThomas Veerman #define INPUT_STRING_2  "1234 foo bar *@&@&###@^$#&#*"
58*357f1050SThomas Veerman 
59*357f1050SThomas Veerman int main(void);
60*357f1050SThomas Veerman 
61*357f1050SThomas Veerman int
main()62*357f1050SThomas Veerman main ()
63*357f1050SThomas Veerman {
64*357f1050SThomas Veerman     char * buf;
65*357f1050SThomas Veerman     int len;
66*357f1050SThomas Veerman     YY_BUFFER_STATE state;
67*357f1050SThomas Veerman     yyscan_t  scanner=NULL;
68*357f1050SThomas Veerman 
69*357f1050SThomas Veerman 
70*357f1050SThomas Veerman     /* Scan a good string. */
71*357f1050SThomas Veerman     printf("Testing: yy_scan_string(%s): ",INPUT_STRING_1); fflush(stdout);
72*357f1050SThomas Veerman     yylex_init(&scanner);
73*357f1050SThomas Veerman     state = yy_scan_string ( INPUT_STRING_1 ,scanner);
74*357f1050SThomas Veerman     yylex(scanner);
75*357f1050SThomas Veerman     yy_delete_buffer(state, scanner);
76*357f1050SThomas Veerman     yylex_destroy(scanner);
77*357f1050SThomas Veerman 
78*357f1050SThomas Veerman     /* Scan only the first 12 chars of a string. */
79*357f1050SThomas Veerman     printf("Testing: yy_scan_bytes(%s): ",INPUT_STRING_2); fflush(stdout);
80*357f1050SThomas Veerman     yylex_init(&scanner);
81*357f1050SThomas Veerman     state = yy_scan_bytes  ( INPUT_STRING_2, 12 ,scanner);
82*357f1050SThomas Veerman     yylex(scanner);
83*357f1050SThomas Veerman     yy_delete_buffer(state,scanner);
84*357f1050SThomas Veerman     yylex_destroy(scanner);
85*357f1050SThomas Veerman 
86*357f1050SThomas Veerman     /* Scan directly from a buffer.
87*357f1050SThomas Veerman        We make a copy, since the buffer will be modified by flex.*/
88*357f1050SThomas Veerman     printf("Testing: yy_scan_buffer(%s): ",INPUT_STRING_1); fflush(stdout);
89*357f1050SThomas Veerman     len = strlen(INPUT_STRING_1) + 2;
90*357f1050SThomas Veerman     buf = (char*)malloc( len );
91*357f1050SThomas Veerman     strcpy( buf, INPUT_STRING_1);
92*357f1050SThomas Veerman     buf[ len -2 ]  = 0; /* Flex requires two NUL bytes at end of buffer. */
93*357f1050SThomas Veerman     buf[ len -1 ] =0;
94*357f1050SThomas Veerman 
95*357f1050SThomas Veerman     yylex_init(&scanner);
96*357f1050SThomas Veerman     state = yy_scan_buffer( buf, len ,scanner);
97*357f1050SThomas Veerman     yylex(scanner);
98*357f1050SThomas Veerman     yy_delete_buffer(state,scanner);
99*357f1050SThomas Veerman     yylex_destroy(scanner);
100*357f1050SThomas Veerman 
101*357f1050SThomas Veerman     printf("TEST RETURNING OK.\n");
102*357f1050SThomas Veerman     return 0;
103*357f1050SThomas Veerman }
104