1 ;; wisi-compile.el --- Grammar compiler for the wisi parser, integrating Wisi OpenToken output. -*- lexical-binding:t -*-
3 ;; Copyright (C) 2012, 2013, 2015, 2016 Free Software Foundation, Inc.
5 ;; Author: Stephen Leake <stephen_leake@member.fsf.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;;; History: first experimental version Jan 2013
30 ;; Semantic (info "(semantic)Top") provides an LALR(1) parser
31 ;; wisent-parse. The grammar used is defined by the functions
32 ;; semantic-grammar-create-package, which reads a bison-like source
33 ;; file and produces corresponding elisp source, and
34 ;; wisent-compile-grammar, which generates a parser table.
36 ;; However, the algorithm used in wisent-compile-grammar cannot cope
37 ;; with the grammar for the Ada language, because it is not
38 ;; LALR(1). So we provide a generalized LALR parser, which spawns
39 ;; parallel LALR parsers at each conflict. Instead of also rewriting
40 ;; the entire semantic grammar compiler, we use the OpenToken LALR
41 ;; parser generator, which is easier to modify (it is written in Ada,
44 ;; The Ada function Wisi.Generate reads the bison-like input and
45 ;; produces corresponding elisp source code, similar to that
46 ;; produced by semantic-grammar-create-package.
48 ;; wisi-compile-grammar (provided here) generates the automaton
49 ;; structure required by wisi-parse
53 (defun wisi-compose-action (value symbol-obarray nonterms)
54 (let* ((nonterm (car value))
56 (symbol (intern-soft (format "%s:%d" nonterm index) symbol-obarray))
57 (rhs (car (nth index (cdr (assoc nonterm nonterms))))))
58 (list nonterm symbol (length rhs))
61 (defun wisi-replace-actions (action symbol-obarray nonterms)
62 "Replace semantic action symbol names in ACTION with list as defined in `wisi-compile-grammar'.
63 ACTION is the alist for one state from the grammar, with the form:
64 ((default . error) ITEM ... )
66 reduction (TOKEN . (NONTERM . INDEX)) where NONTERM . INDEX gives the action symbol name.
68 shift/reduce conflict (STATE (NONTERM . INDEX))
69 reduce/shift conflict ((NONTERM . INDEX) (NONTERM . INDEX))
71 SYMBOL-OBARRAY contains the action symbols.
72 NONTERMS is from the grammar.
73 Return the new action alist."
74 ;; result is list of (nonterm index action-symbol token-count)
77 (setq item (pop action))
80 (memq (cdr item) '(error accept))
81 (numberp (cdr item))) ;; shift
85 (let ((value (cdr item)))
87 ((symbolp (car value))
89 (push (cons (car item)
90 (wisi-compose-action value symbol-obarray nonterms))
93 ((integerp (car value))
94 ;; shift/reduce conflict
95 (push (cons (car item)
97 (wisi-compose-action (cadr value) symbol-obarray nonterms)))
100 (t ;; reduce/reduce conflict
101 (push (cons (car item)
102 (list (wisi-compose-action (car value) symbol-obarray nonterms)
103 (wisi-compose-action (cadr value) symbol-obarray nonterms)))
108 (error "unexpected '%s'; expected 'error, 'accept, numberp, stringp, listp" (cdr item)))
113 (defun wisi-semantic-action (form nonterm iactn symbol-obarray)
114 "Define an Elisp semantic action function for a production, interned in SYMBOL-OBARRAY.
115 FORM is the body of the semantic action.
116 NONTERM is the nonterminal left hand side.
117 IACTN is the index of the production in the NTERM rule.
119 The semantic action function accepts two arguments;
120 - $nterm : the nonterminal
121 - wisi-tokens : the list of tokens to be reduced.
123 It returns nil; it is called for the semantic side-effects only."
124 ;; based on comp.el wisent-semantic-action
125 (let* ((name (format "%s:%d" nonterm iactn))
126 (action-symbol (intern name symbol-obarray)))
129 `(lambda ($nterm wisi-tokens)
133 (defun wisi-compile-grammar (grammar)
134 "Compile the LALR(1) GRAMMAR; return the automaton for wisi-parse.
135 GRAMMAR is a list TERMINALS NONTERMS ACTIONS GOTOS, where:
137 TERMINALS is a list of terminal token symbols.
139 NONTERMS is a list of productions; each production is a
140 list (nonterm (tokens semantic-action) ...) where `semantic-action' is
141 any lisp form. The set of (tokens semantic-action) are the right hand
142 sides; nonterm is the left hand side.
144 ACTIONS is an array indexed by parser state, of alists indexed by
145 terminal tokens. The value of each item in the alists is one of:
151 integer - shift; gives new state
153 '(nonterm . index) - reduce by nonterm production index.
155 '(integer (nonterm . index)) - a shift/reduce conflict
156 '((nonterm . index) (nonterm . index)) - a reduce/reduce conflict
158 The first item in the alist must have the key 'default (not a
159 terminal token); it is used when no other item matches the
162 GOTOS is an array indexed by parser state, of alists giving the
163 new state after a reduce for each nonterminal legal in that
166 The automaton is an array [parser-actions gotos symbol-obarray]:
168 - parser-actions is a copy of the input ACTIONS, with semantic
169 actions replaced by a list (nonterm action-symbol token-count),
172 -- nonterm is a symbol from NONTERMS, and is the non-terminal to
175 -- token-count is the number of tokens in the reduction,
177 -- action-symbol is nil if there is no semantic action, or a
178 symbol interned in symbol-obarray
180 - gotos is a copy of GOTOS.
182 - symbol-obarray is an obarray containing functions that
183 implement the semantic action for each nonterminal; the function
184 names have the format nonterm:index."
185 ;; We store named symbols for semantic actions, not just lambda
186 ;; functions, so we have a name for debug trace.
188 ;; FIXME: TERMINALS is not used. Eliminating it requires decoupling
189 ;; from OpenToken; we'll do that in the move to FastToken.
191 ;; FIXME: eliminate use of semantic-lex-* in *-wy.el. Similarly
192 ;; requires decoupling from OpenToken
194 ;; FIXME: can eliminate obarray? We don't need the obarray to
195 ;; avoid garbage collection of the symbols; they are all referenced in the compiled grammar.
196 ;; But each semantic action function has to be defined (and byte-compiled?) somewhere?
197 ;; currently actions are _not_ byte-compiled; wisi-compile-grammar is run at load time
198 ;; need 'eval-when-compile' to byte-compile them?
199 ;; can't byte-compile obarray?
201 (let ((defs (nth 1 grammar))
202 (symbol-obarray (make-vector 13 0));; for parse actions
203 def nonterm rhs-list rule
204 semantic-action index)
213 (setq rule (car rhs-list)
214 rhs-list (cdr rhs-list)
215 semantic-action (cadr rule))
217 (when semantic-action
218 (wisi-semantic-action semantic-action nonterm index symbol-obarray))
220 (setq index (1+ index))
223 ;; replace semantic actions in ACTIONS with symbols from symbol-obarray
224 (let ((nactions (length (nth 2 grammar)))
225 (actions (nth 2 grammar))
227 (while (< i nactions)
229 (wisi-replace-actions (aref actions i) symbol-obarray (nth 1 grammar)))
237 (provide 'wisi-compile)
238 ;;; wisi-compile.el ends here