2013-08-06 4 views
1

대부분의 키워드는 찾기 쉽고 일부는 찾기 어려운 일부 텍스트가 있습니다. 모든 키워드는 항상 한 행에 왼쪽 정렬되며 세미콜론으로 끝납니다. 그러나 때때로 누군가는 지적 할 것입니다 : 오, 당신은 키워드를 놓쳤습니다. 그리고 그것은 세미콜론으로 끝나지 않는 어떤 구 (줄에 왼쪽으로 맞춰 졌음)가 될 것입니다. 이 경우 다른 유형의 키워드를 만들어야합니다. 그리고 문자열이 줄의 다른 곳에 나타날 경우 (줄 바꿈 또는 무엇이든 일반 텍스트로 처리해야합니다.)다른 유형의 키워드를 문법에 추가 한 다음 문법을 인식하도록 함

보조 노트로 : 나는 또한 출력에서 ​​많은 ' Exception Raised '치명적이지 않은 에러입니다. 내가 말할 수있는 한 그것은 문법에있을 것으로 예상되는 것처럼 보이고, 자동으로 발견 할 수 없다고보고합니다. 필자는 필자가 생각하기에, a 'do not care는 잘못된 포인트에서 otherAction (아래 참조)을 놓치더라도 신경 쓰지 마라.

내 문법은 나의 이전 질문을 따르고 표준 키워드를 잘 다룬다. 키워드의 다른 패턴은 그것을 놓치고 대신 다른 행동의 본문으로 포함합니다. 나도 내가이 특별한 경우 중 하나를 놓칠 때 신체의 끝이 라인의 시작 부분에 세미콜론으로 끝나는 표준 키워드라는 것을 알게된다.

코드 :

#!/usr/bin/python 

from pyparsing import * 


def testString(text, grammar, examineFurther=False): 
    print 'Input Text is \n%s' % text 
    tokens = (grammar + stringEnd).parseString(text) 

    examine = False 
    print 'token groups: %s' % len(tokens) 
    try: 
     for res in tokens: 
      print 'each result in tokens: %s' % res.dump() 
    except Exception, issue: 
     print 'reason 1 %s' % issue 
     pass 
    if examineFurther: 
     try: 
      results, start, end = next(grammar.scanString(text)) 
      print 'Text is length %s and end scan at %s' % (len(text), end) 
      lmx = end+10 
      if len(text) < lmx: 
       lmx = len(text) - 1 
      print 'debug in text: end %s to end+10 is >%s<' % (end, text[end:lmx]) 
     except Exception, issue: 
      print 'reason 2 %s' % issue 
      pass 

    return tokens 

if __name__ == '__main__': 
    text = """look; We come to a fork in the creek and must 
decide which way to travel 
LEFT OFF Another path was seen prior to the fork branching off to the 
northwest 
south; The stream rapidly descends over rocks and the roar of water is heard 
in the distance. Curiosity may drive one to pursue 
further in this direction 
wEst; a rocky gorge ascends up from a dry branch of the creek 
quote; An old rusty pan is nearby 
""" 
    # This grammar 'misses the 'LEFT OFF' keyword 
    data = Word(printables.replace(';', '')) 
    kw = Combine(data + Literal(';'))('ACTION') 
    kw.setDebug(False) 
    body = OneOrMore(~kw + data).setParseAction(lambda tokens: " ".join(
     tokens))('BODY') 
    body.setDebug(False) 
    grammar = OneOrMore(Group(kw + body)) 

    tokens = testString(text, grammar) 
    print 'Test1: Tokens found %s' % tokens 

    # for simplicity just redefine the grammar from scratch 

    data = None 
    kw = None 
    body = None 
    grammar = None 

    # Now try to capture the new keyword: 'LEFT OFF' we just got notified that 
    # we missed 
    # the only given we have is it will begin a line 
    # a keyword is basically an action 

    print '\nTEST2 start\nNow attempt to capture the missed action/keyword' 

    data = Word(printables.replace(';', '')) 
    standardAction = Combine(data + Literal(';'))('ACTION') 
    standardAction.setDebug(True) 
    exceptions = ['LEFT OFF', ] # 'FUTURE XYZZY',] 
    otherAction = Combine(lineStart + oneOf(exceptions))('ACTION') 
    otherAction.setDebug(True) 

    action = otherAction | standardAction 

    body = OneOrMore(~action + data).setParseAction(lambda tokens: " ".join(
     tokens))('BODY') 
    body.setDebug(False) 
    grammar = OneOrMore(Group(action + body)) 

    tokens = testString(text, grammar,examineFurther=False) 
    print 'Test2: Tokens found %s' % tokens 

