123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129(******************************************************************************)(* *)(* Menhir *)(* *)(* Copyright Inria. All rights reserved. This file is distributed under *)(* the terms of the GNU Library General Public License version 2, with a *)(* special exception on linking, as described in the file LICENSE. *)(* *)(******************************************************************************)openTableFormatmodule[@inline]Make(T:TABLES)=structtypestate=inttypetoken=T.tokentypenonterminal=inttypesemv=Obj.ttypeproduction=intexceptionError=T.ErrormoduleSemv=structlettoken2value=T.token2valueletmerge=T.mergeendmoduleProduction=structletstart=T.startletlength=T.lengthletlhs=T.lhslet[@inline]actionprod=(* Indexing into the array [T.semantic_action] is off by [T.start],
because the start productions do not have entries in this array. *)T.semantic_action.(prod-T.start)letprint_prod="<cannot show production>"end(* Production *)moduleState=structletn=T.n(* Decoding the default reduction table. *)let[@inline]decode_DefRedc=c-1letdr_NoDefRed=0(* Decoding the shift bitmap. *)letsb_DoNotFail=0(* Decoding the shift table. *)lets_Fail=0let[@inline]decode_Shiftc=c-1letforeach_shift(s:state)inputyield=(* First determine whether the state [s] has a default reduction. *)(* If there is one, then there are no transitions. *)ifT.default_reductions=dr_NoDefRedthen(* There is no default reduction. Request the lookahead symbol. *)lettoken=Input.lookaheadinputinlett=T.token2terminaltokenin(* Consult the shift bitmap. *)ifT.shift_bitmapst=sb_DoNotFailthen(* The shift bitmap says "go". Consult the shift table. *)letc=T.shiftstinassert(c<>s_Fail);lets'=decode_Shiftcin(* Yield this transition. *)yields'letforeach_reduction(s:state)inputyield=(* First determine whether the state [s] has a default reduction. *)letc=T.default_reductionsinifc<>dr_NoDefRedthenyield(decode_DefRedc)else(* There is no default reduction. Request the lookahead symbol. *)lettoken=Input.lookaheadinputinlett=T.token2terminaltokenin(* Consult the reductions table. *)List.iteryield(T.reductionsst)(* Decoding the goto table. *)letencode_NoGoto=0letdecode_Gotoc=c-1let[@inline]gotostatent=letc=T.gotostatentinassert(c>encode_NoGoto);decode_Gotoc(* Decoding the unique action bitmap. *)letuab_Fail=1(* Decoding the unique action table. *)letua_ReduceOpcode=0b01letua_ShiftOpcode=0b10letua_Fork=0b11letua_Fail=0b00letunique_action(s:state)input=(* First determine whether the state [s] has a default reduction. *)letc=T.default_reductionsinifc<>dr_NoDefRedthen`Reduce(decode_DefRedc)else(* There is no default reduction. Request the lookahead symbol. *)lettoken=Input.lookaheadinputinlett=T.token2terminaltokenin(* Consult the unique action bitmap. *)ifT.unique_action_bitmapst=uab_Failthen`Failelse(* Consult the unique action table. *)letaction=T.unique_actionstinassert(action<>ua_Fail);ifaction=ua_Forkthen`Forkelseletopcode=actionland0b11andparam=actionlsr2inassert(opcode=ua_ShiftOpcode||opcode=ua_ReduceOpcode);ifopcode=ua_ShiftOpcodethen`Shiftparamelse`Reduceparamend(* State *)end