その他

PHPで仮想マシンベースの正規表現エンジンを作ってみる 第二回

こんにちは、久保田です。

PHPで仮想マシンベースの正規表現エンジンを作ってみる 第二回です。

前回の第一回では、PHPで作成する正規表現の仕様の紹介や正規表現のパーサの実装を行いました。今回の記事では、正規表現を実行する仮想マシンをPHPで実装します。

正規表現を実行する仮想マシン

まず、実装する仮想マシンの仕様について解説します。Regular Expression Matching: the Virtual Machine Approachでは仮想マシンについては以下のように記述しています。


    To start, we'll define a regular expression virtual machine (think Java VM). The VM executes one or more threads, each running a regular expression program, which is just a list of regular expression instructions. Each thread maintains two registers while it runs: a program counter (PC) and a string pointer (SP). 

この記述を整理すると、以下のようになります。

  • 仮想マシンは、開始時はひとつか複数のスレッドを持つ
  • 仮想マシンは、正規表現用の命令列を解釈する
  • スレッドは、SPとPCという2つのレジスタを持つ。SPはstring pointer、つまり文字列の位置を示す。PCはprogram counter、つまり現在のプログラムの位置を示す

スレッドとは、プログラミングの文脈でよく利用される並列で実行されるスレッドではなく、ここでは単に仮想マシンの実行時のコンテキストと捉えてください。スレッドが持っているSPとPCという2つのレジスタも、ここでは単にスレッドはそういう変数を持つと捉えれば大丈夫です。

さらに、仮想マシンが解釈できる命令について以下のように記述されています。


     The regular expression instructions are: 
     
        char c     If the character SP points at is not c, stop this thread: it failed.
                   Otherwise, advance SP to the next character and advance PC to the next instruction. 
        match      Stop this thread: it found a match. 
        jmp x      Jump to (set the PC to point at) the instruction at x. 
        split x, y Split execution: continue at both x and y. Create a new thread with SP 
                   copied from the current thread. One thread continues with PC x. 
                   The other continues with PC y. (Like a simultaneous jump to both locations.)

この4つの命令を日本語でまとめると、以下のようになります。

  • char c: SPレジスタの位置にある文字と比較し、同じであればPCとSPを増やします。比較が同じでなければ、スレッドを停止します。
  • match: スレッドを停止し、マッチに成功します。
  • jmp x: PCレジスタにxを代入します。つまり、x番目の命令に移動します。
  • split x, y: 実行を分割します。現在のスレッドをもとにして、x番目の命令へ移動するスレッドと、y番目の命令へ移動するスレッドを生成します。

正規表現と命令の対応

仮想マシンが解釈する4つの命令を紹介しました。基本的な正規表現はこれらの命令の組み合わせによって表現されます。

典型的な正規表現がどのような命令列に変換されるかは以下です。


/a/の場合:
    0| char 'a'
    1| match
 
/abc/の場合:
    0| char 'a'
    1| char 'b'
    2| char 'c'
    3| match
 
/(ab|cd)/の場合:
    0| split 1, 4
    1| char 'a'
    2| char 'b'
    3| jmp 6
    4| char 'c'
    5| char 'd'
    6| match
 
/(ab)*/の場合:
    0| split 1 4
    1| char 'a'
    2| char 'b'
    3| jmp 0
    4| match
 
/a+/の場合:
    0| char 'a'
    1| split 0 2
    2| match
 
/ab?c/の場合: 
    0| char 'a'
    1| split 2 3
    2| char 'b'
    3| char 'c'
    2| match

PHPで仮想マシンを実装する

正規表現を実行する仮想マシンがどのようなものかを紹介しました。次は、上述した仮想マシンをPHPで実装します。約80行程度でできました。


