Perl Parse::RecDescent和带引号的运算符
我有如下几点:Perl Parse::RecDescent和带引号的运算符,perl,parse-recdescent,Perl,Parse Recdescent,我有如下几点: ((x=2 or y=3 ) and (r=3 and c=3) or (x=5 and g=6)) my $grammar = <<'__EOS__'; { # The code in rules is also covered by these pragmas. use strict; use warnings; } parse : expr /\Z/ { $item[1] } # ----
((x=2 or y=3 ) and (r=3 and c=3) or (x=5 and g=6))
my $grammar = <<'__EOS__';
{
# The code in rules is also covered by these pragmas.
use strict;
use warnings;
}
parse : expr /\Z/ { $item[1] }
# ----------------------------------------
# Rules
expr : log_or { $item[1] }
# ---vvv--- Lowest precedence ---vvv---
log_or : log_and log_or_[ $item[1] ]
log_or_ : 'or' log_and log_or_[ [ $item[1], $arg[0], $item[2] ] ]
| { $arg[0] }
log_and : cmp log_and_[ $item[1] ]
log_and_ : 'and' cmp log_and_[ [ $item[1], $arg[0], $item[2] ] ]
| { $arg[0] }
cmp : term cmp_[ $item[1] ]
cmp_ : /=|<[>=]?|>=?/ term cmp_[ [ $item[1], $arg[0], $item[2] ] ]
| { $arg[0] }
# ---^^^--- Highest precedence ---^^^---
term : '(' expr ')' { $item[2] }
| IDENT { [ $item[0], $item[1] ] }
| NUMBER { [ $item[0], $item[1] ] }
# ----------------------------------------
# Tokens
IDENT : /\w+/
NUMBER : /\d+/
__EOS__
我定义:
Token : /\w \= \d/
operator or|and
expression : token operator(s)
quoted_expression : "("expression")"
query : expression (s)|quoted_expression(s)
但是我在解析上面的条件时遇到了问题。你知道如何解析上面的条件吗?在核心部分,你想
expr : log_or
log_or : log_or 'or' log_and
| log_and
log_and : log_and 'and' cmp
| cmp
cmp : cmp /=|<[>=]?|>=?/ term
| term
term : '(' expr ')'
| IDENT
| NUMBER
expr:log\u或
log_或:log_或“或”log_和
|日志和
日志和:日志和“和”cmp
|化学机械抛光
cmp:cmp/=|=]?|>=?/术语
|术语
术语:'('expr')'
|识别
|数
添加必要的代码块后,您将获得以下结果:
((x=2 or y=3 ) and (r=3 and c=3) or (x=5 and g=6))
my $grammar = <<'__EOS__';
{
# The code in rules is also covered by these pragmas.
use strict;
use warnings;
}
parse : expr /\Z/ { $item[1] }
# ----------------------------------------
# Rules
expr : log_or { $item[1] }
# ---vvv--- Lowest precedence ---vvv---
log_or : log_and log_or_[ $item[1] ]
log_or_ : 'or' log_and log_or_[ [ $item[1], $arg[0], $item[2] ] ]
| { $arg[0] }
log_and : cmp log_and_[ $item[1] ]
log_and_ : 'and' cmp log_and_[ [ $item[1], $arg[0], $item[2] ] ]
| { $arg[0] }
cmp : term cmp_[ $item[1] ]
cmp_ : /=|<[>=]?|>=?/ term cmp_[ [ $item[1], $arg[0], $item[2] ] ]
| { $arg[0] }
# ---^^^--- Highest precedence ---^^^---
term : '(' expr ')' { $item[2] }
| IDENT { [ $item[0], $item[1] ] }
| NUMBER { [ $item[0], $item[1] ] }
# ----------------------------------------
# Tokens
IDENT : /\w+/
NUMBER : /\d+/
__EOS__
my$grammar=这些是括号,不是引号。是的,你说得对!非常感谢你的回答,这是我需要的,而且链接非常有用!