wisent.texi 57 KB

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