5

次の入力があります

@Book{press,
  author    = "Press, W. and Teutolsky, S. and Vetterling, W. and Flannery B.",
  title     = "Numerical {R}ecipes in {C}: The {A}rt of {S}cientific {C}omputing",
  year      = 2007,
  publisher = "Cambridge University Press"
}

RecDescent パーサー ジェネレーターの文法を書かなければなりません。出力時のデータは、xml 構造に合わせて変更する必要があり、次のようになります。

<book>
    <keyword>press</keyword>
    <author>Press, W.+Teutolsky, S.+Vetterling, W.+Flannery B.</author>
    <title>Numerical {R}ecipes in {C}: The {A}rt of {S}cientific {C}omputing</title>

    <year>2007</year>
    <publisher>Cambridge University Press</publisher>
</book>

追加の繰り返しフィールドはエラーとして報告する必要があります (行番号を含む適切なメッセージで、それ以上の解析は行われません)。私はこのようなものから始めようとしました:

use Parse::RecDescent;

open(my $in,  "<",  "parsing.txt")  or die "Can't open parsing.txt: $!";

my $text;
while (<$in>) {
    $text .= $_;
}

print $text;

my $grammar = q {
    beginning: "\@Book\{" keyword fields "\}"           { print "<book>\n",$item[2],$item[3],"</book>"; }
    keyword: /[a-zA-Z]+/ ","                            { return "    <keyword>".$item[1]."</keyword>\n"; }
    fields: one "," two "," tree "," four               { return $item[1].$item[3].$item[5].$item[7]; }
    one: "author" "=" "\"" /[a-zA-Z\s\.\,\{\}\:]+/ "\"" {   $item[4] =~ s/\sand\s/\+/g;
                                                            return "    <author>",$item[4],"</author>\n"; }
    two: "title" "=" "\"" /[a-zA-Z\s\.\,\{\}\:]+/ "\""  {   $item[4] =~ s/\sand\s/\+/g;
                                                            return "    <title>",$item[4],"</title>\n"; }
    three: "year" "=" /[0-2][0-9][0-9][0-9]/            {   return "    <year>",$item[3],"</year>\n"; }
    four: "publisher" "=" "\"" /[a-zA-Z\s\.\,\{\}\:]+/ "\"" 
                                                        {   $item[4] =~ s/\sand\s/\+/g;
                                                            return "    <publisher>",$item[4],"</publisher>\n"; }
};

my $parser = new Parse::RecDescent($grammar) or die ("Bad grammar!");
defined $parser->beginning($text) or die ("Bad text!");

しかし、それが正しい方法であるかどうかさえわかりません。助けてください。

もう 1 つ小さな問題があります。入力時のタグは特定の順序ではない場合がありますが、各タグは一度しか表示できません。(著者、タイトル、年、発行者) のすべての順列に対してサブルールを作成する必要がありますか? 私が思いついたので:

fields: field "," field "," field "," field
field: one | two | three | four

しかし、明らかにタグの繰り返しを妨げません。

4

1 に答える 1

9

まず、タイプミスがあります:treeの代わりにthree

プログラムを実行しましたが、次の行を追加しました。

use strict;
use warnings; # you should always have strict and warnings on
$::RD_HINT = 1; # Parse::RecDescent hints
$::RD_TRACE = 1; # Parse::RecDescent trace