출력은 예상대로

하나 "중단"볼 수 있듯이
look; We come to a fork in the creek and must 
decide which way to travel 
LEFT OFF Another path was seen prior to the fork branching off to the 
northwest 
south; The stream rapidly descends over rocks and the roar of water is heard 
in the distance. Curiosity may drive one to pursue 
further in this direction 
wEst; a rocky gorge ascends up from a dry branch of the creek 
quote; An old rusty pan is nearby 

token groups: 4 
each result in tokens: ['look;', 'We come to a fork in the creek and must decide which way to travel LEFT OFF Another path was seen prior to the fork branching off to the northwest'] 
- ACTION: look; 
- BODY: We come to a fork in the creek and must decide which way to travel LEFT OFF Another path was seen prior to the fork branching off to the northwest 
each result in tokens: ['south;', 'The stream rapidly descends over rocks and the roar of water is heard in the distance. Curiosity may drive one to pursue further in this direction'] 
- ACTION: south; 
- BODY: The stream rapidly descends over rocks and the roar of water is heard in the distance. Curiosity may drive one to pursue further in this direction 
each result in tokens: ['wEst;', 'a rocky gorge ascends up from a dry branch of the creek'] 
- ACTION: wEst; 
- BODY: a rocky gorge ascends up from a dry branch of the creek 
each result in tokens: ['quote;', 'An old rusty pan is nearby'] 
- ACTION: quote; 
- BODY: An old rusty pan is nearby 
Test1: Tokens found [['look;', 'We come to a fork in the creek and must decide which way to travel LEFT OFF Another path was seen prior to the fork branching off to the northwest'], ['south;', 'The stream rapidly descends over rocks and the roar of water is heard in the distance. Curiosity may drive one to pursue further in this direction'], ['wEst;', 'a rocky gorge ascends up from a dry branch of the creek'], ['quote;', 'An old rusty pan is nearby']] 

