1 (**************************************************************************)
5 (* François Pottier, INRIA Rocquencourt *)
6 (* Yann Régis-Gianas, PPS, Université Paris Diderot *)
8 (* Copyright 2005-2008 Institut National de Recherche en Informatique *)
9 (* et en Automatique. All rights reserved. This file is distributed *)
10 (* under the terms of the GNU Library General Public License, with the *)
11 (* special exception on linking described in file LICENSE. *)
13 (**************************************************************************)
15 (* This module instantiates the generic [Engine] with a thin decoding layer
16 for the generated tables. Like [Engine], it is part of [MenhirLib]. *)
18 (* The exception [Accept] is pre-declared here: this obviates the need
19 for generating its definition. The exception [Error] is declared
20 within the generated parser. This is preferable to pre-declaring it
21 here, as it ensures that each parser gets its own, distinct [Error]
22 exception. This is consistent with the code-based back-end. *)
24 exception Accept
of Obj.t
26 (* This functor is invoked by the generated parser. *)
28 module Make
(T
: TableFormat.TABLES
)
59 let default_reduction state defred nodefred env
=
60 let code = PackedIntArray.get
T.default_reduction state
in
66 (* This auxiliary function helps access a compressed, two-dimensional
67 matrix, like the action and goto tables. *)
69 let unmarshal2 table i j
=
70 RowDisplacement.getget
76 (* This auxiliary function helps access a flattened, two-dimensional
77 matrix, like the error bitmap. *)
79 let unflatten (n
, data
) i j
=
80 PackedIntArray.get1 data
(n
* i
+ j
)
82 let action state terminal
value shift reduce fail env
=
83 match unflatten T.error state terminal
with
85 let action = unmarshal2 T.action state terminal
in
86 let opcode = action land 0b11
87 and param
= action lsr 2 in
88 if opcode >= 0b10 then
89 (* 0b10 : shift/discard *)
90 (* 0b11 : shift/nodiscard *)
91 let please_discard = (opcode = 0b10) in
92 shift env
please_discard terminal
value param
95 (* 0b00 : cannot happen *)
101 let goto state prod
=
102 let code = unmarshal2 T.goto state
(PackedIntArray.get
T.lhs prod
) in
103 (* code = 1 + state *)
112 type semantic_action
=
113 (state
, semantic_value
, token
) EngineTypes.env
-> unit
115 let semantic_action prod
=
116 T.semantic_action.(prod
)
128 fprintf stderr
"State %d:\n%!" state
132 let shift terminal
state =
134 | Some
(terminals
, _
) ->
135 fprintf stderr
"Shifting (%s) to state %d\n%!" terminals
.(terminal
) state
139 let reduce_or_accept prod
=
141 | Some
(_
, productions
) ->
142 fprintf stderr
"%s\n%!" productions
.(prod
)
146 let lookahead_token lexbuf token
=
148 | Some
(terminals
, _
) ->
149 fprintf stderr
"Lookahead token is now %s (%d-%d)\n%!"
151 lexbuf
.Lexing.lex_start_p
.Lexing.pos_cnum
152 lexbuf
.Lexing.lex_curr_p
.Lexing.pos_cnum
156 let initiating_error_handling () =
159 fprintf stderr
"Initiating error handling\n%!"
163 let resuming_error_handling () =
166 fprintf stderr
"Resuming error handling\n%!"
170 let handling_error state =
173 fprintf stderr
"Handling error in state %d\n%!" state
177 let discarding_last_token token
=
179 | Some
(terminals
, _
) ->
180 fprintf stderr
"Discarding last token read (%s)\n%!" terminals
.(token
)