Non-greedy match

  • Clint M. Frederickson

    Maybe I'm just missing something here, but I don't see a way to do a non-greedy match. For example, I am trying to match:

    "if(condition) {"
    but I am running into cases where the code actually looks like

    "if(condition) return 0; <other code> {"

    I want a minimal match here.

    Thanks in advance for your response and for all your hard work on pyparsing!

    • Paul McGuire

      Paul McGuire - 2005-09-08

      If you are parsing a language such as C, you'll need to handle the case where the if statement is just a single statement, not enclosed in {}'s, as shown in your message.  In this case, it looks like the '{' that you are picking up has nothing to do with the if(condition) part.

      I'm guessing you have something like (this is about 2% of what a complete C grammar would look like):

      stmt = Forward()
      if_stmt = Keyword('if') + LPAREN + expression + RPAREN + stmt + Optional( Keyword('else') + stmt )
      for_stmt = Keyword('for') + ...
      while_stmt = Keyword('while') + ...
      assignment = varName + '=' + expression + SEMI
      stmt << if_stmt | for_stmt | while_stmt | assignment | ( '{' + ZeroOrMore(stmt) + '}'

      But to really answer your question, I'll need to see a bit more of your grammar, and the original BNF if it is not a common language like C/C++/Java/C#.

      If in fact you *are* doing a language parser for one of these languages, I'll caution you that it will be a significant project.  The Verilog parser took me about 8-10 weeks, and I had a very clear BNF to work from, and I was already familiar with pyparsing before I started.

      In any event, best of luck to you, write back if you need more help, and thanks for using pyparsing!

      -- Paul


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks