⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spike.t.svn-base

📁 这是一个DFA简化和生成LL(1)分析表的程序,自动生成表格及图形
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
# spike.tuse strict;use warnings;use File::Temp qw/ tempfile /;use Test::Base;plan tests => 2 * blocks() + 2 * 9;my $plfile;my @plfiles;mkdir 'tmp' if !-d 'tmp';run {    my $block = shift;    my $gm = $block->grammar;    my $input = $block->input;    my $trace = $block->trace;    my $name = $block->name;    if (defined $gm) {        my ($fh, $gmfile) =            tempfile('gm_XXXXXX', SUFFIX => '.grammar', UNLINK => 1, DIR => 'tmp');        #warn "Grammar File: $gmfile";        print $fh $gm;        close $fh;        is system($^X, 'spike.pl', $gmfile), 0, "$name - spike.pl";        ($plfile = $gmfile) =~ s/\.grammar$/.pl/;        ok -f $plfile, "$name - $plfile ok";    }    my ($fh, $infile) = tempfile('in_XXXXXX', UNLINK => 1, DIR => 'tmp');    #warn "Input File: $infile";    print $fh $input;    close $fh;    ok -f $infile, "$name - $infile ok";    my $output = `$^X $plfile -d $infile`;    is $output, $trace, "$name - tracing output ok";    push @plfiles, $plfile;};for my $plfile (@plfiles) {    unlink $plfile;}__DATA__=== TEST 1: Basic--- grammarif_stmt: 'if' <commit> cond block <uncommit> 'else' block       | 'if' cond blockcond: 'cond'block: /{[^}]*}/--- inputif cond { print "a" } else { say "b" }--- trace  trying if_stmt...    [if cond { print...]    trying if_stmt_production_1...      trying 'if'...      >>MATCH<< 'if'...      trying cond...    [ cond { print "...]        trying 'cond'...        >>MATCH<< 'cond'...      >>MATCH<< cond...      trying block...    [ { print "a" } ...]        trying /{[^}]*}/...        >>MATCH<< /{[^}]*}/...      >>MATCH<< block...      trying 'else'...    [ else { say "b"...]      >>MATCH<< 'else'...      trying block...    [ { say "b" }\n]        trying /{[^}]*}/...        >>MATCH<< /{[^}]*}/...      >>MATCH<< block...    >>MATCH<< if_stmt_production_1...  >>MATCH<< if_stmt...success=== TEST 2: Multi-line input--- inputif cond {    print "a"} else {     say "b"}--- trace  trying if_stmt...    [if cond {\n    p...]    trying if_stmt_production_1...      trying 'if'...      >>MATCH<< 'if'...      trying cond...    [ cond {\n    pri...]        trying 'cond'...        >>MATCH<< 'cond'...      >>MATCH<< cond...      trying block...    [ {\n    print "a...]        trying /{[^}]*}/...        >>MATCH<< /{[^}]*}/...      >>MATCH<< block...      trying 'else'...    [ \nelse { \n    s...]      >>MATCH<< 'else'...      trying block...    [ { \n    say "b"...]        trying /{[^}]*}/...        >>MATCH<< /{[^}]*}/...      >>MATCH<< block...    >>MATCH<< if_stmt_production_1...  >>MATCH<< if_stmt...success=== TEST 3: Backtracking works--- inputif cond {    print "a"} --- trace  trying if_stmt...    [if cond {\n    p...]    trying if_stmt_production_1...      trying 'if'...      >>MATCH<< 'if'...      trying cond...    [ cond {\n    pri...]        trying 'cond'...        >>MATCH<< 'cond'...      >>MATCH<< cond...      trying block...    [ {\n    print "a...]        trying /{[^}]*}/...        >>MATCH<< /{[^}]*}/...      >>MATCH<< block...      trying 'else'...    [ \n]      FAIL to match 'else'...    FAIL to match if_stmt_production_1...    trying if_stmt_production_2...    [if cond {\n    p...]      trying 'if'...      >>MATCH<< 'if'...      trying cond...    [ cond {\n    pri...]        trying 'cond'...        >>MATCH<< 'cond'...      >>MATCH<< cond...      trying block...    [ {\n    print "a...]        trying /{[^}]*}/...        >>MATCH<< /{[^}]*}/...      >>MATCH<< block...    >>MATCH<< if_stmt_production_2...  >>MATCH<< if_stmt...success=== TEST 4: <commit> suppresses backtracking--- inputif cond--- trace  trying if_stmt...    [if cond\n]    trying if_stmt_production_1...      trying 'if'...      >>MATCH<< 'if'...      trying cond...    [ cond\n]        trying 'cond'...        >>MATCH<< 'cond'...      >>MATCH<< cond...      trying block...    [\n]        trying /{[^}]*}/...        FAIL to match /{[^}]*}/...      FAIL to match block...    FAIL to match if_stmt_production_1...  FAIL to match if_stmt...fail=== TEST 5: Empty production    Repetition (s) and (s /.../)--- grammarstmt_sequence : stmt stmt_seqstmt_seq      : ';' stmt_sequence              |stmt: 's'--- inputs;s;s--- trace  trying stmt_sequence...    [s;s;s\n]    trying stmt...      trying 's'...      >>MATCH<< 's'...    >>MATCH<< stmt...    trying stmt_seq...    [;s;s\n]      trying stmt_seq_production_1...        trying ';'...        >>MATCH<< ';'...        trying stmt_sequence...    [s;s\n]          trying stmt...            trying 's'...            >>MATCH<< 's'...          >>MATCH<< stmt...          trying stmt_seq...    [;s\n]            trying stmt_seq_production_1...              trying ';'...              >>MATCH<< ';'...              trying stmt_sequence...    [s\n]                trying stmt...                  trying 's'...                  >>MATCH<< 's'...                >>MATCH<< stmt...                trying stmt_seq...    [\n]                  trying stmt_seq_production_1...                    trying ';'...                    FAIL to match ';'...                  FAIL to match stmt_seq_production_1...                  trying stmt_seq_production_2...    []                    trying ''...                    >>MATCH<< ''...                  >>MATCH<< stmt_seq_production_2...                >>MATCH<< stmt_seq...              >>MATCH<< stmt_sequence...            >>MATCH<< stmt_seq_production_1...          >>MATCH<< stmt_seq...        >>MATCH<< stmt_sequence...      >>MATCH<< stmt_seq_production_1...    >>MATCH<< stmt_seq...  >>MATCH<< stmt_sequence...success=== TEST 6: Single 's'--- inputs--- trace  trying stmt_sequence...    [s\n]    trying stmt...      trying 's'...      >>MATCH<< 's'...    >>MATCH<< stmt...    trying stmt_seq...    [\n]      trying stmt_seq_production_1...        trying ';'...        FAIL to match ';'...      FAIL to match stmt_seq_production_1...      trying stmt_seq_production_2...    []        trying ''...        >>MATCH<< ''...      >>MATCH<< stmt_seq_production_2...    >>MATCH<< stmt_seq...  >>MATCH<< stmt_sequence...success=== TEST 7: Empty input--- input--- trace  trying stmt_sequence...    []    trying stmt...      trying 's'...      FAIL to match 's'...    FAIL to match stmt...  FAIL to match stmt_sequence...fail=== TEST 8: Repetition (s) and (s /.../)--- grammarprogram: statement(s) eofileeofile: /^\Z/statement: identifier ':=' exp         | identifier '(' exp_list ')'identifier: /[A-Za-z_]\w*/exp_list: exp(s /\,/)        | ''exp: '0' | '1'--- inputfoo := 1bar(0,1,0)baz()cat(1)--- trace  trying program...    [foo := 1\nbar(0,...]    trying statement...      trying statement_production_1...        trying identifier...          trying /[A-Za-z_]\w*/...          >>MATCH<< /[A-Za-z_]\w*/...        >>MATCH<< identifier...        trying ':='...    [ := 1\nbar(0,1,0...]        >>MATCH<< ':='...        trying exp...    [ 1\nbar(0,1,0)\nb...]          trying exp_production_1...            trying '0'...            FAIL to match '0'...          FAIL to match exp_production_1...          trying exp_production_2...    [1\nbar(0,1,0)\nba...]            trying '1'...            >>MATCH<< '1'...          >>MATCH<< exp_production_2...        >>MATCH<< exp...      >>MATCH<< statement_production_1...    >>MATCH<< statement...    trying statement...    [\nbar(0,1,0)\nbaz...]      trying statement_production_1...        trying identifier...          trying /[A-Za-z_]\w*/...          >>MATCH<< /[A-Za-z_]\w*/...        >>MATCH<< identifier...        trying ':='...    [(0,1,0)\nbaz()\nc...]        FAIL to match ':='...      FAIL to match statement_production_1...      trying statement_production_2...    [\nbar(0,1,0)\nbaz...]        trying identifier...          trying /[A-Za-z_]\w*/...          >>MATCH<< /[A-Za-z_]\w*/...        >>MATCH<< identifier...        trying '('...    [(0,1,0)\nbaz()\nc...]        >>MATCH<< '('...        trying exp_list...    [0,1,0)\nbaz()\nca...]          trying exp_list_production_1...            trying exp...              trying exp_production_1...                trying '0'...                >>MATCH<< '0'...              >>MATCH<< exp_production_1...            >>MATCH<< exp...            trying /\,/...    [,1,0)\nbaz()\ncat...]            >>MATCH<< /\,/...            trying exp...    [1,0)\nbaz()\ncat(...]              trying exp_production_1...                trying '0'...                FAIL to match '0'...              FAIL to match exp_production_1...              trying exp_production_2...                trying '1'...                >>MATCH<< '1'...              >>MATCH<< exp_production_2...            >>MATCH<< exp...            trying /\,/...    [,0)\nbaz()\ncat(1...]            >>MATCH<< /\,/...            trying exp...    [0)\nbaz()\ncat(1)...]              trying exp_production_1...                trying '0'...                >>MATCH<< '0'...              >>MATCH<< exp_production_1...            >>MATCH<< exp...            trying /\,/...    [)\nbaz()\ncat(1)\n]            FAIL to match /\,/...          >>MATCH<< exp_list_production_1...        >>MATCH<< exp_list...        trying ')'...        >>MATCH<< ')'...      >>MATCH<< statement_production_2...    >>MATCH<< statement...    trying statement...    [\nbaz()\ncat(1)\n]      trying statement_production_1...        trying identifier...          trying /[A-Za-z_]\w*/...          >>MATCH<< /[A-Za-z_]\w*/...        >>MATCH<< identifier...        trying ':='...    [()\ncat(1)\n]        FAIL to match ':='...      FAIL to match statement_production_1...      trying statement_production_2...    [\nbaz()\ncat(1)\n]        trying identifier...          trying /[A-Za-z_]\w*/...          >>MATCH<< /[A-Za-z_]\w*/...        >>MATCH<< identifier...        trying '('...    [()\ncat(1)\n]        >>MATCH<< '('...        trying exp_list...    [)\ncat(1)\n]          trying exp_list_production_1...            trying exp...              trying exp_production_1...                trying '0'...                FAIL to match '0'...              FAIL to match exp_production_1...              trying exp_production_2...                trying '1'...                FAIL to match '1'...              FAIL to match exp_production_2...            FAIL to match exp...          FAIL to match exp_list_production_1...          trying exp_list_production_2...            trying ''...            >>MATCH<< ''...          >>MATCH<< exp_list_production_2...        >>MATCH<< exp_list...        trying ')'...        >>MATCH<< ')'...      >>MATCH<< statement_production_2...    >>MATCH<< statement...    trying statement...    [\ncat(1)\n]      trying statement_production_1...        trying identifier...          trying /[A-Za-z_]\w*/...          >>MATCH<< /[A-Za-z_]\w*/...        >>MATCH<< identifier...        trying ':='...    [(1)\n]        FAIL to match ':='...      FAIL to match statement_production_1...      trying statement_production_2...    [\ncat(1)\n]        trying identifier...          trying /[A-Za-z_]\w*/...          >>MATCH<< /[A-Za-z_]\w*/...        >>MATCH<< identifier...        trying '('...    [(1)\n]        >>MATCH<< '('...        trying exp_list...    [1)\n]          trying exp_list_production_1...            trying exp...              trying exp_production_1...                trying '0'...                FAIL to match '0'...              FAIL to match exp_production_1...              trying exp_production_2...                trying '1'...                >>MATCH<< '1'...              >>MATCH<< exp_production_2...            >>MATCH<< exp...            trying /\,/...    [)\n]            FAIL to match /\,/...          >>MATCH<< exp_list_production_1...        >>MATCH<< exp_list...        trying ')'...        >>MATCH<< ')'...      >>MATCH<< statement_production_2...    >>MATCH<< statement...    trying statement...    [\n]      trying statement_production_1...        trying identifier...          trying /[A-Za-z_]\w*/...          FAIL to match /[A-Za-z_]\w*/...        FAIL to match identifier...      FAIL to match statement_production_1...      trying statement_production_2...    []        trying identifier...          trying /[A-Za-z_]\w*/...          FAIL to match /[A-Za-z_]\w*/...        FAIL to match identifier...      FAIL to match statement_production_2...    FAIL to match statement...    trying eofile...      trying /^\Z/...      >>MATCH<< /^\Z/...    >>MATCH<< eofile...  >>MATCH<< program...success=== TEST 9: Whitespace works for (s /.../)--- inputbar( 0, 1, 0 ) baz()cat(--- trace  trying program...    [bar( 0, 1, 0 ) ...]    trying statement...      trying statement_production_1...        trying identifier...          trying /[A-Za-z_]\w*/...          >>MATCH<< /[A-Za-z_]\w*/...        >>MATCH<< identifier...        trying ':='...    [( 0, 1, 0 ) baz...]        FAIL to match ':='...      FAIL to match statement_production_1...      trying statement_production_2...    [bar( 0, 1, 0 ) ...]        trying identifier...          trying /[A-Za-z_]\w*/...          >>MATCH<< /[A-Za-z_]\w*/...        >>MATCH<< identifier...        trying '('...    [( 0, 1, 0 ) baz...]        >>MATCH<< '('...        trying exp_list...    [ 0, 1, 0 ) baz(...]          trying exp_list_production_1...            trying exp...              trying exp_production_1...                trying '0'...                >>MATCH<< '0'...              >>MATCH<< exp_production_1...            >>MATCH<< exp...            trying /\,/...    [, 1, 0 ) baz()\n...]            >>MATCH<< /\,/...            trying exp...    [ 1, 0 ) baz()\nc...]              trying exp_production_1...                trying '0'...                FAIL to match '0'...              FAIL to match exp_production_1...              trying exp_production_2...    [1, 0 ) baz()\nca...]                trying '1'...                >>MATCH<< '1'...              >>MATCH<< exp_production_2...            >>MATCH<< exp...            trying /\,/...    [, 0 ) baz()\ncat...]            >>MATCH<< /\,/...            trying exp...    [ 0 ) baz()\ncat(...]              trying exp_production_1...                trying '0'...                >>MATCH<< '0'...              >>MATCH<< exp_production_1...            >>MATCH<< exp...            trying /\,/...    [ ) baz()\ncat(\n]            FAIL to match /\,/...          >>MATCH<< exp_list_production_1...        >>MATCH<< exp_list...        trying ')'...    [) baz()\ncat(\n]        >>MATCH<< ')'...      >>MATCH<< statement_production_2...    >>MATCH<< statement...    trying statement...    [ baz()\ncat(\n]      trying statement_production_1...        trying identifier...          trying /[A-Za-z_]\w*/...          >>MATCH<< /[A-Za-z_]\w*/...        >>MATCH<< identifier...        trying ':='...    [()\ncat(\n]        FAIL to match ':='...      FAIL to match statement_production_1...      trying statement_production_2...    [ baz()\ncat(\n]        trying identifier...          trying /[A-Za-z_]\w*/...

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -