我有如下内容:
((x=2 or y=3 ) and (r=3 and c=3) or (x=5 and g=6))
我定义了:
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
消除左递归并添加必要的代码块后,您将获得以下内容:
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__