antlr4否定前瞻?

时间:2019-08-10 16:34:18

标签: antlr antlr4

我写了下面的antlr4语法。

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.ticktype import TickType
from ibapi.common import *

from threading import Timer

class TestApp(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)

    def error(self, reqId , errorCode, errorString):
        print("Error: ", reqId, " ", errorCode, " ", errorString)

    def nextValidId(self, orderId ):
        self.nextOrderId = orderId
        self.start()

    def tickPrice(self, reqId: TickerId, tickType: TickType, price: float, attrib: TickAttrib):
        print("TickPrice. TickerId:", reqId, "tickType:", tickType,
              "Price:", price, "CanAutoExecute:", attrib.canAutoExecute,
              "PastLimit:", attrib.pastLimit, end=' ')

    def tickSize(self, reqId: TickerId, tickType: TickType, size: int):
        print("TickSize. TickerId:", reqId, "TickType:", tickType, "Size:", size)

    def start(self):
        contract = Contract()
        contract.symbol = "AAPL"
        contract.secType = "STK"
        contract.exchange = "SMART"
        contract.currency = "USD"
        contract.primaryExchange = "NASDAQ"

        self.reqMarketDataType(4)
        self.reqMktData(1, contract, "", False, False, [])

    def stop(self):
        self.done = True
        self.disconnect()

def main():
    app = TestApp()
    app.nextOrderId = 0
    app.connect("127.0.0.1", 7497, 1)

    Timer(5, app.stop).start()
    app.run()

if __name__ == "__main__":
    main()

然后是输入文本。


grammar antlr_dev;      

prog: content*  ;

content: content_pre ; 

content_pre: TAG_BLOCK
    | TEXT
    ; 


TEXT   : ~[{}]+ ;
TAG_BLOCK   : IDENTIFIR_OPEN ~[{}]+ IDENTIFIR_CLOSE ;

IDENTIFIR_OPEN : '{{#' WORD '}}' ;
IDENTIFIR_CLOSE : '{{/' WORD '}}' ;

WORD : [a-z]+ ;
NEWLINE : '\r'? '\n' | '\r';

然后输出。

foo bar
{{#pug}}
p something
{{/pug}}
foo bar
{{#pug}}p something{{/pug}}
foo bar

这似乎没问题,但是如果文本中包含(prog foo bar\n {{#pug}}\np something\n{{/pug}} \nfoo bar\n {{#pug}}p something{{/pug}} \nfoo bar\n) {,例如},则可能会导致意外错误。因此,我想使用更聪明的方法,例如使用否定前瞻。 上面的内容被重写为负前瞻,但其中一个导致错误。我不知道要解决这个问题。有什么主意吗?

foo{ bar

0 个答案:

没有答案