簡體   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