Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ざっくり学ぶ言語のしくみ

itkrt2y
December 09, 2017
4k

 ざっくり学ぶ言語のしくみ

itkrt2y

December 09, 2017
Tweet

Transcript

  1. ίʔυ τʔΫϯྻ ߏจղੳ Parse ந৅ߏจ໦ ݁Ռ ࣮ߦɾධՁ ίϯύΠϧ Compile ධՁ

    Evaluate ࣈ۟ղੳ Lex ωΠςΟϒίʔυ όΠτίʔυ ͳͲ
  2. ίʔυ τʔΫϯྻ ߏจղੳ Parse ந৅ߏจ໦ ݁Ռ ࣮ߦɾධՁ ίϯύΠϧ Compile ධՁ

    Evaluate ࣈ۟ղੳ Lex ωΠςΟϒίʔυ όΠτίʔυ ͳͲ
  3. ίʔυΛτʔΫϯʢίʔυͷ࠷খ୯Ґʣʹ෼ׂ͢Δ puts ( 2 ࣝผࢠ + ਺஋ ࠨׅހ puts (10

    + 2*3)/4 10 ਺஋ + * ۭന ਺஋ * 3 ) ӈׅހ ۭന / / ਺஋ 4 ۭന
  4. # Structͷ഑ྻ΍࿈૝഑ྻͷ഑ྻͳͲɺ΍Γํ͸৭ʑ [ [:identifier, "puts"], [:space, " "], [:lparen, "("],

    [:int, "10"], [:space, " "], [:plus, "+"], [:space, " "], [:int, "2"], [:asterisk, "*"], [:int, "3"], [:rparen, ")"], [:slash, "/"], [:int, "4"] ] ίʔυ্Ͱ͸͜Μͳײ͡
  5. ίʔυ τʔΫϯྻ ߏจղੳ Parse ந৅ߏจ໦ ݁Ռ ࣮ߦɾධՁ ίϯύΠϧ Compile ධՁ

    Evaluate ࣈ۟ղੳ Lex ωΠςΟϒίʔυ όΠτίʔυ ͳͲ
  6. τʔΫϯྻ͔Βந৅ߏจ໦Λ࡞Δ ident:puts lparen:( int:10 plus:+ space: space: space: int:2 asterisk:*

    int:3 rparen:) slash:/ int:4 int:10 plus:+ int:2 asterisk:* int:3 ident:puts slash:/ int:4 <token>:<literal>
  7. ந৅ߏจ໦ͱ͸ʁ • ໦ߏ଄ͷσʔλߏ଄ • AST (Abstract Syntax Tree) • ந৅

    == ຊ࣭Λൈ͖ग़ͨ͠΋ͷ • ίϝϯτ΍ۭനͳͲɺ࣮ߦ্ෆཁͳ৘ใΛഉআ͠ඞཁ ͳ৘ใͷΈͰߏ੒͞ΕΔ
  8. ident:puts lparen:( int:10 plus:+ space: space: space: int:2 asterisk:* int:3

    rparen:) slash:/ int:4 int:10 plus:+ int:2 asterisk:* int:3 ident:puts slash:/ int:4 ࠓճͷྫͰ͸੺࿮ͷ΋ͷ͕ASTʹ͸ͳ͍
  9. int:10 plus:+ int:2 asterisk:* int:3 ident:puts slash:/ int:4 ͪͳΈʹׅހ͕ͳ͔ͬͨΒ puts

    10 + 2 * 3 / 4 ߏจ໦ͷߏ଄Ͱ࣮ߦॱং͕ දݱ͞Ε͍ͯΔ
  10. ίʔυ τʔΫϯྻ ߏจղੳ Parse ந৅ߏจ໦ ݁Ռ ࣮ߦɾධՁ ίϯύΠϧ Compile ධՁ

    Evaluate ࣈ۟ղੳ Lex ωΠςΟϒίʔυ όΠτίʔυ ͳͲ
  11. ίʔυ τʔΫϯྻ ߏจղੳ Parse ந৅ߏจ໦ ݁Ռ ࣮ߦɾධՁ ίϯύΠϧ Compile ධՁ

    Evaluate ࣈ۟ղੳ Lex ωΠςΟϒίʔυ όΠτίʔυ ͳͲ
  12. ίʔυ τʔΫϯྻ ߏจղੳ Parse ந৅ߏจ໦ ݁Ռ ࣮ߦɾධՁ ίϯύΠϧ Compile ධՁ

    Evaluate ࣈ۟ղੳ Lex Ҏ্ɺͬ͘͟Γͨ͠ݴޠͷ͘͠ΈͰͨ͠ ωΠςΟϒίʔυ όΠτίʔυ ͳͲ
  13. ͪΌΜͱษڧ͍ͨ͠ਓ͸ Writing An Interpreter In Go͕Φεεϝ • ӳޠͷຊͰ͕͢ɺςετίʔυ͕͔ͬ͠Γͯ͠ΔͷͰίʔ υΛಡΊ͹ͳΜͱ͔ͳΓ·͢ •

    A Tour of GoΛҰ௨Γཧղ͍ͯ͠Ε͹େৎ෉ • amazon.co.jpͰkindle൛ΛߪೖՄೳ • 2351ԁʢ2017/12/09࣌఺ʣ
  14. ίʔυ ධՁ ࣈ۟ղੳ + ߏจղੳ ந৅ߏจ໦ ݁Ռ Ruby 3? :"37

    ໋ྩྻ ίϯύΠϧ ωΠςΟϒ ίʔυ ίϯύΠϧ ࣮ߦ
  15. parse.c parse.y bison def/keywords gperf ίʔυ ந৅ߏจ໦ ΑΓਖ਼֬ʹݴ͏ͱ parse.o y.tab.c

    mv lex.c cc Rubyιʔείʔυ׬શղઆ ୈ10ষ ύʔαΑΓ cpp
  16. bison (yacc) • ύʔαδΣωϨʔλ • yaccͷ্Ґޓ׵ • yacc == yet

    another compiler compiler • compiler compiler == compiler generator • Ͱ΋yacc͸parser generator • Backus-Naur FormʢBNF: όοΧεφ΢Ξه๏ʣ • ͜͏͍͏ͷ !
  17. Ripper • Ruby૊ΈࠐΈͷύʔαϥΠϒϥϦ require 'ripper' code = 'puts "Hello World!"'

    # ࣈ۟ղੳ Ripper.lex(code) # ߏจղੳ Ripper.sexp(code) # sexp == symbolic expression (Sࣜ)
  18. puts (10 + 2*3)/4 == disasm: #<ISeq:<main>@sample.rb>===================================== 0000 trace 1

    ( 1) 0002 putself 0003 putobject 10 0005 putobject 2 0007 putobject 3 0009 opt_mult <callinfo!mid:*, argc:1, ARGS_SIMPLE>, <callcache> 0012 opt_plus <callinfo!mid:+, argc:1, ARGS_SIMPLE>, <callcache> 0015 putobject 4 0017 opt_div <callinfo!mid:/, argc:1, ARGS_SIMPLE>, <callcache> 0020 opt_send_without_block <callinfo!mid:puts, argc:1, FCALL|ARGS_SIMPLE>, <callcache> 0023 leave
  19. 0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave YARV໋ྩྻ YARVͷ ಺෦ελοΫ
  20. 0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave YARV໋ྩྻ YARVͷ ಺෦ελοΫ
  21. 0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave TFMG YARV໋ྩྻ YARVͷ ಺෦ελοΫ
  22. 0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave  TFMG YARV໋ྩྻ YARVͷ ಺෦ελοΫ
  23. 0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave   TFMG YARV໋ྩྻ YARVͷ ಺෦ελοΫ
  24. 0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave    TFMG YARV໋ྩྻ YARVͷ ಺෦ελοΫ
  25. 0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave   TFMG YARV໋ྩྻ YARVͷ ಺෦ελοΫ
  26. 0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave  TFMG YARV໋ྩྻ YARVͷ ಺෦ελοΫ
  27. 0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave   TFMG YARV໋ྩྻ YARVͷ ಺෦ελοΫ
  28. 0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave  TFMG YARV໋ྩྻ YARVͷ ಺෦ελοΫ
  29. 0000 trace 0002 putself 0003 putobject 10 0005 putobject 2

    0007 putobject 3 0009 opt_mult 0012 opt_plus 0015 putobject 4 0017 opt_div 0020 opt_send_without_block mid:puts 0023 leave OJM YARV໋ྩྻ YARVͷ ಺෦ελοΫ