PyParsing throws Exception in one string while not in another case

33 views Asked by At

2.2.1
I am a newcomer to PyParsing. I use Python 3.12 on Ubuntu 20.04 and installed PyParsing using pip install

I have 2 different outcomes (an exception from 1 ) and no exception from from 2 different texts . I have enabled setName and the diagnostics messages. The question I have is that both texts are similar but have different outcomes. Any help to fix this will be greatly appreciated

In case of Text 2 no exception is raised This is the code that I have

    prasnaS.setName("prasna")
    parser = englishPreface +invocation +titleLine+pp.ZeroOrMore(EmptyLine)+pp.OneOrMore(prasnaS)+EmptyLine
    result =parser.parse_string(text)

Text 1

Match prasna at loc 155535(2247,2)
  
   ^
Match prasna failed, ParseException raised: Expected prasna, found end of text  (at char 155535), (line:2247, col:2)
Exception  <traceback object at 0x7f53a00739c0>
Traceback (most recent call last):
  File "//home/xxxxx/projects/aksr/doc2txt.py", line 158, in <module>
    result =parser.parse_string(text)
            ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "//home/xxxxx/projects/aksr/myenv1/lib/python3.12/site-packages/pyparsing/core.py", line 1197, in parse_string
    raise exc.with_traceback(None)
pyparsing.exceptions.ParseException: , found end of text  (at char 155535), (line:2247, col:2)

Text 2:

Match prasna at loc 179582(1977,1)
  
  ^
Match prasna failed, ParseException raised: , found end of text  (at char 179585), (line:1980, col:1)
1

There are 1 answers

0
Harihara Vinayakaram On

I removed the EmptyLine in the end and it worked. Now the parser looks like

    parser = englishPreface +invocation +titleLine+pp.ZeroOrMore(EmptyLine)+pp.OneOrMore(prasnaS)

I am still not able to understand why and would appreciate any explanation