• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC SKILL
  3. Code to strip outer parentheses

Stats

  • Locked Locked
  • Replies 8
  • Subscribers 143
  • Views 17072
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Code to strip outer parentheses

CADcasualty
CADcasualty over 7 years ago

Hi,

I am calling a routine that returns either a number or a string (the routine is figuring out a parent parameter defined in some upper parent in the hierarchy). The problem is that if a string is returned it is surrounded by a set of parentheses for every hierarchical level that was traversed finding the parent parameter. So the routine could return a straight number, or it could return some text surrounded by an arbitrary amount of parentheses e.g. "blah" or "(blah)" or "((((((blah))))))".

I can't edit the original routine and I'm stumped trying to figure out a way to remove all outer pairs of parentheses - can anybody point me to some code that does this. Please note that I only want to remove OUTER pairs of parentheses i.e. if I had "(((blah(0))))" then I'd want to end up with "blah(0)".

Many thanks in advance.

  • Cancel
Parents
  • skillUser
    skillUser over 7 years ago

    Hi,

    I think this is what you wanted :

    procedure(removeParens(item)
      cond(
        (stringp(item) removeParens(readstring(item)))
        (listp(item) mapcan('removeParens item))
        (t list(item))
      )
    )

    Results:

    str = "((((((blah))))))"
    removeParens(str)
    => (blah)

    str = "((((((blah(0)))))))"
    removeParens(str)
    => (blah 0)

    You could check to see if there is a cdr() of the returned result, if so return the whole result, otherwise return the car of the result:

    result = removeParens(str)
    if(cdr(result) result car(result))
    => blah OR (blah 0)


    Hopefully this helps?

    Regards,

    Lawrence.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • CADcasualty
    CADcasualty over 7 years ago in reply to skillUser

    Thanks Lawrence - this is almost what I want! I need matching pairs of OUTER parentheses removed. Specifically:

    if I had "(((blah(0))))" then I'd want to end up with "blah(0)"

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Reply
  • CADcasualty
    CADcasualty over 7 years ago in reply to skillUser

    Thanks Lawrence - this is almost what I want! I need matching pairs of OUTER parentheses removed. Specifically:

    if I had "(((blah(0))))" then I'd want to end up with "blah(0)"

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Children
  • Andrew Beckett
    Andrew Beckett over 7 years ago in reply to CADcasualty

    I'd use a pattern matching approach rather than relying on the SKILL parser. Otherwise if you have unusual characters in there, Lawrence's code would probably get a bit upset. Something like this would work:

    procedure(CCFremoveOuterParens(str)
      unless(CCFremoveOuterParens.pat
        CCFremoveOuterParens.pat=pcreCompile("^\\((.*)\\)$")
      )
      if(pcreExecute(CCFremoveOuterParens.pat str) then
        CCFremoveOuterParens(pcreSubstitute(CCFremoveOuterParens.pat "\\1"))
      else
        str
      )
    )
    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • skillUser
    skillUser over 7 years ago in reply to CADcasualty

    But the blah(0) is reformatted to (blah 0) - they are the same in SKILL or LISP.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 7 years ago in reply to skillUser
    skillUser said:
    But the blah(0) is reformatted to (blah 0) - they are the same in SKILL or LISP

    Not sure the strings are necessarily SKILL though? What if you tried to do it on "((blah(+)))" (as a silly example)? That's why I went for the pattern matching approach - it's safer.

    Andrew.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • skillUser
    skillUser over 7 years ago in reply to Andrew Beckett

    True - I was just repurposing something that I had (and based on the assumption that the string would be parse-able by SKILL) rather than writing something new... Your solution is cleaner and safer and much less likely to choke.


    Cheers,

    Lawrence.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 7 years ago in reply to skillUser
    skillUser said:
    True - I was just repurposing something that I had (and based on the assumption that the string would be parse-able by SKILL) rather than writing something new

    Lawrence,

    I'm always a fan of taking advantage of the SKILL parser if you can - so it was a good idea! That's why I tend to format config files for utilities I write in a way that can be easily be slurped in by lineread() for example...

    Andrew.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • CADcasualty
    CADcasualty over 7 years ago in reply to skillUser

    Perfect! Thanks Andrew. BTW - the routine I was referring to was abIPar (which you wrote). Many thanks again!

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information