ããã«æçš¿ïŒ
- ã·ã§ãããã·ã³
- æ£åŒãªææ³
- LR解æ
ã·ã§ãããã·ã³
ååãæ£èŠè¡šçŸã®åé¡ãç¹å®ãããã®äžã«ããã¹ãã解æããããã®DFAãæ§ç¯ããŸããããã©ã±ããããã¹ãã®åãã¬ãã«ã«ãããã©ãããå€æããããšã¯äžå¯èœã§ãã æ£èŠè¡šçŸã䜿çšãããšãããã«çãåé¡ã§ãã解決ããããšã¯äžå¯èœã§ãã {*} *ã®ãããªæååããã§ãã¯ããŠãæ°éã{ quantity }ãšçãããã©ããã確èªããŸãã ãããšã¯éã«ããã®ãããªæ£èŠè¡šçŸãäœæã§ãããšããŸãã ãããã£ãŠãããããDFAãæ§ç¯ããããšãã§ããŸããããã«ãããé©åãªè¡ãšäžé©åãªè¡ãåºå¥ãããŸãã çµæã®DFAã«Nåã®ç¶æ ãããå Žåãã¿ã€ã{ N +1 ïŒã€ãŸã " N +1å{ "ïŒã®è¡ã§ããªãŒãããã³ã¯ããã€ãã®ç¶æ ïŒãã«ãŒããïŒã§è€æ°å蚪åããŸãã ãã®ãããè¡ã®å é ã«æ°ããæå{ãè¿œå ãããšãåããµã€ã¯ã«ã§ããäžåºŠãã·ã³ãå®è¡ã§ããŸã-眮æã«æ°ä»ããªãã§ãããã ãã®çµæããã·ã³ã¯é©åãªã©ã€ã³{ N + 1 } N + 1ãšäžé©åãªã©ã€ã³{ N + M + 1 } N + 1ãåºå¥ã§ããªããªããŸãã ïŒ Mã¯ãã·ã³ã§æ€åºãããã«ãŒãã®é·ãã§ããããã¯{è¡ã®å é ã«è¿œå ãããŸããïŒ
èšãæããã°ãDFAã¯ååãªã¡ã¢ãªããªãããããã¹ããããæ§é ãèªèã§ããŸãããèŠããŠããã®ã¯1ãN ïŒçŸåšã®ç¶æ çªå·ïŒã®å€ã ãã§ãã ãã倧容éã®ãã·ã³ãå¿ èŠã§ããããã®ã¡ã¢ãªã¯å¿ èŠã«å¿ããŠç¡å¶éã«æ¡å€§ã§ããŸãã
çŸåšã®ç¶æ ã«å ããŠã ã¹ãã¢ã¡ã¢ãªãåãããªãŒãããã³ã«ã¯ãæåãæžã蟌ãããšãã§ããã¹ã¿ãã¯ããããŸãã åã¹ãããã§ããã·ã³ã¯æ¬¡ã®å ¥åæåãšã¹ã¿ãã¯ã®å é æåãèªã¿åããŸãã ããªãã«ïŒçŸåšã®ç¶æ ãå ¥åã·ã³ãã«ãã¹ã¿ãã¯ããã®ã·ã³ãã«ïŒã«åŸã£ãŠããã·ã³ã¯ãèªã¿åãã·ã³ãã«ã®ä»£ããã«åãæ¿ããç¶æ ãšã¹ã¿ãã¯ã«æžã蟌ããã®ãéžæããŸãã ã¹ã¿ãã¯ãžã®æžã蟌ã¿ã¯ãªãã·ã§ã³ã§ããèªã¿åãæåãããããåçŽã«æ¶å»ã§ããŸãã ãã®ãããåäœäžã®ã¹ã¿ãã¯ã¯æ¡å€§ãšçž®å°ã®äž¡æ¹ãè¡ãããšãã§ããŸãã
çšèªã«é¢ããŠïŒèªå販売æ©ã¯åºèã®èªå販売æ©ãšã¯é¢ä¿ãããŸããã å¥åŠãªããšã«ãã¹ãã¢ãã·ã³ã¯ãã¹ã¿ãã¯ã®å€å žçãªå®è£ ã§ãããã·ã³ã¹ãã¢ïŒå³ãåç §ïŒã«ã¡ãªãã§åœåãããŠããŸããã«ãŒããªããžã®æäžéšã®
ããã§ãå ã®åé¡ã解決ã§ããŸãã {ãš}ãè¡ã«ãããã©ããã確èªããŸãã å{ãèªã¿åããã¹ã¿ãã¯ã«æžã蟌ã¿ãŸãã å}ãèªã¿åããã¹ã¿ãã¯ãã{ãæ¶å»ããŸãã
ç¶æ 1ïŒ{ ã¹ã¿ãã¯èšå·ïŒ{EOFïŒç©ºã®ã¹ã¿ãã¯ïŒ å ¥åæåïŒ {write {{ã1ã«ãšã©ãŸã{}ã1ã«ãšã©ãŸã }äœãæžããªãã§ãè¡2ã«åãŸããªãïŒ1ïŒ EOFæååãåãŸããªãïŒ2ïŒæååãåãŸãïŒ3ïŒ ç¶æ 2ïŒèæ ®} ã¹ã¿ãã¯èšå·ïŒ{EOFïŒç©ºã®ã¹ã¿ãã¯ïŒ å ¥åæåïŒ {ã©ã€ã³ãåãŸããªãïŒ4ïŒã©ã€ã³ãåãŸããªãïŒ4ïŒ }äœãæžããªãã§ãã ããã2è¡ç®ã«ãšã©ãŸãããšã¯ã§ããŸããïŒ5ïŒ EOFæååãåãŸããªãïŒ6ïŒæååãåãŸãïŒ7ïŒæ³šïŒ
- è¡ã¯}ã§å§ãŸããŸã
- è¡ã®å é ã«è€æ°ã®{ããããŸããããããã®åŸã«ã¯1ã€ã§ã¯ãããŸãã}
- è¡ã¯ç©ºã§ã
- åŸ}è¡ã«ã¯åã³{
- } {以äžïŒã¹ã¿ãã¯ã¯çµäºããŸããããå ¥åã¯ãããŸããïŒ
- }ããå°ãã{ ïŒå ¥åã¯çµäºããããã¹ã¿ãã¯ã¯ãªãïŒ
- åæ§ã«{ãš} ïŒã¹ã¿ãã¯ãšå ¥åã¯åæã«çµäºããŸããïŒ
容éã®å€§ãããã·ã³ã«å ããŠãæ§æãèšå®ããæ°ããæ¹æ³ããŸã å¿ èŠã§ã-æ£èŠè¡šçŸãããæè»ã§ãã
æ£åŒãªææ³
ä»»æã®ãã¹ãããµããŒãããèšèªæ§é ã決å®ããããã«ãããšãã°ãã¹ãããããã©ã±ããã®äžèšã®åé¡ã«å¯ŸããŠãæåïŒã端æ«ãïŒãšå€æ°ïŒãé端æ«ãïŒã§æ§æããããæšè«èŠåãã䜿çšãå§ããŸããã
æå¹ïŒ '{'æå¹ '}'; æå¹ïŒ;å·Šãã³ãã³ã®å-å€æ°ã å³åŽã«ã¯ãããããååŸã§ããããã¹ãããããå®çŸ©ãããŠããå€æ°ãžã®ååž°åç §ãå«ãå€æ°ãå«ããããšãã§ããŸãã
ã¹ããŒã¹ãç¯çŽããããã«ãåãå€æ°ã®ä»£æ¿å®çŸ©ãåå²ã§ããŸã| ïŒ
æå¹ïŒ '{'æå¹ '}' | ;
ããæå³ã®ããäŸã¯ãç®è¡åŒã®ææ³ã§ãã
EXPRïŒNUM | EXPR OP EXPR; NUMïŒDIGIT | NUM DIGIT; DIGITïŒ '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'; OPïŒ '+' | '-' | '*' | '/';
ãã®ã¿ã€ãã®ææ³ã¯context-freeãšåŒã°ããå€æ°ã®å®çŸ©ãã³ã³ããã¹ãã«äŸåããªãããšã瀺ããŸããããšãã°ãNUMã®åã«ã0xããä»ããŠããŠããåžžã«10é²æ°ã®æååã«äžèŽããŸãã å®éã«ã¯ãå¹æçãªæ§æ解æã¡ãœããããªããããç¶æ³äŸåææ³ã¯äœ¿çšãããŸããã
ãããã£ãŠãææ³ã«åŸã£ãŠãåŒ
22+3*4-5
ã¯æ¬¡ã®ããã«èªèãããŸãã
ã2ãã2ãã+ãã3ãã*ãã4ãã-ãã5ã DIGIT DIGIT OP DIGIT OP DIGIT OP DIGIT NUM DIGIT OP NUM OP NUM OP NUM NUM OP EXPR OP EXPR OP EXPR EXPR OP EXPR OP EXPR EXPR OP EXPR EXPRãŸãã¯å€åããïŒ
ã2ãã2ãã+ãã3ãã*ãã4ãã-ãã5ã DIGIT DIGIT OP DIGIT OP DIGIT OP DIGIT NUM DIGIT OP NUM OP NUM OP NUM NUM OP EXPR OP EXPR OP EXPR EXPR OP EXPR OP EXPR EXPR OP EXPR EXPRåè ã®å ŽåãåŒã¯å·ŠåŽã®åèšãšå³åŽã®å·®ã®ç©ãšããŠèªèãããŸãã 第äºã«-ç®è¡ã®èŠåã«åŸã£ãŠã æå®ããã2ã€ãé€ããä»ã®ãªãã·ã§ã³ãå¯èœã§ãã
åäžã®åŒã«è€æ°ã®ã解æããªãŒããããå ŽåãçåãçããŸããããŒãµãŒã¯ã©ã¡ããéžæããå¿ èŠããããŸããïŒ
åæ§ã®åé¡ã¯æ£èŠè¡šçŸã«ãããŸããïŒããããã]ãããããã®è¡ã«ãquick brown foxããé©çšãããšã$ 1 =ãquick brownããš$ 2 =ãfoxãããŸãã¯$ 1 =âè¿ éãªãããã³$ 2 = "è¶è²ã®ããããã æ£èŠè¡šçŸã¯ãã貪欲ã«ãåäœããããšã«åæããå·Šããå³ã«è¡ãèªã¿ãåãµãåŒã«å¯èœãªéãå€ãã®é©åãªæåãåã蟌ã¿ãŸãã ïŒãããã£ãŠãå®éã«ã¯ãã¯ã€ãã¯ãã©ãŠã³ããšããã©ãã¯ã¹ããåŸãããŸãïŒã
ææ³ã«ã¯ãã®ãããªåã決ãã¯ãããŸããã ææ³ããããŸããªåæãå¯èœã«ããå Žåãããã¯æªãã䟡å€ã®ãªãææ³ã§ãããå¥ã®ææ³ãèãåºãå¿ èŠããããŸãã ããšãã°ãå·Šããå³ã«ãã¹ãŠã®ã¢ã¯ã·ã§ã³ãå®è¡ããåžå ŽååŒèšç®æ©ã®ææ³ã¯æ¬¡ã®ããã«ãªããŸãã
EXPR: NUM | EXPR OP NUM ;
NUM: DIGIT | NUM DIGIT ;
DIGIT: '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' ;
OP: '+' | '-' | '*' | '/' ;
çŸåšãEXPRå®çŸ©ã§ã¯ã2çªç®ã®ãªãã©ã³ãã¯åžžã«è£žã®æ°å€ã§ããããã解æã¯æ¬¡ã®ããã«ããã§ããŸããã
ã2ãã2ãã+ãã3ãã*ãã4ãã-ãã5ã DIGIT DIGIT OP DIGIT OP DIGIT OP DIGIT NUM DIGIT OP NUM OP NUM OP NUM NUM OP NUM OP NUM OP NUM EXPR OP NUM OP NUM OP NUM EXPR OP NUM OP NUM EXPR OP NUM EXPR
代ããã«å ç®ãããä¹ç®ã®åªå é äœãç¶æãããå ŽåãEXPRã¯ç©ã®åèšãšããŠå®çŸ©ããå¿ èŠããããŸã ã
EXPR: TERM | EXPR '+' TERM | EXPR '-' TERM ;
TERM: NUM | TERM '*' NUM | TERM '/' NUM ;
NUM: DIGIT | NUM DIGIT ;
DIGIT: '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' ;
'2' '2' '+' '3' '*' '4' '-' '5'
DIGIT DIGIT '+' DIGIT '*' DIGIT '-' DIGIT
NUM DIGIT '+' NUM '*' NUM '-' NUM
NUM '+' TERM '*' NUM '-' TERM
TERM '+' TERM '-' TERM
EXPR '+' TERM '-' TERM
EXPR '-' TERM
EXPR
ããã°ã©ãã³ã°èšèªã§ã¯ãåæ§ã®ãããŸããã¯çãããããŸããããC ++ã§ã¯ãã¹ãŠã®ã¹ãããã§æ£ããã§ããã³ã¡ã³ããŒã¿ãŒã¯ãC ++ã³ã³ãã€ã©ã®æåããæåŸãŸã§ã®éçºã«é¢ããçŽ æŽãããèšäºãçããè·æ¥ãã«èšåããŸããã
ããã¯è©³çŽ°ãæãäžããææã§ã¯ãããŸããããæãç°¡åãªäŸã¯
if (1) if (2) foo(); else bar();
if (1) if (2) foo(); else bar();
if (1) { if (2) foo(); else bar(); }
ããã«è§£éã§ããŸã
if (1) { if (2) foo(); else bar(); }
if (1) { if (2) foo(); else bar(); }
if (1) { if (2) foo(); else bar(); }
ããŸãã¯
if (1) { if (2) foo(); } else bar();
if (1) { if (2) foo(); } else bar();
ææ³ã®ã³ã³ãã€ã©ã解éã®1ã€ãçŠæ¢ããããšã«æ³šæããªãå Žåã
ãããŸããã®ãªãææ³ãæžãããšã¯éèŠãªã¹ãã«ã§ãã
LR解æ
ã¹ãã¢ãªãŒãããã³ã«ææ³ãèªèãããæ¹æ³ã¯ïŒ
å ¥åè¡ã1æåãã€èªã¿åããèªã¿åã£ãæåãã¹ã¿ãã¯ã«æžã蟌ã¿ïŒ shift ïŒãŸãã ææ³èŠåã«äžèŽããã¹ã¿ãã¯ã®æäžéšã§ã·ãŒã±ã³ã¹ïŒæåãšå€æ°ïŒãåéããããšããã«ããã·ã³ã¯ãã®ãã¹ãŠãã¹ã¿ãã¯ããæŒãåºããäžèŽããèŠåã®å·ŠåŽã®å€æ°ã«çœ®ãæããŸãïŒ ç³ã¿èŸŒã¿ ïŒã ãã·ã³ã®ãã¹ãŠã®äœæ¥ã¯ãäžé£ã®ã·ãããšç³ã¿èŸŒã¿ã§ãã
èå³æ·±ãç¹ïŒãã·ã³ã¯ãã©ã®ãã£ã©ã¯ã¿ãŒãã¹ã¿ãã¯ã«ããããæ¬åœã«æ°ã«ããŸããã ããã§ãåãããã«ã圌ã¯ããããææ³ã®ã«ãŒã«ãšæ¯èŒããããšã¯ã§ããŸããã 代ããã«ãçŸåšã®ç¶æ ã«åºã¥ããŠç³ã¿èŸŒã¿ã«é©çšããã«ãŒã«ãéžæããŸãã 圌ã¯ãæãããã¿åŸã«ã©ã®ç¶æ ã«ãªãããç¥ãããã«ãã¹ã¿ãã¯ãå¿ èŠã§ãã ãããè¡ãããã«ãã·ããäžã«ã圌ã¯ã·ã³ãã«ãšãšãã«çŸåšã®ç¶æ ãã¹ã¿ãã¯ã«æžã蟌ã¿ãŸãã ç³ã¿èŸŒã¿äžã«ãã¹ã¿ãã¯ããæ¶å»ããããã¹ãŠã®æåã®äžã«æžã蟌ãŸããç¶æ ãååŸããããã«å¿ããŠæ¬¡ã®ç¶æ ã«ç§»è¡ããŸãã
ããããhabrayuzerã¯ãã¹ãã¢ãªãŒãããã³ã§ã¯ãªãããšã«æ°ä»ãã§ããããåã¹ãããã§ãã¹ã¿ãã¯ãã1ã€ã®èŠçŽ ãæ¶å»ãããã®ã§ã¯ãªããäžåºŠã«è€æ°ã®èŠçŽ ãæ¶å»ãããŸãã ã¹ã¿ãã¯ã®äžçªäžã®èŠçŽ ã§ã¯ãªããæ¶å»ãããèŠçŽ ã®äžã®èŠçŽ ãèŠãŠãã ããã ããã«ãç³ã¿èŸŒã¿äžããã·ã³ã¯å ¥åæåãæªèªã®ãŸãŸã«ããŸãã çè«çã«ã¯ããã®ãããªãæ¡åŒµããã·ã³ããæšæºããã¥ãŒã«å€æããŠãã¹ã¿ãã¯ãã¯ãªã¢ããããã®è¿œå ã®ç¶æ ãäœæã§ããŸãã å®éã«ã¯ãäž¡æ¹ã®ãªãã·ã§ã³ã®å®è£ ã¯ãŸã£ããåãã§ããã€ãŸãããµã€ã¯ã«ãã¹ã¿ãã¯ãããã³é·ç§»ããŒãã«ã¯ãŸã£ããåãã§ãã å€æããŒãã«ã®äžã®ã¹ããŒã¹ãç¯çŽããããã«ã圌ãã¯ãã®ç¶æ ãç¡é§ã«èšããŸããããšãªããé«åºŠãªããã·ã³ã䜿çšããŸãã
æåŸã®ææ³ïŒæ£ããæäœã®åªå é äœãæã€ç®è¡åŒïŒãèªèããã«ã¯ã11ã®ç¶æ ã®ãªãŒãããã³ãå¿ èŠã§ãã
ç¶æ 1ïŒ æ¡ïŒã·ããã2ã«ç§»å ç¶æ 2ïŒ ã¹ã¿ãã¯ãã1æåãåé€ããããã«DIGITãæžã蟌ã¿ãŸã ã¹ã¿ãã¯ã®ç¶æ ã1ãŸãã¯7ãŸãã¯10ã®å Žåã3ã«é²ã¿ãŸã ã¹ã¿ãã¯ã®ç¶æ ã4ãŸãã¯9ã®å Žåã5ã«é²ã¿ãŸã ç¶æ 3ïŒ ã¹ã¿ãã¯ãã1æåãåé€ããããã«NUMãæžã蟌ã¿ãŸã ã¹ã¿ãã¯ã®ç¶æ ã1ãŸãã¯10ã®å Žåã4ã«é²ã¿ãŸã ã¹ã¿ãã¯ã®ç¶æ ã7ã®å Žåã9ã«é²ã¿ãŸã ç¶æ 4ïŒ æ¡ïŒã·ããã2ã«ç§»å ããã§ãªãå ŽåïŒã¹ã¿ãã¯ãã1æåãåé€ããããã«æžã蟌ã¿ãŸãTERM ã¹ã¿ãã¯ã®ç¶æ ã1ã®å Žåã6ã«é²ã¿ãŸã ã¹ã¿ãã¯ã®ç¶æ ã10ã®å Žåã11ã«é²ã¿ãŸã ç¶æ 5ïŒ ã¹ã¿ãã¯ãã2æåãåé€ããããã«NUMãæžã蟌ã¿ãŸã ã¹ã¿ãã¯ã®ç¶æ ã1ã®å Žåã4ã«é²ã¿ãŸã ã¹ã¿ãã¯ã®ç¶æ ã7ã®å Žåã9ã«é²ã¿ãŸã ç¶æ 6ïŒ ã*ããã/ãïŒã·ããã7ãž ãã以å€ã®å ŽåïŒã¹ã¿ãã¯ãã1æåãåé€ããããã«EXPRãæžã蟌ã¿ãŸã ã¹ã¿ãã¯ã®ç¶æ ã1ã®å Žåã8ã«é²ã¿ãŸã ç¶æ 7ïŒ æ¡ïŒã·ããã2ã«ç§»å ç¶æ 8ïŒ EOFïŒåŒã¯æ£åžžã«èªèãããŸãã ã+ããã-ãïŒã·ããã10ã«ç§»å ç¶æ 9ïŒ æ¡ïŒã·ããã2ã«ç§»å ãã以å€ã®å ŽåïŒã¹ã¿ãã¯ãã3æåãåé€ããããã«æžã蟌ã¿ãŸãTERM ã¹ã¿ãã¯ã®ç¶æ ã1ã®å Žåã6ã«é²ã¿ãŸã ã¹ã¿ãã¯ã®ç¶æ ã10ã®å Žåã11ã«é²ã¿ãŸã ç¶æ 10ïŒã·ããEXPRïŒEXPR '+' TERMãŸãã¯ã·ããEXPRïŒEXPR '+' TERM æ¡ïŒã·ããã2ã«ç§»å ç¶æ 11ïŒ ã*ããã/ãïŒã·ããã7ãž ããã§ãªãå ŽåïŒã¹ã¿ãã¯ãã3æåãåé€ããããã«EXPRãæžã蟌ã¿ãŸã ã¹ã¿ãã¯ã®ç¶æ ã1ã®å Žåã8ã«é²ã¿ãŸã
ãã®ãã·ã³ãã©ãããæ¥ãã®ãã¯èããŸããã UFOããããç§ãã¡ã«æã£ãŠãããšããŸãã
ãããã©ã®ããã«æ©èœããããèŠãŠã¿ãŸãããïŒåãåŒ
22+3*4-5
ééãããŸãããïŒ
ç¶æ 1ãå ¥å "22 + 3 * 4-5"ãã¹ã¿ãã¯ã¯ç©ºã§ã *æ°åïŒã·ããã2ã«ç§»å ç¶æ 2ãå ¥å "2 + 3 * 4-5"ãã¹ã¿ã㯠'2'ãïŒ1ïŒ *ã¹ã¿ãã¯ãã1æåãåé€ããããã«DIGITãæžã蟌ã¿ã3ã«é²ã¿ãŸã ç¶æ 3ãå ¥å "2 + 3 * 4-5"ãDIGITã¹ã¿ãã¯ãïŒ1ïŒ *ã¹ã¿ãã¯ãã1æåãåé€ããããã«NUMãæžã蟌ã¿ã4ã«é²ã¿ãŸã ç¶æ 4ãå ¥å "2 + 3 * 4-5"ãã¹ã¿ãã¯NUMãïŒ1ïŒ *æ°åïŒã·ããã2ã«ç§»å ç¶æ 2ãå ¥å "+ 3 * 4-5"ãã¹ã¿ã㯠'2'ãïŒ4ïŒãNUMãïŒ1ïŒ *ã¹ã¿ãã¯ãã1æåãåé€ããããã«DIGITãæžã蟌ã¿ã5ã«é²ã¿ãŸã ç¶æ 5ãå ¥å "+ 3 * 4-5"ãDIGITã¹ã¿ãã¯ãïŒ4ïŒãNUMãïŒ1ïŒ *ã¹ã¿ãã¯ãã2æåãåé€ããããã«NUMãæžã蟌ã¿ã4ã«é²ã¿ãŸã ç¶æ 4ãå ¥å "+ 3 * 4-5"ãã¹ã¿ãã¯NUMãïŒ1ïŒ *ã¹ã¿ãã¯ãã1æåãåé€ããããã«TERMãæžã蟌ã¿ã6ã«é²ã¿ãŸã ç¶æ 6ãå ¥å "+ 3 * 4-5"ãã¹ã¿ãã¯TERMãïŒ1ïŒ *ã¹ã¿ãã¯ãã1æåãåé€ããããã«EXPRãæžã蟌ã¿ã8ã«é²ã¿ãŸã ç¶æ 8ãå ¥å "+ 3 * 4-5"ãEXPRã¹ã¿ãã¯ãïŒ1ïŒ * '+'ïŒã·ããã10ã«ç§»å ç¶æ 10ãå ¥å "3 * 4-5"ãã¹ã¿ã㯠'+'ãïŒ8ïŒãEXPRãïŒ1ïŒ *æ°åïŒã·ããã2ã«ç§»å ç¶æ 2ãå ¥åã* 4-5ããã¹ã¿ãã¯ã3ããïŒ10ïŒãã+ããïŒ8ïŒãEXPRãïŒ1ïŒ *ã¹ã¿ãã¯ãã1æåãåé€ããããã«DIGITãæžã蟌ã¿ã3ã«é²ã¿ãŸã ç¶æ 3ãå ¥åã* 4-5ããDIGITã¹ã¿ãã¯ãïŒ10ïŒãã+ããïŒ8ïŒãEXPRãïŒ1ïŒ *ã¹ã¿ãã¯ãã1æåãåé€ããããã«NUMãæžã蟌ã¿ã4ã«é²ã¿ãŸã ç¶æ 4ãå ¥å "* 4-5"ãã¹ã¿ãã¯NUMãïŒ10ïŒã '+'ãïŒ8ïŒãEXPRãïŒ1ïŒ *ã¹ã¿ãã¯ãã1æåãåé€ããããã«TERMãšæžã蟌ã¿ã11ã«é²ã¿ãŸã ç¶æ 11ãå ¥åã* 4-5ããã¹ã¿ãã¯TERMãïŒ10ïŒãã+ããïŒ8ïŒãEXPRãïŒ1ïŒ * '*'ïŒã·ããã7ãž ç¶æ 7ãå ¥åã4-5ããã¹ã¿ãã¯ã*ããïŒ11ïŒãTERMãïŒ10ïŒãã+ããïŒ8ïŒãEXPRãïŒ1ïŒ *æ°åïŒã·ããã2ã«ç§»å ç¶æ 2ãå ¥åã-5ããã¹ã¿ãã¯ã4ããïŒ7ïŒãã*ããïŒ11ïŒãTERMãïŒ10ïŒãã+ããïŒ8ïŒãEXPRãïŒ1ïŒ *ã¹ã¿ãã¯ãã1æåãåé€ããããã«DIGITãæžã蟌ã¿ã3ã«é²ã¿ãŸã ç¶æ 3ãå ¥åã-5ããDIGITã¹ã¿ãã¯ãïŒ7ïŒãã*ããïŒ11ïŒãTERMãïŒ10ïŒãã+ããïŒ8ïŒãEXPRãïŒ1ïŒ *ã¹ã¿ãã¯ãã1æåãåé€ããããã«NUMãæžã蟌ã¿ã9ã«é²ã¿ãŸã ç¶æ 9ãå ¥åã-5ããNUMã¹ã¿ãã¯ãïŒ7ïŒãã*ããïŒ11ïŒãTERMãïŒ10ïŒãã+ããïŒ8ïŒãEXPRãïŒ1ïŒ *ã¹ã¿ãã¯ãã3æåãåé€ããããã«TERMãšæžã蟌ã¿ã11ã«é²ã¿ãŸã ç¶æ 11ãå ¥åã-5ããã¹ã¿ãã¯TERMãïŒ10ïŒãã+ããïŒ8ïŒãEXPRãïŒ1ïŒ *ã¹ã¿ãã¯ãã3æåãåé€ããããã«EXPRãæžã蟌ã¿ã8ã«é²ã¿ãŸã ç¶æ 8ãå ¥åã-5ããEXPRã¹ã¿ãã¯ãïŒ1ïŒ * '-'ïŒã·ããã10ã«ç§»å ç¶æ 10ãå ¥å "5"ãã¹ã¿ã㯠'-'ãïŒ8ïŒãEXPRãïŒ1ïŒ *æ°åïŒã·ããã2ã«ç§»å ç¶æ 2ãå ¥åã¯ç©ºãã¹ã¿ãã¯ã¯ '5'ãïŒ10ïŒã '-'ãïŒ8ïŒãEXPRãïŒ1ïŒ *ã¹ã¿ãã¯ãã1æåãåé€ããããã«DIGITãæžã蟌ã¿ã3ã«é²ã¿ãŸã ç¶æ 3ãå ¥å空ãDIGITã¹ã¿ãã¯ãïŒ10ïŒãã-ããïŒ8ïŒãEXPRãïŒ1ïŒ *ã¹ã¿ãã¯ãã1æåãåé€ããããã«NUMãæžã蟌ã¿ã4ã«é²ã¿ãŸã ç¶æ 4ãå ¥åã¯ç©ºãã¹ã¿ãã¯ã¯NUMãïŒ10ïŒã '-'ãïŒ8ïŒãEXPRãïŒ1ïŒ *ã¹ã¿ãã¯ãã1æåãåé€ããããã«TERMãšæžã蟌ã¿ã11ã«é²ã¿ãŸã ç¶æ 11ãå ¥åã¯ç©ºãã¹ã¿ãã¯TERMãïŒ10ïŒã '-'ãïŒ8ïŒãEXPRãïŒ1ïŒ *ã¹ã¿ãã¯ãã3æåãåé€ããããã«EXPRãæžã蟌ã¿ã8ã«é²ã¿ãŸã ç¶æ 8ãå ¥åã¯ç©ºãEXPRã¹ã¿ãã¯ãïŒ1ïŒ * EOFïŒåŒã¯æ£åžžã«èªèãããŸãã
å®éããªãŒãããã³ã¯ãå ¥åæååãæå¹ãªç®è¡åŒã§ãããã©ãããå€æããŸãã ããã§ãªãå Žåãããããã®ç¶æ ã§ã¯ã次ã®å ¥åã·ã³ãã«ã«é©ããã¢ã¯ã·ã§ã³ã¯ãããŸããã ãã®åŸããã·ã³ã¯æ§æãšã©ãŒãå ±åããŸãã
åããã·ã³ã䜿çšããŠåŒãèšç®ã§ããŸããã¹ã¿ãã¯äžã®åã·ã³ãã«ã«ã¯ããã®æ°åŠçãªå€ãæ ŒçŽããŸãã ç³ã¿èŸŒããšããã¹ã¿ãã¯ããåé€ããããã®ã«åºã¥ããŠæ°ãããã£ã©ã¯ã¿ãŒã®å€ãèšç®ããŸãã
æäœã®åªå 床ãèæ ®ããé«åºŠãªèšç®æ©ãåŸãããŸãïŒ
ç¶æ 1ãå ¥å "22 + 3 * 4-5"ãã¹ã¿ãã¯ã¯ç©ºã§ã *æ°åïŒã·ããã2ã«ç§»å ç¶æ 2ãå ¥å "2 + 3 * 4-5"ãã¹ã¿ã㯠'2'ãïŒ1ïŒ *ã¹ã¿ãã¯ããã2ããåé€ããããã«DIGIT = 2ãšæžã蟌ã¿ã3ã«é²ã¿ãŸã ç¶æ 3ãå ¥å "2 + 3 * 4-5"ãDIGITã¹ã¿ãã¯= 2ãïŒ1ïŒ *ã¹ã¿ãã¯ããDIGIT = 2ãåé€ããããã«NUM = 2ãæžãââ蟌ã¿ã4ã«é²ã¿ãŸã ç¶æ 4ãå ¥å "2 + 3 * 4-5"ãã¹ã¿ãã¯NUM = 2ãïŒ1ïŒ *æ°åïŒã·ããã2ã«ç§»å ç¶æ 2ãå ¥å "+ 3 * 4-5"ãã¹ã¿ã㯠'2'ãïŒ4ïŒãNUM = 2ãïŒ1ïŒ *ã¹ã¿ãã¯ããã2ãæåãåé€ããããã«DIGIT = 2ãšæžã蟌ã¿ã5ã«é²ã¿ãŸã ç¶æ 5ãå ¥å "+ 3 * 4-5"ãã¹ã¿ãã¯DIGIT = 2ãïŒ4ïŒãNUM = 2ãïŒ1ïŒ *ã¹ã¿ãã¯ããDIGIT = 2ãNUM = 2ãåé€ããããã«NUM = 22ãæžã蟌ã¿ã4ã«é²ã¿ãŸã ç¶æ 4ãå ¥å "+ 3 * 4-5"ãã¹ã¿ãã¯NUM = 22ãïŒ1ïŒ * NUM = 22ãã¹ã¿ãã¯ããåé€ããããã«æžã蟌ãTERM = 22ã6ã«é²ã ç¶æ 6ãå ¥å "+ 3 * 4-5"ãã¹ã¿ãã¯TERMãïŒ1ïŒ *ã¹ã¿ãã¯ããTERM = 22ãåé€ããããã«EXPR = 22ãšæžã蟌ã¿ã8ã«é²ã¿ãŸã ç¶æ 8ãå ¥å "+ 3 * 4-5"ãã¹ã¿ãã¯EXPR = 22ãïŒ1ïŒ * '+'ïŒã·ããã10ã«ç§»å ç¶æ 10ãå ¥å "3 * 4-5"ãã¹ã¿ã㯠'+'ãïŒ8ïŒãEXPR = 22ãïŒ1ïŒ *æ°åïŒã·ããã2ã«ç§»å ç¶æ 2ãå ¥åã* 4-5ããã¹ã¿ãã¯ã3ããïŒ10ïŒãã+ããïŒ8ïŒãEXPR = 22ãïŒ1ïŒ *ã¹ã¿ãã¯ããã3ããåé€ããããã«DIGIT = 3ãšæžã蟌ã¿ã3ã«é²ã¿ãŸã ç¶æ 3ãå ¥å "* 4-5"ãã¹ã¿ãã¯DIGIT = 3ãïŒ10ïŒã '+'ãïŒ8ïŒãEXPR = 22ãïŒ1ïŒ *ã¹ã¿ãã¯ããDIGIT = 3ãåé€ããããã«NUM = 3ãæžã蟌ã¿ã4ã«é²ã¿ãŸã ç¶æ 4ãå ¥å "* 4-5"ãã¹ã¿ãã¯NUM = 3ãïŒ10ïŒã '+'ãïŒ8ïŒãEXPR = 22ãïŒ1ïŒ *ã¹ã¿ãã¯ããNUM = 3æåãåé€ããããã«TERM = 3ãšæžã蟌ã¿ã11ã«é²ã¿ãŸã ç¶æ 11ãå ¥åã* 4-5ããã¹ã¿ãã¯TERM = 3ãïŒ10ïŒãã+ããïŒ8ïŒãEXPR = 22ãïŒ1ïŒ * '*'ïŒã·ããã7ãž ç¶æ 7ãå ¥åã4-5ããã¹ã¿ãã¯ã*ããïŒ11ïŒãTERM = 3ãïŒ10ïŒãã+ããïŒ8ïŒãEXPR = 22ãïŒ1ïŒ *æ°åïŒã·ããã2ã«ç§»å ç¶æ 2ãå ¥åã-5ããã¹ã¿ãã¯ã4ããïŒ7ïŒãã*ããïŒ11ïŒãTERM = 3ãïŒ10ïŒãã+ããïŒ8ïŒãEXPR = 22ãïŒ1ïŒ *ã¹ã¿ãã¯ããã4ããåé€ããããã«DIGIT = 4ãæžã蟌ã¿ã3ã«é²ã¿ãŸã ç¶æ 3ãå ¥åã-5ããã¹ã¿ãã¯DIGIT = 4ãïŒ7ïŒãã*ããïŒ11ïŒãTERM = 3ãïŒ10ïŒãã+ããïŒ8ïŒãEXPR = 22ãïŒ1ïŒ *ã¹ã¿ãã¯ããDIGIT = 4æåãåé€ããããã«NUM = 4ãæžã蟌ã¿ã9ã«é²ã¿ãŸã ç¶æ 9ãå ¥åã-5ããã¹ã¿ãã¯NUM = 4ãïŒ7ïŒãã*ããïŒ11ïŒãTERM = 3ãïŒ10ïŒãã+ããïŒ8ïŒãEXPR = 22ãïŒ1ïŒ * NUM = 4ã '*'ãTERM = 3ãã¹ã¿ãã¯ããåé€ããããã«æžã蟌ã¿TERM = 12ã11ã«ç§»å ç¶æ 11ãå ¥åã-5ããã¹ã¿ãã¯TERM = 12ãïŒ10ïŒãã+ããïŒ8ïŒãEXPR = 22ãïŒ1ïŒ * TERM = 12ã '+'ãEXPR = 22ãã¹ã¿ãã¯ããåé€ããããã«EXPR = 34ãæžã蟌ã¿ã8ã«é²ã¿ãŸã ç¶æ 8ãå ¥å "-5"ãã¹ã¿ãã¯EXPR = 34ãïŒ1ïŒ * '-'ïŒã·ããã10ã«ç§»å ç¶æ 10ãå ¥å "5"ãã¹ã¿ã㯠'-'ãïŒ8ïŒãEXPR = 34ãïŒ1ïŒ *æ°åïŒã·ããã2ã«ç§»å ç¶æ 2ãå ¥åã¯ç©ºãã¹ã¿ãã¯ã¯ã5ããïŒ10ïŒãã-ããïŒ8ïŒãEXPR = 34ãïŒ1ïŒ *ã¹ã¿ãã¯ããã5ããåé€ããDIGIT = 5ãããã«æžã蟌ã¿ã3ã«é²ã¿ãŸã ç¶æ 3ãå ¥åã¯ç©ºãã¹ã¿ãã¯DIGIT = 5ãïŒ10ïŒã '-'ãïŒ8ïŒãEXPR = 34ãïŒ1ïŒ *ã¹ã¿ãã¯ããDIGIT = 5ãåé€ããããã«NUM = 5ãšæžã蟌ã¿ã4ã«é²ã¿ãŸã ç¶æ 4ãå ¥åã¯ç©ºãã¹ã¿ãã¯NUM = 5ãïŒ10ïŒã '-'ãïŒ8ïŒãEXPR = 34ãïŒ1ïŒ * NUM = 5ãã¹ã¿ãã¯ããåé€ããããã«æžã蟌ãTERM = 5ã11ã«é²ã ç¶æ 11ãå ¥åã¯ç©ºãã¹ã¿ãã¯TERM = 5ãïŒ10ïŒã '-'ãïŒ8ïŒãEXPR = 34ãïŒ1ïŒ * TERM = 5ã '-'ãEXPR = 34ãã¹ã¿ãã¯ããåé€ããããã«EXPR = 29ãæžã蟌ã¿ã8ã«é²ã¿ãŸã ç¶æ 8ãå ¥åã空ãã¹ã¿ãã¯EXPR = 29ãïŒ1ïŒ * EOFïŒåŒã¯æ£åžžã«èªèãããå€= 29
å®éã®ã³ã³ãã€ã©ã®ããŒãµãŒã¯åæ§ã«èšèšãããŠãããåç³ã¿èŸŒã¿ã§ã®ã¿åŒãè©äŸ¡ãããã察å¿ããã³ãŒããçæãããŸãã
èšè¿°ãããæ§æ解æã®æ¹æ³ã¯æé ãšåŒã°ããŸããæå°ã®æ§é ãèªèããããããçµåããããšããå§ããŸãã å察ã®ãããããŠã³ã¢ãããŒãããããŸãããããã¹ãå šäœãã®æ§é ããå§ããŠãããã«å°ããªäžäœæ§é ãèªèããŠåå²ããŸãã 2ã€ã®ã¢ãããŒãã®æ¯æè ã®éã«ã¯ãçµæãç·©æ ¢ãªããªããŒãããããšãå€æããŸãããããããã®åŽã¯ã圌女ã®ã¢ãããŒããããèªç¶ã§ããã人ã«ããæ§æã®èªèã«è¿ãããããã£ãŠãããã°ã容æã§ãããšèããŠããŸãã
次åã¯ãã€ãœã³ã«ã€ããŠç¥ããŸãããã€ãœã³ã¯ææ³ã®èŠ³ç¹ããLRãªãŒãããã³ã®èªèãæ§ç¯ããããããçŽ æŽãããé»åãã³ã³ãã€ã«ã§ããŸãã