ã¯ããã«
ããã«ã¡ã¯
ãã·ã¢èªã§ã¯ããã®ã¢ã«ãŽãªãºã ã®ç°¡åã§ãããããã説æã¯èŠã€ãããŸããã§ããã ãã®ã®ã£ãããåããããšã«ããŸããã ãŸããããã¯äœã§ããïŒ LRïŒ0ïŒããŒãµãŒã¯ãäž»ã«ããŒãµãŒã§ãã ããŒãµãŒã®ç®çã¯ãããŒã¯ã³ã®å ¥åã¹ããªãŒã ïŒåå¥ã¢ãã©ã€ã¶ãŒãå ¥åæåã¹ããªãŒã ã«åºã¥ããŠçæããèšèªã®åºæ¬èŠçŽ ãããŒã¯ã³ã®äŸã¯æ°åãã³ã³ããæåïŒãåŠçãããããç¹å®ã®åœ¢åŒã§æå®ãããèšèªã®èª¬æãšæ¯èŒããããšã§ãã æ¯èŒã¯ãç¹å®ã®ããŒã¿æ§é ïŒã»ãšãã©ã®å ŽåããªãŒïŒã®æ§ç¯ã§æ§æãããŸãã ããã«ããã®æ§é ã¯æ¬¡ã®æ®µéã§ããã»ãã³ãã£ãã¯åæã«é²ã¿ãã³ã³ãã€ã©ã¯ãã§ã«ããªãŒã®æå³ãç解ããããšããŠããŸãã
ããŒãµãŒã«ã¯ãã¢ããã¹ããªãŒã ãšããŠã³ã¹ããªãŒã ã®2ã€ã®ã¯ã©ã¹ããããŸãã åè ã¯ãå ¥åããŒã¯ã³ã§ããèããå§ãŸãããªãŒãæ§ç¯ããŸãããåŸè ã¯ãéã«ãããªãŒã®ã«ãŒãããå§ãŸããŸãã å®éãLRã¯ãã¢ãã©ã€ã¶ãŒãã¹ããªãŒã ãå·Šããå³ïŒL-ãå·ŠãïŒã«èªã¿åããäžããäžã«ããªãŒãæ§ç¯ããããšãæå³ããŸãïŒæ··ä¹±ãèµ·ãããã«å³ãæå³ããæåRãå ¥åããŸãïŒã ã€ã³ããã¯ã¹0ã¯ã次ã®ããŒã¯ã³ããã¬ãã¥ãŒãããçŸåšã®ããŒã¯ã³ã§ã®ã¿æ©èœããããšãæå³ããŸãã ãã®ã¿ã€ãã®ã¢ãã©ã€ã¶ãŒãéžæããå©ç¹ã¯äœã§ããïŒ
- 圌ã¯éãã§ãã
- å€ãã®èšèªãã«ããŒããŠããŸãã ã€ãŸããèšèªãçºæããŠèšè¿°ããå ŽåãLRã¢ãã©ã€ã¶ãŒã¯é«ã確çã§ãã®èšèªãåŠçã§ããŸãã
- æ§æãšã©ãŒã¯ã§ããã ãæ©ãæ€åºãããŸãã åã®å ¥åã¹ããªãŒã ãšäžèŽããªãæåãèŠã€ãããšããã«ãããã«é¢ãããšã©ãŒãåºåã§ããŸãã
æ¬ ç¹ããããŸãïŒ
- æ§é ã®çžå¯Ÿçãªè€éãã
- èšèªã®èšè¿°ã®ãããŸããã«ãã£ãŠãã¢ãã©ã€ã¶ãŒãæ··ä¹±ç¶æ ã«è¿œã蟌ãããšãã§ããŸãã
çšèª
端æ«èšå· ïŒ terminal ïŒã¯ããŠãŒã¶ãŒãã¢ãã©ã€ã¶ãŒã«äžããããšãã§ããèšå·ã§ãããã®å Žåããããã¯ããŒã¯ã³ã§ãã
éçµç«¯ èšå· ïŒ nonterminal ã nonterminal ïŒ-èšèªã®ãªããžã§ã¯ãã瀺ãèšå·ã ããšãã°ãèšå·Aã¯çšèªã§ãããšããŸãããã ãã¡ãããè€æ°æåã®ååïŒAã§ã¯ãªãtermïŒãéžæã§ããŸãã
æèèªç±ææ³ ïŒ CFG ïŒ-ãã©ãŒã ã®ã«ãŒã«ã®ã»ãã ããã§ãAã¯é端æ«ãwã¯ç«¯æ«ãšé端æ«ã®ä»»æã®ã³ã¬ã¯ã·ã§ã³ã§ãã ãã®èšäºã§ã¯ããã®æèã®ãªãæå³ã§ãåã«ææ³ã䜿çšããŸãã
ã¢ãã©ã€ã¶ãŒãäœæããææ³ã®å°ããªäŸïŒ
ãã®ææ³ã¯ã0ãš1ã®2ã€ã®æ°å€ã«å¯Ÿããäžå®å šãªç®è¡æŒç®ã®ã»ãããèšè¿°ããŠããŸããææ³ã¯ãèšèªã®èšè¿°ã§ãã å ¥åã¹ããªãŒã ãç§ãã¡ã®èšèªã«å±ããŠããããã©ããã§æ§æãšã©ãŒïŒ1 + 1ã®ä»£ããã«1+ãæžããïŒãã©ããããã§ãã¯ããããã«ãéå§ã®éçµç«¯ããå§ãŸããã®èŠåã«åŸã£ãŠå ¥åã¹ããªãŒã ãååŸããæ¹æ³ãæ¢ããŠããŸãïŒEããããŸãïŒã 1 + 1ã®å Žåããã¹ã¯ãã®ãããªEã«ãªããã«ãŒã«2ãE + Fãã«ãŒã«1ãF + Fãã«ãŒã«4ãT + Fãã«ãŒã«8ã1 + Fãåã³4ã1 + TãæåŸã«8çªç®ãé©çšãã 1 +1ãã芧ã®ãšãããå ¥åæååãååŸã§ããŸãããããã¯ãæ§æçã«æ£ããããšãæå³ããŸãã
ããã§ãã¢ãã©ã€ã¶ãŒã®ååã®Rãšããæåã説æã§ããŸãã ããã¯ãã«ãŒã«ã®æ¥µå³éšåããå ¬çã«ç§»ããã€ãŸããå ã®ïŒ1ïŒãåéããããåçŽãªã«ãŒã«ïŒ7ããã³8ïŒããé²ãããšãæå³ããŸãã Lã¢ãã©ã€ã¶ãŒïŒLLïŒã¯ãã«ãŒã«ã®å·ŠåŽã®éšåãã次ã®åææ¹åãéžæããããšããŸãã
æéç¶æ ãã·ã³ ïŒ FSM ïŒã«ã€ããŠãèšåãã䟡å€ããããŸãã ããã¯ãäžé£ã®ç¶æ ãšå ¥åã¹ããªãŒã ãæã€ã¢ãã«ã§ãã ãã·ã³ã¯åæç¶æ ããèµ·åããçŸåšã®ã·ã³ãã«ãšå ¥åã·ã³ãã«ã«åºã¥ããŠç¶æ ãå€æŽããŸãã ã€ãŸããç¶æ 0ããéå§ããaãå ¥åã«å ¥ããšããªãŒãããã³ã¯ç¶æ 1ã«ãªããbãç¶æ 2ã«ãªããŸããé·ç§»ã®ä»çµã¿ã¯ãè¡šã«ãã£ãŠäžããããŸããåã¯çŸåšã®ç¶æ ãåã¯å ¥åèšå·ã§ãã
ã¢ã«ãŽãªãºã
ã¢ãã©ã€ã¶ãŒãæ©èœãããã«ã¯ãããã€ãã®ããšãå¿ èŠã§ãã
- åæããå ¥åã¹ããªãŒã èªäœã
- ããŒãµãŒã¹ããŒãã®ã¹ã¿ãã¯ïŒLIFOïŒLast In First OutïŒã«ãŒã«ã«æºæ ããããŒã¿æ§é ïŒã¯ãã·ãŒãã®ã¹ã¿ãã¯ãšããŠæ³åããã®ãæãç°¡åã§ã-èãã¹ã¿ãã¯ã®æåŸã«çœ®ããã¹ã¿ãã¯ã®èŠçŽ ãå¿ èŠãªãšãã«æåã«ååŸããŸãïŒã
- ã¢ã¯ã·ã§ã³ããŒãã«ã 圌女ã¯ãçŸåšã®ç¶æ ã§ãå ¥åã§çŸåšã®ãã£ã©ã¯ã¿ãŒãã©ãããããæããŠãããŸãã
- å€æè¡š ã¢ã¯ã·ã§ã³ã®1ã€ã§äœ¿çšãããè£å©ããŒãã«ã
ããã§ã¯ãã¢ãã©ã€ã¶ãŒãã©ã®ããã«æ©èœããããæ確ã«ããå¿ èŠããããŸãã çŸåšã®ç¶æ ã¯ãã¹ã¿ãã¯ã®äžçªäžã®ç¶æ ã§ãã ã¢ã¯ã·ã§ã³ã®ããŒãã«ãèŠãŠãã ããïŒã€ã³ããã¯ã¹ã¯çŸåšã®å ¥åã·ã³ãã«ãšçŸåšã®ç¶æ ã§ãïŒã ãã®ããŒãã«ã«ã¯4çš®é¡ã®ã¬ã³ãŒãããããŸãã
- successïŒacceptedïŒ-å ¥åè¡ããã®ææ³ã«å±ããŠããããšãæå³ããŸãã
- voidïŒãšã©ãŒïŒ-ã¢ã¯ã·ã§ã³ã¯ãããŸãããè¡ãè©°ãŸã£ãŠããŸãããŠãŒã¶ãŒã¯çŸåšã®ãã£ã©ã¯ã¿ãŒãééããŸããã
- transferïŒshiftïŒ-ã¹ã¿ãã¯ã®äžçªäžã«ãå ¥åæåã«å¯Ÿå¿ããç¶æ ãé 眮ãã次ãèªã¿åããŸã
- ãªãã¯ã·ã§ã³ïŒreduceïŒ-ææ³èŠåã䜿çšããŠç¶æ ã眮ãæããããšãã§ããã¹ã¿ãã¯ç¶æ ããããŸããããã§ã¯ãé·ç§»ããŒãã«ã®å€ãååŸããŸãã æåã®ã€ã³ããã¯ã¹ã¯çŸåšã®ç¶æ ã§ãã 2çªç®ã¯ãã«ãŒã«ã®å·ŠåŽã§ãã ã€ãŸããç¶æ ã®ã·ãŒã±ã³ã¹ãå€æãããã®ã§ãã
ã³ãŒã圢åŒã§ã¯ã次ã®ããã«ãªããŸãã
- ã¹ã¿ãã¯ã ããã·ã¥ ïŒç¶æ [ 0 ] ïŒ ;
- while ïŒ ïŒãåãå ¥ããããŸãïŒ
- {
- ç¶æ * st =ã¹ã¿ãã¯ã ããã ïŒ ïŒ ;
- æ«ç«¯é = s [ inp_pos ] ;
- if ïŒ ïŒ termsãIsTerm ïŒ term ïŒ ïŒ
- ãšã©ãŒïŒ ïŒ ;
- ã¢ã¯ã·ã§ã³*ã¢ã¯ã·ã§ã³= actionTableã Get ïŒ stãterm ïŒ ;
- if ïŒ ïŒã¢ã¯ã·ã§ã³ïŒ
- ãšã©ãŒïŒ ïŒ ;
- ã¹ã€ãã ïŒã¢ã¯ã·ã§ã³- >ã¿ã€ãïŒ ïŒ ïŒ
- {
- ã±ãŒã¹ ActionAccept ïŒ
- åãå ¥ãããã= true ;
- äŒæ© ;
- case ActionShift ïŒ
- inp_pos ++ ;
- ã¹ã¿ãã¯ã ããã·ã¥ ïŒã¢ã¯ã·ã§ã³- >ç¶æ ïŒ ïŒ ïŒ ;
- äŒæ© ;
- ã±ãŒã¹ ActionReduce ïŒ
- ã«ãŒã«*ã«ãŒã«=ã¢ã¯ã·ã§ã³- >ã«ãŒã«ïŒ ïŒ ;
- ã¹ã¿ãã¯ã pop ïŒã«ãŒã«- >ãµã€ãºïŒ ïŒ ïŒ ;
- ç¶æ * transferState = transferTableã Get ïŒã¹ã¿ãã¯ã ããã ïŒ ïŒ ãã«ãŒã«- >å·ŠïŒ ïŒ ïŒ ;
- if ïŒ ïŒ transferState ïŒ
- ãšã©ãŒïŒ ïŒ ;
- ã¹ã¿ãã¯ã push ïŒ transferState ïŒ ;
- äŒæ© ;
- }
- }
ã芧ã®ãšãããåæèªäœã«è€éãªããšã¯ãããŸããã ãã ããå šäœã®ããªãã¯ã¯ããããã®ããªãããŒãªããŒãã«ãäœæããããšã§ãã ãŸããããŒãµãŒã®ç¶æ ãææ¡ããŸãããã ããã¯ãã¢ã«ãŽãªãºã ã®ããªãéèŠãªéšåã§ãã ããããããã¯åãªãæ°åã§ã¯ãããŸããã ããã€ãã®æ°ããæŠå¿µãå°å ¥ããå¿ èŠããããŸãã
ãŸãããããã¯ã¢ã€ãã ã§ãã ããã¯ãããŒã«ãŒãšããæ°ããããããã£ãæã€ã«ãŒã«ã§ãã ããŒã«ãŒã¯ãçŸåšæåŸ ããŠããèŠçŽ ã瀺ããŸãã ãããã£ãŠãåã«ãŒã«ã¯n + 1åã®ããŒã¯ã³ãçæããŸããnã¯ã«ãŒã«ã®å³åŽã®æåæ°ã§ãã ããšãã°ãã«ãŒã«3ã䜿çšããŸããåã®äžã®ãã©ã¹èšå·ã¯ãããŒã«ãŒã®å Žæã瀺ããŸãã
ããšãã°ã2çªç®ã®æ®µèœã®ããŒã«ãŒã¯ãçŸåšã®æåã«ãã€ãã¹èšå·ã衚瀺ãããããšã瀺ããŠããŸãã è€æ°ã®ã¢ã€ãã ãçµã¿åããããšãã¢ã€ãã ã®ã»ããã«ãªããŸãã å®éã«ã¯ãç¶æ ã¯ç¹å®ã®æ¹æ³ã§äžç·ã«çµã¿ç«ãŠãããã¢ã€ãã ã®ã»ããã§ãã
ãã ããç¶æ ãæäœããã«ã¯ãæåã«ã»ãããéããå¿ èŠããããŸãã ããã¯ãå®å šãªã¢ãã©ã€ã¶ãŒãã©ã³ããååŸããããšãæå³ããŸãã ã€ãŸããã»ããå ã«ããŒã«ãŒãéçµç«¯ç¹ãæããã€ã³ããããå ŽåïŒãããŠããã®å Žåããã¹ãŠã®éçµç«¯ç¹ã¯å·ŠåŽã®éšåã§ãïŒã察å¿ããéçµç«¯ç¹ãããããã€ãããå¿ èŠããããŸãã ããã¯ãå·Šéšåããã®éçµç«¯ã§ããã¢ã€ãã ãè¿œå ããã ãã§èµ·ãããããŒã«ãŒã¯æåã®æåãæããŸãã åç¬ã§ãååž°çã«å±éããŸããæ°ããè¿œå ããã段èœã§æåã®æåãéçµç«¯èšå·ã§ããå Žåã¯ãéããŸãã ãã«ã»ãããååŸãããŸã§ã ãã€ã³ãã1ã€ãããªãã»ããïŒåã®äŸã§ã¯3çªïŒãéããŸãã
Fãå±éãããšããã€ã³ã2ã3ã4ãåŸãããŸãã3ãš4ã§ã¯ãåã³Fãå±éããããææ¡ãããŸããããããã®ã«ãŒã«ã¯æ¢ã«ã»ããã«ããã®ã§ãã¹ãããããŸãã ããããTããããã€ãããŠããªãããã5ãš6ãåŸãããŸãããã¹ãŠãã¯ããŒãžã£ãŒã®æºåãã§ããŸããã
- for ïŒ itemsetã®closed_item ïŒ
- {
- if ïŒ closed_itemãisClose ïŒ
- ç¶ãã ;
- èŠçŽ ããŒã«ãŒ= closed_itemã ããŒã«ãŒ ïŒ ïŒ ;
- if ïŒããŒã«ãŒã ã¿ã€ã ïŒ ïŒ ïŒ = ElementNonTerm ïŒ
- {
- closed_itemã isClose = true ;
- ç¶ãã ;
- }
- NonTerminal nonTerm =ããŒã«ãŒã NonTerm ïŒ ïŒ ;
- item = allitems- > First ïŒ 0 ãnonTerm ïŒ ;
- while ïŒ ïŒ itemãisend ïŒ ïŒ ïŒ
- {
- if ïŒ ïŒ itemsetãexists ïŒ item ïŒ ïŒ
- ã¢ã€ãã ã»ããã è¿œå ïŒã¢ã€ãã ïŒ ;
- ã¢ã€ãã ã next ïŒ 0 ãnonTerm ïŒ ;
- }
- closed_itemã isClose = true ;
- }
ç¶æ ãäœã§ããããç解ãããããããã®æ§ç¯ãéå§ã§ããŸãã æåã«ãåºåã®åºç€ã§ãããæåŸã«å°éããå¿ èŠãããæ°ããã«ãŒã«ãå°å ¥ããŸãã
ãã¡ãããæåã®ç¶æ ã¯ããã®ã«ãŒã«ã«åºã¥ããŠãããŒã«ãŒãEã«åããŠã¢ã€ãã ãéããããšã§ããããã§ãäžæçãªæéç¶æ ãã·ã³ã®ããŒãã«ã®æ§ç¯ãéå§ããŸããããã¯ãé·ç§»ããŒãã«ãšã¢ã¯ã·ã§ã³ããŒãã«ã®åºç€ãšããŠæ©èœããŸãã ããŒã«ãŒãæãã·ã³ãã«ã®åºæºã«åŸã£ãŠãç¶æ ãã°ã«ãŒãã«åããŸãã ãã®äŸã®ã¯ããŒãžã£ã§ã¯ãFã°ã«ãŒããTã°ã«ãŒãã0ã°ã«ãŒãã1ã°ã«ãŒãã®4ã€ã®ã°ã«ãŒãããããŸãã åã°ã«ãŒãã¯ãæ°ããç¶æ ãžã®ç§»è¡ã§ãã é·ç§»ããã®æåã®ã€ã³ããã¯ã¹ã¯ãå®éã«ã°ã«ãŒãåããã·ã³ãã«ïŒFãTã0ã1ïŒã§ãã 2çªç®ã®ã€ã³ããã¯ã¹ã¯çŸåšã®ç¶æ ã§ãã ãããŠãããŒãã«å ã®å€ã¯ãæž¡ãç¶æ ã§ãã ãããã£ãŠã4ã€ã®æ°ããç¶æ ãååŸããŸãã ããããæ§ç¯ããã®ã¯éåžžã«ç°¡åã§ã-åãã€ã³ãã§ã°ã«ãŒãå ã§ondäœçœ®ã®ããŒã«ãŒãå³ã«ç§»åããçµæã»ãããéããŸãã ããã¯æ°ããç¶æ ã«ãªããŸãã
- firstStateã è¿œå ïŒé ç®ã æå ïŒ ïŒ ïŒ ;
- firstStateã MakeClosure ïŒ ïŒ ;
- ç¶æ ã add ïŒ firstState ïŒ ;
- size_t state_idx = 0 ;
- while ïŒ state_idx < statesãsize ïŒ ïŒ ïŒ
- {
- ç¶æ * st =ç¶æ [ state_idx ] ;
- GroupedItems group = st- > Group ïŒ ïŒ ;
- for ïŒ groupã®group_class ïŒ
- {
- if ïŒ group_class- > firstã ã¿ã€ã ïŒ ïŒ == ElementEnd ïŒ
- ç¶ãã ;
- State newState ïŒ ïŒ itemsãstatesãSize ïŒ ïŒ ïŒ ;
- for ïŒ group_classã®group_item ïŒ
- newStateã è¿œå ïŒ group_itemãgroup_itemãMarkerInt ïŒ ïŒ + 1 ïŒ ;
- newStateã MakeClosure ïŒ ïŒ ;
- ç¶æ oldState =ç¶æ ã æ€çŽ¢ ïŒ newState ïŒ
- if ïŒ ïŒ oldState ïŒ
- {
- ç¶æ ã add ïŒ newState ïŒ ;
- fsmTableã Add ïŒ stãgroup_class- > firstãnewState ïŒ ;
- }
- ä»ã«
- fsmTableã Add ïŒ stãgroup_class- > firstãoldState ïŒ ;
- }
- state_idx ++ ;
- }
é·ç§»ããŒãã«ã¯éåžžã«ç°¡åã«æ§ç¯ãããŸã-ã€ã³ããã¯ã¹ãéçµç«¯ã§ããFSMããŒãã«ããåã転éããŸãã
ã¢ã¯ã·ã§ã³ããŒãã«ã¯ããå°ãèå³æ·±ããã®ã§ãã ãŸããããŒãã¯FSMããã¿ãŒããã«ã€ã³ããã¯ã¹ä»ãã®åã«è»¢éãããŸãããå ã®å®å®è¹ããŒãã«ã«èšé²ãããç¶æ ãã©ã¡ãŒã¿ãŒã䜿çšããã·ããã¢ã¯ã·ã§ã³ã¯ããŒãã«ã»ã«ã«æžã蟌ãŸããŸãã 次ã«ãå ¥åè¡ã®çµããã瀺ãæ°ããåã$ããè¿œå ããŸãã ãã®åã«ã¯ãåãå ¥ããããã€ãã³ããå ¥åããŸããããã¯ãã€ã³ããã¯ã¹ç¶æ ã«ã¢ã€ãã ãå«ãŸããŠããå Žåã«æžã蟌ãŸããŸã ã ããã¯æåãæå³ããäž»èŠãªã«ãŒã«ã«å€ãããåæã«å ¥åã¹ããªãŒã ãçµäºããŸãã 次ã«ãç³ã¿èŸŒã¿ã¢ã¯ã·ã§ã³ããããŸãã ã¢ã€ãã ãããåå·ã«ã€ã㊠ãããã§wã¯ç«¯æ«ãšé端æ«ã®ä»»æã®çµã¿åããã§ãè¡å šäœïŒãã¡ãããä»ã®ã³ãã³ãã§å æãããŠããªãããªãŒã»ã«ã®ã¿ãæå³ããŸãïŒã®reduceã³ãã³ããšããã®ã¢ã€ãã ãå±ãã察å¿ããã«ãŒã«ã®ãã©ã¡ãŒã¿ãŒãèšè¿°ããŸãã
- fsmTableã FeedTransferTable ïŒ transferTable ïŒ ;
- fsmTableã FeedActionTable ïŒ actionTable ïŒ ;
- ã¢ã€ãã endItem =ã¢ã€ãã ã GetItem ïŒ 1 ã 'S' ãElements ïŒ "E" ãnonTerms ïŒ ïŒ ;
- for ïŒ st in states ïŒ
- if ïŒ stãHaveItem ïŒ endItem ïŒ ïŒ
- actionTableã Add ïŒ stã '$' ã new Action ïŒ ïŒ ïŒ ;
- for ïŒ st in states ïŒ
- {
- ItemListãªã¹ã= stã GetReducable ïŒ ïŒ ;
- for ïŒãªã¹ãå ã®listItem ïŒ
- actionTableã Add ïŒ stã new Action ïŒ listItemãGetRule ïŒ ïŒ ïŒ ïŒ ;
- }
æåŠ
ã³ã³ãã€ã©ïŒååããã¯ããã¯ãããã³ããŒã«ã1986幎ãã¢ã«ãã¬ããV.ã¢ããã©ãã»ãã£ããžã§ããªãŒD.ãŠã«ãã³