TEST2 start 
Now attempt to capture the missed action/keyword 
Input Text is 
look; We come to a fork in the creek and must 
decide which way to travel 
LEFT OFF Another path was seen prior to the fork branching off to the 
northwest 
south; The stream rapidly descends over rocks and the roar of water is heard 
in the distance. Curiosity may drive one to pursue 
further in this direction 
wEst; a rocky gorge ascends up from a dry branch of the creek 
quote; An old rusty pan is nearby 

Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 0(1,1) 
Exception raised:Expected Re:('LEFT\\ OFF') (at char 0), (line:1, col:1) 
Match Combine:({W:(0123...) ";"}) at loc 0(1,1) 
Matched Combine:({W:(0123...) ";"}) -> ['look;'] 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 5(1,6) 
Exception raised:Expected lineStart (at char 6), (line:1, col:7) 
Match Combine:({W:(0123...) ";"}) at loc 5(1,6) 
Exception raised:Expected ";" (at char 8), (line:1, col:9) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 8(1,9) 
Exception raised:Expected lineStart (at char 9), (line:1, col:10) 
Match Combine:({W:(0123...) ";"}) at loc 8(1,9) 
Exception raised:Expected ";" (at char 13), (line:1, col:14) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 13(1,14) 
Exception raised:Expected lineStart (at char 14), (line:1, col:15) 
Match Combine:({W:(0123...) ";"}) at loc 13(1,14) 
Exception raised:Expected ";" (at char 16), (line:1, col:17) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 16(1,17) 
Exception raised:Expected lineStart (at char 17), (line:1, col:18) 
Match Combine:({W:(0123...) ";"}) at loc 16(1,17) 
Exception raised:Expected ";" (at char 18), (line:1, col:19) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 18(1,19) 
Exception raised:Expected lineStart (at char 19), (line:1, col:20) 
Match Combine:({W:(0123...) ";"}) at loc 18(1,19) 
Exception raised:Expected ";" (at char 23), (line:1, col:24) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 23(1,24) 
Exception raised:Expected lineStart (at char 24), (line:1, col:25) 
Match Combine:({W:(0123...) ";"}) at loc 23(1,24) 
Exception raised:Expected ";" (at char 26), (line:1, col:27) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 26(1,27) 
Exception raised:Expected lineStart (at char 27), (line:1, col:28) 
Match Combine:({W:(0123...) ";"}) at loc 26(1,27) 
Exception raised:Expected ";" (at char 30), (line:1, col:31) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 30(1,31) 
Exception raised:Expected lineStart (at char 31), (line:1, col:32) 
Match Combine:({W:(0123...) ";"}) at loc 30(1,31) 
Exception raised:Expected ";" (at char 36), (line:1, col:37) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 36(1,37) 
Exception raised:Expected lineStart (at char 37), (line:1, col:38) 
Match Combine:({W:(0123...) ";"}) at loc 36(1,37) 
Exception raised:Expected ";" (at char 40), (line:1, col:41) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 40(1,41) 
Exception raised:Expected lineStart (at char 41), (line:1, col:42) 
Match Combine:({W:(0123...) ";"}) at loc 40(1,41) 
Exception raised:Expected ";" (at char 45), (line:1, col:1) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 45(1,1) 
Exception raised:Expected lineStart (at char 45), (line:1, col:1) 
Match Combine:({W:(0123...) ";"}) at loc 45(1,1) 
Exception raised:Expected ";" (at char 52), (line:2, col:7) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 52(2,7) 
Exception raised:Expected lineStart (at char 53), (line:2, col:8) 
Match Combine:({W:(0123...) ";"}) at loc 52(2,7) 
Exception raised:Expected ";" (at char 58), (line:2, col:13) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 58(2,13) 
Exception raised:Expected lineStart (at char 59), (line:2, col:14) 
Match Combine:({W:(0123...) ";"}) at loc 58(2,13) 
Exception raised:Expected ";" (at char 62), (line:2, col:17) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 62(2,17) 
Exception raised:Expected lineStart (at char 63), (line:2, col:18) 
Match Combine:({W:(0123...) ";"}) at loc 62(2,17) 
Exception raised:Expected ";" (at char 65), (line:2, col:20) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 65(2,20) 
Exception raised:Expected lineStart (at char 66), (line:2, col:21) 
Match Combine:({W:(0123...) ";"}) at loc 65(2,20) 
Exception raised:Expected ";" (at char 72), (line:2, col:1) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 72(2,1) 
Exception raised:Expected lineStart (at char 72), (line:2, col:1) 
Match Combine:({W:(0123...) ";"}) at loc 72(2,1) 
Exception raised:Expected ";" (at char 77), (line:3, col:5) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 77(3,5) 
Exception raised:Expected lineStart (at char 78), (line:3, col:6) 
Match Combine:({W:(0123...) ";"}) at loc 77(3,5) 
Exception raised:Expected ";" (at char 81), (line:3, col:9) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 81(3,9) 
Exception raised:Expected lineStart (at char 82), (line:3, col:10) 
Match Combine:({W:(0123...) ";"}) at loc 81(3,9) 
Exception raised:Expected ";" (at char 89), (line:3, col:17) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 89(3,17) 
Exception raised:Expected lineStart (at char 90), (line:3, col:18) 
Match Combine:({W:(0123...) ";"}) at loc 89(3,17) 
Exception raised:Expected ";" (at char 94), (line:3, col:22) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 94(3,22) 
Exception raised:Expected lineStart (at char 95), (line:3, col:23) 
Match Combine:({W:(0123...) ";"}) at loc 94(3,22) 
Exception raised:Expected ";" (at char 98), (line:3, col:26) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 98(3,26) 
Exception raised:Expected lineStart (at char 99), (line:3, col:27) 
Match Combine:({W:(0123...) ";"}) at loc 98(3,26) 
Exception raised:Expected ";" (at char 103), (line:3, col:31) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 103(3,31) 
Exception raised:Expected lineStart (at char 104), (line:3, col:32) 
Match Combine:({W:(0123...) ";"}) at loc 103(3,31) 
Exception raised:Expected ";" (at char 109), (line:3, col:37) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 109(3,37) 
Exception raised:Expected lineStart (at char 110), (line:3, col:38) 
Match Combine:({W:(0123...) ";"}) at loc 109(3,37) 
Exception raised:Expected ";" (at char 112), (line:3, col:40) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 112(3,40) 
Exception raised:Expected lineStart (at char 113), (line:3, col:41) 
Match Combine:({W:(0123...) ";"}) at loc 112(3,40) 
Exception raised:Expected ";" (at char 116), (line:3, col:44) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 116(3,44) 
Exception raised:Expected lineStart (at char 117), (line:3, col:45) 
Match Combine:({W:(0123...) ";"}) at loc 116(3,44) 
Exception raised:Expected ";" (at char 121), (line:3, col:49) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 121(3,49) 
Exception raised:Expected lineStart (at char 122), (line:3, col:50) 
Match Combine:({W:(0123...) ";"}) at loc 121(3,49) 
Exception raised:Expected ";" (at char 131), (line:3, col:59) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 131(3,59) 
Exception raised:Expected lineStart (at char 132), (line:3, col:60) 
Match Combine:({W:(0123...) ";"}) at loc 131(3,59) 
Exception raised:Expected ";" (at char 135), (line:3, col:63) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 135(3,63) 
Exception raised:Expected lineStart (at char 136), (line:3, col:64) 
Match Combine:({W:(0123...) ";"}) at loc 135(3,63) 
Exception raised:Expected ";" (at char 138), (line:3, col:66) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 138(3,66) 
Exception raised:Expected lineStart (at char 139), (line:3, col:67) 
Match Combine:({W:(0123...) ";"}) at loc 138(3,66) 
Exception raised:Expected ";" (at char 142), (line:3, col:1) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 142(3,1) 
Exception raised:Expected lineStart (at char 142), (line:3, col:1) 
Match Combine:({W:(0123...) ";"}) at loc 142(3,1) 
Exception raised:Expected ";" (at char 152), (line:4, col:1) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 152(4,1) 
Exception raised:Expected lineStart (at char 152), (line:4, col:1) 
Match Combine:({W:(0123...) ";"}) at loc 152(4,1) 
Matched Combine:({W:(0123...) ";"}) -> ['south;'] 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 153(5,1) 
Exception raised:Expected Re:('LEFT\\ OFF') (at char 153), (line:5, col:1) 
Match Combine:({W:(0123...) ";"}) at loc 153(5,1) 
Matched Combine:({W:(0123...) ";"}) -> ['south;'] 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 159(5,7) 
Exception raised:Expected lineStart (at char 160), (line:5, col:8) 
Match Combine:({W:(0123...) ";"}) at loc 159(5,7) 
Exception raised:Expected ";" (at char 163), (line:5, col:11) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 163(5,11) 
Exception raised:Expected lineStart (at char 164), (line:5, col:12) 
Match Combine:({W:(0123...) ";"}) at loc 163(5,11) 
Exception raised:Expected ";" (at char 170), (line:5, col:18) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 170(5,18) 
Exception raised:Expected lineStart (at char 171), (line:5, col:19) 
Match Combine:({W:(0123...) ";"}) at loc 170(5,18) 
Exception raised:Expected ";" (at char 178), (line:5, col:26) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 178(5,26) 
Exception raised:Expected lineStart (at char 179), (line:5, col:27) 
Match Combine:({W:(0123...) ";"}) at loc 178(5,26) 
Exception raised:Expected ";" (at char 187), (line:5, col:35) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 187(5,35) 
Exception raised:Expected lineStart (at char 188), (line:5, col:36) 
Match Combine:({W:(0123...) ";"}) at loc 187(5,35) 
Exception raised:Expected ";" (at char 192), (line:5, col:40) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 192(5,40) 
Exception raised:Expected lineStart (at char 193), (line:5, col:41) 
Match Combine:({W:(0123...) ";"}) at loc 192(5,40) 
Exception raised:Expected ";" (at char 198), (line:5, col:46) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 198(5,46) 
Exception raised:Expected lineStart (at char 199), (line:5, col:47) 
Match Combine:({W:(0123...) ";"}) at loc 198(5,46) 
Exception raised:Expected ";" (at char 202), (line:5, col:50) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 202(5,50) 
Exception raised:Expected lineStart (at char 203), (line:5, col:51) 
Match Combine:({W:(0123...) ";"}) at loc 202(5,50) 
Exception raised:Expected ";" (at char 206), (line:5, col:54) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 206(5,54) 
Exception raised:Expected lineStart (at char 207), (line:5, col:55) 
Match Combine:({W:(0123...) ";"}) at loc 206(5,54) 
Exception raised:Expected ";" (at char 211), (line:5, col:59) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 211(5,59) 
Exception raised:Expected lineStart (at char 212), (line:5, col:60) 
Match Combine:({W:(0123...) ";"}) at loc 211(5,59) 
Exception raised:Expected ";" (at char 214), (line:5, col:62) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 214(5,62) 
Exception raised:Expected lineStart (at char 215), (line:5, col:63) 
Match Combine:({W:(0123...) ";"}) at loc 214(5,62) 
Exception raised:Expected ";" (at char 220), (line:5, col:68) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 220(5,68) 
Exception raised:Expected lineStart (at char 221), (line:5, col:69) 
Match Combine:({W:(0123...) ";"}) at loc 220(5,68) 
Exception raised:Expected ";" (at char 223), (line:5, col:71) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 223(5,71) 
Exception raised:Expected lineStart (at char 224), (line:5, col:72) 
Match Combine:({W:(0123...) ";"}) at loc 223(5,71) 
Exception raised:Expected ";" (at char 229), (line:5, col:1) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 229(5,1) 
Exception raised:Expected lineStart (at char 229), (line:5, col:1) 
Match Combine:({W:(0123...) ";"}) at loc 229(5,1) 
Exception raised:Expected ";" (at char 232), (line:6, col:3) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 232(6,3) 
Exception raised:Expected lineStart (at char 233), (line:6, col:4) 
Match Combine:({W:(0123...) ";"}) at loc 232(6,3) 
Exception raised:Expected ";" (at char 236), (line:6, col:7) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 236(6,7) 
Exception raised:Expected lineStart (at char 237), (line:6, col:8) 
Match Combine:({W:(0123...) ";"}) at loc 236(6,7) 
Exception raised:Expected ";" (at char 246), (line:6, col:17) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 246(6,17) 
Exception raised:Expected lineStart (at char 247), (line:6, col:18) 
Match Combine:({W:(0123...) ";"}) at loc 246(6,17) 
Exception raised:Expected ";" (at char 256), (line:6, col:27) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 256(6,27) 
Exception raised:Expected lineStart (at char 257), (line:6, col:28) 
Match Combine:({W:(0123...) ";"}) at loc 256(6,27) 
Exception raised:Expected ";" (at char 260), (line:6, col:31) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 260(6,31) 
Exception raised:Expected lineStart (at char 261), (line:6, col:32) 
Match Combine:({W:(0123...) ";"}) at loc 260(6,31) 
Exception raised:Expected ";" (at char 266), (line:6, col:37) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 266(6,37) 
Exception raised:Expected lineStart (at char 267), (line:6, col:38) 
Match Combine:({W:(0123...) ";"}) at loc 266(6,37) 
Exception raised:Expected ";" (at char 270), (line:6, col:41) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 270(6,41) 
Exception raised:Expected lineStart (at char 271), (line:6, col:42) 
Match Combine:({W:(0123...) ";"}) at loc 270(6,41) 
Exception raised:Expected ";" (at char 273), (line:6, col:44) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 273(6,44) 
Exception raised:Expected lineStart (at char 274), (line:6, col:45) 
Match Combine:({W:(0123...) ";"}) at loc 273(6,44) 
Exception raised:Expected ";" (at char 280), (line:6, col:1) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 280(6,1) 
Exception raised:Expected lineStart (at char 280), (line:6, col:1) 
Match Combine:({W:(0123...) ";"}) at loc 280(6,1) 
Exception raised:Expected ";" (at char 288), (line:7, col:8) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 288(7,8) 
Exception raised:Expected lineStart (at char 289), (line:7, col:9) 
Match Combine:({W:(0123...) ";"}) at loc 288(7,8) 
Exception raised:Expected ";" (at char 291), (line:7, col:11) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 291(7,11) 
Exception raised:Expected lineStart (at char 292), (line:7, col:12) 
Match Combine:({W:(0123...) ";"}) at loc 291(7,11) 
Exception raised:Expected ";" (at char 296), (line:7, col:16) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 296(7,16) 
Exception raised:Expected lineStart (at char 297), (line:7, col:17) 
Match Combine:({W:(0123...) ";"}) at loc 296(7,16) 
Exception raised:Expected ";" (at char 306), (line:7, col:1) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 306(7,1) 
Exception raised:Expected lineStart (at char 306), (line:7, col:1) 
Match Combine:({W:(0123...) ";"}) at loc 306(7,1) 
Matched Combine:({W:(0123...) ";"}) -> ['wEst;'] 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 307(8,1) 
Exception raised:Expected Re:('LEFT\\ OFF') (at char 307), (line:8, col:1) 
Match Combine:({W:(0123...) ";"}) at loc 307(8,1) 
Matched Combine:({W:(0123...) ";"}) -> ['wEst;'] 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 312(8,6) 
Exception raised:Expected lineStart (at char 313), (line:8, col:7) 
Match Combine:({W:(0123...) ";"}) at loc 312(8,6) 

