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 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 61int main(void); 62 63int 64main () 65{ 66 char * buf; 67 int len; 68 YY_BUFFER_STATE state; 69 70 71 /* Scan a good string. */ 72 printf("Testing: yy_scan_string(%s): ",INPUT_STRING_1); fflush(stdout); 73 state = yy_scan_string ( INPUT_STRING_1 ); 74 yylex(); 75 yy_delete_buffer(state); 76 77 /* Scan only the first 12 chars of a string. */ 78 printf("Testing: yy_scan_bytes(%s): ",INPUT_STRING_2); fflush(stdout); 79 state = yy_scan_bytes ( INPUT_STRING_2, 12 ); 80 yylex(); 81 yy_delete_buffer(state); 82 83 /* Scan directly from a buffer. 84 We make a copy, since the buffer will be modified by flex.*/ 85 printf("Testing: yy_scan_buffer(%s): ",INPUT_STRING_1); fflush(stdout); 86 len = strlen(INPUT_STRING_1) + 2; 87 buf = (char*)malloc( len ); 88 strcpy( buf, INPUT_STRING_1); 89 buf[ len -2 ] = 0; /* Flex requires two NUL bytes at end of buffer. */ 90 buf[ len -1 ] =0; 91 92 state = yy_scan_buffer( buf, len ); 93 yylex(); 94 yy_delete_buffer(state); 95 96 printf("TEST RETURNING OK.\n"); 97 return 0; 98} 99