1 /* $NetBSD: scanner.l,v 1.1.1.1 2009/10/26 00:29:50 christos Exp $ */ 2 3 /* 4 * This file is part of flex. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE. 24 */ 25 26 %{ 27 /* A template scanner file to build "scanner.c". */ 28 #include <stdio.h> 29 #include <stdlib.h> 30 #include "config.h" 31 32 #define NUMBER 200 33 #define WORD 201 34 35 %} 36 37 %option 8bit outfile="scanner.c" prefix="test" 38 %option nounput nomain nodefault noyywrap 39 %option warn reentrant 40 41 42 %% 43 44 [[:space:]]+ { } 45 [[:digit:]]+ { printf("NUMBER "); fflush(stdout);} 46 [[:alpha:]]+ { printf("WORD "); fflush(stdout);} 47 . { 48 fprintf(stderr,"*** Error: Unrecognized character '%c' while scanning.\n", 49 yytext[0]); 50 yyterminate(); 51 } 52 53 <<EOF>> { printf("<<EOF>>\n"); yyterminate();} 54 55 %% 56 57 58 #define INPUT_STRING_1 "1234 foo bar" 59 #define INPUT_STRING_2 "1234 foo bar *@&@&###@^$#&#*" 60 61 int main(void); 62 63 int 64 main () 65 { 66 char * buf; 67 int len; 68 YY_BUFFER_STATE state; 69 yyscan_t scanner=NULL; 70 71 72 /* Scan a good string. */ 73 printf("Testing: yy_scan_string(%s): ",INPUT_STRING_1); fflush(stdout); 74 yylex_init(&scanner); 75 state = yy_scan_string ( INPUT_STRING_1 ,scanner); 76 yylex(scanner); 77 yy_delete_buffer(state, scanner); 78 yylex_destroy(scanner); 79 80 /* Scan only the first 12 chars of a string. */ 81 printf("Testing: yy_scan_bytes(%s): ",INPUT_STRING_2); fflush(stdout); 82 yylex_init(&scanner); 83 state = yy_scan_bytes ( INPUT_STRING_2, 12 ,scanner); 84 yylex(scanner); 85 yy_delete_buffer(state,scanner); 86 yylex_destroy(scanner); 87 88 /* Scan directly from a buffer. 89 We make a copy, since the buffer will be modified by flex.*/ 90 printf("Testing: yy_scan_buffer(%s): ",INPUT_STRING_1); fflush(stdout); 91 len = strlen(INPUT_STRING_1) + 2; 92 buf = (char*)malloc( len ); 93 strcpy( buf, INPUT_STRING_1); 94 buf[ len -2 ] = 0; /* Flex requires two NUL bytes at end of buffer. */ 95 buf[ len -1 ] =0; 96 97 yylex_init(&scanner); 98 state = yy_scan_buffer( buf, len ,scanner); 99 yylex(scanner); 100 yy_delete_buffer(state,scanner); 101 yylex_destroy(scanner); 102 103 printf("TEST RETURNING OK.\n"); 104 return 0; 105 } 106