... deleted so i could post the question 

Exception raised:Expected lineStart (at char 384), (line:9, col:15) 
Match Combine:({W:(0123...) ";"}) at loc 383(9,14) 
Exception raised:Expected ";" (at char 389), (line:9, col:20) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 389(9,20) 
Exception raised:Expected lineStart (at char 390), (line:9, col:21) 
Match Combine:({W:(0123...) ";"}) at loc 389(9,20) 
Exception raised:Expected ";" (at char 393), (line:9, col:24) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 393(9,24) 
Exception raised:Expected lineStart (at char 394), (line:9, col:25) 
Match Combine:({W:(0123...) ";"}) at loc 393(9,24) 
Exception raised:Expected ";" (at char 396), (line:9, col:27) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 396(9,27) 
Exception raised:Expected lineStart (at char 397), (line:9, col:28) 
Match Combine:({W:(0123...) ";"}) at loc 396(9,27) 
Exception raised:Expected ";" (at char 403), (line:9, col:1) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 403(9,1) 
Exception raised:Expected lineStart (at char 403), (line:9, col:1) 
Match Combine:({W:(0123...) ";"}) at loc 403(9,1) 
Exception raised:Expected W:(0123...) (at char 404), (line:10, col:1) 
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 404(10,1) 
Exception raised:Expected Re:('LEFT\\ OFF') (at char 404), (line:10, col:1) 
Match Combine:({W:(0123...) ";"}) at loc 404(10,1) 
Exception raised:Expected W:(0123...) (at char 404), (line:10, col:1) 
token groups: 4 
each result in tokens: ['look;', 'We come to a fork in the creek and must decide which way to travel LEFT OFF Another path was seen prior to the fork branching off to the northwest'] 
- ACTION: look; 
- BODY: We come to a fork in the creek and must decide which way to travel LEFT OFF Another path was seen prior to the fork branching off to the northwest 
each result in tokens: ['south;', 'The stream rapidly descends over rocks and the roar of water is heard in the distance. Curiosity may drive one to pursue further in this direction'] 
- ACTION: south; 
- BODY: The stream rapidly descends over rocks and the roar of water is heard in the distance. Curiosity may drive one to pursue further in this direction 
each result in tokens: ['wEst;', 'a rocky gorge ascends up from a dry branch of the creek'] 
- ACTION: wEst; 
- BODY: a rocky gorge ascends up from a dry branch of the creek 
each result in tokens: ['quote;', 'An old rusty pan is nearby'] 
- ACTION: quote; 
- BODY: An old rusty pan is nearby 
Test2: Tokens found [['look;', 'We come to a fork in the creek and must decide which way to travel LEFT OFF Another path was seen prior to the fork branching off to the northwest'], ['south;', 'The stream rapidly descends over rocks and the roar of water is heard in the distance. Curiosity may drive one to pursue further in this direction'], ['wEst;', 'a rocky gorge ascends up from a dry branch of the creek'], ['quote;', 'An old rusty pan is nearby']] 

