- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下输入
@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"
}
<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!");
fields: field "," field "," field "," field
field: one | two | three | four
最佳答案
首先,你有一个错字: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,
正在被放回输入流。这是因为您使用的是
return
而不是
$return =
作为 Parse::RecDescent 手册
says you should .
$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!");
关于perl - 使用 Parse::RecDescent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4702352/
我有以下输入 @Book{press, author = "Press, W. and Teutolsky, S. and Vetterling, W. and Flannery B.",
我设法开始工作的是 STRING、PARAMS、VARIABLE 和 FUNCNAMEFUNCTION好像有问题,就是看不出来。 use strict; use Parse::RecDescent;
目前我用它来解析算术表达式: expr : '(' expr ')' | number op expr | variable op expr | number
我正在尝试使用 Parse::RecDescent制作一个可以解析括号表达式和一元运算符 ? 的解析器。 到目前为止,我在创建解析器时失败了,因为规则 expression 是左递归的: use st
我正在研究 Parse::RecDescent 语法来读取给定的人类可读规则集,然后吐出一个计算机更容易阅读的文件。 其中一个标记是“关键字”列表;大约 26 个不同的关键字。这些可能会随着时间而改变
我有一个 Web 应用程序,它使用通过 Parse::RecDescent 创建的解析器。应用程序的多个部分都需要一个解析器对象,并且由于解析器占用相当多的内存,我到目前为止将解析器对象视为一个单例。
我正在尝试从我的完整性检查中删除一个错误 [当我将代码推送到我的 git 存储库时,有一个钩子(Hook)可以使用 perltidy 检查代码。 & critic ... 使用 tidyall作为处理
重要更新:问题与 Apache 或 mod_perl 无关。 最简单的演示: > perl -le 'use PerlIO::via::QuotedPrint; binmode(\*STDERR, "
我是一名优秀的程序员,十分优秀!