130da1778Schristos /*
230da1778Schristos * This file is part of flex.
330da1778Schristos *
430da1778Schristos * Redistribution and use in source and binary forms, with or without
530da1778Schristos * modification, are permitted provided that the following conditions
630da1778Schristos * are met:
730da1778Schristos *
830da1778Schristos * 1. Redistributions of source code must retain the above copyright
930da1778Schristos * notice, this list of conditions and the following disclaimer.
1030da1778Schristos * 2. Redistributions in binary form must reproduce the above copyright
1130da1778Schristos * notice, this list of conditions and the following disclaimer in the
1230da1778Schristos * documentation and/or other materials provided with the distribution.
1330da1778Schristos *
1430da1778Schristos * Neither the name of the University nor the names of its contributors
1530da1778Schristos * may be used to endorse or promote products derived from this software
1630da1778Schristos * without specific prior written permission.
1730da1778Schristos *
1830da1778Schristos * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1930da1778Schristos * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
2030da1778Schristos * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2130da1778Schristos * PURPOSE.
2230da1778Schristos */
2330da1778Schristos
2430da1778Schristos %{
2530da1778Schristos /* A template scanner file to build "scanner.c". */
2630da1778Schristos #include <stdio.h>
2730da1778Schristos #include <stdlib.h>
2830da1778Schristos #include "config.h"
2930da1778Schristos
3030da1778Schristos #define NUMBER 200
3130da1778Schristos #define WORD 201
3230da1778Schristos
3330da1778Schristos %}
3430da1778Schristos
3530da1778Schristos %option 8bit prefix="test"
3630da1778Schristos %option nounput nomain nodefault noyywrap noinput
3730da1778Schristos %option warn
3830da1778Schristos
3930da1778Schristos
4030da1778Schristos %%
4130da1778Schristos
4230da1778Schristos [[:space:]]+ { }
4330da1778Schristos [[:digit:]]+ { printf("NUMBER "); fflush(stdout);}
4430da1778Schristos [[:alpha:]]+ { printf("WORD "); fflush(stdout);}
4530da1778Schristos . {
4630da1778Schristos fprintf(stderr,"*** Error: Unrecognized character '%c' while scanning.\n",
4730da1778Schristos yytext[0]);
4830da1778Schristos yyterminate();
4930da1778Schristos }
5030da1778Schristos
5130da1778Schristos <<EOF>> { printf("<<EOF>>\n"); yyterminate();}
5230da1778Schristos
5330da1778Schristos %%
5430da1778Schristos
5530da1778Schristos
5630da1778Schristos #define INPUT_STRING_1 "1234 foo bar"
5730da1778Schristos #define INPUT_STRING_2 "1234 foo bar *@&@&###@^$#&#*"
5830da1778Schristos
5930da1778Schristos int main(void);
6030da1778Schristos
6130da1778Schristos int
main(void)62*56bd8546Schristos main (void)
6330da1778Schristos {
6430da1778Schristos char * buf;
65*56bd8546Schristos size_t len;
6630da1778Schristos YY_BUFFER_STATE state;
6730da1778Schristos
6830da1778Schristos
6930da1778Schristos /* Scan a good string. */
70*56bd8546Schristos printf("Testing: test_scan_string(%s): ",INPUT_STRING_1); fflush(stdout);
71*56bd8546Schristos state = test_scan_string ( INPUT_STRING_1 );
72*56bd8546Schristos testlex();
7330da1778Schristos yy_delete_buffer(state);
7430da1778Schristos
7530da1778Schristos /* Scan only the first 12 chars of a string. */
76*56bd8546Schristos printf("Testing: test_scan_bytes(%s): ",INPUT_STRING_2); fflush(stdout);
77*56bd8546Schristos state = test_scan_bytes ( INPUT_STRING_2, 12 );
78*56bd8546Schristos testlex();
79*56bd8546Schristos test_delete_buffer(state);
8030da1778Schristos
8130da1778Schristos /* Scan directly from a buffer.
8230da1778Schristos We make a copy, since the buffer will be modified by flex.*/
83*56bd8546Schristos printf("Testing: test_scan_buffer(%s): ",INPUT_STRING_1); fflush(stdout);
8430da1778Schristos len = strlen(INPUT_STRING_1) + 2;
85*56bd8546Schristos buf = malloc(len);
8630da1778Schristos strcpy( buf, INPUT_STRING_1);
8730da1778Schristos buf[ len -2 ] = 0; /* Flex requires two NUL bytes at end of buffer. */
8830da1778Schristos buf[ len -1 ] =0;
8930da1778Schristos
90*56bd8546Schristos state = test_scan_buffer( buf, len );
91*56bd8546Schristos testlex();
92*56bd8546Schristos test_delete_buffer(state);
9330da1778Schristos
9430da1778Schristos printf("TEST RETURNING OK.\n");
9530da1778Schristos return 0;
9630da1778Schristos }
97