繁体   English   中英

如何区分运算符和数字

[英]How to differentiate between operators and numbers

我正在创建一种简单的查询语言来为股市事件生成信号。

例如

//Get notified when the RSI(14) falls below 25
bnbCache.onCondition("rsi(14) < 25", (args) => console.log(args))

//Get notified when the SMA(7) crosses the SMA(25)
bnbCache.onCondition("last(sma(7) > sma(25)) AND (sma(7) < sma(25))", (args) => console.log(args))

//Get notified when the SMA(14) drops 2 within the last 15m candle.
bnbCache.onCondition("change(period, sma(14)) < -2", (res, interval, condition) => {
    if (interval === '15m')
        logger.warning`Condition '${condition}' matched for interval ${interval} ${res}`;
});

(最可能众所周知的)问题是我需要从操作4-2中消除数字-2的歧义。 词法分析应该在第一种情况下返回一个number类型的标记,在后一种情况下返回三个标记 [number, sub, number]。 这个问题最直接的解决方案是什么?

最直接和最常见的解决方案是让词法分析器为-无论哪种方式生成一个sub标记(尽管我会将其重命名为minus) ,并让解析器解析number minus number作为减法,将minus number作为负数。

不要将此作为词法分析的一部分 - 解析-作为一个标记,而 integer 作为另一个标记。

解析器将决定它是一元运算符(意思是否定)还是二元运算符(意思是减法)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM