slc-lex.l revision 178825
1178825Sdfr%{ 2178825Sdfr/* 3178825Sdfr * Copyright (c) 2004 Kungliga Tekniska H�gskolan 4178825Sdfr * (Royal Institute of Technology, Stockholm, Sweden). 5178825Sdfr * All rights reserved. 6178825Sdfr * 7178825Sdfr * Redistribution and use in source and binary forms, with or without 8178825Sdfr * modification, are permitted provided that the following conditions 9178825Sdfr * are met: 10178825Sdfr * 11178825Sdfr * 1. Redistributions of source code must retain the above copyright 12178825Sdfr * notice, this list of conditions and the following disclaimer. 13178825Sdfr * 14178825Sdfr * 2. Redistributions in binary form must reproduce the above copyright 15178825Sdfr * notice, this list of conditions and the following disclaimer in the 16178825Sdfr * documentation and/or other materials provided with the distribution. 17178825Sdfr * 18178825Sdfr * 3. Neither the name of the Institute nor the names of its contributors 19178825Sdfr * may be used to endorse or promote products derived from this software 20178825Sdfr * without specific prior written permission. 21178825Sdfr * 22178825Sdfr * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 23178825Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24178825Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25178825Sdfr * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 26178825Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27178825Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28178825Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29178825Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30178825Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31178825Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32178825Sdfr * SUCH DAMAGE. 33178825Sdfr */ 34178825Sdfr 35178825Sdfr/* $Id: slc-lex.l 15118 2005-05-10 22:19:01Z lha $ */ 36178825Sdfr 37178825Sdfr#undef ECHO 38178825Sdfr 39178825Sdfr#include <stdio.h> 40178825Sdfr#include <string.h> 41178825Sdfr#include <stdarg.h> 42178825Sdfr#include <stdlib.h> 43178825Sdfr#include "slc.h" 44178825Sdfr#include "slc-gram.h" 45178825Sdfrunsigned lineno = 1; 46178825Sdfr 47178825Sdfrstatic void handle_comment(void); 48178825Sdfrstatic char * handle_string(void); 49178825Sdfr 50178825Sdfr#define YY_NO_UNPUT 51178825Sdfr 52178825Sdfr#undef ECHO 53178825Sdfr 54178825Sdfr%} 55178825Sdfr%% 56178825Sdfr[A-Za-z][-A-Za-z0-9_]* { 57178825Sdfr yylval.string = strdup ((const char *)yytext); 58178825Sdfr return LITERAL; 59178825Sdfr } 60178825Sdfr"\"" { yylval.string = handle_string(); return STRING; } 61178825Sdfr\n { ++lineno; } 62178825Sdfr\/\* { handle_comment(); } 63178825Sdfr[={}] { return *yytext; } 64178825Sdfr[ \t] ; 65178825Sdfr%% 66178825Sdfr 67178825Sdfrvoid 68178825Sdfrerror_message (const char *format, ...) 69178825Sdfr{ 70178825Sdfr va_list args; 71178825Sdfr 72178825Sdfr va_start (args, format); 73178825Sdfr fprintf (stderr, "%s:%d: ", filename, lineno); 74178825Sdfr vfprintf (stderr, format, args); 75178825Sdfr va_end (args); 76178825Sdfr error_flag++; 77178825Sdfr} 78178825Sdfr 79178825Sdfrvoid 80178825Sdfryyerror (char *s) 81178825Sdfr{ 82178825Sdfr error_message("%s\n", s); 83178825Sdfr} 84178825Sdfr 85178825Sdfrstatic void 86178825Sdfrhandle_comment(void) 87178825Sdfr{ 88178825Sdfr int c; 89178825Sdfr int start_lineno = lineno; 90178825Sdfr int level = 1; 91178825Sdfr int seen_star = 0; 92178825Sdfr int seen_slash = 0; 93178825Sdfr while((c = input()) != EOF) { 94178825Sdfr if(c == '/') { 95178825Sdfr if(seen_star) { 96178825Sdfr if(--level == 0) 97178825Sdfr return; 98178825Sdfr seen_star = 0; 99178825Sdfr continue; 100178825Sdfr } 101178825Sdfr seen_slash = 1; 102178825Sdfr continue; 103178825Sdfr } 104178825Sdfr if(seen_star && c == '/') { 105178825Sdfr if(--level == 0) 106178825Sdfr return; 107178825Sdfr seen_star = 0; 108178825Sdfr continue; 109178825Sdfr } 110178825Sdfr if(c == '*') { 111178825Sdfr if(seen_slash) { 112178825Sdfr level++; 113178825Sdfr seen_star = seen_slash = 0; 114178825Sdfr continue; 115178825Sdfr } 116178825Sdfr seen_star = 1; 117178825Sdfr continue; 118178825Sdfr } 119178825Sdfr seen_star = seen_slash = 0; 120178825Sdfr if(c == '\n') { 121178825Sdfr lineno++; 122178825Sdfr continue; 123178825Sdfr } 124178825Sdfr } 125178825Sdfr if(c == EOF) 126178825Sdfr error_message("unterminated comment, possibly started on line %d\n", start_lineno); 127178825Sdfr} 128178825Sdfr 129178825Sdfrstatic char * 130178825Sdfrhandle_string(void) 131178825Sdfr{ 132178825Sdfr char x[1024]; 133178825Sdfr int i = 0; 134178825Sdfr int c; 135178825Sdfr int quote = 0; 136178825Sdfr while((c = input()) != EOF){ 137178825Sdfr if(quote) { 138178825Sdfr x[i++] = '\\'; 139178825Sdfr x[i++] = c; 140178825Sdfr quote = 0; 141178825Sdfr continue; 142178825Sdfr } 143178825Sdfr if(c == '\n'){ 144178825Sdfr error_message("unterminated string"); 145178825Sdfr lineno++; 146178825Sdfr break; 147178825Sdfr } 148178825Sdfr if(c == '\\'){ 149178825Sdfr quote++; 150178825Sdfr continue; 151178825Sdfr } 152178825Sdfr if(c == '\"') 153178825Sdfr break; 154178825Sdfr x[i++] = c; 155178825Sdfr } 156178825Sdfr x[i] = '\0'; 157178825Sdfr return strdup(x); 158178825Sdfr} 159178825Sdfr 160178825Sdfrint 161178825Sdfryywrap () 162178825Sdfr{ 163178825Sdfr return 1; 164178825Sdfr} 165