xref: /netbsd-src/sys/arch/hpc/hpc/platid_gen/gram.y (revision 1c9d295ac50e01b09df0d79bf02aee4de7cc31dd)
1 %{
2 /*	$NetBSD: gram.y,v 1.5 2014/03/26 17:54:46 christos Exp $	*/
3 
4 /*-
5  * Copyright (c) 1999
6  *         Shin Takemura and PocketBSD Project. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the PocketBSD project
19  *	and its contributors.
20  * 4. Neither the name of the project nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  */
37 
38 #include <stdio.h>
39 #include <strings.h>
40 #include <ctype.h>
41 
42 #include "platid_gen.h"
43 #include "gram.h"
44 
45 #define LIST_NEW(l)	{ \
46 	(l) = new_node(N_LIST, 0, NULL, NULL, NULL); \
47 }
48 #define LIST_ADD(l, i)	{ \
49 	if ((l)->ptr1 == NULL) { \
50 		(l)->ptr1 = (i); \
51 		(l)->ptr2 = (i); \
52 	} else { \
53 		((node_t*)(l)->ptr2)->link = (i); \
54 		(l)->ptr2 = (i); \
55 	} \
56 	(i)->link = NULL; \
57 	(l)->val++; \
58 }
59 
60 %}
61 
62 %union {
63 	struct node_s *node;
64 	const char *str;
65 	int	val;
66 }
67 
68 %token '{' '}' '=' ':'
69 %token <str>FSYM
70 %token <str>SYM
71 %token <str>MOD
72 %token <str>NAME
73 %token <str>DIRECTIVE
74 
75 %type <str>sym
76 %type <val>name_prefix
77 %type <node>name_opt
78 %type <node>ent
79 %type <node>sub_list
80 %type <node>sub_item
81 %type <node>list
82 %type <node>item
83 
84 %%
85 
86 start: list { def_tree = $1; };
87 
88 list:
89   list item { LIST_ADD($1, $2); $$ = $1; } |
90   /* empty */ { LIST_NEW($$); };
91 
92 item:
93 sym ':' { $$ = new_node(N_LABEL, 0, $1, NULL, NULL); } |
94 sym '=' sym  { $$ = new_node(N_MODIFIER, 0, $1, $3, NULL); } |
95 ent { $$ = $1; }|
96 '{' sub_list '}' { $$ = $2; } |
97 DIRECTIVE { $$ = new_node(N_DIRECTIVE, 0, $1, NULL, NULL); };
98 
99 sub_list:
100   sub_list sub_item { LIST_ADD($1, $2); $$ = $1; } |
101   /* empty */ { LIST_NEW($$); };
102 
103 sub_item:
104   sym '=' sym { $$ = new_node(N_MODIFIER, 0, $1, $3, NULL); }|
105   ent { $$ = $1; } |
106   '{' sub_list '}' { $$ = $2; } |
107    DIRECTIVE { $$ = new_node(N_DIRECTIVE, 0, $1, NULL, NULL); };
108 
109 ent : sym name_opt {
110 	$2->ptr1 = $1;
111 	/*
112 	if ($2->ptr2 == NULL) {
113 		$2->ptr2 = strdup($1);
114 	}
115 	touppers((char*)$2->ptr1);
116 	*/
117 	$$ = $2;
118 };
119 
120 name_opt:
121     name_prefix NAME { $$ = new_node(N_ENTRY, $1, NULL, $2, NULL); } |
122     name_prefix { $$ = new_node(N_ENTRY, $1, NULL, NULL, NULL); };
123 
124 name_prefix:
125   name_prefix '-' { $$ = $1 + 1; } |
126   /* empty */ { $$ = 0; }
127 
128 sym:
129   FSYM  { $$ = $1; } |
130   SYM { $$ = $1; } |
131   MOD  { $$ = $1; };
132 
133 %%
134 
135 extern int YYLEX_DECL();
136 
137 char*
touppers(s)138 touppers(s)
139 	char *s;
140 {
141 	char *p;
142 
143 	for (p = s; *p != '\0'; p++)
144 		*p = toupper(*p);
145 	return (s);
146 }
147 
148 void*
mem_alloc(size)149 mem_alloc(size)
150 	int size;
151 {
152 	void *res;
153 
154 	if ((res = malloc(size)) == NULL) {
155 		fprintf(stderr, "memory allocation failed.\n");
156 		exit(1);
157 	}
158 	return (res);
159 }
160 
161 node_t*
new_node(type,val,ptr1,ptr2,link)162 new_node(type, val, ptr1, ptr2, link)
163 	int type;
164 	int val;
165 	const void *ptr1, *ptr2;
166 	node_t *link;
167 {
168 	node_t *res;
169 
170 	res = mem_alloc(sizeof(node_t));
171 	res->type = type;
172 	res->val = val;
173 	res->ptr1 = ptr1;
174 	res->ptr2 = ptr2;
175 	res->link = link;
176 	return (res);
177 }
178 
179 void
dump_node(prefix,n)180 dump_node(prefix, n)
181 	char *prefix;
182 	node_t* n;
183 {
184 	char prefix2[1024];
185 	node_t *np;
186 
187 	snprintf(prefix2, sizeof(prefix2), "%s    ", prefix);
188 
189 	switch (n->type) {
190 	case N_LABEL:
191 		printf("%s%s:\n", prefix, n->ptr1);
192 		break;
193 	case N_MODIFIER:
194 		printf("%s%s=%s\n", prefix, n->ptr1, n->ptr2);
195 		break;
196 	case N_ENTRY:
197 		if (n->val == 0)
198 			printf("%s%s(%s)\n", prefix, n->ptr1, n->ptr2);
199 		else
200 			printf("%s%s(-%d, %s)\n",
201 			       prefix, n->ptr1, n->val, n->ptr2);
202 		break;
203 	case N_LIST:
204 		printf("%s{\n", prefix);
205 	       	for (np = (node_t*)n->ptr1; np; np = np->link) {
206 			dump_node(prefix2, np);
207 		}
208 		printf("%s}\n", prefix);
209 		break;
210 	case N_DIRECTIVE:
211 		printf("%s", n->ptr1);
212 		break;
213 		break;
214 	default:
215 		printf("%s???\n", prefix);
216 		break;
217 	}
218 }
219 
220 void
yyerror(s)221 yyerror(s)
222 	const char *s;
223 {
224 	extern int yyline;
225 
226 	fprintf(stderr, "%d: %s\n", yyline, s);
227 }
228