1178825Sdfr%{ 2178825Sdfr/* 3233294Sstas * Copyright (c) 2004 Kungliga Tekniska H��gskolan 4233294Sstas * (Royal Institute of Technology, Stockholm, Sweden). 5233294Sstas * All rights reserved. 6178825Sdfr * 7233294Sstas * Redistribution and use in source and binary forms, with or without 8233294Sstas * modification, are permitted provided that the following conditions 9233294Sstas * are met: 10178825Sdfr * 11233294Sstas * 1. Redistributions of source code must retain the above copyright 12233294Sstas * notice, this list of conditions and the following disclaimer. 13178825Sdfr * 14233294Sstas * 2. Redistributions in binary form must reproduce the above copyright 15233294Sstas * notice, this list of conditions and the following disclaimer in the 16233294Sstas * documentation and/or other materials provided with the distribution. 17178825Sdfr * 18233294Sstas * 3. Neither the name of the Institute nor the names of its contributors 19233294Sstas * may be used to endorse or promote products derived from this software 20233294Sstas * without specific prior written permission. 21178825Sdfr * 22233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 23233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25233294Sstas * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 26233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32233294Sstas * SUCH DAMAGE. 33178825Sdfr */ 34178825Sdfr 35233294Sstas/* $Id$ */ 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#undef ECHO 51178825Sdfr 52178825Sdfr%} 53233294Sstas 54233294Sstas%option nounput 55233294Sstas 56178825Sdfr%% 57178825Sdfr[A-Za-z][-A-Za-z0-9_]* { 58178825Sdfr yylval.string = strdup ((const char *)yytext); 59178825Sdfr return LITERAL; 60178825Sdfr } 61178825Sdfr"\"" { yylval.string = handle_string(); return STRING; } 62178825Sdfr\n { ++lineno; } 63178825Sdfr\/\* { handle_comment(); } 64178825Sdfr[={}] { return *yytext; } 65178825Sdfr[ \t] ; 66178825Sdfr%% 67178825Sdfr 68178825Sdfrvoid 69178825Sdfrerror_message (const char *format, ...) 70178825Sdfr{ 71178825Sdfr va_list args; 72178825Sdfr 73178825Sdfr va_start (args, format); 74178825Sdfr fprintf (stderr, "%s:%d: ", filename, lineno); 75178825Sdfr vfprintf (stderr, format, args); 76178825Sdfr va_end (args); 77178825Sdfr error_flag++; 78178825Sdfr} 79178825Sdfr 80178825Sdfrvoid 81178825Sdfryyerror (char *s) 82178825Sdfr{ 83178825Sdfr error_message("%s\n", s); 84178825Sdfr} 85178825Sdfr 86178825Sdfrstatic void 87178825Sdfrhandle_comment(void) 88178825Sdfr{ 89178825Sdfr int c; 90178825Sdfr int start_lineno = lineno; 91178825Sdfr int level = 1; 92178825Sdfr int seen_star = 0; 93178825Sdfr int seen_slash = 0; 94178825Sdfr while((c = input()) != EOF) { 95178825Sdfr if(c == '/') { 96178825Sdfr if(seen_star) { 97178825Sdfr if(--level == 0) 98178825Sdfr return; 99178825Sdfr seen_star = 0; 100178825Sdfr continue; 101178825Sdfr } 102178825Sdfr seen_slash = 1; 103178825Sdfr continue; 104233294Sstas } else if(c == '*') { 105178825Sdfr if(seen_slash) { 106178825Sdfr level++; 107178825Sdfr seen_star = seen_slash = 0; 108178825Sdfr continue; 109233294Sstas } 110178825Sdfr seen_star = 1; 111178825Sdfr continue; 112178825Sdfr } 113178825Sdfr seen_star = seen_slash = 0; 114178825Sdfr if(c == '\n') { 115178825Sdfr lineno++; 116178825Sdfr continue; 117178825Sdfr } 118178825Sdfr } 119178825Sdfr if(c == EOF) 120178825Sdfr error_message("unterminated comment, possibly started on line %d\n", start_lineno); 121178825Sdfr} 122178825Sdfr 123178825Sdfrstatic char * 124178825Sdfrhandle_string(void) 125178825Sdfr{ 126178825Sdfr char x[1024]; 127178825Sdfr int i = 0; 128178825Sdfr int c; 129178825Sdfr int quote = 0; 130178825Sdfr while((c = input()) != EOF){ 131178825Sdfr if(quote) { 132178825Sdfr x[i++] = '\\'; 133178825Sdfr x[i++] = c; 134178825Sdfr quote = 0; 135178825Sdfr continue; 136178825Sdfr } 137178825Sdfr if(c == '\n'){ 138178825Sdfr error_message("unterminated string"); 139178825Sdfr lineno++; 140178825Sdfr break; 141178825Sdfr } 142178825Sdfr if(c == '\\'){ 143178825Sdfr quote++; 144178825Sdfr continue; 145178825Sdfr } 146178825Sdfr if(c == '\"') 147178825Sdfr break; 148178825Sdfr x[i++] = c; 149178825Sdfr } 150178825Sdfr x[i] = '\0'; 151178825Sdfr return strdup(x); 152178825Sdfr} 153178825Sdfr 154178825Sdfrint 155233294Sstasyywrap () 156178825Sdfr{ 157178825Sdfr return 1; 158178825Sdfr} 159