Adding a different type of keyword to a grammar and then getting it to recognize it - pyparsing

I have a some text where most of the keywords are easy to find and some are difficult to find. All keywords are always left justified on a line if present and are terminated with a semicolon. However occasionally someone will point out: OH you missed a keyword and it will be some phrase (left justified on a line) that isn't terminated with a semicolon. In those cases I have to build up a different type of keyword. And if the string shows up anywhere else in a line (wrapped or whatever I'm supposed to just treat it as normal text.
As a side note: I've also noticed in the output I get a lot of 'Exception Raised' non fatal errors where as far as I can tell it looks like its expecting something to be there in the grammar and just automatically reports it couldn't find it. and I think in my case because I have | pyparsing treats it as a 'don't care, even though it misses my otherAction (see below) at the wrong point.
My grammar is a follow on of my previous question and handles the standard keyword just fine but when I try adding in a different pattern of the keyword it misses it and instead includes it as a body of a different action. I assume someone will tell me when I miss one of these special cases so the end of the body is a standard keyword ending with a semicolon at the start of a line.
the code:
#!/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
the output:
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
As one can see 'LEFT OFF' wasn't parsed out as expected.

The question is very valid.
Source code shows the two behaviors as two separate tests:
first test is without the new "LEFT OFF" keyword, that is: just using any word followed by ";", which does work as expected
the second test, referred to as "TEST2" is after adding a new unique keyword that must follow this syntax: first two words in a line must be exactly "LEFT OFF"
"Why?" is unimportant. The fact is TEST2 fails, that's the intrigging question.
The problem seems to come from line:
otherAction = Combine(lineStart + oneOf(exceptions))('ACTION')
since this quick workaround replacement makes the app work:
otherAction = oneOf(exceptions)('ACTION')
though this matches "LEFT OFF" anywhere, and not, as defined, only at the beginning of a line.
Mr.McGuire, what would you think is the way to describe this "otherAction" to meet the requirement?

Related

ESC/POS 124Chars limit in QRcode printing

