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

Declarative parse.y

yui-knk
November 29, 2023

Declarative parse.y

yui-knk

November 29, 2023
Tweet

More Decks by yui-knk

Other Decks in Programming

Transcript

  1. About me • Yuichiro Kaneko • yui-knk (GitHub) / spikeolaf

    (Twitter) • The author of ruby/lrama LALR parser generator • CRuby committer, mainly develop parser related features • Code positions to RNode (2018, Ruby 2.6) • Coverage • Error reporting • RubyVM::AbstractSyntaxTree (2018, Ruby 2.6) • keep_tokens & error_tolerant option (2022, Ruby 3.2)
  2. Declarative • Declarative = “એݴత” • ྫ • SQL •

    ར఺ • ਺ֶత/࿦ཧֶͳߏ଄Λഎܠʹ࣋ͭ͜ͱ͕ଟ͍ • SQLͷ৔߹͸ؔ܎୅਺ • ڊਓͷݞʹ৐Δ͜ͱ͕Ͱ͖Δ • ղ͘΂͖໰୊ʹϑΥʔΧεͰ͖Δ
  3. Declarativeͳจ๏ͱparser • BNF (Backus-Naur form) • ڞ௨ݴޠ(Ͱ͢ΑͶ) • ΦʔτϚτϯ •

    ܭࢉػͷ਺ֶతͳϞσϧ • ༗ݶΦʔτϚτϯ/ϓογϡμ΢ϯɾΦʔτϚτϯ/ઢܗ߆ଋΦʔτϚτϯ/ νϡʔϦϯάϚγϯ … • ܗࣜݴޠཧ࿦ • (ϓϩάϥϛϯά)ݴޠΛϞσϧԽͨ͠΋ͷ • ਖ਼نݴޠ/จ຺ࣗ༝ݴޠ/จ຺ґଘݴޠ/ؼೲతՄࢉݴޠ …
  4. Declarativeʁ • arg_value, *arg_value, * ͷ͍ͣΕ͔͕ ‘,’ ۠੾ΓͰฒΜͰ͍Δͱ͍͏ϧʔ ϧ •

    declarative͚ͩͲprimitiveͳͷͰݟ௨͕͠ѱ͍ • ͜ͷޙͷ ydah͞Μͷൃදʹظ଴
  5. ΞΫγϣϯͷத਎ • ASTΛߏங͢Δ • Ripper༻ͷϩδοΫ • ҙຯղੳ • def಺෦ͰͷclassఆٛͳͲͳͲ •

    https://i.loveruby.net/ja/rhg/book/syntree.html “ҙຯղੳ” ΋ࢀর • ͜ΕΛdeclarativeʹͰ͖ͳ͍͔?
  6. ม਺ҰͭͰstackΛ؅ཧ͢Δ A k_class A ctxt B k_class A k_class A

    A ctxt ctxt ctxt ctxt A A ୀආ ্ॻ͖ ϦετΞ ୀආઌ͕ফ͑Δ
  7. StackΛ௥Ճͯ͠ΈΔ class A body end def m1 body end class

    B body end in_class in_def Ture Ture Ture False True False
  8. ΦʔτϚτϯͷ߹੒ x0 x1 x2 x3 a b c a d

    y0 y1 y2 y3 a a b a e ฏੴ๜඙ (2019) ܗࣜతϞσϧԽ ཭ࢄࣄ৅ʗ࣮࣌ؒʗϋΠϒϦουγεςϜͷϞσϧԽͱղੳ. ৿๺ग़൛. P.25 ਤ2.7Λࢀߟʹ࡞੒
  9. ΦʔτϚτϯͷ߹੒ (x0, y0) (x0, y1) (x0, y3) (x0, y2) (x1,

    y0) (x1, y1) (x1, y3) (x1, y2) (x2, y0) (x2, y1) (x2, y3) (x2, y2) (x3, y0) (x3, y1) (x3, y3) (x3, y2) a c d c d c d e e e ฏੴ๜඙ (2019) ܗࣜతϞσϧԽ ཭ࢄࣄ৅ʗ࣮࣌ؒʗϋΠϒϦουγεςϜͷϞσϧԽͱղੳ. ৿๺ग़൛. P.25 ਤ2.9Λࢀߟʹ࡞੒
  10. node͸list struct parser_params { rb_node_exits_t *exits; } struct RNode_EXITS {

    NODE *nd_chain; } struct RNode_BREAK { NODE *nd_chain; } struct RNode_BREAK { NODE *nd_chain; }
  11. Parser States (local_vars) • parser stateքͷϘε • ԾҾ਺ɺlocalม਺ɺม਺͕ ࢖༻͞Ε͍ͯΔ͔൱͔ɺ numbered

    parameterʹؔ ͢Δ͋Ε͜ΕΛ1ͭʹ·ͱΊ ͨເͷΑ͏ͳߏ଄ମ • ελοΫͱू߹(Set)ͷ૊Έ ߹Θͤ