Process finished with exit code 0 

파싱되지 않았다.

+1

LEFT OFF가 특별한 이유는 무엇입니까? 특수 문자열 "LEFT OFF"입니까?줄의 시작 부분에있는 모든 대문자에서 하나 이상의 단어입니까? 라인의 시작 부분에서 "RIGHT ABOUT"가 키워드일까요? 줄의 시작 부분에서 "이것은 키워드가 아닙니다"는 무엇입니까? "LEFT OFF"가 "단어 바로 뒤에 세미콜론"이있는 패턴과 분명히 일치하지 않으므로 키워드가되는 이유는 무엇입니까? – PaulMcG

+0

"나는 이것이 잘못되었다고 생각한다"라고하는 줄이 시작되면 "나는"키워드가 될까요? 또는 "시간의 틈새가 Stein을 저장"으로 시작하는 라인은 "A"가 키워드일까요? – PaulMcG

답변

1

질문은 유효합니다.

소스 코드는 두 개의 분리 된 테스트와 두 동작을 보여준다

  1. 첫번째 테스트는 새로운 "중단"키워드, 즉없이 : 단지 다음 단어를 이용하여, 수행 "," 줄의 처음 두 단어를 정확히

    "왜"중단 "해야,

  2. 두 번째 테스트를 예상대로 작업은"TEST2 "는이 구문을 따라야 고유 한 새 키워드를 추가 한 후입니다라고 ? " 중요하지 않습니다. 사실 TEST2가 실패했습니다. 그것은 흥미로운 질문입니다.