I'm having an issue, regarding printing commands through ESC/POS to print a QR code, with more than 124 chars. I have seen different posts on Stack Overflow, but can't understand what is wrong with this.
To contextualize, the idea, is to print a qrcode in a receipt. The data is written to a text file, and after that is used by a service (rawbt) which prints the receipt in a thermal printer (those small ones)
I have a code that prints until 124 chars of data in qrcode correctly, but can't figure out how to print more in the same QR. extra text just shows up above QR code.
string QrData = "123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 XX";
int store_len = (QrData).Length + 3;
byte store_pL = (byte)(store_len % 256);
byte store_pH = (byte)(store_len / 256);
byte[] b = new byte[] { 29, 40, 107, 4, 0, 49, 65, 50, 0 };
byte[] b1 = new byte[] { 29, 40, 107, 3, 0, 49, 67, 9 };
byte[] b2 = new byte[] { 29, 40, 107, 3, 0, 49, 69, 48 };
byte[] b3 = new byte[] { 29, 40, 107, store_pL, store_pH, 49, 80, 48 };
byte[] bytes = Encoding.ASCII.GetBytes(QrData);
byte[] b4 = new byte[] { 29, 40, 107, 3, 0, 49, 81, 48 };
after that just use BinaryWriter to write this to the text file.
I've already tried other encodings, like utf-8 and iso-8859-1, but no luck.
Also did some play with store_pL and store_pH.
Did also some tests, when if the qrdata.length is > 124, adds up 128 to it, making the store_pL higher, and setting the store_pH to 0. If store_pH is higher than 0, no printing occurs.
note: this is a xamarin app.
Anyone knows please how to solve this?
Thanks in advance!
I am working with an Epson TM-m30II.
I found a solution for this problem. The problem is well described in this post:
EPSON ESCPOS QRCode >380 characters not print
In my program I worked with the java.io.PrintWriter. This apparently only supports the ASCII character set up to Dec(127) (https://en.wikipedia.org/wiki/ASCII).
If the QR is longer than 124 + 3 characters, an incorrect value is sent in the store_pL byte and the printer does not know the length of the transmitted data.
I solved the problem by not using the java.io.PrintWriter but the java.io.DataOutputStream.
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class EscPos {
public static void main(String[] args) throws IOException {
Socket sock = new Socket("192.168.10.45", 9100);
DataOutputStream outputStream = new DataOutputStream(sock.getOutputStream());
String qrCode = "123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 XX";
int store_len = qrCode.length() + 3;
byte store_pL = (byte) (store_len % 256);
byte store_pH = (byte) (store_len / 256);
outputStream.flush();
// https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=140 (165)
byte [] bSendFunktion165 = {(byte)0x1D, (byte)0x28, (byte)0x6B, (byte)0x04, (byte)0x00, (byte)0x31, (byte)0x41, (byte)0x32, (byte)0x00};
outputStream.write(bSendFunktion165);
//https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=141 (167)
byte [] bSendFunktion167 = {(byte)0x1D, (byte)0x28, (byte)0x6B, (byte)0x03, (byte)0x00, (byte)0x31, (byte)0x43, (byte)0x9};
outputStream.write(bSendFunktion167);
//https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=142 (169)
byte [] bSendFunktion169 = {(byte)0x1D, (byte)0x28, (byte)0x6B, (byte)0x03, (byte)0x00, (byte)0x31, (byte)0x45, (byte)0x30};
outputStream.write(bSendFunktion169);
//https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=143 (180)
byte [] bSendFunktion180 = {(byte)0x1D, (byte)0x28, (byte)0x6B, store_pL, store_pH, (byte)0x31, (byte)0x50, (byte)0x30};
outputStream.write(bSendFunktion180);
byte [] bSendQRCode = qrCode.getBytes();
outputStream.write(bSendQRCode);
//https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=143 (181)
byte [] bSendFunktion181 = {(byte)0x1D, (byte)0x28, (byte)0x6B, (byte)0x03, (byte)0x00, (byte)0x31, (byte)0x51, (byte)0x30};
outputStream.write(bSendFunktion181);
outputStream.flush();
outputStream.write((byte)0x0A);
outputStream.write((byte)0x0A);
outputStream.write((byte)0x0A);
outputStream.write((byte)0x0A);
byte [] bCut = {(byte)0x1D, (byte)0x56, (byte)0x30};
outputStream.write(bCut);
//------------------------------------------------------------------
outputStream.close();
sock.close();
}
}
I hope I could help you. Happy holidays
Steffi

MPI Send and receive a pointer in MPI_Type_struct

I want to send a set of data with the MPI_Type_struct and one of them is a pointer to an array (because the matrices that I'm going to use are going to be very large and I need to do malloc). The problem I see is that all the data is passed correctly except the matrix. I know that it is possible to pass a matrix through the pointer since if I only send the pointer of the matrix, correct results are observed.
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
void main(int argc, char *argv[])
{
MPI_Init(&argc, &argv);
int size, rank;
int m,n;
m=n=2;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
typedef struct estruct
{
float *array;
int sizeM, sizeK, sizeN, rank_or;
} ;
struct estruct kernel, server;
MPI_Datatype types[5] = {MPI_FLOAT, MPI_INT,MPI_INT,MPI_INT,MPI_INT};
MPI_Datatype newtype;
int lengths[5] = {n*m,1,1,1,1};
MPI_Aint displacements[5];
displacements[0] = (size_t) & (kernel.array[0]) - (size_t)&kernel;
displacements[1] = (size_t) & (kernel.sizeM) - (size_t)&kernel;
displacements[2] = (size_t) & (kernel.sizeK) - (size_t)&kernel;
displacements[3] = (size_t) & (kernel.sizeN) - (size_t)&kernel;
displacements[4] = (size_t) & (kernel.rank_or) - (size_t)&kernel;
MPI_Type_struct(5, lengths, displacements, types, &newtype);
MPI_Type_commit(&newtype);
if (rank == 0)
{
kernel.array = (float *)malloc(m * n * sizeof(float));
for(int i = 0; i < m*n; i++) kernel.array[i] = i;
kernel.sizeM = 5;
kernel.sizeK = 5;
kernel.sizeN = 5;
kernel.rank_or = 5;
MPI_Send(&kernel, 1, newtype, 1, 0, MPI_COMM_WORLD);
}
else
{
server.array = (float *)malloc(m * n * sizeof(float));
MPI_Recv(&server, 1, newtype, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("%i \n", server.sizeM);
printf("%i \n", server.sizeK);
printf("%i \n", server.sizeN);
printf("%i \n", server.rank_or);
for(int i = 0; i < m*n; i++) printf("%f\n",server.array[i]);
}
MPI_Finalize();
}
Assuming that only two processes are executed,I expect that process with rank = 1 receive and display the correct elements of the matrix on the screen (the other elements are well received), but the actual output is:
5
5
5
5
0.065004
0.000000
0.000000
0.000000
===================================================================================
= BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
= PID 26206 RUNNING AT pmul
= EXIT CODE: 11
= CLEANING UP REMAINING PROCESSES
= YOU CAN IGNORE THE BELOW CLEANUP MESSAGES
===================================================================================
YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Segmentation fault (signal 11)
This typically refers to a problem with your application.
Please see the FAQ page for debugging suggestions
I hope someone can help me.

STM32 USB buffer not retrieved correctly

im pretty new on the STM32 and i encountered a problem.
With a Qt App i'm sending stuff over the USB with the following code:
if (m_hidDevice->isOpen())
{
QByteArray buffer(m_hidDevice->readOutputBufferSize(), 0);
buffer[0] = 16;
buffer[1] = 18;
uint16_t number = 4096;
uint16_t randomValue = qrand() % number;
buffer[2] = (char)((randomValue >> 8) & 0x00ff);
buffer[3] = (char)(randomValue & 0x00ff);
buffer[4] = (char)((2556 >> 8) & 0x00ff);
buffer[5] = (char)(2556 & 0x00ff);
qDebug() << "------------" << randomValue;
qDebug() << "//" << (uint8_t)buffer[2] << "//" << (uint8_t)buffer[3];
qDebug() << "//" << (uint8_t)buffer[4] << "//" << (uint8_t)buffer[5];
m_hidDevice->write(buffer);
and on the STM32F4 im using
switch (buffer[1])
{
case 18:
x = ((uint16_t)buffer[2] << 8) + buffer[3];
y = ((uint16_t)buffer[4] << 8) + buffer[5];
sr.m_value1[0] = x;
sr.m_value1[1] = y;
do(M);
m_value has size 4 and it is uint16_t;
The output on the Qt App is
------------ 2083
// 8 // 35
// 9 // 252
while on the STM32F4 x and y have values
x = 2083 (as expected)
y = 0
Now the size of the buffer should be 64 bytes while my data is 8*5 = 40 bytes.
My question then is why i can't retrieve correctly the values in the buffer?
Regards,
0x09, 0x01, // USAGE (Vendor Usage x) --> x = 1,2,3
0x75, 0x08, // REPORT_SIZE (8) --> 2^8 = 255
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
0x85, 0x02, // REPORT_ID (n) --> n must be the report id
0x95, 0x3f, // REPORT_COUNT (63) --> size
0x91, 0x02 // OUTPUT (Data,Var,Abs)

Recursive BinarySearch Problems

In my code I'm working on i have a binary search that is suppose to find specific numbers but as of right now i can't figure out why it tells me every single number isn't found. I am attempting to use recursion.
public class BinarySearch {
private static boolean binarySearch(int[] myList, int numberToFind) {
// So this will be your recursive method.
// Right now it just returns false.
// But you need to change this code.
return false;
}
public static void main(String[] args) {
// Create an array of sorted numbers
int[] evenList =
{ 2, 4, 9, 11, 17, 19, 22, 29, 30, 33,
39, 43, 46, 47, 51, 52, 54, 56, 58, 59,
63, 69, 70, 79, 88, 89, 92, 96, 98, 99 };
// Can we find every number?
for (int i = evenList.length -1; i >= 0; i--) {
if (binarySearch(evenList, evenList[i]))
System.out.printf("%d was found.\n\n", evenList[i]);
else
System.out.printf("%d was not found.\n\n", evenList[i]);
}
// Will we not find these numbers?
int[] testCases = { 1, 44, 100, 32 };
for (int i = 0; i > testCases.length; i--) {
if (binarySearch(evenList, testCases[i]))
System.out.printf("%d was found.\n\n", testCases[i]);
else
System.out.printf("%d was not found.\n\n", testCases[i]);
}
}
}
Well check out this code
private static boolean binarySearch(int[] myList, int numberToFind) {
// So this will be your recursive method.
// Right now it just returns false.
// But you need to change this code.
return false;
You need to implement that method before it will work.

A simple MPI program

I appreciate it if somebody tell me why this simple MPI send and receive code doesn't run on two processors, when the value of n=40(at line 20), but works for n <=30. In other words, if the message size goes beyond an specific number (which is not that large, roughly a 1-D array of size 8100) the MPI deadlocks.
#include "mpi.h"
#include "stdio.h"
#include "stdlib.h"
#include "iostream"
#include "math.h"
using namespace std;
int main(int argc, char *argv[])
{
int processor_count, processor_rank;
double *buff_H, *buff_send_H;
int N_pa_prim1, l, n, N_p0;
MPI_Status status;
MPI_Init (&argc, &argv);
MPI_Comm_size (MPI_COMM_WORLD, &processor_count);
MPI_Comm_rank (MPI_COMM_WORLD, &processor_rank);
N_pa_prim1=14; l=7; n=40; N_p0=7;
buff_H = new double [n*n*N_p0+1]; //Receive buffer allocation
buff_send_H = new double [n*n*N_p0+1]; //Send buffer allocation
for (int j = 0; j < n*n*N_p0+1; j++)
buff_send_H[j] = 1e-8*rand();
if (processor_rank == 0)
MPI_Send(buff_send_H, n*n*N_p0+1, MPI_DOUBLE, 1, 163, MPI_COMM_WORLD);
else if(processor_rank == 1)
MPI_Send(buff_send_H, n*n*N_p0+1, MPI_DOUBLE, 0, 163, MPI_COMM_WORLD);
MPI_Recv(buff_H, n*n*N_p0+1, MPI_DOUBLE, MPI_ANY_SOURCE, 163, MPI_COMM_WORLD, &status);
cout << "Received successfully by " << processor_rank << endl;
MPI_Finalize();
return 0;
}
The deadlocking is correct behaviour; you have a deadlock in your code.
The MPI Specification allows MPI_Send to behave as MPI_Ssend -- that is, to be blocking. A blocking communications primitive does not return until the communications "have completed" in some sense, which (in the case of a blocking send) probably means the receive has started.
Your code looks like:
If Processor 0:
Send to processor 1
If Processor 1:
Send to processor 0
Receive
That is -- the receive doesn't start until the sends have completed. You're sending, but they'll never return, because no one is receiving! (The fact that this works for small messages is an implementation artifact - most mpi implementations use so called a so-called "eager protocol" for "small enough" messages; but this can't be counted upon in general.)
Note that there are other logic errors here, too -- this program will also deadlock for more than 2 processors, as processors of rank >= 2 will be waiting for a message which never comes.
You can fix your program by alternating sends and receives by rank:
if (processor_rank == 0) {
MPI_Send(buff_send_H, n*n*N_p0+1, MPI_DOUBLE, 1, 163, MPI_COMM_WORLD);
MPI_Recv(buff_H, n*n*N_p0+1, MPI_DOUBLE, MPI_ANY_SOURCE, 163, MPI_COMM_WORLD, &status);
} else if (processor_rank == 1) {
MPI_Recv(buff_H, n*n*N_p0+1, MPI_DOUBLE, MPI_ANY_SOURCE, 163, MPI_COMM_WORLD, &status);
MPI_Send(buff_send_H, n*n*N_p0+1, MPI_DOUBLE, 0, 163, MPI_COMM_WORLD);
}
or by using MPI_Sendrecv (which is a blocking (send + receive), rather than a blocking send + a blocking receive):
int sendto;
if (processor_rank == 0)
sendto = 1;
else if (processor_rank == 1)
sendto = 0;
if (processor_rank == 0 || processor_rank == 1) {
MPI_Sendrecv(buff_send_H, n*n*N_p0+1, MPI_DOUBLE, sendto, 163,
buff_H, n*n*N_p0+1, MPI_DOUBLE, MPI_ANY_SOURCE, 163,
MPI_COMM_WORLD, &status);
}
Or by using non-blocking sends and receives:
MPI_Request reqs[2];
MPI_Status statuses[2];
if (processor_rank == 0) {
MPI_Isend(buff_send_H, n*n*N_p0+1, MPI_DOUBLE, 1, 163, MPI_COMM_WORLD, &reqs[0]);
} else if (processor_rank == 1) {
MPI_Isend(buff_send_H, n*n*N_p0+1, MPI_DOUBLE, 0, 163, MPI_COMM_WORLD, &reqs[0]);
}
if (processor_rank == 0 || processor_rank == 1)
MPI_Irecv(buff_H, n*n*N_p0+1, MPI_DOUBLE, MPI_ANY_SOURCE, 163, MPI_COMM_WORLD, &reqs[1]);
MPI_Waitall(2, reqs, statuses);
Thank you Jonathan for your help. Here I have chosen the third solution and written a similar code to yours except adding "for" loops to send a number of messages. This time it doesn't deadlock; however processors keep on receiving only the last message. (since the messages are long, I've only printed their last elements to check the consistency)
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
using namespace std;
int main(int argc, char *argv[])
{
int processor_count, processor_rank;
//Initialize MPI
MPI_Init (&argc, &argv);
MPI_Comm_size (MPI_COMM_WORLD, &processor_count);
MPI_Comm_rank (MPI_COMM_WORLD, &processor_rank);
double **buff_H, *buff_send_H;
int N_pa_prim1, l, n, N_p0, count, temp;
N_pa_prim1=5; l=7; n=50; N_p0=7;
MPI_Request reqs[N_pa_prim1];
MPI_Status statuses[N_pa_prim1];
buff_H = new double *[N_pa_prim1]; //Receive buffer allocation
for (int i = 0; i < N_pa_prim1; i++)
buff_H[i] = new double [n*n*N_p0+1];
buff_send_H = new double [n*n*N_p0+1]; //Send buffer allocation
if (processor_rank == 0) {
for (int i = 0; i < N_pa_prim1; i++){
for (int j = 0; j < n*n*N_p0+1; j++)
buff_send_H[j] = 2.0325e-8*rand();
cout << processor_rank << "\t" << buff_send_H[n*n*N_p0] << "\t" << "Send" << "\t" << endl;
MPI_Isend(buff_send_H, n*n*N_p0+1, MPI_DOUBLE, 1, 163, MPI_COMM_WORLD, &reqs[i]);
}
}
else if (processor_rank == 1) {
for (int i = 0; i < N_pa_prim1; i++){
for (int j = 0; j < n*n*N_p0+1; j++)
buff_send_H[j] = 3.5871e-8*rand();
cout << processor_rank << "\t" << buff_send_H[n*n*N_p0] << "\t" << "Send" << "\t" << endl;
MPI_Isend(buff_send_H, n*n*N_p0+1, MPI_DOUBLE, 0, 163, MPI_COMM_WORLD, &reqs[i]);
}
}
for (int i = 0; i < N_pa_prim1; i++)
MPI_Irecv(buff_H[i], n*n*N_p0+1, MPI_DOUBLE, MPI_ANY_SOURCE, 163, MPI_COMM_WORLD, &reqs[N_pa_prim1+i]);
MPI_Waitall(2*N_pa_prim1, reqs, statuses);
for (int i = 0; i < N_pa_prim1; i++)
cout << processor_rank << "\t" << buff_H[i][n*n*N_p0] << "\t" << "Receive" << endl;
MPI_Finalize();
return 0;
}

Resources