* doc/misc/wisent.texi (Wisent Overview): Remove incorrect, unnecessary uref.
[bpt/emacs.git] / doc / misc / wisent.texi
CommitLineData
9e7abd17
EL
1\input texinfo @c -*-texinfo-*-
2@c %**start of header
bfe6ffb6 3@setfilename ../../info/wisent
9e7abd17
EL
4@set TITLE Wisent Parser Development
5@set AUTHOR Eric M. Ludlam, David Ponce, and Richard Y. Kim
6@settitle @value{TITLE}
7
8@c *************************************************************************
9@c @ Header
10@c *************************************************************************
11
12@c Merge all indexes into a single index for now.
13@c We can always separate them later into two or more as needed.
14@syncodeindex vr cp
15@syncodeindex fn cp
16@syncodeindex ky cp
17@syncodeindex pg cp
18@syncodeindex tp cp
19
20@c @footnotestyle separate
21@c @paragraphindent 2
22@c @@smallbook
23@c %**end of header
24
25@copying
f0ddbf7b 26Copyright @copyright{} 1988--1993, 1995, 1998--2004, 2007, 2012--2013
bfe6ffb6 27Free Software Foundation, Inc.
9e7abd17 28
bfe6ffb6
GM
29@c Since we are both GNU manuals, we do not need to ack each other here.
30@ignore
9e7abd17
EL
31Some texts are borrowed or adapted from the manual of Bison version
321.35. The text in section entitled ``Understanding the automaton'' is
33adapted from the section ``Understanding Your Parser'' in the manual
34of Bison version 1.49.
bfe6ffb6 35@end ignore
9e7abd17
EL
36
37@quotation
38Permission is granted to copy, distribute and/or modify this document
bfe6ffb6
GM
39under the terms of the GNU Free Documentation License, Version 1.3 or
40any later version published by the Free Software Foundation; with no
41Invariant Sections, with the Front-Cover texts being ``A GNU Manual,''
42and with the Back-Cover Texts as in (a) below. A copy of the license
43is included in the section entitled ``GNU Free Documentation License''.
44
45(a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
6bf430d1 46modify this GNU manual.''
9e7abd17
EL
47@end quotation
48@end copying
49
bfe6ffb6 50@dircategory Emacs misc features
9e7abd17 51@direntry
bfe6ffb6 52* Wisent: (wisent). Semantic Wisent parser development.
9e7abd17 53@end direntry
9e7abd17
EL
54
55@iftex
56@finalout
57@end iftex
58
59@c @setchapternewpage odd
60@c @setchapternewpage off
61
9e7abd17
EL
62@titlepage
63@sp 10
64@title @value{TITLE}
65@author by @value{AUTHOR}
9e7abd17
EL
66@page
67@vskip 0pt plus 1 fill
68@insertcopying
69@end titlepage
70@page
71
bfe6ffb6
GM
72@macro semantic{}
73@i{Semantic}
74@end macro
9e7abd17
EL
75
76@c *************************************************************************
77@c @ Document
78@c *************************************************************************
79@contents
80
81@node top
82@top @value{TITLE}
83
84Wisent (the European Bison ;-) is an Emacs Lisp implementation of the
85GNU Compiler Compiler Bison.
86
87This manual describes how to use Wisent to develop grammars for
88programming languages, and how to use grammars to parse language
89source in Emacs buffers.
90
91It also describes how Wisent is used with the @semantic{} tool set
92described in the @ref{Top, Semantic Manual, Semantic Manual, semantic}.
93
bfe6ffb6
GM
94@ifnottex
95@insertcopying
96@end ifnottex
97
9e7abd17 98@menu
bfe6ffb6
GM
99* Wisent Overview::
100* Wisent Grammar::
101* Wisent Parsing::
102* Wisent Semantic::
103* GNU Free Documentation License::
104* Index::
9e7abd17
EL
105@end menu
106
107@node Wisent Overview
108@chapter Wisent Overview
109
110@dfn{Wisent} (the European Bison) is an implementation in Emacs Lisp
111of the GNU Compiler Compiler Bison. Its code is a port of the C code
112of GNU Bison 1.28 & 1.31.
113
114For more details on the basic concepts for understanding Wisent, it is
2ecfdaa6 115worthwhile to read the @ref{Top, Bison Manual, , bison}.
9e7abd17
EL
116
117Wisent can generate compilers compatible with the @semantic{} tool set.
118See the @ref{Top, Semantic Manual, , semantic}.
119
120It benefits from these Bison features:
121
122@itemize @bullet
bfe6ffb6 123@item
9e7abd17
EL
124It uses a fast but not so space-efficient encoding for the parse
125tables, described in Corbett's PhD thesis from Berkeley:
126@quotation
127@cite{Static Semantics in Compiler Error Recovery}@*
128June 1985, Report No. UCB/CSD 85/251.
129@end quotation
130
bfe6ffb6 131@item
9e7abd17
EL
132For generating the lookahead sets, Wisent uses the well-known
133technique of F. DeRemer and A. Pennello they described in:
134@quotation
135@cite{Efficient Construction of LALR(1) Lookahead Sets}@*
136October 1982, ACM TOPLS Vol 4 No 4.
137@end quotation
138
bfe6ffb6 139@item
9e7abd17
EL
140Wisent resolves shift/reduce conflicts using operator precedence and
141associativity.
142
bfe6ffb6 143@item
9e7abd17
EL
144Parser error recovery is accomplished using rules which match the
145special token @code{error}.
146@end itemize
147
148Nevertheless there are some fundamental differences between Bison and
149Wisent.
150
151@itemize
152@item
153Wisent is intended to be used in Emacs. It reads and produces Emacs
154Lisp data structures. All the additional code used in grammars is
155Emacs Lisp code.
156
157@item
158Contrary to Bison, Wisent does not generate a parser which combines
159Emacs Lisp code and grammar constructs. They exist separately.
160Wisent reads the grammar from a Lisp data structure and then generates
161grammar constructs as tables. Afterward, the derived tables can be
162included and byte-compiled in separate Emacs Lisp files, and be used
163at a later time by the Wisent's parser engine.
164
165@item
166Wisent allows multiple start nonterminals and allows a call to the
167parsing function to be made for a particular start nonterminal. For
168example, this is particularly useful to parse a region of an Emacs
169buffer. @semantic{} heavily depends on the availability of this feature.
170@end itemize
171
172@node Wisent Grammar
173@chapter Wisent Grammar
174
175@cindex context-free grammar
176@cindex rule
177In order for Wisent to parse a language, it must be described by a
178@dfn{context-free grammar}. That is a grammar specified as rules that
179can be applied regardless of context. For more information, see
180@ref{Language and Grammar, , , bison}, in the Bison manual.
181
182@cindex terminal
183@cindex nonterminal
184The formal grammar is formulated using @dfn{terminal} and
185@dfn{nonterminal} items. Terminals can be Emacs Lisp symbols or
186characters, and nonterminals are symbols only.
187
188@cindex token
189Terminals (also known as @dfn{tokens}) represent the lexical
190elements of the language like numbers, strings, etc..
191
192For example @samp{PLUS} can represent the operator @samp{+}.
193
194Nonterminal symbols are described by rules:
195
196@example
197@group
198RESULT @equiv{} COMPONENTS@dots{}
199@end group
200@end example
201
202@samp{RESULT} is a nonterminal that this rule describes and
203@samp{COMPONENTS} are various terminals and nonterminals that are put
204together by this rule.
205
206For example, this rule:
207
208@example
209@group
210exp @equiv{} exp PLUS exp
211@end group
212@end example
213
214Says that two groupings of type @samp{exp}, with a @samp{PLUS} token
215in between, can be combined into a larger grouping of type @samp{exp}.
bfe6ffb6 216
9e7abd17 217@menu
bfe6ffb6
GM
218* Grammar format::
219* Example::
220* Compiling a grammar::
221* Conflicts::
9e7abd17
EL
222@end menu
223
224@node Grammar format, Example, Wisent Grammar, Wisent Grammar
225@comment node-name, next, previous, up
226@section Grammar format
227
228@cindex grammar format
229To be acceptable by Wisent a context-free grammar must respect a
230particular format. That is, must be represented as an Emacs Lisp list
231of the form:
232
233@code{(@var{terminals} @var{assocs} . @var{non-terminals})}
234
235@table @var
236@item terminals
237Is the list of terminal symbols used in the grammar.
238
239@cindex associativity
240@item assocs
241Specify the associativity of @var{terminals}. It is @code{nil} when
242there is no associativity defined, or an alist of
243@w{@code{(@var{assoc-type} . @var{assoc-value})}} elements.
244
245@var{assoc-type} must be one of the @code{default-prec},
246@code{nonassoc}, @code{left} or @code{right} symbols. When
247@var{assoc-type} is @code{default-prec}, @var{assoc-value} must be
248@code{nil} or @code{t} (the default). Otherwise it is a list of
249tokens which must have been previously declared in @var{terminals}.
250
251For details, see @ref{Contextual Precedence, , , bison}, in the
252Bison manual.
253
254@item non-terminals
255Is the list of nonterminal definitions. Each definition has the form:
256
257@code{(@var{nonterm} . @var{rules})}
258
259Where @var{nonterm} is the nonterminal symbol defined and
260@var{rules} the list of rules that describe this nonterminal. Each
261rule is a list:
262
263@code{(@var{components} [@var{precedence}] [@var{action}])}
264
265Where:
266
267@table @var
268@item components
269Is a list of various terminals and nonterminals that are put together
270by this rule.
271
272For example,
273
274@example
275@group
276(exp ((exp ?+ exp)) ;; exp: exp '+' exp
277 ) ;; ;
278@end group
279@end example
280
281Says that two groupings of type @samp{exp}, with a @samp{+} token in
282between, can be combined into a larger grouping of type @samp{exp}.
283
284@cindex grammar coding conventions
285By convention, a nonterminal symbol should be in lower case, such as
286@samp{exp}, @samp{stmt} or @samp{declaration}. Terminal symbols
287should be upper case to distinguish them from nonterminals: for
288example, @samp{INTEGER}, @samp{IDENTIFIER}, @samp{IF} or
289@samp{RETURN}. A terminal symbol that represents a particular keyword
290in the language is conventionally the same as that keyword converted
291to upper case. The terminal symbol @code{error} is reserved for error
292recovery.
293
294@cindex middle-rule actions
295Scattered among the components can be @dfn{middle-rule} actions.
296Usually only @var{action} is provided (@pxref{action}).
297
298If @var{components} in a rule is @code{nil}, it means that the rule
299can match the empty string. For example, here is how to define a
300comma-separated sequence of zero or more @samp{exp} groupings:
301
2ecfdaa6 302@smallexample
9e7abd17
EL
303@group
304(expseq (nil) ;; expseq: ;; empty
305 ((expseq1)) ;; | expseq1
306 ) ;; ;
307
308(expseq1 ((exp)) ;; expseq1: exp
309 ((expseq1 ?, exp)) ;; | expseq1 ',' exp
310 ) ;; ;
311@end group
2ecfdaa6 312@end smallexample
9e7abd17
EL
313
314@cindex precedence level
315@item precedence
316Assign the rule the precedence of the given terminal item, overriding
317the precedence that would be deduced for it, that is the one of the
318last terminal in it. Notice that only terminals declared in
319@var{assocs} have a precedence level. The altered rule precedence
320then affects how conflicts involving that rule are resolved.
321
322@var{precedence} is an optional vector of one terminal item.
323
324Here is how @var{precedence} solves the problem of unary minus.
325First, declare a precedence for a fictitious terminal symbol named
326@code{UMINUS}. There are no tokens of this type, but the symbol
327serves to stand for its precedence:
328
329@example
330@dots{}
331((default-prec t) ;; This is the default
332 (left '+' '-')
333 (left '*')
334 (left UMINUS))
335@end example
336
337Now the precedence of @code{UMINUS} can be used in specific rules:
338
2ecfdaa6 339@smallexample
9e7abd17
EL
340@group
341(exp @dots{} ;; exp: @dots{}
342 ((exp ?- exp)) ;; | exp '-' exp
343 @dots{} ;; @dots{}
344 ((?- exp) [UMINUS]) ;; | '-' exp %prec UMINUS
345 @dots{} ;; @dots{}
346 ) ;; ;
347@end group
2ecfdaa6 348@end smallexample
9e7abd17
EL
349
350If you forget to append @code{[UMINUS]} to the rule for unary minus,
351Wisent silently assumes that minus has its usual precedence. This
352kind of problem can be tricky to debug, since one typically discovers
353the mistake only by testing the code.
354
355Using @code{(default-prec nil)} declaration makes it easier to
356discover this kind of problem systematically. It causes rules that
357lack a @var{precedence} modifier to have no precedence, even if the
358last terminal symbol mentioned in their components has a declared
359precedence.
360
361If @code{(default-prec nil)} is in effect, you must specify
362@var{precedence} for all rules that participate in precedence conflict
363resolution. Then you will see any shift/reduce conflict until you
364tell Wisent how to resolve it, either by changing your grammar or by
365adding an explicit precedence. This will probably add declarations to
366the grammar, but it helps to protect against incorrect rule
367precedences.
368
369The effect of @code{(default-prec nil)} can be reversed by giving
370@code{(default-prec t)}, which is the default.
371
372For more details, see @ref{Contextual Precedence, , , bison}, in the
373Bison manual.
374
375It is important to understand that @var{assocs} declarations defines
376associativity but also assign a precedence level to terminals. All
377terminals declared in the same @code{left}, @code{right} or
378@code{nonassoc} association get the same precedence level. The
379precedence level is increased at each new association.
380
381On the other hand, @var{precedence} explicitly assign the precedence
382level of the given terminal to a rule.
383
384@cindex semantic actions
385@item @anchor{action}action
386An action is an optional Emacs Lisp function call, like this:
387
388@code{(identity $1)}
389
390The result of an action determines the semantic value of a rule.
391
392From an implementation standpoint, the function call will be embedded
393in a lambda expression, and several useful local variables will be
394defined:
395
396@table @code
397@vindex $N
398@item $@var{n}
399Where @var{n} is a positive integer. Like in Bison, the value of
400@code{$@var{n}} is the semantic value of the @var{n}th element of
401@var{components}, starting from 1. It can be of any Lisp data
402type.
403
404@vindex $region@var{n}
405@item $regionN
406Where @var{n} is a positive integer. For each @code{$@var{n}}
407variable defined there is a corresponding @code{$region@var{n}}
408variable. Its value is a pair @code{(@var{start-pos} .
409@var{end-pos})} that represent the start and end positions (in the
410lexical input stream) of the @code{$@var{n}} value. It can be
411@code{nil} when the component positions are not available, like for an
412empty string component for example.
413
414@vindex $region
415@item $region
416Its value is the leftmost and rightmost positions of input data
417matched by all @var{components} in the rule. This is a pair
418@code{(@var{leftmost-pos} . @var{rightmost-pos})}. It can be
419@code{nil} when components positions are not available.
420
421@vindex $nterm
422@item $nterm
423This variable is initialized with the nonterminal symbol
424(@var{nonterm}) the rule belongs to. It could be useful to improve
425error reporting or debugging. It is also used to automatically
426provide incremental re-parse entry points for @semantic{} tags
427(@pxref{Wisent Semantic}).
428
429@vindex $action
430@item $action
431The value of @code{$action} is the symbolic name of the current
432semantic action (@pxref{Debugging actions}).
433@end table
434
435When an action is not specified a default value is supplied, it is
436@code{(identity $1)}. This means that the default semantic value of a
437rule is the value of its first component. Excepted for a rule
438matching the empty string, for which the default action is to return
439@code{nil}.
440@end table
441@end table
442
443@node Example, Compiling a grammar, Grammar format, Wisent Grammar
444@comment node-name, next, previous, up
445@section Example
446
447@cindex grammar example
448Here is an example to parse simple infix arithmetic expressions. See
449@ref{Infix Calc, , , bison}, in the Bison manual for details.
450
451@lisp
452@group
453'(
454 ;; Terminals
455 (NUM)
bfe6ffb6 456
9e7abd17
EL
457 ;; Terminal associativity & precedence
458 ((nonassoc ?=)
459 (left ?- ?+)
460 (left ?* ?/)
461 (left NEG)
462 (right ?^))
bfe6ffb6 463
9e7abd17
EL
464 ;; Rules
465 (input
466 ((line))
467 ((input line)
468 (format "%s %s" $1 $2))
469 )
470
471 (line
472 ((?;)
473 (progn ";"))
474 ((exp ?;)
475 (format "%s;" $1))
476 ((error ?;)
477 (progn "Error;")))
478 )
479
480 (exp
481 ((NUM)
482 (string-to-number $1))
483 ((exp ?= exp)
484 (= $1 $3))
485 ((exp ?+ exp)
486 (+ $1 $3))
487 ((exp ?- exp)
488 (- $1 $3))
489 ((exp ?* exp)
490 (* $1 $3))
491 ((exp ?/ exp)
492 (/ $1 $3))
493 ((?- exp) [NEG]
494 (- $2))
495 ((exp ?^ exp)
496 (expt $1 $3))
497 ((?\( exp ?\))
498 (progn $2))
499 )
500 )
501@end group
502@end lisp
503
504In the bison-like @dfn{WY} format (@pxref{Wisent Semantic}) the
505grammar looks like this:
506
507@example
508@group
509%token <number> NUM
510
511%nonassoc '=' ;; comparison
512%left '-' '+'
513%left '*' '/'
514%left NEG ;; negation--unary minus
515%right '^' ;; exponentiation
516
517%%
518
519input:
520 line
521 | input line
522 (format "%s %s" $1 $2)
523 ;
524
525line:
526 ';'
527 @{";"@}
528 | exp ';'
529 (format "%s;" $1)
530 | error ';'
531 @{"Error;"@}
532 ;
533
534exp:
535 NUM
536 (string-to-number $1)
537 | exp '=' exp
538 (= $1 $3)
539 | exp '+' exp
540 (+ $1 $3)
541 | exp '-' exp
542 (- $1 $3)
543 | exp '*' exp
544 (* $1 $3)
545 | exp '/' exp
546 (/ $1 $3)
547 | '-' exp %prec NEG
548 (- $2)
549 | exp '^' exp
550 (expt $1 $3)
551 | '(' exp ')'
552 @{$2@}
553 ;
554
555%%
556@end group
557@end example
558
559@node Compiling a grammar, Conflicts, Example, Wisent Grammar
560@comment node-name, next, previous, up
561@section Compiling a grammar
562
563@cindex automaton
564After providing a context-free grammar in a suitable format, it must
565be translated into a set of tables (an @dfn{automaton}) that will be
566used to derive the parser. Like Bison, Wisent translates grammars that
567must be @dfn{LALR(1)}.
568
569@cindex LALR(1) grammar
570@cindex look-ahead token
571A grammar is @acronym{LALR(1)} if it is possible to tell how to parse
572any portion of an input string with just a single token of look-ahead:
573the @dfn{look-ahead token}. See @ref{Language and Grammar, , ,
574bison}, in the Bison manual for more information.
575
576@cindex grammar compilation
577Grammar translation (compilation) is achieved by the function:
578
579@cindex compiling a grammar
580@vindex wisent-single-start-flag
581@findex wisent-compile-grammar
582@defun wisent-compile-grammar grammar &optional start-list
583Compile @var{grammar} and return an @acronym{LALR(1)} automaton.
584
585Optional argument @var{start-list} is a list of start symbols
586(nonterminals). If @code{nil} the first nonterminal defined in the
587grammar is the default start symbol. If @var{start-list} contains
588only one element, it defines the start symbol. If @var{start-list}
589contains more than one element, all are defined as potential start
590symbols, unless @code{wisent-single-start-flag} is non-@code{nil}. In
591that case the first element of @var{start-list} defines the start
592symbol and others are ignored.
593
594The @acronym{LALR(1)} automaton is a vector of the form:
595
596@code{[@var{actions gotos starts functions}]}
597
598@table @var
599@item actions
600A state/token matrix telling the parser what to do at every state
601based on the current look-ahead token. That is shift, reduce, accept
602or error. See also @ref{Wisent Parsing}.
603
604@item gotos
605A state/nonterminal matrix telling the parser the next state to go to
606after reducing with each rule.
607
608@item starts
609An alist which maps the allowed start symbols (nonterminals) to
610lexical tokens that will be first shifted into the parser stack.
611
612@item functions
613An obarray of semantic action symbols. A semantic action is actually
614an Emacs Lisp function (lambda expression).
615@end table
616@end defun
617
618@node Conflicts, , Compiling a grammar, Wisent Grammar
619@comment node-name, next, previous, up
620@section Conflicts
621
622Normally, a grammar should produce an automaton where at each state
623the parser has only one action to do (@pxref{Wisent Parsing}).
624
625@cindex ambiguous grammar
626In certain cases, a grammar can produce an automaton where, at some
627states, there are more than one action possible. Such a grammar is
628@dfn{ambiguous}, and generates @dfn{conflicts}.
629
630@cindex deterministic automaton
631The parser can't be driven by an automaton which isn't completely
632@dfn{deterministic}, that is which contains conflicts. It is
633necessary to resolve the conflicts to eliminate them. Wisent resolves
634conflicts like Bison does.
635
636@cindex grammar conflicts
637@cindex conflicts resolution
638There are two sorts of conflicts:
639
640@table @dfn
641@cindex shift/reduce conflicts
642@item shift/reduce conflicts
643When either a shift or a reduction would be valid at the same state.
644
645Such conflicts are resolved by choosing to shift, unless otherwise
646directed by operator precedence declarations.
647See @ref{Shift/Reduce , , , bison}, in the Bison manual for more
648information.
649
650@cindex reduce/reduce conflicts
651@item reduce/reduce conflicts
652That occurs if there are two or more rules that apply to the same
653sequence of input. This usually indicates a serious error in the
654grammar.
655
656Such conflicts are resolved by choosing to use the rule that appears
657first in the grammar, but it is very risky to rely on this. Every
658reduce/reduce conflict must be studied and usually eliminated. See
659@ref{Reduce/Reduce , , , bison}, in the Bison manual for more
660information.
661@end table
662
663@menu
bfe6ffb6
GM
664* Grammar Debugging::
665* Understanding the automaton::
9e7abd17
EL
666@end menu
667
668@node Grammar Debugging
669@subsection Grammar debugging
670
671@cindex grammar debugging
672@cindex grammar verbose description
673To help writing a new grammar, @code{wisent-compile-grammar} can
674produce a verbose report containing a detailed description of the
675grammar and parser (equivalent to what Bison reports with the
676@option{--verbose} option).
677
678To enable the verbose report you can set to non-@code{nil} the
679variable:
680
681@vindex wisent-verbose-flag
682@deffn Option wisent-verbose-flag
683non-@code{nil} means to report verbose information on generated parser.
684@end deffn
685
686Or interactively use the command:
687
688@findex wisent-toggle-verbose-flag
689@deffn Command wisent-toggle-verbose-flag
690Toggle whether to report verbose information on generated parser.
691@end deffn
692
693The verbose report is printed in the temporary buffer
694@code{*wisent-log*} when running interactively, or in file
695@file{wisent.output} when running in batch mode. Different
696reports are separated from each other by a line like this:
697
698@example
699@group
700*** Wisent @var{source-file} - 2002-06-27 17:33
701@end group
702@end example
703
704where @var{source-file} is the name of the Emacs Lisp file from which
705the grammar was read. See @ref{Understanding the automaton}, for
706details on the verbose report.
707
708@table @strong
709@item Please Note
710To help debugging the grammar compiler itself, you can set this
711variable to print the content of some internal data structures:
712
713@vindex wisent-debug-flag
714@defvar wisent-debug-flag
715non-@code{nil} means enable some debug stuff.
716@end defvar
717@end table
718
719@node Understanding the automaton
720@subsection Understanding the automaton
721
722@cindex understanding the automaton
723This section (took from the manual of Bison 1.49) describes how to use
724the verbose report printed by @code{wisent-compile-grammar} to
725understand the generated automaton, to tune or fix a grammar.
726
727We will use the following example:
728
729@example
730@group
731(let ((wisent-verbose-flag t)) ;; Print a verbose report!
732 (wisent-compile-grammar
733 '((NUM STR) ; %token NUM STR
734
bfe6ffb6 735 ((left ?+ ?-) ; %left '+' '-';
9e7abd17
EL
736 (left ?*)) ; %left '*'
737
738 (exp ; exp:
739 ((exp ?+ exp)) ; exp '+' exp
740 ((exp ?- exp)) ; | exp '-' exp
741 ((exp ?* exp)) ; | exp '*' exp
742 ((exp ?/ exp)) ; | exp '/' exp
743 ((NUM)) ; | NUM
744 ) ; ;
745
746 (useless ; useless:
747 ((STR)) ; STR
748 ) ; ;
749 )
750 'nil) ; no %start declarations
751 )
752@end group
753@end example
754
755When evaluating the above expression, grammar compilation first issues
756the following two clear messages:
757
758@example
759@group
760Grammar contains 1 useless nonterminals and 1 useless rules
761Grammar contains 7 shift/reduce conflicts
762@end group
763@end example
764
765The @samp{*wisent-log*} buffer details things!
766
767The first section reports conflicts that were solved using precedence
768and/or associativity:
769
770@example
771@group
772Conflict in state 7 between rule 1 and token '+' resolved as reduce.
773Conflict in state 7 between rule 1 and token '-' resolved as reduce.
774Conflict in state 7 between rule 1 and token '*' resolved as shift.
775Conflict in state 8 between rule 2 and token '+' resolved as reduce.
776Conflict in state 8 between rule 2 and token '-' resolved as reduce.
777Conflict in state 8 between rule 2 and token '*' resolved as shift.
778Conflict in state 9 between rule 3 and token '+' resolved as reduce.
779Conflict in state 9 between rule 3 and token '-' resolved as reduce.
780Conflict in state 9 between rule 3 and token '*' resolved as reduce.
781@end group
782@end example
783
784The next section reports useless tokens, nonterminal and rules (note
785that useless tokens might be used by the scanner):
786
787@example
788@group
789Useless nonterminals:
790
791 useless
792
793
794Terminals which are not used:
795
796 STR
797
798
799Useless rules:
800
801#6 useless: STR;
802@end group
803@end example
804
805The next section lists states that still have conflicts:
806
807@example
808@group
809State 7 contains 1 shift/reduce conflict.
810State 8 contains 1 shift/reduce conflict.
811State 9 contains 1 shift/reduce conflict.
812State 10 contains 4 shift/reduce conflicts.
813@end group
814@end example
815
816The next section reproduces the grammar used:
817
818@example
819@group
820Grammar
821
822 Number, Rule
823 1 exp -> exp '+' exp
824 2 exp -> exp '-' exp
825 3 exp -> exp '*' exp
826 4 exp -> exp '/' exp
827 5 exp -> NUM
828@end group
829@end example
830
831And reports the uses of the symbols:
832
833@example
834@group
835Terminals, with rules where they appear
836
837$EOI (-1)
838error (1)
839NUM (2) 5
840STR (3) 6
841'+' (4) 1
842'-' (5) 2
843'*' (6) 3
844'/' (7) 4
845
846
847Nonterminals, with rules where they appear
848
849exp (8)
850 on left: 1 2 3 4 5, on right: 1 2 3 4
851@end group
852@end example
853
854The report then details the automaton itself, describing each state
855with it set of @dfn{items}, also known as @dfn{pointed rules}. Each
856item is a production rule together with a point (marked by @samp{.})
857that the input cursor.
858
859@example
860@group
861state 0
862
863 NUM shift, and go to state 1
864
865 exp go to state 2
866@end group
867@end example
868
869State 0 corresponds to being at the very beginning of the parsing, in
870the initial rule, right before the start symbol (@samp{exp}). When
871the parser returns to this state right after having reduced a rule
872that produced an @samp{exp}, it jumps to state 2. If there is no such
873transition on a nonterminal symbol, and the lookahead is a @samp{NUM},
874then this token is shifted on the parse stack, and the control flow
875jumps to state 1. Any other lookahead triggers a parse error.
876
877In the state 1...
878
879@example
880@group
881state 1
882
883 exp -> NUM . (rule 5)
884
885 $default reduce using rule 5 (exp)
886@end group
887@end example
888
889the rule 5, @samp{exp: NUM;}, is completed. Whatever the lookahead
890(@samp{$default}), the parser will reduce it. If it was coming from
891state 0, then, after this reduction it will return to state 0, and
892will jump to state 2 (@samp{exp: go to state 2}).
893
894@example
895@group
896state 2
897
898 exp -> exp . '+' exp (rule 1)
899 exp -> exp . '-' exp (rule 2)
900 exp -> exp . '*' exp (rule 3)
901 exp -> exp . '/' exp (rule 4)
902
903 $EOI shift, and go to state 11
904 '+' shift, and go to state 3
905 '-' shift, and go to state 4
906 '*' shift, and go to state 5
907 '/' shift, and go to state 6
908@end group
909@end example
910
911In state 2, the automaton can only shift a symbol. For instance,
912because of the item @samp{exp -> exp . '+' exp}, if the lookahead if
913@samp{+}, it will be shifted on the parse stack, and the automaton
914control will jump to state 3, corresponding to the item
915@samp{exp -> exp . '+' exp}:
916
917@example
918@group
919state 3
920
921 exp -> exp '+' . exp (rule 1)
922
923 NUM shift, and go to state 1
924
925 exp go to state 7
926@end group
927@end example
928
929Since there is no default action, any other token than those listed
930above will trigger a parse error.
931
932The interpretation of states 4 to 6 is straightforward:
933
934@example
935@group
936state 4
937
938 exp -> exp '-' . exp (rule 2)
939
940 NUM shift, and go to state 1
941
942 exp go to state 8
943
944
945
946state 5
947
948 exp -> exp '*' . exp (rule 3)
949
950 NUM shift, and go to state 1
951
952 exp go to state 9
953
954
955
956state 6
957
958 exp -> exp '/' . exp (rule 4)
959
960 NUM shift, and go to state 1
961
962 exp go to state 10
963@end group
964@end example
965
966As was announced in beginning of the report, @samp{State 7 contains 1
967shift/reduce conflict.}:
968
969@example
970@group
971state 7
972
973 exp -> exp . '+' exp (rule 1)
974 exp -> exp '+' exp . (rule 1)
975 exp -> exp . '-' exp (rule 2)
976 exp -> exp . '*' exp (rule 3)
977 exp -> exp . '/' exp (rule 4)
978
979 '*' shift, and go to state 5
980 '/' shift, and go to state 6
981
982 '/' [reduce using rule 1 (exp)]
983 $default reduce using rule 1 (exp)
984@end group
985@end example
986
987Indeed, there are two actions associated to the lookahead @samp{/}:
988either shifting (and going to state 6), or reducing rule 1. The
989conflict means that either the grammar is ambiguous, or the parser
990lacks information to make the right decision. Indeed the grammar is
991ambiguous, as, since we did not specify the precedence of @samp{/},
992the sentence @samp{NUM + NUM / NUM} can be parsed as @samp{NUM + (NUM
993/ NUM)}, which corresponds to shifting @samp{/}, or as @samp{(NUM +
994NUM) / NUM}, which corresponds to reducing rule 1.
995
996Because in @acronym{LALR(1)} parsing a single decision can be made,
997Wisent arbitrarily chose to disable the reduction, see
998@ref{Conflicts}. Discarded actions are reported in between square
999brackets.
1000
1001Note that all the previous states had a single possible action: either
1002shifting the next token and going to the corresponding state, or
1003reducing a single rule. In the other cases, i.e., when shifting
1004@emph{and} reducing is possible or when @emph{several} reductions are
1005possible, the lookahead is required to select the action. State 7 is
1006one such state: if the lookahead is @samp{*} or @samp{/} then the
1007action is shifting, otherwise the action is reducing rule 1. In other
1008words, the first two items, corresponding to rule 1, are not eligible
1009when the lookahead is @samp{*}, since we specified that @samp{*} has
1010higher precedence that @samp{+}. More generally, some items are
1011eligible only with some set of possible lookaheads.
1012
1013States 8 to 10 are similar:
1014
1015@example
1016@group
1017state 8
1018
1019 exp -> exp . '+' exp (rule 1)
1020 exp -> exp . '-' exp (rule 2)
1021 exp -> exp '-' exp . (rule 2)
1022 exp -> exp . '*' exp (rule 3)
1023 exp -> exp . '/' exp (rule 4)
1024
1025 '*' shift, and go to state 5
1026 '/' shift, and go to state 6
1027
1028 '/' [reduce using rule 2 (exp)]
1029 $default reduce using rule 2 (exp)
1030
1031
9e7abd17
EL
1032state 9
1033
1034 exp -> exp . '+' exp (rule 1)
1035 exp -> exp . '-' exp (rule 2)
1036 exp -> exp . '*' exp (rule 3)
1037 exp -> exp '*' exp . (rule 3)
1038 exp -> exp . '/' exp (rule 4)
1039
1040 '/' shift, and go to state 6
1041
1042 '/' [reduce using rule 3 (exp)]
1043 $default reduce using rule 3 (exp)
1044
1045
9e7abd17
EL
1046state 10
1047
1048 exp -> exp . '+' exp (rule 1)
1049 exp -> exp . '-' exp (rule 2)
1050 exp -> exp . '*' exp (rule 3)
1051 exp -> exp . '/' exp (rule 4)
1052 exp -> exp '/' exp . (rule 4)
1053
1054 '+' shift, and go to state 3
1055 '-' shift, and go to state 4
1056 '*' shift, and go to state 5
1057 '/' shift, and go to state 6
1058
1059 '+' [reduce using rule 4 (exp)]
1060 '-' [reduce using rule 4 (exp)]
1061 '*' [reduce using rule 4 (exp)]
1062 '/' [reduce using rule 4 (exp)]
1063 $default reduce using rule 4 (exp)
1064@end group
1065@end example
1066
1067Observe that state 10 contains conflicts due to the lack of precedence
1068of @samp{/} wrt @samp{+}, @samp{-}, and @samp{*}, but also because the
1069associativity of @samp{/} is not specified.
1070
1071Finally, the state 11 (plus 12) is named the @dfn{final state}, or the
1072@dfn{accepting state}:
1073
1074@example
1075@group
1076state 11
1077
1078 $EOI shift, and go to state 12
1079
1080
1081
1082state 12
1083
1084 $default accept
1085@end group
1086@end example
1087
1088The end of input is shifted @samp{$EOI shift,} and the parser exits
1089successfully (@samp{go to state 12}, that terminates).
1090
1091@node Wisent Parsing
1092@chapter Wisent Parsing
1093
1094@cindex bottom-up parser
1095@cindex shift-reduce parser
1096The Wisent's parser is what is called a @dfn{bottom-up} or
1097@dfn{shift-reduce} parser which repeatedly:
1098
1099@table @dfn
1100@cindex shift
1101@item shift
1102That is pushes the value of the last lexical token read (the
1103look-ahead token) into a value stack, and reads a new one.
1104
1105@cindex reduce
1106@item reduce
1107That is replaces a nonterminal by its semantic value. The values of
1108the components which form the right hand side of a rule are popped
1109from the value stack and reduced by the semantic action of this rule.
1110The result is pushed back on top of value stack.
1111@end table
1112
1113The parser will stop on:
1114
1115@table @dfn
1116@cindex accept
1117@item accept
1118When all input has been successfully parsed. The semantic value of
1119the start nonterminal is on top of the value stack.
1120
1121@cindex syntax error
1122@item error
1123When a syntax error (an unexpected token in input) has been detected.
1124At this point the parser issues an error message and either stops or
1125calls a recovery routine to try to resume parsing.
1126@end table
1127
1128@cindex table-driven parser
1129The above elementary actions are driven by the @acronym{LALR(1)}
1130automaton built by @code{wisent-compile-grammar} from a context-free
1131grammar.
1132
1133The Wisent's parser is entered by calling the function:
1134
1135@findex wisent-parse
1136@defun wisent-parse automaton lexer &optional error start
1137Parse input using the automaton specified in @var{automaton}.
1138
1139@table @var
1140@item automaton
1141Is an @acronym{LALR(1)} automaton generated by
1142@code{wisent-compile-grammar} (@pxref{Wisent Grammar}).
1143
1144@item lexer
1145Is a function with no argument called by the parser to obtain the next
1146terminal (token) in input (@pxref{Writing a lexer}).
1147
1148@item error
1149Is an optional reporting function called when a parse error occurs.
1150It receives a message string to report. It defaults to the function
1151@code{wisent-message} (@pxref{Report errors}).
1152
1153@item start
1154Specify the start symbol (nonterminal) used by the parser as its goal.
1155It defaults to the start symbol defined in the grammar
1156(@pxref{Wisent Grammar}).
1157@end table
1158@end defun
1159
1160The following two normal hooks permit to do some useful processing
1161respectively before to start parsing, and after the parser terminated.
1162
1163@vindex wisent-pre-parse-hook
1164@defvar wisent-pre-parse-hook
1165Normal hook run just before entering the @var{LR} parser engine.
1166@end defvar
1167
1168@vindex wisent-post-parse-hook
1169@defvar wisent-post-parse-hook
1170Normal hook run just after the @var{LR} parser engine terminated.
1171@end defvar
1172
1173@menu
bfe6ffb6
GM
1174* Writing a lexer::
1175* Actions goodies::
1176* Report errors::
1177* Error recovery::
1178* Debugging actions::
9e7abd17
EL
1179@end menu
1180
1181@node Writing a lexer
1182@section What the parser must receive
1183
1184It is important to understand that the parser does not parse
1185characters, but lexical tokens, and does not know anything about
1186characters in text streams!
1187
1188@cindex lexical analysis
1189@cindex lexer
1190@cindex scanner
1191Reading input data to produce lexical tokens is performed by a lexer
1192(also called a scanner) in a lexical analysis step, before the syntax
1193analysis step performed by the parser. The parser automatically calls
1194the lexer when it needs the next token to parse.
1195
1196@cindex lexical tokens
1197A Wisent's lexer is an Emacs Lisp function with no argument. It must
1198return a valid lexical token of the form:
1199
1200@code{(@var{token-class value} [@var{start} . @var{end}])}
1201
1202@table @var
1203@item token-class
1204Is a category of lexical token identifying a terminal as specified in
1205the grammar (@pxref{Wisent Grammar}). It can be a symbol or a character
1206literal.
1207
1208@item value
1209Is the value of the lexical token. It can be of any valid Emacs Lisp
1210data type.
1211
1212@item start
1213@itemx end
1214Are the optionals beginning and end positions of @var{value} in the
1215input stream.
1216@end table
1217
1218When there are no more tokens to read the lexer must return the token
1219@code{(list wisent-eoi-term)} to each request.
1220
1221@vindex wisent-eoi-term
1222@defvar wisent-eoi-term
1223Predefined constant, End-Of-Input terminal symbol.
1224@end defvar
1225
1226@code{wisent-lex} is an example of a lexer that reads lexical tokens
1227produced by a @semantic{} lexer, and translates them into lexical tokens
1228suitable to the Wisent parser. See also @ref{Wisent Lex}.
1229
1230To call the lexer in a semantic action use the function
1231@code{wisent-lexer}. See also @ref{Actions goodies}.
1232
1233@node Actions goodies
1234@section Variables and macros useful in grammar actions.
1235
1236@vindex wisent-input
1237@defvar wisent-input
1238The last token read.
1239This variable only has meaning in the scope of @code{wisent-parse}.
1240@end defvar
1241
1242@findex wisent-lexer
1243@defun wisent-lexer
1244Obtain the next terminal in input.
1245@end defun
1246
1247@findex wisent-region
1248@defun wisent-region &rest positions
1249Return the start/end positions of the region including
1250@var{positions}. Each element of @var{positions} is a pair
1251@w{@code{(@var{start-pos} . @var{end-pos})}} or @code{nil}. The
1252returned value is the pair @w{@code{(@var{min-start-pos} .
1253@var{max-end-pos})}} or @code{nil} if no @var{positions} are
1254available.
1255@end defun
1256
1257@node Report errors
1258@section The error reporting function
1259
1260@cindex error reporting
1261When the parser encounters a syntax error it calls a user-defined
1262function. It must be an Emacs Lisp function with one argument: a
1263string containing the message to report.
1264
1265By default the parser uses this function to report error messages:
1266
1267@findex wisent-message
1268@defun wisent-message string &rest args
1269Print a one-line message if @code{wisent-parse-verbose-flag} is set.
1270Pass @var{string} and @var{args} arguments to @dfn{message}.
1271@end defun
1272
1273@table @strong
1274@item Please Note:
1275@code{wisent-message} uses the following function to print lexical
1276tokens:
1277
1278@defun wisent-token-to-string token
1279Return a printed representation of lexical token @var{token}.
1280@end defun
1281
1282The general printed form of a lexical token is:
1283
1284@w{@code{@var{token}(@var{value})@@@var{location}}}
1285@end table
1286
1287To control the verbosity of the parser you can set to non-@code{nil}
1288this variable:
1289
1290@vindex wisent-parse-verbose-flag
1291@deffn Option wisent-parse-verbose-flag
1292non-@code{nil} means to issue more messages while parsing.
1293@end deffn
1294
1295Or interactively use the command:
1296
1297@findex wisent-parse-toggle-verbose-flag
1298@deffn Command wisent-parse-toggle-verbose-flag
1299Toggle whether to issue more messages while parsing.
1300@end deffn
1301
1302When the error reporting function is entered the variable
1303@code{wisent-input} contains the unexpected token as returned by the
1304lexer.
1305
1306The error reporting function can be called from a semantic action too
1307using the special macro @code{wisent-error}. When called from a
1308semantic action entered by error recovery (@pxref{Error recovery}) the
1309value of the variable @code{wisent-recovering} is non-@code{nil}.
1310
1311@node Error recovery
1312@section Error recovery
1313
1314@cindex error recovery
1315The error recovery mechanism of the Wisent's parser conforms to the
1316one Bison uses. See @ref{Error Recovery, , , bison}, in the Bison
1317manual for details.
1318
1319@cindex error token
1320To recover from a syntax error you must write rules to recognize the
1321special token @code{error}. This is a terminal symbol that is
1322automatically defined and reserved for error handling.
1323
1324When the parser encounters a syntax error, it pops the state stack
1325until it finds a state that allows shifting the @code{error} token.
1326After it has been shifted, if the old look-ahead token is not
1327acceptable to be shifted next, the parser reads tokens and discards
1328them until it finds a token which is acceptable.
1329
1330@cindex error recovery strategy
1331Strategies for error recovery depend on the choice of error rules in
1332the grammar. A simple and useful strategy is simply to skip the rest
1333of the current statement if an error is detected:
1334
1335@example
1336@group
1337(stmnt (( error ?; )) ;; on error, skip until ';' is read
1338 )
1339@end group
1340@end example
1341
1342It is also useful to recover to the matching close-delimiter of an
1343opening-delimiter that has already been parsed:
1344
1345@example
1346@group
1347(primary (( ?@{ expr ?@} ))
1348 (( ?@{ error ?@} ))
1349 @dots{}
1350 )
1351@end group
1352@end example
1353
1354@cindex error recovery actions
1355Note that error recovery rules may have actions, just as any other
1356rules can. Here are some predefined hooks, variables, functions or
1357macros, useful in such actions:
1358
1359@vindex wisent-nerrs
1360@defvar wisent-nerrs
1361The number of parse errors encountered so far.
1362@end defvar
1363
1364@vindex wisent-recovering
1365@defvar wisent-recovering
1366non-@code{nil} means that the parser is recovering.
1367This variable only has meaning in the scope of @code{wisent-parse}.
1368@end defvar
1369
1370@findex wisent-error
1371@defun wisent-error msg
1372Call the user supplied error reporting function with message
1373@var{msg} (@pxref{Report errors}).
1374
1375For an example of use, @xref{wisent-skip-token}.
1376@end defun
1377
1378@findex wisent-errok
1379@defun wisent-errok
1380Resume generating error messages immediately for subsequent syntax
1381errors.
1382
1383The parser suppress error message for syntax errors that happens
1384shortly after the first, until three consecutive input tokens have
1385been successfully shifted.
1386
1387Calling @code{wisent-errok} in an action, make error messages resume
1388immediately. No error messages will be suppressed if you call it in
1389an error rule's action.
1390
1391For an example of use, @xref{wisent-skip-token}.
1392@end defun
1393
1394@findex wisent-clearin
1395@defun wisent-clearin
1396Discard the current lookahead token.
1397This will cause a new lexical token to be read.
1398
1399In an error rule's action the previous lookahead token is reanalyzed
1400immediately. @code{wisent-clearin} may be called to clear this token.
1401
1402For example, suppose that on a parse error, an error handling routine
1403is called that advances the input stream to some point where parsing
1404should once again commence. The next symbol returned by the lexical
1405scanner is probably correct. The previous lookahead token ought to
1406be discarded with @code{wisent-clearin}.
1407
1408For an example of use, @xref{wisent-skip-token}.
1409@end defun
1410
1411@findex wisent-abort
1412@defun wisent-abort
1413Abort parsing and save the lookahead token.
1414@end defun
1415
1416@findex wisent-set-region
1417@defun wisent-set-region start end
1418Change the region of text matched by the current nonterminal.
1419@var{start} and @var{end} are respectively the beginning and end
1420positions of the region occupied by the group of components associated
1421to this nonterminal. If @var{start} or @var{end} values are not a
1422valid positions the region is set to @code{nil}.
1423
1424For an example of use, @xref{wisent-skip-token}.
1425@end defun
1426
1427@vindex wisent-discarding-token-functions
1428@defvar wisent-discarding-token-functions
1429List of functions to be called when discarding a lexical token.
1430These functions receive the lexical token discarded.
1431When the parser encounters unexpected tokens, it can discards them,
1432based on what directed by error recovery rules. Either when the
1433parser reads tokens until one is found that can be shifted, or when an
1434semantic action calls the function @code{wisent-skip-token} or
1435@code{wisent-skip-block}.
1436For language specific hooks, make sure you define this as a local
1437hook.
1438
1439For example, in @semantic{}, this hook is set to the function
1440@code{wisent-collect-unmatched-syntax} to collect unmatched lexical
1441tokens (@pxref{Useful functions}).
1442@end defvar
1443
1444@findex wisent-skip-token
1445@defun wisent-skip-token
1446@anchor{wisent-skip-token}
1447Skip the lookahead token in order to resume parsing.
1448Return nil.
1449Must be used in error recovery semantic actions.
1450
1451It typically looks like this:
1452
1453@lisp
1454@group
1455(wisent-message "%s: skip %s" $action
1456 (wisent-token-to-string wisent-input))
1457(run-hook-with-args
1458 'wisent-discarding-token-functions wisent-input)
1459(wisent-clearin)
1460(wisent-errok)))
1461@end group
1462@end lisp
1463@end defun
1464
1465@findex wisent-skip-block
1466@defun wisent-skip-block
1467Safely skip a block in order to resume parsing.
1468Return nil.
1469Must be used in error recovery semantic actions.
1470
1471A block is data between an open-delimiter (syntax class @code{(}) and
1472a matching close-delimiter (syntax class @code{)}):
1473
1474@example
1475@group
1476(a parenthesized block)
1477[a block between brackets]
1478@{a block between braces@}
1479@end group
1480@end example
1481
1482The following example uses @code{wisent-skip-block} to safely skip a
1483block delimited by @samp{LBRACE} (@code{@{}) and @samp{RBRACE}
1484(@code{@}}) tokens, when a syntax error occurs in
1485@samp{other-components}:
1486
1487@example
1488@group
1489(block ((LBRACE other-components RBRACE))
1490 ((LBRACE RBRACE))
1491 ((LBRACE error)
1492 (wisent-skip-block))
1493 )
1494@end group
1495@end example
1496@end defun
1497
1498@node Debugging actions
1499@section Debugging semantic actions
1500
1501@cindex semantic action symbols
1502Each semantic action is represented by a symbol interned in an
1503@dfn{obarray} that is part of the @acronym{LALR(1)} automaton
1504(@pxref{Compiling a grammar}). @code{symbol-function} on a semantic
1505action symbol return the semantic action lambda expression.
1506
1507A semantic action symbol name has the form
1508@code{@var{nonterminal}:@var{index}}, where @var{nonterminal} is the
1509name of the nonterminal symbol the action belongs to, and @var{index}
1510is an action sequence number within the scope of @var{nonterminal}.
1511For example, this nonterminal definition:
1512
1513@example
1514@group
1515input:
1516 line [@code{input:0}]
1517 | input line
1518 (format "%s %s" $1 $2) [@code{input:1}]
1519 ;
1520@end group
1521@end example
1522
1523Will produce two semantic actions, and associated symbols:
1524
1525@table @code
1526@item input:0
1527A default action that returns @code{$1}.
1528
1529@item input:1
1530That returns @code{(format "%s %s" $1 $2)}.
1531@end table
1532
1533@cindex debugging semantic actions
1534Debugging uses the Lisp debugger to investigate what is happening
1535during execution of semantic actions.
1536Three commands are available to debug semantic actions. They receive
1537two arguments:
1538
1539@itemize @bullet
1540@item The automaton that contains the semantic action.
1541
1542@item The semantic action symbol.
1543@end itemize
1544
1545@findex wisent-debug-on-entry
1546@deffn Command wisent-debug-on-entry automaton function
1547Request @var{automaton}'s @var{function} to invoke debugger each time it is called.
1548@var{function} must be a semantic action symbol that exists in @var{automaton}.
1549@end deffn
1550
1551@findex wisent-cancel-debug-on-entry
1552@deffn Command wisent-cancel-debug-on-entry automaton function
1553Undo effect of @code{wisent-debug-on-entry} on @var{automaton}'s @var{function}.
1554@var{function} must be a semantic action symbol that exists in @var{automaton}.
1555@end deffn
1556
1557@findex wisent-debug-show-entry
1558@deffn Command wisent-debug-show-entry automaton function
1559Show the source of @var{automaton}'s semantic action @var{function}.
1560@var{function} must be a semantic action symbol that exists in @var{automaton}.
1561@end deffn
1562
1563@node Wisent Semantic
1564@chapter How to use Wisent with Semantic
1565
1566@cindex tags
1567This section presents how the Wisent's parser can be used to produce
1568@dfn{tags} for the @semantic{} tool set.
1569
1570@semantic{} tags form a hierarchy of Emacs Lisp data structures that
1571describes a program in a way independent of programming languages.
1572Tags map program declarations, like functions, methods, variables,
1573data types, classes, includes, grammar rules, etc..
1574
1575@cindex WY grammar format
1576To use the Wisent parser with @semantic{} you have to define
1577your grammar in @dfn{WY} form, a grammar format very close
1578to the one used by Bison.
1579
1580Please @inforef{top, Semantic Grammar Framework Manual, grammar-fw}
1581for more information on @semantic{} grammars.
1582
1583@menu
bfe6ffb6
GM
1584* Grammar styles::
1585* Wisent Lex::
9e7abd17
EL
1586@end menu
1587
1588@node Grammar styles
1589@section Grammar styles
1590
1591@cindex grammar styles
1592@semantic{} parsing heavily depends on how you wrote the grammar.
1593There are mainly two styles to write a Wisent's grammar intended to be
1594used with the @semantic{} tool set: the @dfn{Iterative style} and the
1595@dfn{Bison style}. Each one has pros and cons, and in certain cases
1596it can be worth a mix of the two styles!
1597
1598@menu
bfe6ffb6
GM
1599* Iterative style::
1600* Bison style::
1601* Mixed style::
1602* Start nonterminals::
1603* Useful functions::
9e7abd17
EL
1604@end menu
1605
1606@node Iterative style, Bison style, Grammar styles, Grammar styles
1607@subsection Iterative style
1608
1609@cindex grammar iterative style
1610The @dfn{iterative style} is the preferred style to use with @semantic{}.
1611It relies on an iterative parser back-end mechanism which parses start
1612nonterminals one at a time and automagically skips unexpected lexical
1613tokens in input.
1614
1615Compared to rule-based iterative functions (@pxref{Bison style}),
1616iterative parsers are better in that they can handle obscure errors
1617more cleanly.
1618
1619@cindex raw tag
1620Each start nonterminal must produces a @dfn{raw tag} by calling a
1621@code{TAG}-like grammar macro with appropriate parameters. See also
1622@ref{Start nonterminals}.
1623
1624@cindex expanded tag
1625Then, each parsing iteration automatically translates a raw tag into
1626@dfn{expanded tags}, updating the raw tag structure with internal
1627properties and buffer related data.
1628
1629After parsing completes, it results in a tree of expanded tags.
1630
1631The following example is a snippet of the iterative style Java grammar
1632provided in the @semantic{} distribution in the file
1633@file{semantic/wisent/java-tags.wy}.
1634
1635@example
1636@group
1637@dots{}
1638;; Alternate entry points
1639;; - Needed by partial re-parse
1640%start formal_parameter
1641@dots{}
1642;; - Needed by EXPANDFULL clauses
1643%start formal_parameters
1644@dots{}
1645
1646formal_parameter_list
1647 : PAREN_BLOCK
1648 (EXPANDFULL $1 formal_parameters)
1649 ;
1650
1651formal_parameters
1652 : LPAREN
1653 ()
1654 | RPAREN
1655 ()
1656 | formal_parameter COMMA
1657 | formal_parameter RPAREN
1658 ;
1659
1660formal_parameter
1661 : formal_parameter_modifier_opt type variable_declarator_id
1662 (VARIABLE-TAG $3 $2 nil :typemodifiers $1)
1663 ;
1664@end group
1665@end example
1666
1667@findex EXPANDFULL
1668It shows the use of the @code{EXPANDFULL} grammar macro to parse a
1669@samp{PAREN_BLOCK} which contains a @samp{formal_parameter_list}.
1670@code{EXPANDFULL} tells to recursively parse @samp{formal_parameters}
1671inside @samp{PAREN_BLOCK}. The parser iterates until it digested all
1672available input data inside the @samp{PAREN_BLOCK}, trying to match
1673any of the @samp{formal_parameters} rules:
1674
1675@itemize
1676@item @samp{LPAREN}
1677
1678@item @samp{RPAREN}
1679
1680@item @samp{formal_parameter COMMA}
1681
1682@item @samp{formal_parameter RPAREN}
1683@end itemize
1684
1685At each iteration it will return a @samp{formal_parameter} raw tag,
1686or @code{nil} to skip unwanted (single @samp{LPAREN} or @samp{RPAREN}
1687for example) or unexpected input data. Those raw tags will be
1688automatically expanded by the iterative back-end parser.
1689
1690@node Bison style
1691@subsection Bison style
1692
1693@cindex grammar bison style
1694What we call the @dfn{Bison style} is the traditional style of Bison's
1695grammars. Compared to iterative style, it is not straightforward to
1696use grammars written in Bison style in @semantic{}. Mainly because such
1697grammars are designed to parse the whole input data in one pass, and
1698don't use the iterative parser back-end mechanism (@pxref{Iterative
1699style}). With Bison style the parser is called once to parse the
1700grammar start nonterminal.
1701
1702The following example is a snippet of the Bison style Java grammar
1703provided in the @semantic{} distribution in the file
1704@file{semantic/wisent/java.wy}.
1705
1706@example
1707@group
1708%start formal_parameter
1709@dots{}
1710
1711formal_parameter_list
1712 : formal_parameter_list COMMA formal_parameter
1713 (cons $3 $1)
1714 | formal_parameter
1715 (list $1)
1716 ;
1717
1718formal_parameter
1719 : formal_parameter_modifier_opt type variable_declarator_id
1720 (EXPANDTAG
1721 (VARIABLE-TAG $3 $2 :typemodifiers $1)
1722 )
1723 ;
1724@end group
1725@end example
1726
1727The first consequence is that syntax errors are not automatically
1728handled by @semantic{}. Thus, it is necessary to explicitly handle
1729them at the grammar level, providing error recovery rules to skip
1730unexpected input data.
1731
1732The second consequence is that the iterative parser can't do automatic
1733tag expansion, except for the start nonterminal value. It is
1734necessary to explicitly expand tags from concerned semantic actions by
1735calling the grammar macro @code{EXPANDTAG} with a raw tag as
1736parameter. See also @ref{Start nonterminals}, for incremental
1737re-parse considerations.
1738
1739@node Mixed style
1740@subsection Mixed style
1741
1742@cindex grammar mixed style
1743@example
1744@group
1745%start grammar
1746;; Reparse
1747%start prologue epilogue declaration nonterminal rule
1748@dots{}
1749
1750%%
1751
1752grammar:
1753 prologue
1754 | epilogue
1755 | declaration
1756 | nonterminal
1757 | PERCENT_PERCENT
1758 ;
1759@dots{}
1760
1761nonterminal:
1762 SYMBOL COLON rules SEMI
1763 (TAG $1 'nonterminal :children $3)
1764 ;
1765
1766rules:
1767 lifo_rules
1768 (apply 'nconc (nreverse $1))
1769 ;
1770
1771lifo_rules:
1772 lifo_rules OR rule
1773 (cons $3 $1)
1774 | rule
1775 (list $1)
1776 ;
1777
1778rule:
1779 rhs
1780 (let* ((rhs $1)
1781 name type comps prec action elt)
1782 @dots{}
1783 (EXPANDTAG
1784 (TAG name 'rule :type type :value comps :prec prec :expr action)
1785 ))
1786 ;
1787@end group
1788@end example
1789
1790This example shows how iterative and Bison styles can be combined in
1791the same grammar to obtain a good compromise between grammar
1792complexity and an efficient parsing strategy in an interactive
1793environment.
1794
1795@samp{nonterminal} is parsed using iterative style via the main
1796@samp{grammar} rule. The semantic action uses the @code{TAG} macro to
1797produce a raw tag, automagically expanded by @semantic{}.
1798
1799But @samp{rules} part is parsed in Bison style! Why?
1800
1801Rule delimiters are the colon (@code{:}), that follows the nonterminal
1802name, and a final semicolon (@code{;}). Unfortunately these
1803delimiters are not @code{open-paren}/@code{close-paren} type, and the
1804Emacs' syntactic analyzer can't easily isolate data between them to
1805produce a @samp{RULES_PART} parenthesis-block-like lexical token.
1806Consequently it is not possible to use @code{EXPANDFULL} to iterate in
1807@samp{RULES_PART}, like this:
1808
1809@example
1810@group
1811nonterminal:
1812 SYMBOL COLON rules SEMI
1813 (TAG $1 'nonterminal :children $3)
1814 ;
1815
1816rules:
1817 RULES_PART ;; @strong{Map a parenthesis-block-like lexical token}
1818 (EXPANDFULL $1 'rules)
1819 ;
1820
1821rules:
1822 COLON
1823 ()
1824 OR
1825 ()
1826 SEMI
1827 ()
1828 rhs
1829 rhs
1830 (let* ((rhs $1)
1831 name type comps prec action elt)
1832 @dots{}
1833 (TAG name 'rule :type type :value comps :prec prec :expr action)
1834 )
1835 ;
1836@end group
1837@end example
1838
1839In such cases, when it is difficult for Emacs to obtain
1840parenthesis-block-like lexical tokens, the best solution is to use the
1841traditional Bison style with error recovery!
1842
1843In some extreme cases, it can also be convenient to extend the lexer,
1844to deliver new lexical tokens, to simplify the grammar.
1845
1846@node Start nonterminals
1847@subsection Start nonterminals
1848
1849@cindex start nonterminals
1850@cindex @code{reparse-symbol} property
1851When you write a grammar for @semantic{}, it is important to carefully
1852indicate the start nonterminals. Each one defines an entry point in
1853the grammar, and after parsing its semantic value is returned to the
1854back-end iterative engine. Consequently:
1855
1856@strong{The semantic value of a start nonterminal must be a produced
1857by a TAG like grammar macro}.
1858
1859Start nonterminals are declared by @code{%start} statements. When
1860nothing is specified the first nonterminal that appears in the grammar
1861is the start nonterminal.
1862
1863Generally, the following nonterminals must be declared as start
1864symbols:
1865
1866@itemize @bullet
1867@item The main grammar entry point
1868@quotation
1869Of course!
1870@end quotation
1871
1872@item nonterminals passed to @code{EXPAND}/@code{EXPANDFULL}
1873@quotation
1874These grammar macros recursively parse a part of input data, based on
1875rules of the given nonterminal.
1876
1877For example, the following will parse @samp{PAREN_BLOCK} data using
1878the @samp{formal_parameters} rules:
1879
1880@example
1881@group
1882formal_parameter_list
1883 : PAREN_BLOCK
1884 (EXPANDFULL $1 formal_parameters)
1885 ;
1886@end group
1887@end example
1888
1889The semantic value of @samp{formal_parameters} becomes the value of
1890the @code{EXPANDFULL} expression. It is a list of @semantic{} tags
1891spliced in the tags tree.
1892
1893Because the automaton must know that @samp{formal_parameters} is a
1894start symbol, you must declare it like this:
1895
1896@example
1897@group
1898%start formal_parameters
1899@end group
1900@end example
1901@end quotation
1902@end itemize
1903
1904@cindex incremental re-parse
1905@cindex reparse-symbol
1906The @code{EXPANDFULL} macro has a side effect it is important to know,
1907related to the incremental re-parse mechanism of @semantic{}: the
1908nonterminal symbol parameter passed to @code{EXPANDFULL} also becomes
1909the @code{reparse-symbol} property of the tag returned by the
1910@code{EXPANDFULL} expression.
1911
1912When buffer's data mapped by a tag is modified, @semantic{}
1913schedules an incremental re-parse of that data, using the tag's
1914@code{reparse-symbol} property as start nonterminal.
1915
1916@strong{The rules associated to such start symbols must be carefully
1917reviewed to ensure that the incremental parser will work!}
1918
1919Things are a little bit different when the grammar is written in Bison
bfe6ffb6 1920style.
9e7abd17
EL
1921
1922@strong{The @code{reparse-symbol} property is set to the nonterminal
1923symbol the rule that explicitly uses @code{EXPANDTAG} belongs to.}
1924
1925For example:
1926
1927@example
1928@group
1929rule:
1930 rhs
1931 (let* ((rhs $1)
1932 name type comps prec action elt)
1933 @dots{}
1934 (EXPANDTAG
1935 (TAG name 'rule :type type :value comps :prec prec :expr action)
1936 ))
1937 ;
1938@end group
1939@end example
1940
1941Set the @code{reparse-symbol} property of the expanded tag to
1942@samp{rule}. A important consequence is that:
1943
1944@strong{Every nonterminal having any rule that calls @code{EXPANDTAG}
1945in a semantic action, should be declared as a start symbol!}
1946
1947@node Useful functions
1948@subsection Useful functions
1949
1950Here is a description of some predefined functions it might be useful
1951to know when writing new code to use Wisent in @semantic{}:
1952
1953@findex wisent-collect-unmatched-syntax
1954@defun wisent-collect-unmatched-syntax input
1955Add @var{input} lexical token to the cache of unmatched tokens, in
1956variable @code{semantic-unmatched-syntax-cache}.
1957
1958See implementation of the function @code{wisent-skip-token} in
1959@ref{Error recovery}, for an example of use.
1960@end defun
1961
1962@node Wisent Lex
1963@section The Wisent Lex lexer
1964
1965@findex semantic-lex
1966The lexical analysis step of @semantic{} is performed by the general
1967function @code{semantic-lex}. For more information, @inforef{Writing
1968Lexers, ,semantic-langdev}.
1969
1970@code{semantic-lex} produces lexical tokens of the form:
1971
1972@example
1973@group
1974@code{(@var{token-class start} . @var{end})}
1975@end group
1976@end example
1977
1978@table @var
1979@item token-class
1980Is a symbol that identifies a lexical token class, like @code{symbol},
1981@code{string}, @code{number}, or @code{PAREN_BLOCK}.
1982
1983@item start
1984@itemx end
1985Are the start and end positions of mapped data in the input buffer.
1986@end table
bfe6ffb6 1987
9e7abd17
EL
1988The Wisent's parser doesn't depend on the nature of analyzed input
1989stream (buffer, string, etc.), and requires that lexical tokens have a
1990different form (@pxref{Writing a lexer}):
1991
1992@example
1993@group
1994@code{(@var{token-class value} [@var{start} . @var{end}])}
1995@end group
1996@end example
1997
1998@cindex lexical token mapping
1999@code{wisent-lex} is the default Wisent's lexer used in @semantic{}.
2000
2001@vindex wisent-lex-istream
2002@findex wisent-lex
2003@defun wisent-lex
2004Return the next available lexical token in Wisent's form.
2005
2006The variable @code{wisent-lex-istream} contains the list of lexical
2007tokens produced by @code{semantic-lex}. Pop the next token available
2008and convert it to a form suitable for the Wisent's parser.
2009@end defun
2010
2011Mapping of lexical tokens as produced by @code{semantic-lex} into
2012equivalent Wisent lexical tokens is straightforward:
2013
2014@example
2015@group
2016(@var{token-class start} . @var{end})
2017 @result{} (@var{token-class value start} . @var{end})
2018@end group
2019@end example
2020
2021@var{value} is the input @code{buffer-substring} from @var{start} to
2022@var{end}.
2023
2024@node GNU Free Documentation License
2025@appendix GNU Free Documentation License
2026
bfe6ffb6 2027@include doclicense.texi
9e7abd17
EL
2028
2029@node Index
2030@unnumbered Index
2031@printindex cp
2032
2033@iftex
2034@contents
2035@summarycontents
2036@end iftex
2037
2038@bye
2039
2040@c Following comments are for the benefit of ispell.
2041
2042@c LocalWords: Wisent automagically wisent Wisent's LALR obarray