Menu

#7 No ParseException when using ZeroOrMore()

closed
nobody
None
5
2007-12-14
2007-12-05
Anonymous
No

Hello,

I find that the use of ZeroOrMore() seems to suppress
exceptions. The following little script illustrates this

==============================
from pyparsing import *

expression = Word(alphas) + ZeroOrMore("+" + Word(alphas))

if __name__ == "__main__":

try:
results = expression.parseString('a-b')
print results
except Exception, inst:
print inst

===============================

results in ['a'] being printed to screen.
If I redefine

expression = Word(alphas) + "+" + Word(alphas)

I do get an exception as expected:
%Expected "+" (at char 1), (line:1, col:2)

Is there some way to still get the ParseException?

Thanks in advance,

RJB

Discussion

  • Paul McGuire

    Paul McGuire - 2007-12-05

    Logged In: YES
    user_id=893320
    Originator: NO

    Do you mean that the input must have at least one "+" followed by a Word(alphas)? Then use OneOrMore instead of ZeroOrMore.

    Or do you mean that "a-b" is not valid because of that pesky "-b" on the end? In that case, modify your grammar to show that, after the ZeroOrMore("+" + Word(alphas) you expect to be at the end of the input string. Do this by appending StringEnd() to your grammar:

    expression = Word(alphas) + ZeroOrMore("+" + Word(alphas)) + StringEnd()

    You can see more discussion of this at http://pyparsing-public.wikispaces.com/FAQs#tocFAQs1.

    Welcome to pyparsing!
    -- Paul

     
  • rjbe

    rjbe - 2007-12-13

    Logged In: YES
    user_id=1952450
    Originator: NO

    Thanks for the reply, Paul.

    Actually, I prepared this little example just to illustrate the problem that I had.

    I was working on a VHDL parser when I found that the parser would not complain when
    I artificially introduced errors in e.g. a port list (where ZeroOrMore is applicable).
    I found that OneOrMore and ZeroOrMore behave the same in that respect.

    Your suggestion of adding the StringEnd() now makes the parser raise an exception when
    introducing a syntax error inside a section where ZerOrMore is applicable.
    The information about where the error is is not very helpful though, but this is less of a
    problem for me (at least for the moment)

    Thanks again!

    RJB

     
  • Paul McGuire

    Paul McGuire - 2007-12-14

    Logged In: YES
    user_id=893320
    Originator: NO

    Ok, I will close this request then.

    The problem of exceptions not really reporting the exact location of a syntax error is a confounding problem. I've taken several attempts at it, but it is an issue with the fundamental design of pyparsing, and Zero/OneOrMore are the usual suspects. I'm glad this is not a showstopper for you (at least for the moment).

    -- Paul

     
  • Paul McGuire

    Paul McGuire - 2007-12-14
    • status: open --> closed
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.