そして、このデバッグ出力を取得しました:

 1|beginning |>>Matched terminal<< (return value:   |
  |          |[@Book{])                             |
 1|beginning |                                      |"press,\n author = "Press,
  |          |                                      |W. and Teutolsky, S. and
  |          |                                      |Vetterling, W. and Flannery
  |          |                                      |B.",\n title = "Numerical
  |          |                                      |{R}ecipes in {C}: The {A}rt
  |          |                                      |of {S}cientific
  |          |                                      |{C}omputing",\n year =
  |          |                                      |2007,\n publisher =
  |          |                                      |"Cambridge University
  |          |                                      |Press"\n}\n"
 1|beginning |Trying subrule: [keyword]             |
 2| keyword  |Trying rule: [keyword]                |
 2| keyword  |Trying production: [/[a-zA-Z]+/ ',']  |
 2| keyword  |Trying terminal: [/[a-zA-Z]+/]        |
 2| keyword  |>>Matched terminal<< (return value:   |
  |          |[press])                              |
 2| keyword  |                                      |",\n author = "Press, W. and
  |          |                                      |Teutolsky, S. and
  |          |                                      |Vetterling, W. and Flannery
  |          |                                      |B.",\n title = "Numerical
  |          |                                      |{R}ecipes in {C}: The {A}rt
  |          |                                      |of {S}cientific
  |          |                                      |{C}omputing",\n year =
  |          |                                      |2007,\n publisher =
  |          |                                      |"Cambridge University
  |          |                                      |Press"\n}\n"
 2| keyword  |Trying terminal: [',']                |
 2| keyword  |>>Matched terminal<< (return value:   |
  |          |[,])                                  |
 2| keyword  |                                      |"\n author = "Press, W. and
  |          |                                      |Teutolsky, S. and
  |          |                                      |Vetterling, W. and Flannery
  |          |                                      |B.",\n title = "Numerical
  |          |                                      |{R}ecipes in {C}: The {A}rt
  |          |                                      |of {S}cientific
  |          |                                      |{C}omputing",\n year =
  |          |                                      |2007,\n publisher =
  |          |                                      |"Cambridge University
  |          |                                      |Press"\n}\n"
 2| keyword  |Trying action                         |
 1|beginning |>>Matched subrule: [keyword]<< (return|
  |          |value: [    <keyword>press</keyword> ]|
 1|beginning |                                      |"press,\n author = "Press,
  |          |                                      |W. and Teutolsky, S. and
  |          |                                      |Vetterling, W. and Flannery
  |          |                                      |B.",\n title = "Numerical
  |          |                                      |{R}ecipes in {C}: The {A}rt
  |          |                                      |of {S}cientific
  |          |                                      |{C}omputing",\n year =
  |          |                                      |2007,\n publisher =
  |          |                                      |"Cambridge University
  |          |                                      |Press"\n}\n"
 1|beginning |Trying subrule: [fields]              |
 2|  fields  |Trying rule: [fields]                 |
 2|  fields  |Trying production: [one ',' two ','   |
  |          |three ',' four]                       |
 2|  fields  |Trying subrule: [one]                 |
 3|   one    |Trying rule: [one]                    |
 3|   one    |Trying production: ['author' '=' '\"' |
  |          |/[a-zA-Z\s\.\,{}\:]+/ '\"']           |
 3|   one    |Trying terminal: ['author']           |
 3|   one    |<<Didn't match terminal>>             |
 3|   one    |<<Didn't match rule>>                 |
 2|  fields  |<<Didn't match subrule: [one]>>       |
 2|  fields  |<<Didn't match rule>>                 |
 1|beginning |<<Didn't match subrule: [fields]>>    |
 1|beginning |<<Didn't match rule>>                 |
Bad text! at parser.pl line 32, <$in> line 6.

これは、サブルールoneでスタックしpress,ていて、入力ストリームに戻されていることを示しています。これは、Parse :: RecDescentのマニュアルでは、使用する必要があると述べているreturnのではなく、使用しているためです。$return =

さらに、変数に割り当てると$return、リストを返すことができなくなり、文字列を手動で連結する必要があります。

最終結果は次のとおりです。

use strict;
use warnings;
use Parse::RecDescent;

open(my $in,  "<",  "parsing.txt")  or die "Can't open parsing.txt: $!";

my $text;
while (<$in>) {
    $text .= $_;
}

print $text;

my $grammar = q {
    beginning: "\@Book\{" keyword fields /\s*\}\s*/           { print "<book>\n",$item[2],$item[3],"</book>"; }
    keyword: /[a-zA-Z]+/ ","                            { $return = "    <keyword>$item[1]</keyword>\n"; }
    fields: one /,\s*/ two /,\s*/ three /,\s*/ four               { $return = $item[1].$item[3].$item[5].$item[7]; }
    one: "author" "=" "\"" /[a-zA-Z\s\.\,\{\}\:]+/ "\"" {   $item[4] =~ s/\sand\s/\+/g;
                                                            $return =  "    <author>$item[4]</author>\n"; }
    two: "title" "=" "\"" /[a-zA-Z\s\.\,\{\}\:]+/ "\""  {   $item[4] =~ s/\sand\s/\+/g;
                                                            $return =  "    <title>$item[4]</title>\n"; }
    three: "year" "=" /[0-2][0-9][0-9][0-9]/            {   $return = "    <year>$item[3]</year>\n"; }
    four: "publisher" "=" "\"" /[a-zA-Z\s\.\,\{\}\:]+/ "\"" 
                                                        {   $item[4] =~ s/\sand\s/\+/g;
                                                            $return = "    <publisher>$item[4]</publisher>\n"; }
};

my $parser = new Parse::RecDescent($grammar) or die ("Bad grammar!");
defined $parser->beginning($text) or die ("Bad text!");
于 2011-01-16T04:06:07.537 に答える