1/* $NetBSD$ */ 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 scanner file to build "scanner.c". 28 Input language is any text. 29 "#include <filename>" causes a new scanner to be created. 30 */ 31#include <stdio.h> 32#include <stdlib.h> 33#include "config.h" 34%} 35 36%option 8bit outfile="scanner.c" prefix="test" 37%option nounput nomain noyywrap 38%option reentrant 39%option warn 40 41%x GET_FILENAME 42 43%% 44 45<INITIAL>{ 46^"#include"[[:blank:]]+"<" { BEGIN(GET_FILENAME); } 47.|\n { ECHO; } 48} 49 50<GET_FILENAME>{ 51[[:alnum:]_.-]+> { 52 /* recurse */ 53 yyscan_t scanner; 54 FILE * fp; 55 yytext[yyleng-1]='\0'; 56 if((fp=fopen(yytext,"r"))==NULL) { 57 fprintf(stderr,"*** Error: Could not open include file \"%s\".\n", 58 yytext); 59 yyterminate(); 60 } 61 yylex_init(&scanner); 62 yyset_in( fp, scanner); 63 yyset_out( stdout, scanner); 64 yylex(scanner); 65 yylex_destroy(scanner); 66 67 BEGIN(0); 68 } 69.|\n { 70 fprintf(stderr,"Invalid input \"%s\".\n", yytext); 71 yyterminate(); 72 } 73} 74 75<<EOF>> { fclose(yyin); yyterminate();} 76 77%% 78 79int main (int argc, char** argv); 80 81int 82main ( argc, argv ) 83 int argc; 84 char ** argv; 85{ 86 FILE * fp; 87 yyscan_t scanner; 88 if( argc != 2 ) { 89 fprintf(stderr,"*** Error: Must specifiy one filename.\n"); 90 exit(-1); 91 } 92 if((fp=fopen(argv[1],"r"))==NULL) { 93 fprintf(stderr,"*** Error: fopen(%s) failed.\n",argv[1]); 94 exit(-1); 95 } 96 yylex_init(&scanner); 97 yyset_in( fp, scanner); 98 yyset_out( stdout, scanner); 99 yylex(scanner); 100 yylex_destroy(scanner); 101 printf("TEST RETURNING OK.\n"); 102 return 0; 103} 104