1*0a6a1f1dSLionel Sambuc /* $NetBSD: parse.y,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc %{
4ebfedea0SLionel Sambuc /*
5ebfedea0SLionel Sambuc * Copyright (c) 1998 - 2000 Kungliga Tekniska Högskolan
6ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
7ebfedea0SLionel Sambuc * All rights reserved.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
10ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
11ebfedea0SLionel Sambuc * are met:
12ebfedea0SLionel Sambuc *
13ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
17ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
18ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
19ebfedea0SLionel Sambuc *
20ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
21ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
22ebfedea0SLionel Sambuc * without specific prior written permission.
23ebfedea0SLionel Sambuc *
24ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
25ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
28ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34ebfedea0SLionel Sambuc * SUCH DAMAGE.
35ebfedea0SLionel Sambuc */
36ebfedea0SLionel Sambuc
37ebfedea0SLionel Sambuc #include "compile_et.h"
38ebfedea0SLionel Sambuc #include "lex.h"
39ebfedea0SLionel Sambuc
40ebfedea0SLionel Sambuc void yyerror (char *s);
41ebfedea0SLionel Sambuc static long name2number(const char *str);
42ebfedea0SLionel Sambuc
43ebfedea0SLionel Sambuc extern char *yytext;
44ebfedea0SLionel Sambuc
45ebfedea0SLionel Sambuc /* This is for bison */
46ebfedea0SLionel Sambuc
47ebfedea0SLionel Sambuc #if !defined(alloca) && !defined(HAVE_ALLOCA)
48ebfedea0SLionel Sambuc #define alloca(x) malloc(x)
49ebfedea0SLionel Sambuc #endif
50ebfedea0SLionel Sambuc
51ebfedea0SLionel Sambuc #define YYMALLOC malloc
52ebfedea0SLionel Sambuc #define YYFREE free
53ebfedea0SLionel Sambuc
54ebfedea0SLionel Sambuc %}
55ebfedea0SLionel Sambuc
56ebfedea0SLionel Sambuc %union {
57ebfedea0SLionel Sambuc char *string;
58ebfedea0SLionel Sambuc int number;
59ebfedea0SLionel Sambuc }
60ebfedea0SLionel Sambuc
61ebfedea0SLionel Sambuc %token ET INDEX PREFIX EC ID END
62ebfedea0SLionel Sambuc %token <string> STRING
63ebfedea0SLionel Sambuc %token <number> NUMBER
64ebfedea0SLionel Sambuc
65ebfedea0SLionel Sambuc %%
66ebfedea0SLionel Sambuc
67ebfedea0SLionel Sambuc file : /* */
68ebfedea0SLionel Sambuc | header statements
69ebfedea0SLionel Sambuc ;
70ebfedea0SLionel Sambuc
71ebfedea0SLionel Sambuc header : id et
72ebfedea0SLionel Sambuc | et
73ebfedea0SLionel Sambuc ;
74ebfedea0SLionel Sambuc
75ebfedea0SLionel Sambuc id : ID STRING
76ebfedea0SLionel Sambuc {
77ebfedea0SLionel Sambuc id_str = $2;
78ebfedea0SLionel Sambuc }
79ebfedea0SLionel Sambuc ;
80ebfedea0SLionel Sambuc
81ebfedea0SLionel Sambuc et : ET STRING
82ebfedea0SLionel Sambuc {
83ebfedea0SLionel Sambuc base_id = name2number($2);
84ebfedea0SLionel Sambuc strlcpy(name, $2, sizeof(name));
85ebfedea0SLionel Sambuc free($2);
86ebfedea0SLionel Sambuc }
87ebfedea0SLionel Sambuc | ET STRING STRING
88ebfedea0SLionel Sambuc {
89ebfedea0SLionel Sambuc base_id = name2number($2);
90ebfedea0SLionel Sambuc strlcpy(name, $3, sizeof(name));
91ebfedea0SLionel Sambuc free($2);
92ebfedea0SLionel Sambuc free($3);
93ebfedea0SLionel Sambuc }
94ebfedea0SLionel Sambuc ;
95ebfedea0SLionel Sambuc
96ebfedea0SLionel Sambuc statements : statement
97ebfedea0SLionel Sambuc | statements statement
98ebfedea0SLionel Sambuc ;
99ebfedea0SLionel Sambuc
100ebfedea0SLionel Sambuc statement : INDEX NUMBER
101ebfedea0SLionel Sambuc {
102ebfedea0SLionel Sambuc number = $2;
103ebfedea0SLionel Sambuc }
104ebfedea0SLionel Sambuc | PREFIX STRING
105ebfedea0SLionel Sambuc {
106ebfedea0SLionel Sambuc free(prefix);
107ebfedea0SLionel Sambuc asprintf (&prefix, "%s_", $2);
108ebfedea0SLionel Sambuc if (prefix == NULL)
109ebfedea0SLionel Sambuc errx(1, "malloc");
110ebfedea0SLionel Sambuc free($2);
111ebfedea0SLionel Sambuc }
112ebfedea0SLionel Sambuc | PREFIX
113ebfedea0SLionel Sambuc {
114ebfedea0SLionel Sambuc prefix = realloc(prefix, 1);
115ebfedea0SLionel Sambuc if (prefix == NULL)
116ebfedea0SLionel Sambuc errx(1, "malloc");
117ebfedea0SLionel Sambuc *prefix = '\0';
118ebfedea0SLionel Sambuc }
119ebfedea0SLionel Sambuc | EC STRING ',' STRING
120ebfedea0SLionel Sambuc {
121ebfedea0SLionel Sambuc struct error_code *ec = malloc(sizeof(*ec));
122ebfedea0SLionel Sambuc
123ebfedea0SLionel Sambuc if (ec == NULL)
124ebfedea0SLionel Sambuc errx(1, "malloc");
125ebfedea0SLionel Sambuc
126ebfedea0SLionel Sambuc ec->next = NULL;
127ebfedea0SLionel Sambuc ec->number = number;
128ebfedea0SLionel Sambuc if(prefix && *prefix != '\0') {
129ebfedea0SLionel Sambuc asprintf (&ec->name, "%s%s", prefix, $2);
130ebfedea0SLionel Sambuc if (ec->name == NULL)
131ebfedea0SLionel Sambuc errx(1, "malloc");
132ebfedea0SLionel Sambuc free($2);
133ebfedea0SLionel Sambuc } else
134ebfedea0SLionel Sambuc ec->name = $2;
135ebfedea0SLionel Sambuc ec->string = $4;
136ebfedea0SLionel Sambuc APPEND(codes, ec);
137ebfedea0SLionel Sambuc number++;
138ebfedea0SLionel Sambuc }
139ebfedea0SLionel Sambuc | END
140ebfedea0SLionel Sambuc {
141ebfedea0SLionel Sambuc YYACCEPT;
142ebfedea0SLionel Sambuc }
143ebfedea0SLionel Sambuc ;
144ebfedea0SLionel Sambuc
145ebfedea0SLionel Sambuc %%
146ebfedea0SLionel Sambuc
147ebfedea0SLionel Sambuc static long
148ebfedea0SLionel Sambuc name2number(const char *str)
149ebfedea0SLionel Sambuc {
150ebfedea0SLionel Sambuc const char *p;
151ebfedea0SLionel Sambuc long num = 0;
152ebfedea0SLionel Sambuc const char *x = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
153ebfedea0SLionel Sambuc "abcdefghijklmnopqrstuvwxyz0123456789_";
154ebfedea0SLionel Sambuc if(strlen(str) > 4) {
155ebfedea0SLionel Sambuc yyerror("table name too long");
156ebfedea0SLionel Sambuc return 0;
157ebfedea0SLionel Sambuc }
158ebfedea0SLionel Sambuc for(p = str; *p; p++){
159ebfedea0SLionel Sambuc char *q = strchr(x, *p);
160ebfedea0SLionel Sambuc if(q == NULL) {
161ebfedea0SLionel Sambuc yyerror("invalid character in table name");
162ebfedea0SLionel Sambuc return 0;
163ebfedea0SLionel Sambuc }
164ebfedea0SLionel Sambuc num = (num << 6) + (q - x) + 1;
165ebfedea0SLionel Sambuc }
166ebfedea0SLionel Sambuc num <<= 8;
167ebfedea0SLionel Sambuc if(num > 0x7fffffff)
168ebfedea0SLionel Sambuc num = -(0xffffffff - num + 1);
169ebfedea0SLionel Sambuc return num;
170ebfedea0SLionel Sambuc }
171ebfedea0SLionel Sambuc
172ebfedea0SLionel Sambuc void
yyerror(char * s)173ebfedea0SLionel Sambuc yyerror (char *s)
174ebfedea0SLionel Sambuc {
175ebfedea0SLionel Sambuc _lex_error_message ("%s\n", s);
176ebfedea0SLionel Sambuc }
177