1*ebfedea0SLionel Sambuc /* $NetBSD: lex.l,v 1.1.1.1 2011/04/13 18:14:43 elric Exp $ */ 2*ebfedea0SLionel Sambuc 3*ebfedea0SLionel Sambuc %{ 4*ebfedea0SLionel Sambuc /* 5*ebfedea0SLionel Sambuc * Copyright (c) 1998 - 2000 Kungliga Tekniska Högskolan 6*ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden). 7*ebfedea0SLionel Sambuc * All rights reserved. 8*ebfedea0SLionel Sambuc * 9*ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without 10*ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions 11*ebfedea0SLionel Sambuc * are met: 12*ebfedea0SLionel Sambuc * 13*ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright 14*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer. 15*ebfedea0SLionel Sambuc * 16*ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright 17*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the 18*ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution. 19*ebfedea0SLionel Sambuc * 20*ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors 21*ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software 22*ebfedea0SLionel Sambuc * without specific prior written permission. 23*ebfedea0SLionel Sambuc * 24*ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 25*ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26*ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27*ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 28*ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29*ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30*ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31*ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32*ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33*ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34*ebfedea0SLionel Sambuc * SUCH DAMAGE. 35*ebfedea0SLionel Sambuc */ 36*ebfedea0SLionel Sambuc 37*ebfedea0SLionel Sambuc /* 38*ebfedea0SLionel Sambuc * This is to handle the definition of this symbol in some AIX 39*ebfedea0SLionel Sambuc * headers, which will conflict with the definition that lex will 40*ebfedea0SLionel Sambuc * generate for it. It's only a problem for AIX lex. 41*ebfedea0SLionel Sambuc */ 42*ebfedea0SLionel Sambuc 43*ebfedea0SLionel Sambuc #undef ECHO 44*ebfedea0SLionel Sambuc 45*ebfedea0SLionel Sambuc #include "compile_et.h" 46*ebfedea0SLionel Sambuc #include "parse.h" 47*ebfedea0SLionel Sambuc #include "lex.h" 48*ebfedea0SLionel Sambuc 49*ebfedea0SLionel Sambuc static unsigned lineno = 1; 50*ebfedea0SLionel Sambuc static int getstring(void); 51*ebfedea0SLionel Sambuc 52*ebfedea0SLionel Sambuc #define YY_NO_UNPUT 53*ebfedea0SLionel Sambuc 54*ebfedea0SLionel Sambuc #undef ECHO 55*ebfedea0SLionel Sambuc 56*ebfedea0SLionel Sambuc %} 57*ebfedea0SLionel Sambuc 58*ebfedea0SLionel Sambuc %option nounput 59*ebfedea0SLionel Sambuc 60*ebfedea0SLionel Sambuc %% 61*ebfedea0SLionel Sambuc et { return ET; } 62*ebfedea0SLionel Sambuc error_table { return ET; } 63*ebfedea0SLionel Sambuc ec { return EC; } 64*ebfedea0SLionel Sambuc error_code { return EC; } 65*ebfedea0SLionel Sambuc prefix { return PREFIX; } 66*ebfedea0SLionel Sambuc index { return INDEX; } 67*ebfedea0SLionel Sambuc id { return ID; } 68*ebfedea0SLionel Sambuc end { return END; } 69*ebfedea0SLionel Sambuc [0-9]+ { yylval.number = atoi(yytext); return NUMBER; } 70*ebfedea0SLionel Sambuc #[^\n]* ; 71*ebfedea0SLionel Sambuc [ \t] ; 72*ebfedea0SLionel Sambuc \n { lineno++; } 73*ebfedea0SLionel Sambuc \" { return getstring(); } 74*ebfedea0SLionel Sambuc [a-zA-Z0-9_]+ { yylval.string = strdup(yytext); return STRING; } 75*ebfedea0SLionel Sambuc . { return *yytext; } 76*ebfedea0SLionel Sambuc %% 77*ebfedea0SLionel Sambuc 78*ebfedea0SLionel Sambuc #ifndef yywrap /* XXX */ 79*ebfedea0SLionel Sambuc int 80*ebfedea0SLionel Sambuc yywrap () 81*ebfedea0SLionel Sambuc { 82*ebfedea0SLionel Sambuc return 1; 83*ebfedea0SLionel Sambuc } 84*ebfedea0SLionel Sambuc #endif 85*ebfedea0SLionel Sambuc 86*ebfedea0SLionel Sambuc static int 87*ebfedea0SLionel Sambuc getstring(void) 88*ebfedea0SLionel Sambuc { 89*ebfedea0SLionel Sambuc char x[128]; 90*ebfedea0SLionel Sambuc int i = 0; 91*ebfedea0SLionel Sambuc int c; 92*ebfedea0SLionel Sambuc int quote = 0; 93*ebfedea0SLionel Sambuc while(i < sizeof(x) - 1 && (c = input()) != EOF){ 94*ebfedea0SLionel Sambuc if(quote) { 95*ebfedea0SLionel Sambuc x[i++] = c; 96*ebfedea0SLionel Sambuc quote = 0; 97*ebfedea0SLionel Sambuc continue; 98*ebfedea0SLionel Sambuc } 99*ebfedea0SLionel Sambuc if(c == '\n'){ 100*ebfedea0SLionel Sambuc _lex_error_message("unterminated string"); 101*ebfedea0SLionel Sambuc lineno++; 102*ebfedea0SLionel Sambuc break; 103*ebfedea0SLionel Sambuc } 104*ebfedea0SLionel Sambuc if(c == '\\'){ 105*ebfedea0SLionel Sambuc quote++; 106*ebfedea0SLionel Sambuc continue; 107*ebfedea0SLionel Sambuc } 108*ebfedea0SLionel Sambuc if(c == '\"') 109*ebfedea0SLionel Sambuc break; 110*ebfedea0SLionel Sambuc x[i++] = c; 111*ebfedea0SLionel Sambuc } 112*ebfedea0SLionel Sambuc x[i] = '\0'; 113*ebfedea0SLionel Sambuc yylval.string = strdup(x); 114*ebfedea0SLionel Sambuc if (yylval.string == NULL) 115*ebfedea0SLionel Sambuc err(1, "malloc"); 116*ebfedea0SLionel Sambuc return STRING; 117*ebfedea0SLionel Sambuc } 118*ebfedea0SLionel Sambuc 119*ebfedea0SLionel Sambuc void 120*ebfedea0SLionel Sambuc _lex_error_message (const char *format, ...) 121*ebfedea0SLionel Sambuc { 122*ebfedea0SLionel Sambuc va_list args; 123*ebfedea0SLionel Sambuc 124*ebfedea0SLionel Sambuc va_start (args, format); 125*ebfedea0SLionel Sambuc fprintf (stderr, "%s:%d:", filename, lineno); 126*ebfedea0SLionel Sambuc vfprintf (stderr, format, args); 127*ebfedea0SLionel Sambuc va_end (args); 128*ebfedea0SLionel Sambuc numerror++; 129*ebfedea0SLionel Sambuc } 130