1 ;;; semantic-grammar.wy -- LALR grammar of Semantic input grammars
3 ;; Copyright (C) 2002-2013 Free Software Foundation, Inc.
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Maintainer: David Ponce <david@dponce.com>
7 ;; Created: 26 Aug 2002
9 ;; X-RCS: $Id: semantic-grammar.wy,v 1.16 2005/09/30 20:20:27 zappo Exp $
11 ;; This file is part of GNU Emacs.
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.
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.
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/>.
26 %package semantic-grammar-wy
27 %provide semantic/grammar-wy
30 (defvar semantic-grammar-lex-c-char-re)
32 ;; Current parsed nonterminal name.
33 (defvar semantic-grammar-wy--nterm nil)
34 ;; Index of rule in a nonterminal clause.
35 (defvar semantic-grammar-wy--rindx nil)
43 %start prologue epilogue declaration nonterminal rule
45 %start put_names put_values use_names
49 %keyword DEFAULT-PREC "%default-prec"
50 %keyword NO-DEFAULT-PREC "%no-default-prec"
51 %keyword KEYWORD "%keyword"
52 %keyword LANGUAGEMODE "%languagemode"
54 %keyword NONASSOC "%nonassoc"
55 %keyword PACKAGE "%package"
56 %keyword PROVIDE "%provide"
59 %keyword QUOTEMODE "%quotemode"
60 %keyword RIGHT "%right"
61 %keyword SCOPESTART "%scopestart"
62 %keyword START "%start"
63 %keyword TOKEN "%token"
65 %keyword USE-MACROS "%use-macros"
69 %token <string> STRING
71 %type <symbol> syntax ":?\\(\\sw\\|\\s_\\)+"
72 %token <symbol> SYMBOL
73 %token <symbol> PERCENT_PERCENT "\\`%%\\'"
75 %type <char> syntax semantic-grammar-lex-c-char-re
76 %token <char> CHARACTER
78 %type <qlist> matchdatatype sexp syntax "\\s'\\s-*("
79 %token <qlist> PREFIXED_LIST
81 %type <sexp> matchdatatype sexp syntax "\\="
84 ;; Don't generate these analyzers which needs special handling code.
85 %token <code> PROLOGUE "%{...%}"
86 %token <code> EPILOGUE "%%...EOF"
88 ;; Blocks & Parenthesis
90 %token <block> PAREN_BLOCK "(LPAREN RPAREN)"
91 %token <block> BRACE_BLOCK "(LBRACE RBRACE)"
92 %token <open-paren> LPAREN "("
93 %token <close-paren> RPAREN ")"
94 %token <open-paren> LBRACE "{"
95 %token <close-paren> RBRACE "}"
99 %token <punctuation> COLON ":"
100 %token <punctuation> SEMI ";"
101 %token <punctuation> OR "|"
102 %token <punctuation> LT "<"
103 %token <punctuation> GT ">"
115 ;;; Prologue/Epilogue
119 (CODE-TAG "prologue" nil)
124 (CODE-TAG "epilogue" nil)
136 | no_default_prec_decl
153 `(TAG "default-prec" 'assoc :value '("t"))
156 no_default_prec_decl:
158 `(TAG "default-prec" 'assoc :value '("nil"))
163 `(TAG ',(car $2) 'languagemode :rest ',(cdr $2))
168 `(PACKAGE-TAG ',$2 nil)
177 associativity token_type_opt items
178 `(TAG ',$1 'assoc :type ',$2 :value ',$3)
191 PUT put_name put_value
192 `(TAG ',$2 'put :value ',(list $3))
193 | PUT put_name put_value_list
194 `(TAG ',$2 'put :value ',$3)
195 | PUT put_name_list put_value
196 `(TAG ',(car $2) 'put :rest ',(cdr $2) :value ',(list $3))
197 | PUT put_name_list put_value_list
198 `(TAG ',(car $2) 'put :rest ',(cdr $2) :value ',$3)
203 (mapcar 'semantic-tag-name (EXPANDFULL $1 put_names))
212 ;; Must return a list of Semantic tags to EXPANDFULL!
223 (mapcar 'semantic-tag-code-detail (EXPANDFULL $1 put_values))
232 ;; Must return a list of Semantic tags to EXPANDFULL!
233 (CODE-TAG "put-value" $1)
243 `(TAG ',$2 'scopestart)
248 `(TAG ',$2 'quotemode)
253 `(TAG ',(car $2) 'start :rest ',(cdr $2))
257 KEYWORD SYMBOL string_value
258 `(TAG ',$2 'keyword :value ',$3)
262 TOKEN token_type_opt SYMBOL string_value
263 `(TAG ',$3 ',(if $2 'token 'keyword) :type ',$2 :value ',$4)
264 | TOKEN token_type_opt symbols
265 `(TAG ',(car $3) 'token :type ',$2 :rest ',(cdr $3))
279 TYPE token_type plist_opt
280 `(TAG ',$2 'type :value ',$3)
290 (append (list $2) $1)
297 (mapcar 'semantic-tag-name (EXPANDFULL $1 use_names))
306 ;; Must return a list of Semantic tags to EXPANDFULL!
311 USE-MACROS SYMBOL use_name_list
312 `(TAG "macro" 'macro :type ',$2 :value ',$3)
320 ;; Return a Lisp readable form
345 (setq semantic-grammar-wy--nterm $1
346 semantic-grammar-wy--rindx 0)
348 (TAG $1 'nonterminal :children $4)
353 (apply 'nconc (nreverse $1))
365 (let* ((nterm semantic-grammar-wy--nterm)
366 (rindx semantic-grammar-wy--rindx)
368 comps prec action elt)
369 (setq semantic-grammar-wy--rindx (1+ semantic-grammar-wy--rindx))
377 (error "Duplicate %%prec in `%s:%d' rule" nterm rindx))
378 (setq prec (aref elt 0)))
381 ;; don't forget that rhs items are in reverse order, so
382 ;; the end-of-rule semantic action is the first item.
383 (if (or action comps)
385 (setq comps (cons elt comps)
386 ;; keep rule and action index synchronized
387 semantic-grammar-wy--rindx
388 (1+ semantic-grammar-wy--rindx))
389 ;; the end-of-rule action
390 (setq action (car elt))))
393 (setq comps (cons elt comps)))))
395 (TAG (format "%s:%d" nterm rindx) 'rule
396 :type (if comps "group" "empty")
397 :value comps :prec prec :expr action)))
407 (cons (vector $3) $1)
414 (format "(progn\n%s)"
416 (if (string-match "^{[\r\n\t ]*" s)
417 (setq s (substring s (match-end 0))))
418 (if (string-match "[\r\n\t ]*}$" s)
419 (setq s (substring s 0 (match-beginning 0))))
442 ;;; grammar.wy ends here