<?php
class RegexVMThread
{
    public $stringPointer = 0, $programCounter = 0;
}
class RegexVMInstruction
{
    const CHAR = 0;
    const MATCH = 1;
    const JUMP = 2;
    const SPLIT = 3;
    public $type, $param;
    function __construct($instructionType, $param = null)
    {
        $this->type = $instructionType;
        $this->param = $param;
    }
}
class RegexVM
{
    static function run($string, array $instructions)
    {
        $threads = array();
        $thread = new RegexVMThread;
        for (;;) {
            // 現在の命令を取り出す
            $instruction = $instructions[$thread->programCounter];
            if (!$instruction) {
                throw new Exception();
            }
            switch (true) {
                
                case $instruction->type === RegexVMInstruction::CHAR:
                    $char = substr($string, $thread->stringPointer, 1);
                    // マッチする場合
                    if ($char === $instruction->param) {
                        $thread->stringPointer++;
                        $thread->programCounter++;
                    } else {
                        if ($threads) {
                            // スレッドがまだある場合には、
                            // 現在のスレッドを捨ててそのスレッドに切り替える
                            $thread = array_shift($threads);
                        } else {
                            // スレッドがもうない場合には、失敗する
                            return false;
                        }
                    }
                    break;
                case $instruction->type === RegexVMInstruction::SPLIT:
                    // スレッドを分割する
                    $newThread = clone($thread);
                    $newThread->programCounter = $instruction->param;
                    array_unshift($threads, $newThread);
                    $thread->programCounter++;
                    break;
                case $instruction->type === RegexVMInstruction::MATCH:
                    // マッチする
                    return true;
                case $instruction->type === RegexVMInstruction::JUMP:
                    // 特定の命令に飛ぶ
                    $thread->programCounter = $instruction->param;
                    break;
            }
        }
        
    }
}

RegexVM::run()メソッドは、与えられた命令列を解釈し、文字列がそれにマッチするかどうかを返します。実際に試したい場合には以下のコードを実行してみてください。


<?php
include_once __DIR__ . '/../src/PHPRegex.php';
/a/という正規表現を表現
$instructions = array(
    new RegexVMInstruction(RegexVMInstruction::CHAR, 'a'),
    new RegexVMInstruction(RegexVMInstruction::MATCH)
);
var_dump(RegexVM::run('a', $instructions)); // => true
var_dump(RegexVM::run('b', $instructions)); // => false
/abc/という正規表現を表現
$instructions = array(
    new RegexVMInstruction(RegexVMInstruction::CHAR, 'a'),
    new RegexVMInstruction(RegexVMInstruction::CHAR, 'b'),
    new RegexVMInstruction(RegexVMInstruction::CHAR, 'c'),
    new RegexVMInstruction(RegexVMInstruction::MATCH)
);
var_dump(RegexVM::run('abc', $instructions)); // => true
var_dump(RegexVM::run('bc', $instructions));  // => false
/(ab)|(cd)/という正規表現を表現する命令列
$instructions = array(
    new RegexVMInstruction(RegexVMInstruction::SPLIT, 4),
    new RegexVMInstruction(RegexVMInstruction::CHAR, 'a'),
    new RegexVMInstruction(RegexVMInstruction::CHAR, 'b'),
    new RegexVMInstruction(RegexVMInstruction::JUMP, 6),
    new RegexVMInstruction(RegexVMInstruction::CHAR, 'c'),
    new RegexVMInstruction(RegexVMInstruction::CHAR, 'd'),
    new RegexVMInstruction(RegexVMInstruction::MATCH)
);
var_dump(RegexVM::run('ab', $instructions)); // => true
var_dump(RegexVM::run('cd', $instructions)); // => true
var_dump(RegexVM::run('bc', $instructions)); // => false
/a?/という正規表現を表現する命令列
$instructions = array(
    new RegexVMInstruction(RegexVMInstruction::SPLIT, 2),
    new RegexVMInstruction(RegexVMInstruction::CHAR, 'a'),
    new RegexVMInstruction(RegexVMInstruction::MATCH)
);
var_dump(RegexVM::run('', $instructions));  // => true
var_dump(RegexVM::run('a', $instructions)); // => true
/(ab)*/という正規表現を表現する命令列
$instructions = array(
    new RegexVMInstruction(RegexVMInstruction::SPLIT, 4),
    new RegexVMInstruction(RegexVMInstruction::CHAR, 'a'),
    new RegexVMInstruction(RegexVMInstruction::CHAR, 'b'),
    new RegexVMInstruction(RegexVMInstruction::JUMP, 0),
    new RegexVMInstruction(RegexVMInstruction::MATCH)
);
var_dump(RegexVM::run('ab', $instructions));     // => true
var_dump(RegexVM::run('abab', $instructions));   // => true
var_dump(RegexVM::run('ababab', $instructions)); // => true 
var_dump(RegexVM::run('', $instructions));       // => true

これらのコードはgithub上に公開していますので、実際に確かめたい方は触ってみてください。

まとめ

今回の記事では、正規表現を実行する仮想マシンとその命令について解説し、それをPHPで実装しました。次回は、実際の正規表現から今回実装した仮想マシン用の命令列に変換するコンパイラを実装します。

author img for asial

asial

前の記事へ

次の記事へ

一覧へ戻る
PAGE TOP