1 \input texinfo @c -*-texinfo-*-
3 @setfilename ../../info/wisent
4 @set TITLE Wisent Parser Development
5 @set AUTHOR Eric M. Ludlam, David Ponce, and Richard Y. Kim
6 @settitle @value{TITLE}
8 @c *************************************************************************
10 @c *************************************************************************
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.
20 @c @footnotestyle separate
26 Copyright @copyright{} 1988--1993, 1995, 1998--2004, 2007, 2012
27 Free Software Foundation, Inc.
29 @c Since we are both GNU manuals, we do not need to ack each other here.
31 Some texts are borrowed or adapted from the manual of Bison version
32 1.35. The text in section entitled ``Understanding the automaton'' is
33 adapted from the section ``Understanding Your Parser'' in the manual
34 of Bison version 1.49.
38 Permission is granted to copy, distribute and/or modify this document
39 under the terms of the GNU Free Documentation License, Version 1.3 or
40 any later version published by the Free Software Foundation; with no
41 Invariant Sections, with the Front-Cover texts being ``A GNU Manual,''
42 and with the Back-Cover Texts as in (a) below. A copy of the license
43 is included in the section entitled ``GNU Free Documentation License''.
45 (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
46 modify this GNU manual.''
50 @dircategory Emacs misc features
52 * Wisent: (wisent). Semantic Wisent parser development.
59 @c @setchapternewpage odd
60 @c @setchapternewpage off
65 @author by @value{AUTHOR}
67 @vskip 0pt plus 1 fill
76 @c *************************************************************************
78 @c *************************************************************************
84 Wisent (the European Bison ;-) is an Emacs Lisp implementation of the
85 GNU Compiler Compiler Bison.
87 This manual describes how to use Wisent to develop grammars for
88 programming languages, and how to use grammars to parse language
89 source in Emacs buffers.
91 It also describes how Wisent is used with the @semantic{} tool set
92 described in the @ref{Top, Semantic Manual, Semantic Manual, semantic}.
103 * GNU Free Documentation License::
107 @node Wisent Overview
108 @chapter Wisent Overview
110 @dfn{Wisent} (the European Bison) is an implementation in Emacs Lisp
111 of the GNU Compiler Compiler Bison. Its code is a port of the C code
112 of GNU Bison 1.28 & 1.31.
114 For more details on the basic concepts for understanding Wisent, it is
115 worthwhile to read the @ref{Top, Bison Manual, bison}.
117 @uref{http://www.gnu.org/manual/bison/html_node/index.html}.
120 Wisent can generate compilers compatible with the @semantic{} tool set.
121 See the @ref{Top, Semantic Manual, , semantic}.
123 It benefits from these Bison features:
127 It uses a fast but not so space-efficient encoding for the parse
128 tables, described in Corbett's PhD thesis from Berkeley:
130 @cite{Static Semantics in Compiler Error Recovery}@*
131 June 1985, Report No. UCB/CSD 85/251.
135 For generating the lookahead sets, Wisent uses the well-known
136 technique of F. DeRemer and A. Pennello described in:
138 @cite{Efficient Computation of LALR(1) Look-Ahead Sets}@*
139 October 1982, ACM TOPLAS Vol 4 No 4, 615--49,
140 @uref{http://dx.doi.org/10.1145/69622.357187}.
144 Wisent resolves shift/reduce conflicts using operator precedence and
148 Parser error recovery is accomplished using rules which match the
149 special token @code{error}.
152 Nevertheless there are some fundamental differences between Bison and
157 Wisent is intended to be used in Emacs. It reads and produces Emacs
158 Lisp data structures. All the additional code used in grammars is
162 Contrary to Bison, Wisent does not generate a parser which combines
163 Emacs Lisp code and grammar constructs. They exist separately.
164 Wisent reads the grammar from a Lisp data structure and then generates
165 grammar constructs as tables. Afterward, the derived tables can be
166 included and byte-compiled in separate Emacs Lisp files, and be used
167 at a later time by the Wisent's parser engine.
170 Wisent allows multiple start nonterminals and allows a call to the
171 parsing function to be made for a particular start nonterminal. For
172 example, this is particularly useful to parse a region of an Emacs
173 buffer. @semantic{} heavily depends on the availability of this feature.
177 @chapter Wisent Grammar
179 @cindex context-free grammar
181 In order for Wisent to parse a language, it must be described by a
182 @dfn{context-free grammar}. That is a grammar specified as rules that
183 can be applied regardless of context. For more information, see
184 @ref{Language and Grammar, , , bison}, in the Bison manual.
188 The formal grammar is formulated using @dfn{terminal} and
189 @dfn{nonterminal} items. Terminals can be Emacs Lisp symbols or
190 characters, and nonterminals are symbols only.
193 Terminals (also known as @dfn{tokens}) represent the lexical
194 elements of the language like numbers, strings, etc..
196 For example @samp{PLUS} can represent the operator @samp{+}.
198 Nonterminal symbols are described by rules:
202 RESULT @equiv{} COMPONENTS@dots{}
206 @samp{RESULT} is a nonterminal that this rule describes and
207 @samp{COMPONENTS} are various terminals and nonterminals that are put
208 together by this rule.
210 For example, this rule:
214 exp @equiv{} exp PLUS exp
218 Says that two groupings of type @samp{exp}, with a @samp{PLUS} token
219 in between, can be combined into a larger grouping of type @samp{exp}.
224 * Compiling a grammar::
228 @node Grammar format, Example, Wisent Grammar, Wisent Grammar
229 @comment node-name, next, previous, up
230 @section Grammar format
232 @cindex grammar format
233 To be acceptable by Wisent a context-free grammar must respect a
234 particular format. That is, must be represented as an Emacs Lisp list
237 @code{(@var{terminals} @var{assocs} . @var{non-terminals})}
241 Is the list of terminal symbols used in the grammar.
243 @cindex associativity
245 Specify the associativity of @var{terminals}. It is @code{nil} when
246 there is no associativity defined, or an alist of
247 @w{@code{(@var{assoc-type} . @var{assoc-value})}} elements.
249 @var{assoc-type} must be one of the @code{default-prec},
250 @code{nonassoc}, @code{left} or @code{right} symbols. When
251 @var{assoc-type} is @code{default-prec}, @var{assoc-value} must be
252 @code{nil} or @code{t} (the default). Otherwise it is a list of
253 tokens which must have been previously declared in @var{terminals}.
255 For details, see @ref{Contextual Precedence, , , bison}, in the
259 Is the list of nonterminal definitions. Each definition has the form:
261 @code{(@var{nonterm} . @var{rules})}
263 Where @var{nonterm} is the nonterminal symbol defined and
264 @var{rules} the list of rules that describe this nonterminal. Each
267 @code{(@var{components} [@var{precedence}] [@var{action}])}
273 Is a list of various terminals and nonterminals that are put together
280 (exp ((exp ?+ exp)) ;; exp: exp '+' exp
285 Says that two groupings of type @samp{exp}, with a @samp{+} token in
286 between, can be combined into a larger grouping of type @samp{exp}.
288 @cindex grammar coding conventions
289 By convention, a nonterminal symbol should be in lower case, such as
290 @samp{exp}, @samp{stmt} or @samp{declaration}. Terminal symbols
291 should be upper case to distinguish them from nonterminals: for
292 example, @samp{INTEGER}, @samp{IDENTIFIER}, @samp{IF} or
293 @samp{RETURN}. A terminal symbol that represents a particular keyword
294 in the language is conventionally the same as that keyword converted
295 to upper case. The terminal symbol @code{error} is reserved for error
298 @cindex middle-rule actions
299 Scattered among the components can be @dfn{middle-rule} actions.
300 Usually only @var{action} is provided (@pxref{action}).
302 If @var{components} in a rule is @code{nil}, it means that the rule
303 can match the empty string. For example, here is how to define a
304 comma-separated sequence of zero or more @samp{exp} groupings:
308 (expseq (nil) ;; expseq: ;; empty
309 ((expseq1)) ;; | expseq1
312 (expseq1 ((exp)) ;; expseq1: exp
313 ((expseq1 ?, exp)) ;; | expseq1 ',' exp
318 @cindex precedence level
320 Assign the rule the precedence of the given terminal item, overriding
321 the precedence that would be deduced for it, that is the one of the
322 last terminal in it. Notice that only terminals declared in
323 @var{assocs} have a precedence level. The altered rule precedence
324 then affects how conflicts involving that rule are resolved.
326 @var{precedence} is an optional vector of one terminal item.
328 Here is how @var{precedence} solves the problem of unary minus.
329 First, declare a precedence for a fictitious terminal symbol named
330 @code{UMINUS}. There are no tokens of this type, but the symbol
331 serves to stand for its precedence:
335 ((default-prec t) ;; This is the default
341 Now the precedence of @code{UMINUS} can be used in specific rules:
345 (exp @dots{} ;; exp: @dots{}
346 ((exp ?- exp)) ;; | exp '-' exp
348 ((?- exp) [UMINUS]) ;; | '-' exp %prec UMINUS
354 If you forget to append @code{[UMINUS]} to the rule for unary minus,
355 Wisent silently assumes that minus has its usual precedence. This
356 kind of problem can be tricky to debug, since one typically discovers
357 the mistake only by testing the code.
359 Using @code{(default-prec nil)} declaration makes it easier to
360 discover this kind of problem systematically. It causes rules that
361 lack a @var{precedence} modifier to have no precedence, even if the
362 last terminal symbol mentioned in their components has a declared
365 If @code{(default-prec nil)} is in effect, you must specify
366 @var{precedence} for all rules that participate in precedence conflict
367 resolution. Then you will see any shift/reduce conflict until you
368 tell Wisent how to resolve it, either by changing your grammar or by
369 adding an explicit precedence. This will probably add declarations to
370 the grammar, but it helps to protect against incorrect rule
373 The effect of @code{(default-prec nil)} can be reversed by giving
374 @code{(default-prec t)}, which is the default.
376 For more details, see @ref{Contextual Precedence, , , bison}, in the
379 It is important to understand that @var{assocs} declarations defines
380 associativity but also assign a precedence level to terminals. All
381 terminals declared in the same @code{left}, @code{right} or
382 @code{nonassoc} association get the same precedence level. The
383 precedence level is increased at each new association.
385 On the other hand, @var{precedence} explicitly assign the precedence
386 level of the given terminal to a rule.
388 @cindex semantic actions
389 @item @anchor{action}action
390 An action is an optional Emacs Lisp function call, like this:
394 The result of an action determines the semantic value of a rule.
396 From an implementation standpoint, the function call will be embedded
397 in a lambda expression, and several useful local variables will be
403 Where @var{n} is a positive integer. Like in Bison, the value of
404 @code{$@var{n}} is the semantic value of the @var{n}th element of
405 @var{components}, starting from 1. It can be of any Lisp data
408 @vindex $region@var{n}
410 Where @var{n} is a positive integer. For each @code{$@var{n}}
411 variable defined there is a corresponding @code{$region@var{n}}
412 variable. Its value is a pair @code{(@var{start-pos} .
413 @var{end-pos})} that represent the start and end positions (in the
414 lexical input stream) of the @code{$@var{n}} value. It can be
415 @code{nil} when the component positions are not available, like for an
416 empty string component for example.
420 Its value is the leftmost and rightmost positions of input data
421 matched by all @var{components} in the rule. This is a pair
422 @code{(@var{leftmost-pos} . @var{rightmost-pos})}. It can be
423 @code{nil} when components positions are not available.
427 This variable is initialized with the nonterminal symbol
428 (@var{nonterm}) the rule belongs to. It could be useful to improve
429 error reporting or debugging. It is also used to automatically
430 provide incremental re-parse entry points for @semantic{} tags
431 (@pxref{Wisent Semantic}).
435 The value of @code{$action} is the symbolic name of the current
436 semantic action (@pxref{Debugging actions}).
439 When an action is not specified a default value is supplied, it is
440 @code{(identity $1)}. This means that the default semantic value of a
441 rule is the value of its first component. Excepted for a rule
442 matching the empty string, for which the default action is to return
447 @node Example, Compiling a grammar, Grammar format, Wisent Grammar
448 @comment node-name, next, previous, up
451 @cindex grammar example
452 Here is an example to parse simple infix arithmetic expressions. See
453 @ref{Infix Calc, , , bison}, in the Bison manual for details.
461 ;; Terminal associativity & precedence
472 (format "%s %s" $1 $2))
486 (string-to-number $1))
508 In the bison-like @dfn{WY} format (@pxref{Wisent Semantic}) the
509 grammar looks like this:
515 %nonassoc '=' ;; comparison
518 %left NEG ;; negation--unary minus
519 %right '^' ;; exponentiation
526 (format "%s %s" $1 $2)
540 (string-to-number $1)
563 @node Compiling a grammar, Conflicts, Example, Wisent Grammar
564 @comment node-name, next, previous, up
565 @section Compiling a grammar
568 After providing a context-free grammar in a suitable format, it must
569 be translated into a set of tables (an @dfn{automaton}) that will be
570 used to derive the parser. Like Bison, Wisent translates grammars that
571 must be @dfn{LALR(1)}.
573 @cindex LALR(1) grammar
574 @cindex look-ahead token
575 A grammar is @acronym{LALR(1)} if it is possible to tell how to parse
576 any portion of an input string with just a single token of look-ahead:
577 the @dfn{look-ahead token}. See @ref{Language and Grammar, , ,
578 bison}, in the Bison manual for more information.
580 @cindex grammar compilation
581 Grammar translation (compilation) is achieved by the function:
583 @cindex compiling a grammar
584 @vindex wisent-single-start-flag
585 @findex wisent-compile-grammar
586 @defun wisent-compile-grammar grammar &optional start-list
587 Compile @var{grammar} and return an @acronym{LALR(1)} automaton.
589 Optional argument @var{start-list} is a list of start symbols
590 (nonterminals). If @code{nil} the first nonterminal defined in the
591 grammar is the default start symbol. If @var{start-list} contains
592 only one element, it defines the start symbol. If @var{start-list}
593 contains more than one element, all are defined as potential start
594 symbols, unless @code{wisent-single-start-flag} is non-@code{nil}. In
595 that case the first element of @var{start-list} defines the start
596 symbol and others are ignored.
598 The @acronym{LALR(1)} automaton is a vector of the form:
600 @code{[@var{actions gotos starts functions}]}
604 A state/token matrix telling the parser what to do at every state
605 based on the current look-ahead token. That is shift, reduce, accept
606 or error. See also @ref{Wisent Parsing}.
609 A state/nonterminal matrix telling the parser the next state to go to
610 after reducing with each rule.
613 An alist which maps the allowed start symbols (nonterminals) to
614 lexical tokens that will be first shifted into the parser stack.
617 An obarray of semantic action symbols. A semantic action is actually
618 an Emacs Lisp function (lambda expression).
622 @node Conflicts, , Compiling a grammar, Wisent Grammar
623 @comment node-name, next, previous, up
626 Normally, a grammar should produce an automaton where at each state
627 the parser has only one action to do (@pxref{Wisent Parsing}).
629 @cindex ambiguous grammar
630 In certain cases, a grammar can produce an automaton where, at some
631 states, there are more than one action possible. Such a grammar is
632 @dfn{ambiguous}, and generates @dfn{conflicts}.
634 @cindex deterministic automaton
635 The parser can't be driven by an automaton which isn't completely
636 @dfn{deterministic}, that is which contains conflicts. It is
637 necessary to resolve the conflicts to eliminate them. Wisent resolves
638 conflicts like Bison does.
640 @cindex grammar conflicts
641 @cindex conflicts resolution
642 There are two sorts of conflicts:
645 @cindex shift/reduce conflicts
646 @item shift/reduce conflicts
647 When either a shift or a reduction would be valid at the same state.
649 Such conflicts are resolved by choosing to shift, unless otherwise
650 directed by operator precedence declarations.
651 See @ref{Shift/Reduce , , , bison}, in the Bison manual for more
654 @cindex reduce/reduce conflicts
655 @item reduce/reduce conflicts
656 That occurs if there are two or more rules that apply to the same
657 sequence of input. This usually indicates a serious error in the
660 Such conflicts are resolved by choosing to use the rule that appears
661 first in the grammar, but it is very risky to rely on this. Every
662 reduce/reduce conflict must be studied and usually eliminated. See
663 @ref{Reduce/Reduce , , , bison}, in the Bison manual for more
668 * Grammar Debugging::
669 * Understanding the automaton::
672 @node Grammar Debugging
673 @subsection Grammar debugging
675 @cindex grammar debugging
676 @cindex grammar verbose description
677 To help writing a new grammar, @code{wisent-compile-grammar} can
678 produce a verbose report containing a detailed description of the
679 grammar and parser (equivalent to what Bison reports with the
680 @option{--verbose} option).
682 To enable the verbose report you can set to non-@code{nil} the
685 @vindex wisent-verbose-flag
686 @deffn Option wisent-verbose-flag
687 non-@code{nil} means to report verbose information on generated parser.
690 Or interactively use the command:
692 @findex wisent-toggle-verbose-flag
693 @deffn Command wisent-toggle-verbose-flag
694 Toggle whether to report verbose information on generated parser.
697 The verbose report is printed in the temporary buffer
698 @code{*wisent-log*} when running interactively, or in file
699 @file{wisent.output} when running in batch mode. Different
700 reports are separated from each other by a line like this:
704 *** Wisent @var{source-file} - 2002-06-27 17:33
708 where @var{source-file} is the name of the Emacs Lisp file from which
709 the grammar was read. See @ref{Understanding the automaton}, for
710 details on the verbose report.
714 To help debugging the grammar compiler itself, you can set this
715 variable to print the content of some internal data structures:
717 @vindex wisent-debug-flag
718 @defvar wisent-debug-flag
719 non-@code{nil} means enable some debug stuff.
723 @node Understanding the automaton
724 @subsection Understanding the automaton
726 @cindex understanding the automaton
727 This section (took from the manual of Bison 1.49) describes how to use
728 the verbose report printed by @code{wisent-compile-grammar} to
729 understand the generated automaton, to tune or fix a grammar.
731 We will use the following example:
735 (let ((wisent-verbose-flag t)) ;; Print a verbose report!
736 (wisent-compile-grammar
737 '((NUM STR) ; %token NUM STR
739 ((left ?+ ?-) ; %left '+' '-';
740 (left ?*)) ; %left '*'
743 ((exp ?+ exp)) ; exp '+' exp
744 ((exp ?- exp)) ; | exp '-' exp
745 ((exp ?* exp)) ; | exp '*' exp
746 ((exp ?/ exp)) ; | exp '/' exp
754 'nil) ; no %start declarations
759 When evaluating the above expression, grammar compilation first issues
760 the following two clear messages:
764 Grammar contains 1 useless nonterminals and 1 useless rules
765 Grammar contains 7 shift/reduce conflicts
769 The @samp{*wisent-log*} buffer details things!
771 The first section reports conflicts that were solved using precedence
772 and/or associativity:
776 Conflict in state 7 between rule 1 and token '+' resolved as reduce.
777 Conflict in state 7 between rule 1 and token '-' resolved as reduce.
778 Conflict in state 7 between rule 1 and token '*' resolved as shift.
779 Conflict in state 8 between rule 2 and token '+' resolved as reduce.
780 Conflict in state 8 between rule 2 and token '-' resolved as reduce.
781 Conflict in state 8 between rule 2 and token '*' resolved as shift.
782 Conflict in state 9 between rule 3 and token '+' resolved as reduce.
783 Conflict in state 9 between rule 3 and token '-' resolved as reduce.
784 Conflict in state 9 between rule 3 and token '*' resolved as reduce.
788 The next section reports useless tokens, nonterminal and rules (note
789 that useless tokens might be used by the scanner):
793 Useless nonterminals:
798 Terminals which are not used:
809 The next section lists states that still have conflicts:
813 State 7 contains 1 shift/reduce conflict.
814 State 8 contains 1 shift/reduce conflict.
815 State 9 contains 1 shift/reduce conflict.
816 State 10 contains 4 shift/reduce conflicts.
820 The next section reproduces the grammar used:
835 And reports the uses of the symbols:
839 Terminals, with rules where they appear
851 Nonterminals, with rules where they appear
854 on left: 1 2 3 4 5, on right: 1 2 3 4
858 The report then details the automaton itself, describing each state
859 with it set of @dfn{items}, also known as @dfn{pointed rules}. Each
860 item is a production rule together with a point (marked by @samp{.})
861 that the input cursor.
867 NUM shift, and go to state 1
873 State 0 corresponds to being at the very beginning of the parsing, in
874 the initial rule, right before the start symbol (@samp{exp}). When
875 the parser returns to this state right after having reduced a rule
876 that produced an @samp{exp}, it jumps to state 2. If there is no such
877 transition on a nonterminal symbol, and the lookahead is a @samp{NUM},
878 then this token is shifted on the parse stack, and the control flow
879 jumps to state 1. Any other lookahead triggers a parse error.
887 exp -> NUM . (rule 5)
889 $default reduce using rule 5 (exp)
893 the rule 5, @samp{exp: NUM;}, is completed. Whatever the lookahead
894 (@samp{$default}), the parser will reduce it. If it was coming from
895 state 0, then, after this reduction it will return to state 0, and
896 will jump to state 2 (@samp{exp: go to state 2}).
902 exp -> exp . '+' exp (rule 1)
903 exp -> exp . '-' exp (rule 2)
904 exp -> exp . '*' exp (rule 3)
905 exp -> exp . '/' exp (rule 4)
907 $EOI shift, and go to state 11
908 '+' shift, and go to state 3
909 '-' shift, and go to state 4
910 '*' shift, and go to state 5
911 '/' shift, and go to state 6
915 In state 2, the automaton can only shift a symbol. For instance,
916 because of the item @samp{exp -> exp . '+' exp}, if the lookahead if
917 @samp{+}, it will be shifted on the parse stack, and the automaton
918 control will jump to state 3, corresponding to the item
919 @samp{exp -> exp . '+' exp}:
925 exp -> exp '+' . exp (rule 1)
927 NUM shift, and go to state 1
933 Since there is no default action, any other token than those listed
934 above will trigger a parse error.
936 The interpretation of states 4 to 6 is straightforward:
942 exp -> exp '-' . exp (rule 2)
944 NUM shift, and go to state 1
952 exp -> exp '*' . exp (rule 3)
954 NUM shift, and go to state 1
962 exp -> exp '/' . exp (rule 4)
964 NUM shift, and go to state 1
970 As was announced in beginning of the report, @samp{State 7 contains 1
971 shift/reduce conflict.}:
977 exp -> exp . '+' exp (rule 1)
978 exp -> exp '+' exp . (rule 1)
979 exp -> exp . '-' exp (rule 2)
980 exp -> exp . '*' exp (rule 3)
981 exp -> exp . '/' exp (rule 4)
983 '*' shift, and go to state 5
984 '/' shift, and go to state 6
986 '/' [reduce using rule 1 (exp)]
987 $default reduce using rule 1 (exp)
991 Indeed, there are two actions associated to the lookahead @samp{/}:
992 either shifting (and going to state 6), or reducing rule 1. The
993 conflict means that either the grammar is ambiguous, or the parser
994 lacks information to make the right decision. Indeed the grammar is
995 ambiguous, as, since we did not specify the precedence of @samp{/},
996 the sentence @samp{NUM + NUM / NUM} can be parsed as @samp{NUM + (NUM
997 / NUM)}, which corresponds to shifting @samp{/}, or as @samp{(NUM +
998 NUM) / NUM}, which corresponds to reducing rule 1.
1000 Because in @acronym{LALR(1)} parsing a single decision can be made,
1001 Wisent arbitrarily chose to disable the reduction, see
1002 @ref{Conflicts}. Discarded actions are reported in between square
1005 Note that all the previous states had a single possible action: either
1006 shifting the next token and going to the corresponding state, or
1007 reducing a single rule. In the other cases, i.e., when shifting
1008 @emph{and} reducing is possible or when @emph{several} reductions are
1009 possible, the lookahead is required to select the action. State 7 is
1010 one such state: if the lookahead is @samp{*} or @samp{/} then the
1011 action is shifting, otherwise the action is reducing rule 1. In other
1012 words, the first two items, corresponding to rule 1, are not eligible
1013 when the lookahead is @samp{*}, since we specified that @samp{*} has
1014 higher precedence that @samp{+}. More generally, some items are
1015 eligible only with some set of possible lookaheads.
1017 States 8 to 10 are similar:
1023 exp -> exp . '+' exp (rule 1)
1024 exp -> exp . '-' exp (rule 2)
1025 exp -> exp '-' exp . (rule 2)
1026 exp -> exp . '*' exp (rule 3)
1027 exp -> exp . '/' exp (rule 4)
1029 '*' shift, and go to state 5
1030 '/' shift, and go to state 6
1032 '/' [reduce using rule 2 (exp)]
1033 $default reduce using rule 2 (exp)
1039 exp -> exp . '+' exp (rule 1)
1040 exp -> exp . '-' exp (rule 2)
1041 exp -> exp . '*' exp (rule 3)
1042 exp -> exp '*' exp . (rule 3)
1043 exp -> exp . '/' exp (rule 4)
1045 '/' shift, and go to state 6
1047 '/' [reduce using rule 3 (exp)]
1048 $default reduce using rule 3 (exp)
1054 exp -> exp . '+' exp (rule 1)
1055 exp -> exp . '-' exp (rule 2)
1056 exp -> exp . '*' exp (rule 3)
1057 exp -> exp . '/' exp (rule 4)
1058 exp -> exp '/' exp . (rule 4)
1060 '+' shift, and go to state 3
1061 '-' shift, and go to state 4
1062 '*' shift, and go to state 5
1063 '/' shift, and go to state 6
1065 '+' [reduce using rule 4 (exp)]
1066 '-' [reduce using rule 4 (exp)]
1067 '*' [reduce using rule 4 (exp)]
1068 '/' [reduce using rule 4 (exp)]
1069 $default reduce using rule 4 (exp)
1073 Observe that state 10 contains conflicts due to the lack of precedence
1074 of @samp{/} wrt @samp{+}, @samp{-}, and @samp{*}, but also because the
1075 associativity of @samp{/} is not specified.
1077 Finally, the state 11 (plus 12) is named the @dfn{final state}, or the
1078 @dfn{accepting state}:
1084 $EOI shift, and go to state 12
1094 The end of input is shifted @samp{$EOI shift,} and the parser exits
1095 successfully (@samp{go to state 12}, that terminates).
1097 @node Wisent Parsing
1098 @chapter Wisent Parsing
1100 @cindex bottom-up parser
1101 @cindex shift-reduce parser
1102 The Wisent's parser is what is called a @dfn{bottom-up} or
1103 @dfn{shift-reduce} parser which repeatedly:
1108 That is pushes the value of the last lexical token read (the
1109 look-ahead token) into a value stack, and reads a new one.
1113 That is replaces a nonterminal by its semantic value. The values of
1114 the components which form the right hand side of a rule are popped
1115 from the value stack and reduced by the semantic action of this rule.
1116 The result is pushed back on top of value stack.
1119 The parser will stop on:
1124 When all input has been successfully parsed. The semantic value of
1125 the start nonterminal is on top of the value stack.
1127 @cindex syntax error
1129 When a syntax error (an unexpected token in input) has been detected.
1130 At this point the parser issues an error message and either stops or
1131 calls a recovery routine to try to resume parsing.
1134 @cindex table-driven parser
1135 The above elementary actions are driven by the @acronym{LALR(1)}
1136 automaton built by @code{wisent-compile-grammar} from a context-free
1139 The Wisent's parser is entered by calling the function:
1141 @findex wisent-parse
1142 @defun wisent-parse automaton lexer &optional error start
1143 Parse input using the automaton specified in @var{automaton}.
1147 Is an @acronym{LALR(1)} automaton generated by
1148 @code{wisent-compile-grammar} (@pxref{Wisent Grammar}).
1151 Is a function with no argument called by the parser to obtain the next
1152 terminal (token) in input (@pxref{Writing a lexer}).
1155 Is an optional reporting function called when a parse error occurs.
1156 It receives a message string to report. It defaults to the function
1157 @code{wisent-message} (@pxref{Report errors}).
1160 Specify the start symbol (nonterminal) used by the parser as its goal.
1161 It defaults to the start symbol defined in the grammar
1162 (@pxref{Wisent Grammar}).
1166 The following two normal hooks permit to do some useful processing
1167 respectively before to start parsing, and after the parser terminated.
1169 @vindex wisent-pre-parse-hook
1170 @defvar wisent-pre-parse-hook
1171 Normal hook run just before entering the @var{LR} parser engine.
1174 @vindex wisent-post-parse-hook
1175 @defvar wisent-post-parse-hook
1176 Normal hook run just after the @var{LR} parser engine terminated.
1184 * Debugging actions::
1187 @node Writing a lexer
1188 @section What the parser must receive
1190 It is important to understand that the parser does not parse
1191 characters, but lexical tokens, and does not know anything about
1192 characters in text streams!
1194 @cindex lexical analysis
1197 Reading input data to produce lexical tokens is performed by a lexer
1198 (also called a scanner) in a lexical analysis step, before the syntax
1199 analysis step performed by the parser. The parser automatically calls
1200 the lexer when it needs the next token to parse.
1202 @cindex lexical tokens
1203 A Wisent's lexer is an Emacs Lisp function with no argument. It must
1204 return a valid lexical token of the form:
1206 @code{(@var{token-class value} [@var{start} . @var{end}])}
1210 Is a category of lexical token identifying a terminal as specified in
1211 the grammar (@pxref{Wisent Grammar}). It can be a symbol or a character
1215 Is the value of the lexical token. It can be of any valid Emacs Lisp
1220 Are the optional beginning and ending positions of @var{value} in the
1224 When there are no more tokens to read the lexer must return the token
1225 @code{(list wisent-eoi-term)} to each request.
1227 @vindex wisent-eoi-term
1228 @defvar wisent-eoi-term
1229 Predefined constant, End-Of-Input terminal symbol.
1232 @code{wisent-lex} is an example of a lexer that reads lexical tokens
1233 produced by a @semantic{} lexer, and translates them into lexical tokens
1234 suitable to the Wisent parser. See also @ref{Wisent Lex}.
1236 To call the lexer in a semantic action use the function
1237 @code{wisent-lexer}. See also @ref{Actions goodies}.
1239 @node Actions goodies
1240 @section Variables and macros useful in grammar actions.
1242 @vindex wisent-input
1243 @defvar wisent-input
1244 The last token read.
1245 This variable only has meaning in the scope of @code{wisent-parse}.
1248 @findex wisent-lexer
1250 Obtain the next terminal in input.
1253 @findex wisent-region
1254 @defun wisent-region &rest positions
1255 Return the start/end positions of the region including
1256 @var{positions}. Each element of @var{positions} is a pair
1257 @w{@code{(@var{start-pos} . @var{end-pos})}} or @code{nil}. The
1258 returned value is the pair @w{@code{(@var{min-start-pos} .
1259 @var{max-end-pos})}} or @code{nil} if no @var{positions} are
1264 @section The error reporting function
1266 @cindex error reporting
1267 When the parser encounters a syntax error it calls a user-defined
1268 function. It must be an Emacs Lisp function with one argument: a
1269 string containing the message to report.
1271 By default the parser uses this function to report error messages:
1273 @findex wisent-message
1274 @defun wisent-message string &rest args
1275 Print a one-line message if @code{wisent-parse-verbose-flag} is set.
1276 Pass @var{string} and @var{args} arguments to @dfn{message}.
1281 @code{wisent-message} uses the following function to print lexical
1284 @defun wisent-token-to-string token
1285 Return a printed representation of lexical token @var{token}.
1288 The general printed form of a lexical token is:
1290 @w{@code{@var{token}(@var{value})@@@var{location}}}
1293 To control the verbosity of the parser you can set to non-@code{nil}
1296 @vindex wisent-parse-verbose-flag
1297 @deffn Option wisent-parse-verbose-flag
1298 non-@code{nil} means to issue more messages while parsing.
1301 Or interactively use the command:
1303 @findex wisent-parse-toggle-verbose-flag
1304 @deffn Command wisent-parse-toggle-verbose-flag
1305 Toggle whether to issue more messages while parsing.
1308 When the error reporting function is entered the variable
1309 @code{wisent-input} contains the unexpected token as returned by the
1312 The error reporting function can be called from a semantic action too
1313 using the special macro @code{wisent-error}. When called from a
1314 semantic action entered by error recovery (@pxref{Error recovery}) the
1315 value of the variable @code{wisent-recovering} is non-@code{nil}.
1317 @node Error recovery
1318 @section Error recovery
1320 @cindex error recovery
1321 The error recovery mechanism of the Wisent's parser conforms to the
1322 one Bison uses. See @ref{Error Recovery, , , bison}, in the Bison
1326 To recover from a syntax error you must write rules to recognize the
1327 special token @code{error}. This is a terminal symbol that is
1328 automatically defined and reserved for error handling.
1330 When the parser encounters a syntax error, it pops the state stack
1331 until it finds a state that allows shifting the @code{error} token.
1332 After it has been shifted, if the old look-ahead token is not
1333 acceptable to be shifted next, the parser reads tokens and discards
1334 them until it finds a token which is acceptable.
1336 @cindex error recovery strategy
1337 Strategies for error recovery depend on the choice of error rules in
1338 the grammar. A simple and useful strategy is simply to skip the rest
1339 of the current statement if an error is detected:
1343 (statement (( error ?; )) ;; on error, skip until ';' is read
1348 It is also useful to recover to the matching close-delimiter of an
1349 opening-delimiter that has already been parsed:
1353 (primary (( ?@{ expr ?@} ))
1360 @cindex error recovery actions
1361 Note that error recovery rules may have actions, just as any other
1362 rules can. Here are some predefined hooks, variables, functions or
1363 macros, useful in such actions:
1365 @vindex wisent-nerrs
1366 @defvar wisent-nerrs
1367 The number of parse errors encountered so far.
1370 @vindex wisent-recovering
1371 @defvar wisent-recovering
1372 non-@code{nil} means that the parser is recovering.
1373 This variable only has meaning in the scope of @code{wisent-parse}.
1376 @findex wisent-error
1377 @defun wisent-error msg
1378 Call the user supplied error reporting function with message
1379 @var{msg} (@pxref{Report errors}).
1381 For an example of use, @xref{wisent-skip-token}.
1384 @findex wisent-errok
1386 Resume generating error messages immediately for subsequent syntax
1389 The parser suppress error message for syntax errors that happens
1390 shortly after the first, until three consecutive input tokens have
1391 been successfully shifted.
1393 Calling @code{wisent-errok} in an action, make error messages resume
1394 immediately. No error messages will be suppressed if you call it in
1395 an error rule's action.
1397 For an example of use, @xref{wisent-skip-token}.
1400 @findex wisent-clearin
1401 @defun wisent-clearin
1402 Discard the current lookahead token.
1403 This will cause a new lexical token to be read.
1405 In an error rule's action the previous lookahead token is reanalyzed
1406 immediately. @code{wisent-clearin} may be called to clear this token.
1408 For example, suppose that on a parse error, an error handling routine
1409 is called that advances the input stream to some point where parsing
1410 should once again commence. The next symbol returned by the lexical
1411 scanner is probably correct. The previous lookahead token ought to
1412 be discarded with @code{wisent-clearin}.
1414 For an example of use, @xref{wisent-skip-token}.
1417 @findex wisent-abort
1419 Abort parsing and save the lookahead token.
1422 @findex wisent-set-region
1423 @defun wisent-set-region start end
1424 Change the region of text matched by the current nonterminal.
1425 @var{start} and @var{end} are respectively the beginning and end
1426 positions of the region occupied by the group of components associated
1427 to this nonterminal. If @var{start} or @var{end} values are not a
1428 valid positions the region is set to @code{nil}.
1430 For an example of use, @xref{wisent-skip-token}.
1433 @vindex wisent-discarding-token-functions
1434 @defvar wisent-discarding-token-functions
1435 List of functions to be called when discarding a lexical token.
1436 These functions receive the lexical token discarded.
1437 When the parser encounters unexpected tokens, it can discards them,
1438 based on what directed by error recovery rules. Either when the
1439 parser reads tokens until one is found that can be shifted, or when an
1440 semantic action calls the function @code{wisent-skip-token} or
1441 @code{wisent-skip-block}.
1442 For language specific hooks, make sure you define this as a local
1445 For example, in @semantic{}, this hook is set to the function
1446 @code{wisent-collect-unmatched-syntax} to collect unmatched lexical
1447 tokens (@pxref{Useful functions}).
1450 @findex wisent-skip-token
1451 @defun wisent-skip-token
1452 @anchor{wisent-skip-token}
1453 Skip the lookahead token in order to resume parsing.
1455 Must be used in error recovery semantic actions.
1457 It typically looks like this:
1461 (wisent-message "%s: skip %s" $action
1462 (wisent-token-to-string wisent-input))
1464 'wisent-discarding-token-functions wisent-input)
1471 @findex wisent-skip-block
1472 @defun wisent-skip-block
1473 Safely skip a block in order to resume parsing.
1475 Must be used in error recovery semantic actions.
1477 A block is data between an open-delimiter (syntax class @code{(}) and
1478 a matching close-delimiter (syntax class @code{)}):
1482 (a parenthesized block)
1483 [a block between brackets]
1484 @{a block between braces@}
1488 The following example uses @code{wisent-skip-block} to safely skip a
1489 block delimited by @samp{LBRACE} (@code{@{}) and @samp{RBRACE}
1490 (@code{@}}) tokens, when a syntax error occurs in
1491 @samp{other-components}:
1495 (block ((LBRACE other-components RBRACE))
1498 (wisent-skip-block))
1504 @node Debugging actions
1505 @section Debugging semantic actions
1507 @cindex semantic action symbols
1508 Each semantic action is represented by a symbol interned in an
1509 @dfn{obarray} that is part of the @acronym{LALR(1)} automaton
1510 (@pxref{Compiling a grammar}). @code{symbol-function} on a semantic
1511 action symbol return the semantic action lambda expression.
1513 A semantic action symbol name has the form
1514 @code{@var{nonterminal}:@var{index}}, where @var{nonterminal} is the
1515 name of the nonterminal symbol the action belongs to, and @var{index}
1516 is an action sequence number within the scope of @var{nonterminal}.
1517 For example, this nonterminal definition:
1522 line [@code{input:0}]
1524 (format "%s %s" $1 $2) [@code{input:1}]
1529 Will produce two semantic actions, and associated symbols:
1533 A default action that returns @code{$1}.
1536 That returns @code{(format "%s %s" $1 $2)}.
1539 @cindex debugging semantic actions
1540 Debugging uses the Lisp debugger to investigate what is happening
1541 during execution of semantic actions.
1542 Three commands are available to debug semantic actions. They receive
1546 @item The automaton that contains the semantic action.
1548 @item The semantic action symbol.
1551 @findex wisent-debug-on-entry
1552 @deffn Command wisent-debug-on-entry automaton function
1553 Request @var{automaton}'s @var{function} to invoke debugger each time it is called.
1554 @var{function} must be a semantic action symbol that exists in @var{automaton}.
1557 @findex wisent-cancel-debug-on-entry
1558 @deffn Command wisent-cancel-debug-on-entry automaton function
1559 Undo effect of @code{wisent-debug-on-entry} on @var{automaton}'s @var{function}.
1560 @var{function} must be a semantic action symbol that exists in @var{automaton}.
1563 @findex wisent-debug-show-entry
1564 @deffn Command wisent-debug-show-entry automaton function
1565 Show the source of @var{automaton}'s semantic action @var{function}.
1566 @var{function} must be a semantic action symbol that exists in @var{automaton}.
1569 @node Wisent Semantic
1570 @chapter How to use Wisent with Semantic
1573 This section presents how the Wisent's parser can be used to produce
1574 @dfn{tags} for the @semantic{} tool set.
1576 @semantic{} tags form a hierarchy of Emacs Lisp data structures that
1577 describes a program in a way independent of programming languages.
1578 Tags map program declarations, like functions, methods, variables,
1579 data types, classes, includes, grammar rules, etc..
1581 @cindex WY grammar format
1582 To use the Wisent parser with @semantic{} you have to define
1583 your grammar in @dfn{WY} form, a grammar format very close
1584 to the one used by Bison.
1586 Please @inforef{top, Semantic Grammar Framework Manual, grammar-fw}
1587 for more information on @semantic{} grammars.
1594 @node Grammar styles
1595 @section Grammar styles
1597 @cindex grammar styles
1598 @semantic{} parsing heavily depends on how you wrote the grammar.
1599 There are mainly two styles to write a Wisent's grammar intended to be
1600 used with the @semantic{} tool set: the @dfn{Iterative style} and the
1601 @dfn{Bison style}. Each one has pros and cons, and in certain cases
1602 it can be worth a mix of the two styles!
1608 * Start nonterminals::
1609 * Useful functions::
1612 @node Iterative style, Bison style, Grammar styles, Grammar styles
1613 @subsection Iterative style
1615 @cindex grammar iterative style
1616 The @dfn{iterative style} is the preferred style to use with @semantic{}.
1617 It relies on an iterative parser back-end mechanism which parses start
1618 nonterminals one at a time and automagically skips unexpected lexical
1621 Compared to rule-based iterative functions (@pxref{Bison style}),
1622 iterative parsers are better in that they can handle obscure errors
1626 Each start nonterminal must produces a @dfn{raw tag} by calling a
1627 @code{TAG}-like grammar macro with appropriate parameters. See also
1628 @ref{Start nonterminals}.
1630 @cindex expanded tag
1631 Then, each parsing iteration automatically translates a raw tag into
1632 @dfn{expanded tags}, updating the raw tag structure with internal
1633 properties and buffer related data.
1635 After parsing completes, it results in a tree of expanded tags.
1637 The following example is a snippet of the iterative style Java grammar
1638 provided in the @semantic{} distribution in the file
1639 @file{semantic/wisent/java-tags.wy}.
1644 ;; Alternate entry points
1645 ;; - Needed by partial re-parse
1646 %start formal_parameter
1648 ;; - Needed by EXPANDFULL clauses
1649 %start formal_parameters
1652 formal_parameter_list
1654 (EXPANDFULL $1 formal_parameters)
1662 | formal_parameter COMMA
1663 | formal_parameter RPAREN
1667 : formal_parameter_modifier_opt type variable_declarator_id
1668 (VARIABLE-TAG $3 $2 nil :typemodifiers $1)
1674 It shows the use of the @code{EXPANDFULL} grammar macro to parse a
1675 @samp{PAREN_BLOCK} which contains a @samp{formal_parameter_list}.
1676 @code{EXPANDFULL} tells to recursively parse @samp{formal_parameters}
1677 inside @samp{PAREN_BLOCK}. The parser iterates until it digested all
1678 available input data inside the @samp{PAREN_BLOCK}, trying to match
1679 any of the @samp{formal_parameters} rules:
1686 @item @samp{formal_parameter COMMA}
1688 @item @samp{formal_parameter RPAREN}
1691 At each iteration it will return a @samp{formal_parameter} raw tag,
1692 or @code{nil} to skip unwanted (single @samp{LPAREN} or @samp{RPAREN}
1693 for example) or unexpected input data. Those raw tags will be
1694 automatically expanded by the iterative back-end parser.
1697 @subsection Bison style
1699 @cindex grammar bison style
1700 What we call the @dfn{Bison style} is the traditional style of Bison's
1701 grammars. Compared to iterative style, it is not straightforward to
1702 use grammars written in Bison style in @semantic{}. Mainly because such
1703 grammars are designed to parse the whole input data in one pass, and
1704 don't use the iterative parser back-end mechanism (@pxref{Iterative
1705 style}). With Bison style the parser is called once to parse the
1706 grammar start nonterminal.
1708 The following example is a snippet of the Bison style Java grammar
1709 provided in the @semantic{} distribution in the file
1710 @file{semantic/wisent/java.wy}.
1714 %start formal_parameter
1717 formal_parameter_list
1718 : formal_parameter_list COMMA formal_parameter
1725 : formal_parameter_modifier_opt type variable_declarator_id
1727 (VARIABLE-TAG $3 $2 :typemodifiers $1)
1733 The first consequence is that syntax errors are not automatically
1734 handled by @semantic{}. Thus, it is necessary to explicitly handle
1735 them at the grammar level, providing error recovery rules to skip
1736 unexpected input data.
1738 The second consequence is that the iterative parser can't do automatic
1739 tag expansion, except for the start nonterminal value. It is
1740 necessary to explicitly expand tags from concerned semantic actions by
1741 calling the grammar macro @code{EXPANDTAG} with a raw tag as
1742 parameter. See also @ref{Start nonterminals}, for incremental
1743 re-parse considerations.
1746 @subsection Mixed style
1748 @cindex grammar mixed style
1753 %start prologue epilogue declaration nonterminal rule
1768 SYMBOL COLON rules SEMI
1769 (TAG $1 'nonterminal :children $3)
1774 (apply 'nconc (nreverse $1))
1787 name type comps prec action elt)
1790 (TAG name 'rule :type type :value comps :prec prec :expr action)
1796 This example shows how iterative and Bison styles can be combined in
1797 the same grammar to obtain a good compromise between grammar
1798 complexity and an efficient parsing strategy in an interactive
1801 @samp{nonterminal} is parsed using iterative style via the main
1802 @samp{grammar} rule. The semantic action uses the @code{TAG} macro to
1803 produce a raw tag, automagically expanded by @semantic{}.
1805 But @samp{rules} part is parsed in Bison style! Why?
1807 Rule delimiters are the colon (@code{:}), that follows the nonterminal
1808 name, and a final semicolon (@code{;}). Unfortunately these
1809 delimiters are not @code{open-paren}/@code{close-paren} type, and the
1810 Emacs' syntactic analyzer can't easily isolate data between them to
1811 produce a @samp{RULES_PART} parenthesis-block-like lexical token.
1812 Consequently it is not possible to use @code{EXPANDFULL} to iterate in
1813 @samp{RULES_PART}, like this:
1818 SYMBOL COLON rules SEMI
1819 (TAG $1 'nonterminal :children $3)
1823 RULES_PART ;; @strong{Map a parenthesis-block-like lexical token}
1824 (EXPANDFULL $1 'rules)
1837 name type comps prec action elt)
1839 (TAG name 'rule :type type :value comps :prec prec :expr action)
1845 In such cases, when it is difficult for Emacs to obtain
1846 parenthesis-block-like lexical tokens, the best solution is to use the
1847 traditional Bison style with error recovery!
1849 In some extreme cases, it can also be convenient to extend the lexer,
1850 to deliver new lexical tokens, to simplify the grammar.
1852 @node Start nonterminals
1853 @subsection Start nonterminals
1855 @cindex start nonterminals
1856 @cindex @code{reparse-symbol} property
1857 When you write a grammar for @semantic{}, it is important to carefully
1858 indicate the start nonterminals. Each one defines an entry point in
1859 the grammar, and after parsing its semantic value is returned to the
1860 back-end iterative engine. Consequently:
1862 @strong{The semantic value of a start nonterminal must be a produced
1863 by a TAG like grammar macro}.
1865 Start nonterminals are declared by @code{%start} statements. When
1866 nothing is specified the first nonterminal that appears in the grammar
1867 is the start nonterminal.
1869 Generally, the following nonterminals must be declared as start
1873 @item The main grammar entry point
1878 @item nonterminals passed to @code{EXPAND}/@code{EXPANDFULL}
1880 These grammar macros recursively parse a part of input data, based on
1881 rules of the given nonterminal.
1883 For example, the following will parse @samp{PAREN_BLOCK} data using
1884 the @samp{formal_parameters} rules:
1888 formal_parameter_list
1890 (EXPANDFULL $1 formal_parameters)
1895 The semantic value of @samp{formal_parameters} becomes the value of
1896 the @code{EXPANDFULL} expression. It is a list of @semantic{} tags
1897 spliced in the tags tree.
1899 Because the automaton must know that @samp{formal_parameters} is a
1900 start symbol, you must declare it like this:
1904 %start formal_parameters
1910 @cindex incremental re-parse
1911 @cindex reparse-symbol
1912 The @code{EXPANDFULL} macro has a side effect it is important to know,
1913 related to the incremental re-parse mechanism of @semantic{}: the
1914 nonterminal symbol parameter passed to @code{EXPANDFULL} also becomes
1915 the @code{reparse-symbol} property of the tag returned by the
1916 @code{EXPANDFULL} expression.
1918 When buffer's data mapped by a tag is modified, @semantic{}
1919 schedules an incremental re-parse of that data, using the tag's
1920 @code{reparse-symbol} property as start nonterminal.
1922 @strong{The rules associated to such start symbols must be carefully
1923 reviewed to ensure that the incremental parser will work!}
1925 Things are a little bit different when the grammar is written in Bison
1928 @strong{The @code{reparse-symbol} property is set to the nonterminal
1929 symbol the rule that explicitly uses @code{EXPANDTAG} belongs to.}
1938 name type comps prec action elt)
1941 (TAG name 'rule :type type :value comps :prec prec :expr action)
1947 Set the @code{reparse-symbol} property of the expanded tag to
1948 @samp{rule}. A important consequence is that:
1950 @strong{Every nonterminal having any rule that calls @code{EXPANDTAG}
1951 in a semantic action, should be declared as a start symbol!}
1953 @node Useful functions
1954 @subsection Useful functions
1956 Here is a description of some predefined functions it might be useful
1957 to know when writing new code to use Wisent in @semantic{}:
1959 @findex wisent-collect-unmatched-syntax
1960 @defun wisent-collect-unmatched-syntax input
1961 Add @var{input} lexical token to the cache of unmatched tokens, in
1962 variable @code{semantic-unmatched-syntax-cache}.
1964 See implementation of the function @code{wisent-skip-token} in
1965 @ref{Error recovery}, for an example of use.
1969 @section The Wisent Lex lexer
1971 @findex semantic-lex
1972 The lexical analysis step of @semantic{} is performed by the general
1973 function @code{semantic-lex}. For more information, @inforef{Writing
1974 Lexers, ,semantic-langdev}.
1976 @code{semantic-lex} produces lexical tokens of the form:
1980 @code{(@var{token-class start} . @var{end})}
1986 Is a symbol that identifies a lexical token class, like @code{symbol},
1987 @code{string}, @code{number}, or @code{PAREN_BLOCK}.
1991 Are the start and end positions of mapped data in the input buffer.
1994 The Wisent's parser doesn't depend on the nature of analyzed input
1995 stream (buffer, string, etc.), and requires that lexical tokens have a
1996 different form (@pxref{Writing a lexer}):
2000 @code{(@var{token-class value} [@var{start} . @var{end}])}
2004 @cindex lexical token mapping
2005 @code{wisent-lex} is the default Wisent's lexer used in @semantic{}.
2007 @vindex wisent-lex-istream
2010 Return the next available lexical token in Wisent's form.
2012 The variable @code{wisent-lex-istream} contains the list of lexical
2013 tokens produced by @code{semantic-lex}. Pop the next token available
2014 and convert it to a form suitable for the Wisent's parser.
2017 Mapping of lexical tokens as produced by @code{semantic-lex} into
2018 equivalent Wisent lexical tokens is straightforward:
2022 (@var{token-class start} . @var{end})
2023 @result{} (@var{token-class value start} . @var{end})
2027 @var{value} is the input @code{buffer-substring} from @var{start} to
2030 @node GNU Free Documentation License
2031 @appendix GNU Free Documentation License
2033 @include doclicense.texi
2046 @c Following comments are for the benefit of ispell.
2048 @c LocalWords: Wisent automagically wisent Wisent's LALR obarray