]> code.delx.au - gnu-emacs/blob - etc/grammars/grammar.wy
d1a2bf15abfc9989243a49bf12c1ea11ad68d6df
[gnu-emacs] / etc / grammars / grammar.wy
1 ;;; semantic-grammar.wy -- LALR grammar of Semantic input grammars
2 ;;
3 ;; Copyright (C) 2002-2011 Free Software Foundation, Inc.
4 ;;
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Maintainer: David Ponce <david@dponce.com>
7 ;; Created: 26 Aug 2002
8 ;; Keywords: syntax
9 ;; X-RCS: $Id: semantic-grammar.wy,v 1.16 2005/09/30 20:20:27 zappo Exp $
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 %{
27 ;; Current parsed nonterminal name.
28 (defvar semantic-grammar-wy--nterm nil)
29 ;; Index of rule in a nonterminal clause.
30 (defvar semantic-grammar-wy--rindx nil)
31 }
32
33 %package semantic-grammar-wy
34
35 %languagemode wy-mode
36
37 ;; Main
38 %start grammar
39 ;; Reparse
40 %start prologue epilogue declaration nonterminal rule
41 ;; EXPANDFULL
42 %start put_names put_values use_names
43
44 ;; Keywords
45 %type <keyword>
46 %keyword DEFAULT-PREC "%default-prec"
47 %keyword NO-DEFAULT-PREC "%no-default-prec"
48 %keyword KEYWORD "%keyword"
49 %keyword LANGUAGEMODE "%languagemode"
50 %keyword LEFT "%left"
51 %keyword NONASSOC "%nonassoc"
52 %keyword PACKAGE "%package"
53 %keyword PREC "%prec"
54 %keyword PUT "%put"
55 %keyword QUOTEMODE "%quotemode"
56 %keyword RIGHT "%right"
57 %keyword SCOPESTART "%scopestart"
58 %keyword START "%start"
59 %keyword TOKEN "%token"
60 %keyword TYPE "%type"
61 %keyword USE-MACROS "%use-macros"
62
63 ;; Literals
64 %type <string>
65 %token <string> STRING
66
67 %type <symbol> syntax ":?\\(\\sw\\|\\s_\\)+"
68 %token <symbol> SYMBOL
69 %token <symbol> PERCENT_PERCENT "\\`%%\\'"
70
71 %type <char> syntax semantic-grammar-lex-c-char-re
72 %token <char> CHARACTER
73
74 %type <qlist> matchdatatype sexp syntax "\\s'\\s-*("
75 %token <qlist> PREFIXED_LIST
76
77 %type <sexp> matchdatatype sexp syntax "\\="
78 %token <sexp> SEXP
79
80 ;; Don't generate these analyzers which needs special handling code.
81 %token <code> PROLOGUE "%{...%}"
82 %token <code> EPILOGUE "%%...EOF"
83
84 ;; Blocks & Parenthesis
85 %type <block>
86 %token <block> PAREN_BLOCK "(LPAREN RPAREN)"
87 %token <block> BRACE_BLOCK "(LBRACE RBRACE)"
88 %token <open-paren> LPAREN "("
89 %token <close-paren> RPAREN ")"
90 %token <open-paren> LBRACE "{"
91 %token <close-paren> RBRACE "}"
92
93 ;; Punctuations
94 %type <punctuation>
95 %token <punctuation> COLON ":"
96 %token <punctuation> SEMI ";"
97 %token <punctuation> OR "|"
98 %token <punctuation> LT "<"
99 %token <punctuation> GT ">"
100
101 %%
102
103 grammar:
104 prologue
105 | epilogue
106 | declaration
107 | nonterminal
108 | PERCENT_PERCENT
109 ;
110
111 ;;; Prologue/Epilogue
112 ;;
113 prologue:
114 PROLOGUE
115 (CODE-TAG "prologue" nil)
116 ;
117
118 epilogue:
119 EPILOGUE
120 (CODE-TAG "epilogue" nil)
121 ;
122
123 ;;; Declarations
124 ;;
125 declaration:
126 decl
127 (eval $1)
128 ;
129
130 decl:
131 default_prec_decl
132 | no_default_prec_decl
133 | languagemode_decl
134 | package_decl
135 | precedence_decl
136 | put_decl
137 | quotemode_decl
138 | scopestart_decl
139 | start_decl
140 | keyword_decl
141 | token_decl
142 | type_decl
143 | use_macros_decl
144 ;
145
146 default_prec_decl:
147 DEFAULT-PREC
148 `(TAG "default-prec" 'assoc :value '("t"))
149 ;
150
151 no_default_prec_decl:
152 NO-DEFAULT-PREC
153 `(TAG "default-prec" 'assoc :value '("nil"))
154 ;
155
156 languagemode_decl:
157 LANGUAGEMODE symbols
158 `(TAG ',(car $2) 'languagemode :rest ',(cdr $2))
159 ;
160
161 package_decl:
162 PACKAGE SYMBOL
163 `(PACKAGE-TAG ',$2 nil)
164 ;
165
166 precedence_decl:
167 associativity token_type_opt items
168 `(TAG ',$1 'assoc :type ',$2 :value ',$3)
169 ;
170
171 associativity:
172 LEFT
173 (progn "left")
174 | RIGHT
175 (progn "right")
176 | NONASSOC
177 (progn "nonassoc")
178 ;
179
180 put_decl:
181 PUT put_name put_value
182 `(TAG ',$2 'put :value ',(list $3))
183 | PUT put_name put_value_list
184 `(TAG ',$2 'put :value ',$3)
185 | PUT put_name_list put_value
186 `(TAG ',(car $2) 'put :rest ',(cdr $2) :value ',(list $3))
187 | PUT put_name_list put_value_list
188 `(TAG ',(car $2) 'put :rest ',(cdr $2) :value ',$3)
189 ;
190
191 put_name_list:
192 BRACE_BLOCK
193 (mapcar 'semantic-tag-name (EXPANDFULL $1 put_names))
194 ;
195
196 put_names:
197 LBRACE
198 ()
199 | RBRACE
200 ()
201 | put_name
202 ;; Must return a list of Semantic tags to EXPANDFULL!
203 (TAG $1 'put-name)
204 ;
205
206 put_name:
207 SYMBOL
208 | token_type
209 ;
210
211 put_value_list:
212 BRACE_BLOCK
213 (mapcar 'semantic-tag-code-detail (EXPANDFULL $1 put_values))
214 ;
215
216 put_values:
217 LBRACE
218 ()
219 | RBRACE
220 ()
221 | put_value
222 ;; Must return a list of Semantic tags to EXPANDFULL!
223 (CODE-TAG "put-value" $1)
224 ;
225
226 put_value:
227 SYMBOL any_value
228 (cons $1 $2)
229 ;
230
231 scopestart_decl:
232 SCOPESTART SYMBOL
233 `(TAG ',$2 'scopestart)
234 ;
235
236 quotemode_decl:
237 QUOTEMODE SYMBOL
238 `(TAG ',$2 'quotemode)
239 ;
240
241 start_decl:
242 START symbols
243 `(TAG ',(car $2) 'start :rest ',(cdr $2))
244 ;
245
246 keyword_decl:
247 KEYWORD SYMBOL string_value
248 `(TAG ',$2 'keyword :value ',$3)
249 ;
250
251 token_decl:
252 TOKEN token_type_opt SYMBOL string_value
253 `(TAG ',$3 ',(if $2 'token 'keyword) :type ',$2 :value ',$4)
254 | TOKEN token_type_opt symbols
255 `(TAG ',(car $3) 'token :type ',$2 :rest ',(cdr $3))
256 ;
257
258 token_type_opt:
259 ;; EMPTY
260 | token_type
261 ;
262
263 token_type:
264 LT SYMBOL GT
265 (progn $2)
266 ;
267
268 type_decl:
269 TYPE token_type plist_opt
270 `(TAG ',$2 'type :value ',$3)
271 ;
272
273 plist_opt:
274 ;;EMPTY
275 | plist
276 ;
277
278 plist:
279 plist put_value
280 (append (list $2) $1)
281 | put_value
282 (list $1)
283 ;
284
285 use_name_list:
286 BRACE_BLOCK
287 (mapcar 'semantic-tag-name (EXPANDFULL $1 use_names))
288 ;
289
290 use_names:
291 LBRACE
292 ()
293 | RBRACE
294 ()
295 | SYMBOL
296 ;; Must return a list of Semantic tags to EXPANDFULL!
297 (TAG $1 'use-name)
298 ;
299
300 use_macros_decl:
301 USE-MACROS SYMBOL use_name_list
302 `(TAG "macro" 'macro :type ',$2 :value ',$3)
303 ;
304
305 string_value:
306 STRING
307 (read $1)
308 ;
309
310 ;; Return a Lisp readable form
311 any_value:
312 SYMBOL
313 | STRING
314 | PAREN_BLOCK
315 | PREFIXED_LIST
316 | SEXP
317 ;
318
319 symbols:
320 lifo_symbols
321 (nreverse $1)
322 ;
323
324 lifo_symbols:
325 lifo_symbols SYMBOL
326 (cons $2 $1)
327 | SYMBOL
328 (list $1)
329 ;
330
331 ;;; Grammar rules
332 ;;
333 nonterminal:
334 SYMBOL
335 (setq semantic-grammar-wy--nterm $1
336 semantic-grammar-wy--rindx 0)
337 COLON rules SEMI
338 (TAG $1 'nonterminal :children $4)
339 ;
340
341 rules:
342 lifo_rules
343 (apply 'nconc (nreverse $1))
344 ;
345
346 lifo_rules:
347 lifo_rules OR rule
348 (cons $3 $1)
349 | rule
350 (list $1)
351 ;
352
353 rule:
354 rhs
355 (let* ((nterm semantic-grammar-wy--nterm)
356 (rindx semantic-grammar-wy--rindx)
357 (rhs $1)
358 comps prec action elt)
359 (setq semantic-grammar-wy--rindx (1+ semantic-grammar-wy--rindx))
360 (while rhs
361 (setq elt (car rhs)
362 rhs (cdr rhs))
363 (cond
364 ;; precedence level
365 ((vectorp elt)
366 (if prec
367 (error "Duplicate %%prec in `%s:%d' rule" nterm rindx))
368 (setq prec (aref elt 0)))
369 ;; action
370 ((consp elt)
371 ;; don't forget that rhs items are in reverse order, so
372 ;; the end-of-rule semantic action is the first item.
373 (if (or action comps)
374 ;; a mid-rule action
375 (setq comps (cons elt comps)
376 ;; keep rule and action index synchronized
377 semantic-grammar-wy--rindx
378 (1+ semantic-grammar-wy--rindx))
379 ;; the end-of-rule action
380 (setq action (car elt))))
381 ;; item
382 (t
383 (setq comps (cons elt comps)))))
384 (EXPANDTAG
385 (TAG (format "%s:%d" nterm rindx) 'rule
386 :type (if comps "group" "empty")
387 :value comps :prec prec :expr action)))
388 ;
389
390 rhs:
391 ;; EMPTY
392 | rhs item
393 (cons $2 $1)
394 | rhs action
395 (cons (list $2) $1)
396 | rhs PREC item
397 (cons (vector $3) $1)
398 ;
399
400 action:
401 PAREN_BLOCK
402 | PREFIXED_LIST
403 | BRACE_BLOCK
404 (format "(progn\n%s)"
405 (let ((s $1))
406 (if (string-match "^{[\r\n\t ]*" s)
407 (setq s (substring s (match-end 0))))
408 (if (string-match "[\r\n\t ]*}$" s)
409 (setq s (substring s 0 (match-beginning 0))))
410 s))
411 ;
412
413 items:
414 lifo_items
415 (nreverse $1)
416 ;
417
418 lifo_items:
419 lifo_items item
420 (cons $2 $1)
421 | item
422 (list $1)
423 ;
424
425 item:
426 SYMBOL
427 | CHARACTER
428 ;
429
430 %%
431
432 ;;; grammar.wy ends here