- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是 jison 的新手,并且设法拼凑出一个有用的查询解析器。我现在正在尝试创建一个解析器,它可以将像“a == 1 and b == 1 and c == 1”这样的字符串解析成像
这样的对象{and: [
{a: {eq: 1}},
{b: {eq: 1}},
{c: {eq: 2}}
]}
而像“a == 1 或 b == 1 和 c == 1”这样的字符串应该解析成像这样的对象
{or: [
{a: {eq: 1}},
{and: [
{b: {eq: 1}},
{c: {eq: 1}}
]}
]}
到目前为止我的语法是这样的:
%lex
%%
\s+ /*skip whitespace*/
\"(\\.|[^"])*\" yytext = yytext.substr(1, yyleng-2); return 'STRING';
"==" return '==';
and[^\w] return 'and';
or[^\w] return 'or';
[0-9]+(?:\.[0-9]+)?\b return 'NUMBER';
[a-zA-Z][\.a-zA-Z0-9_]* return 'SYMBOL';
<<EOF>> return 'EOF';
/lex
%left 'or'
%left 'and'
%left '=='
%start expressions
%%
expressions
: e EOF
{$$ = $1;}
;
e
: property '==' value
{ $$ = {}; $[$1] = {eq: $3}; }
| boolAnd
{ $$ = {and: $1}}
| boolOr
{ $$ = {or: $1}}
;
boolAnd
: boolAnd 'and' e
{$$ = $1; $1.push($3);}
| e 'and' e
{$$ = [$1, $3];}
;
boolOr
: boolOr 'or' e
{$$ = $1; $1.push($3);}
| e 'or' e
{$$ = [$1, $3];}
;
property
: SYMBOL
{$$ = $1;}
;
value
: NUMBER
{$$ = Number(yytext);}
| STRING
{$$ = yytext; }
;
它给了我以下冲突错误:
Conflict in grammar: multiple actions possible when lookahead token is and in state 4
- reduce by rule: e -> boolAnd
- shift token (then go to state 11)
Conflict in grammar: multiple actions possible when lookahead token is or in state 5
- reduce by rule: e -> boolOr
- shift token (then go to state 12)
States with conflicts:
State 4
e -> boolAnd . #lookaheads= EOF and or
boolAnd -> boolAnd .and e #lookaheads= EOF and or
State 5
e -> boolOr . #lookaheads= EOF and or
boolOr -> boolOr .or e #lookaheads= EOF or and
有人能就我做错了什么提出建议吗?非常感谢
最佳答案
自
e : boolAnd
无法在以下两者之间做出决定:
boolAnd: e 'and' e
| boolAnd 'and' e
这就是 jison 所提示的。 (值得注意的是,将 boolAnd
简化为 e
似乎不是你想要的。这实际上是一个类型错误,或者如果 JS 有类型。)
就我个人而言,我只使用二叉树;根据我的经验,事实证明他们更容易合作。您可以使用单个非终结符和优先级声明轻松做到这一点。
%left 'or'
%left 'and'
%%
e
: property '==' value
{ $$ = {eq: [$1, $3]}; /* This seems better to me. YMMV. }
| e 'and' e
{ $$ = {and: [$1, $3]}}
| e 'or' e
{ $$ = {or: [$1, $3]}}
| '(' e ')'
{ $$ = $2 /* You didn't have this one, but it seems useful */ }
;
可以创建一个语法来处理可变运算符(即,将 x OP y OP z
减少为 {OP: [x, y, z]}
) 但要使其正确实际上需要大量工作,而且它不容易屈服于基于优先声明的解决方案。除非你真的想区分 x OP y OP z
和 x OP (y OP z)
,这在 bool 运算符的情况下是不必要的,通常更容易和更一般的做法是在解析树的第二次遍历中折叠多个相似的二元运算符,或者在创建二元节点时直接通过检查子表达式的运算符类型来折叠多个相似的二元运算符。
关于javascript - 递归 bool 值和/或数组 jison 解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37082976/
我有一个带有列的表提供者 implied(tiny int)(something like nullable bool) provi
我正在阅读 VideoFileWriter来自 AForge.Video.FFMPEG 的类(class)通过 ILSPY 组装(我很想看看特定方法是如何工作的)并发现了这个: public bool
这是我的完整代码... import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import
我有一个输入 list类型 [Maybe SomeType]和一个谓词 p类型 SomeType -> Bool ,我想回答这个问题“谓词 p 是否适用于所有碰巧在输入中的 SomeType ?”。
使用 !!x 有什么区别吗?对比(bool)x ? 假设__STDC_VERSION__ >= 199901L和 #include 他们都保证结果是0吗?或 1 ,并且无论 x 的大小和值如何,都不
我正在编写一些 C++ 代码,我想调用两个函数(checkXDirty 和 checkYDirty),并返回 true如果任一返回 true。即使一个返回 true 我也需要评估两者,所以我的第一个想
我注意到 bool在 QtCreator 中以不同于其他类型的颜色突出显示: 只有在包含某些 header 时才会发生这种情况,最终我将其追踪到 . QtCreator 的代码检查器似乎无法手动跟踪
有一个函数: func (first: Int) -> Int -> Bool -> String { return ? } 返回值怎么写?我对上面 func 的返回类型感到很困惑。 最
训练神经网络学习“异或” 我正在尝试使用“批量归一化”,我创建了一个批量归一化层函数“batch_norm1”。 import tensorflow as tf import nump
我已经创建了任务函数来验证我的 json 文件。一切正常,直到我没有使用结果。当我试图从 async task function 获得结果时它显示错误为 Cannot implicitly conve
我有一个函数 func login (parameters: [(String, Any)], completion: @escaping (Bool) -> Vo
我正在处理最近从 X/Motif 转移到 Qt 的 C++ 代码库。我正在尝试编写一个 Perl 脚本,它将用 bool 替换所有出现的 Boolean(来自 X)。该脚本只是做了一个简单的替换。 s
嗨,我正尝试创建一个Visiblity小部件,如果用户在Firebase数据库阵列上,该小部件将显示。看起来像这样(成员数组): 如您所见,我创建了一个StreamBuilder,如果当前用户的用户名
我创建了如下的rest api方法, Future activateAccount(int id, int code) async{ final body = {"code": '$c
在我的Flutter应用中,我有一个返回Future的函数,但我想将结果作为Stream。这是函数: Future isGpsOn() async { if (await Geolocat
我可以看到 BOOLEAN 覆盖了 __visit_name__ class BOOLEAN(Boolean): __visit_name__ = 'BOOLEAN' 控制调度员选择的访问者方
考虑以下代码: bool x; bool? y = null; x = y?? true; 将 bool? 分配给 bool 是一个编译时错误,但上面的代码在编译和运行时都成功了。为什么?尽管第三条语
我正在重写一些 Javascript 代码以在 Excel VBA 中工作。由于在这个网站上搜索,我已经设法翻译了几乎所有的 Javascript 代码!但是,有些代码我无法准确理解它在做什么。这是一
我想拍一张bool来自Vec并在 if 语句中进行比较。如何解决以下错误? | 7 | if cell { | ^^^^ expected
我在我的应用程序崩溃跟踪工具中发现了一些崩溃。基本上我有一个 tabBarController,其中一个选项卡有一个嵌入式 UIWebView,另一个选项卡有一个带有 UITableView 的 Co
我是一名优秀的程序员,十分优秀!