문제는 라인에서 오는 것이 어디 "중단"일치하지만

otherAction = oneOf(exceptions)('ACTION') 

: 교체 응용 프로그램 작업을하게이 빠른 해결 이후

otherAction = Combine(lineStart + oneOf(exceptions))('ACTION') 

정의 된 바와 같이 라인의 시작 부분에서만.

Mr.McGuire이 요구 사항을 충족시키기 위해이 "otherAction"을 설명하는 방법은 무엇이라고 생각하십니까?

+0

질문이 유효하지 않다고 말한 적이 없으며, 나는 OP가 생각하고 키워드가 아닌 키워드를 만드는 것을 쓰기를 원했습니다. 내가 왜 "왜?"라고 묻는 것처럼 들릴지는 몰라도, 그것은 OP에 달려 있습니다. 내가 묻는 것은 "그런 *이 키워드라는 것을 어떻게 알 수 있습니까?" 파서를 작성할 때 사람들은 흔히 필터링 컨텍스트를 기반으로 하나의 항목 또는 다른 항목을 암시 적으로 구분하지만, 동일한 유형의 구현이 동일한 암시 적 필터링을 수행하지 않으면 혼란 스럽습니다. OP 크레디트에서 그는 부정적인 선견지명을 사용하여 정확하게 '몸'을 구축하고 있습니다. – PaulMcG

+0

'otherAction'을 유효한 액션으로 만드는 것이 컬럼 1에 있다는 사실이라면'otherAction'에 첨부 된 구문 분석 액션을 작성하고 pyparsing의'col' 함수를 사용하여 액션이 시작되는 컬럼을 볼 수 있습니다 , 1 이외의 열에서 ParseException을 발생시킵니다. – PaulMcG

+1

문제는'lineStart'가 선행 필터로 사용하기 가장 좋은 표현이 아니라는 것입니다. 그것은 lookbehind의 무언가로 구현되지만, 수정해야합니다. parse-action-that-tests-for-column-1은보다 안정적입니다. – PaulMcG