1 ;;; semantic-grammar.wy -- LALR grammar of Semantic input grammars
3 ;; Copyright (C) 2002-2012 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/>.
27 (defvar semantic-grammar-lex-c-char-re)
29 ;; Current parsed nonterminal name.
30 (defvar semantic-grammar-wy--nterm nil)
31 ;; Index of rule in a nonterminal clause.
32 (defvar semantic-grammar-wy--rindx nil)
35 %package semantic-grammar-wy
42 %start prologue epilogue declaration nonterminal rule
44 %start put_names put_values use_names
48 %keyword DEFAULT-PREC "%default-prec"
49 %keyword NO-DEFAULT-PREC "%no-default-prec"
50 %keyword KEYWORD "%keyword"
51 %keyword LANGUAGEMODE "%languagemode"
53 %keyword NONASSOC "%nonassoc"
54 %keyword PACKAGE "%package"
57 %keyword QUOTEMODE "%quotemode"
58 %keyword RIGHT "%right"
59 %keyword SCOPESTART "%scopestart"
60 %keyword START "%start"
61 %keyword TOKEN "%token"
63 %keyword USE-MACROS "%use-macros"
67 %token <string> STRING
69 %type <symbol> syntax ":?\\(\\sw\\|\\s_\\)+"
70 %token <symbol> SYMBOL
71 %token <symbol> PERCENT_PERCENT "\\`%%\\'"
73 %type <char> syntax semantic-grammar-lex-c-char-re
74 %token <char> CHARACTER
76 %type <qlist> matchdatatype sexp syntax "\\s'\\s-*("
77 %token <qlist> PREFIXED_LIST
79 %type <sexp> matchdatatype sexp syntax "\\="
82 ;; Don't generate these analyzers which needs special handling code.
83 %token <code> PROLOGUE "%{...%}"
84 %token <code> EPILOGUE "%%...EOF"
86 ;; Blocks & Parenthesis
88 %token <block> PAREN_BLOCK "(LPAREN RPAREN)"
89 %token <block> BRACE_BLOCK "(LBRACE RBRACE)"
90 %token <open-paren> LPAREN "("
91 %token <close-paren> RPAREN ")"
92 %token <open-paren> LBRACE "{"
93 %token <close-paren> RBRACE "}"
97 %token <punctuation> COLON ":"
98 %token <punctuation> SEMI ";"
99 %token <punctuation> OR "|"
100 %token <punctuation> LT "<"
101 %token <punctuation> GT ">"
113 ;;; Prologue/Epilogue
117 (CODE-TAG "prologue" nil)
122 (CODE-TAG "epilogue" nil)
134 | no_default_prec_decl
150 `(TAG "default-prec" 'assoc :value '("t"))
153 no_default_prec_decl:
155 `(TAG "default-prec" 'assoc :value '("nil"))
160 `(TAG ',(car $2) 'languagemode :rest ',(cdr $2))
165 `(PACKAGE-TAG ',$2 nil)
169 associativity token_type_opt items
170 `(TAG ',$1 'assoc :type ',$2 :value ',$3)
183 PUT put_name put_value
184 `(TAG ',$2 'put :value ',(list $3))
185 | PUT put_name put_value_list
186 `(TAG ',$2 'put :value ',$3)
187 | PUT put_name_list put_value
188 `(TAG ',(car $2) 'put :rest ',(cdr $2) :value ',(list $3))
189 | PUT put_name_list put_value_list
190 `(TAG ',(car $2) 'put :rest ',(cdr $2) :value ',$3)
195 (mapcar 'semantic-tag-name (EXPANDFULL $1 put_names))
204 ;; Must return a list of Semantic tags to EXPANDFULL!
215 (mapcar 'semantic-tag-code-detail (EXPANDFULL $1 put_values))
224 ;; Must return a list of Semantic tags to EXPANDFULL!
225 (CODE-TAG "put-value" $1)
235 `(TAG ',$2 'scopestart)
240 `(TAG ',$2 'quotemode)
245 `(TAG ',(car $2) 'start :rest ',(cdr $2))
249 KEYWORD SYMBOL string_value
250 `(TAG ',$2 'keyword :value ',$3)
254 TOKEN token_type_opt SYMBOL string_value
255 `(TAG ',$3 ',(if $2 'token 'keyword) :type ',$2 :value ',$4)
256 | TOKEN token_type_opt symbols
257 `(TAG ',(car $3) 'token :type ',$2 :rest ',(cdr $3))
271 TYPE token_type plist_opt
272 `(TAG ',$2 'type :value ',$3)
282 (append (list $2) $1)
289 (mapcar 'semantic-tag-name (EXPANDFULL $1 use_names))
298 ;; Must return a list of Semantic tags to EXPANDFULL!
303 USE-MACROS SYMBOL use_name_list
304 `(TAG "macro" 'macro :type ',$2 :value ',$3)
312 ;; Return a Lisp readable form
337 (setq semantic-grammar-wy--nterm $1
338 semantic-grammar-wy--rindx 0)
340 (TAG $1 'nonterminal :children $4)
345 (apply 'nconc (nreverse $1))
357 (let* ((nterm semantic-grammar-wy--nterm)
358 (rindx semantic-grammar-wy--rindx)
360 comps prec action elt)
361 (setq semantic-grammar-wy--rindx (1+ semantic-grammar-wy--rindx))
369 (error "Duplicate %%prec in `%s:%d' rule" nterm rindx))
370 (setq prec (aref elt 0)))
373 ;; don't forget that rhs items are in reverse order, so
374 ;; the end-of-rule semantic action is the first item.
375 (if (or action comps)
377 (setq comps (cons elt comps)
378 ;; keep rule and action index synchronized
379 semantic-grammar-wy--rindx
380 (1+ semantic-grammar-wy--rindx))
381 ;; the end-of-rule action
382 (setq action (car elt))))
385 (setq comps (cons elt comps)))))
387 (TAG (format "%s:%d" nterm rindx) 'rule
388 :type (if comps "group" "empty")
389 :value comps :prec prec :expr action)))
399 (cons (vector $3) $1)
406 (format "(progn\n%s)"
408 (if (string-match "^{[\r\n\t ]*" s)
409 (setq s (substring s (match-end 0))))
410 (if (string-match "[\r\n\t ]*}$" s)
411 (setq s (substring s 0 (match-beginning 0))))
434 ;;; grammar.wy ends here