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

PHPで学ぶVM型正規表現エンジンの仕組み

 PHPで学ぶVM型正規表現エンジンの仕組み

PHPカンファレンス福岡での発表資料です。

久保田光則

June 27, 2015
Tweet

More Decks by 久保田光則

Other Decks in Technology

Transcript

  1. Ծ૝ϚγϯͰ࣮ߦ VM char ‘h’ char ‘o’ char ‘g’ char ‘e’

    split 1, 6 jmp 11 ্͔Β ໋ྩղऍ ͍ͯ͘͠ ‣ ݁Ռ༩͑ΒΕͨจࣈྻ͕Ϛον͢Δ͔Λ൑ఆ
  2. ਤ PC=0 SP=2 PC=1 SP=2 split 1,5 PC=5 SP=2 ‣

    ෳ੡͕ऴΘͬͨΒ্ͷεϨου͕ݱࡏͷεϨουʹͳΔ
  3. B Φϓγϣϯ 0 split 1,2 1 char ‘a’ 2 match

    ‣ DIBSbB`ͷͱ͜Ζʹ͸͸ଞͷਖ਼نදݱͷ໋ྩྻ͕ೖΕΒΕΔ ‣ TQMJU໋ྩ͕؊ɻҰݸҰݸ௥͍ͬͯ͜͏
  4. ͋͞ਓؒ7.ʹͳΖ͏ Thread PC SP Execution T1 0 split 1,2 aaa

    T2(PC=2)࡞੒ T1 1 char ‘a’ aaa Ϛον͢ΔͷͰSPΛ૿΍͢ T1 2 match aaa Ϛον׬ྃ ‣ จࣈྻ͕zBBBzͩͬͨ৔߹5ͰϚον׬ྃ
  5. จࣈྻ͕zCCCzͩͬͨΒ Thread PC SP Execution T1 0 split 1,2 bbb

    T2(PC=2)࡞੒ T1 1 char ‘a’ bbb จࣈϚονࣦഊ: T1ফ͑Δ T2 2 match bbb Ϛον׬ྃ ‣ 5Ͱ͸จࣈϚονࣦഊ͢Δ͕5ͰNBUDI͕࣮ߦ͞ΕΔ ‣ ݁ՌϚον੒ޭ
  6. BcCબ୒ 0 split 1,3 1 char ‘a’ 2 jmp 4

    3 char ‘b’ 4 match ‣ DIBSbB` DIBSbC`ͷͱ͜Ζʹ͸͸೚ҙͷਖ਼نදݱͷ໋ ྩྻ͕ೖΕΒΕΔ
  7. B ݸҎ্܁Γฦ͠ 0 char ‘a’ 1 split 0, 2 2

    match ‣ ܁Γฦ͠ͷϚονʹ΋TQMJU໋ྩ͕׆༂ ‣ DIBSbB`ͷͱ͜Ζʹ͸೚ҙͷ໋ྩྻΛೖΕΒΕΔ
  8. B ݸҎ্܁Γฦ͠ 0 split 1,3 1 char ‘a’ 2 jmp

    0 3 match ‣ DIBSbB`ͷͱ͜Ζʹ͸ʜ ҎԼུ