parse.y revision 50764
150764Smarkm%{
250764Smarkm/*
350764Smarkm * Copyright (c) 1998, 1999 Kungliga Tekniska H�gskolan
450764Smarkm * (Royal Institute of Technology, Stockholm, Sweden).
550764Smarkm * All rights reserved.
650764Smarkm *
750764Smarkm * Redistribution and use in source and binary forms, with or without
850764Smarkm * modification, are permitted provided that the following conditions
950764Smarkm * are met:
1050764Smarkm *
1150764Smarkm * 1. Redistributions of source code must retain the above copyright
1250764Smarkm *    notice, this list of conditions and the following disclaimer.
1350764Smarkm *
1450764Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1550764Smarkm *    notice, this list of conditions and the following disclaimer in the
1650764Smarkm *    documentation and/or other materials provided with the distribution.
1750764Smarkm *
1850764Smarkm * 3. All advertising materials mentioning features or use of this software
1950764Smarkm *    must display the following acknowledgement:
2050764Smarkm *      This product includes software developed by Kungliga Tekniska
2150764Smarkm *      H�gskolan and its contributors.
2250764Smarkm *
2350764Smarkm * 4. Neither the name of the Institute nor the names of its contributors
2450764Smarkm *    may be used to endorse or promote products derived from this software
2550764Smarkm *    without specific prior written permission.
2650764Smarkm *
2750764Smarkm * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2850764Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2950764Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3050764Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
3150764Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3250764Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3350764Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3450764Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3550764Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3650764Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3750764Smarkm * SUCH DAMAGE.
3850764Smarkm */
3950764Smarkm
4050764Smarkm#include "compile_et.h"
4150764SmarkmRCSID("$Id: parse.y,v 1.9 1999/07/04 14:54:58 assar Exp $");
4250764Smarkm
4350764Smarkmvoid yyerror (char *s);
4450764Smarkmlong name2number(const char *str);
4550764Smarkmvoid error_message(char *, ...);
4650764Smarkm
4750764Smarkmextern char *yytext;
4850764Smarkm
4950764Smarkm/* This is for bison */
5050764Smarkm
5150764Smarkm#if !defined(alloca) && !defined(HAVE_ALLOCA)
5250764Smarkm#define alloca(x) malloc(x)
5350764Smarkm#endif
5450764Smarkm
5550764Smarkm%}
5650764Smarkm
5750764Smarkm%union {
5850764Smarkm  char *string;
5950764Smarkm  int number;
6050764Smarkm}
6150764Smarkm
6250764Smarkm%token ET INDEX PREFIX EC ID END
6350764Smarkm%token <string> STRING
6450764Smarkm%token <number> NUMBER
6550764Smarkm
6650764Smarkm%%
6750764Smarkm
6850764Smarkmfile		: /* */
6950764Smarkm		| header statements
7050764Smarkm		;
7150764Smarkm
7250764Smarkmheader		: id et
7350764Smarkm		| et
7450764Smarkm		;
7550764Smarkm
7650764Smarkmid		: ID STRING
7750764Smarkm		{
7850764Smarkm		    id_str = $2;
7950764Smarkm		}
8050764Smarkm		;
8150764Smarkm
8250764Smarkmet		: ET STRING
8350764Smarkm		{
8450764Smarkm		    base = name2number($2);
8550764Smarkm		    strncpy(name, $2, sizeof(name));
8650764Smarkm		    name[sizeof(name) - 1] = '\0';
8750764Smarkm		    free($2);
8850764Smarkm		}
8950764Smarkm		| ET STRING STRING
9050764Smarkm		{
9150764Smarkm		    base = name2number($2);
9250764Smarkm		    strncpy(name, $3, sizeof(name));
9350764Smarkm		    name[sizeof(name) - 1] = '\0';
9450764Smarkm		    free($2);
9550764Smarkm		    free($3);
9650764Smarkm		}
9750764Smarkm		;
9850764Smarkm
9950764Smarkmstatements	: statement
10050764Smarkm		| statements statement
10150764Smarkm		;
10250764Smarkm
10350764Smarkmstatement	: INDEX NUMBER
10450764Smarkm		{
10550764Smarkm			number = $2;
10650764Smarkm		}
10750764Smarkm		| PREFIX STRING
10850764Smarkm		{
10950764Smarkm		    prefix = realloc(prefix, strlen($2) + 2);
11050764Smarkm		    strcpy(prefix, $2);
11150764Smarkm		    strcat(prefix, "_");
11250764Smarkm		    free($2);
11350764Smarkm		}
11450764Smarkm		| PREFIX
11550764Smarkm		{
11650764Smarkm		    prefix = realloc(prefix, 1);
11750764Smarkm		    *prefix = '\0';
11850764Smarkm		}
11950764Smarkm		| EC STRING ',' STRING
12050764Smarkm		{
12150764Smarkm		    struct error_code *ec = malloc(sizeof(*ec));
12250764Smarkm
12350764Smarkm		    ec->next = NULL;
12450764Smarkm		    ec->number = number;
12550764Smarkm		    if(prefix && *prefix != '\0') {
12650764Smarkm			asprintf (&ec->name, "%s%s", prefix, $2);
12750764Smarkm			free($2);
12850764Smarkm		    } else
12950764Smarkm			ec->name = $2;
13050764Smarkm		    ec->string = $4;
13150764Smarkm		    APPEND(codes, ec);
13250764Smarkm		    number++;
13350764Smarkm		}
13450764Smarkm		| END
13550764Smarkm		{
13650764Smarkm			YYACCEPT;
13750764Smarkm		}
13850764Smarkm		;
13950764Smarkm
14050764Smarkm%%
14150764Smarkm
14250764Smarkmlong
14350764Smarkmname2number(const char *str)
14450764Smarkm{
14550764Smarkm    const char *p;
14650764Smarkm    long base = 0;
14750764Smarkm    const char *x = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
14850764Smarkm	"abcdefghijklmnopqrstuvwxyz0123456789_";
14950764Smarkm    if(strlen(str) > 4) {
15050764Smarkm	yyerror("table name too long");
15150764Smarkm	return 0;
15250764Smarkm    }
15350764Smarkm    for(p = str; *p; p++){
15450764Smarkm	char *q = strchr(x, *p);
15550764Smarkm	if(q == NULL) {
15650764Smarkm	    yyerror("invalid character in table name");
15750764Smarkm	    return 0;
15850764Smarkm	}
15950764Smarkm	base = (base << 6) + (q - x) + 1;
16050764Smarkm    }
16150764Smarkm    base <<= 8;
16250764Smarkm    if(base > 0x7fffffff)
16350764Smarkm	base = -(0xffffffff - base + 1);
16450764Smarkm    return base;
16550764Smarkm}
16650764Smarkm
16750764Smarkmvoid
16850764Smarkmyyerror (char *s)
16950764Smarkm{
17050764Smarkm     error_message ("%s\n", s);
17150764Smarkm}
172