# Ruby and parsing

**URL:** <https://www.ruby-forum.com/t/ruby-and-parsing/52149>\
**Category:** Ruby\
**Created:** [January 11, 2006, 1:35pm UTC](https://www.ruby-forum.com/t/ruby-and-parsing/52149 "2006-01-11T13:35:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![matrixise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@matrixise](https://www.ruby-forum.com/u/matrixise)\
**Post date:** [January 11, 2006, 1:35pm UTC](https://www.ruby-forum.com/t/ruby-and-parsing/52149/1 "2006-01-11T13:35:46Z")

</div>

Hi all,

Firstly, Happy New Year 😛

Secondly, I have a c++ source code with many “try catch”.

try {  
  
}  
catch (Exception &pException) {  
\<BODY OF CATCH (for Exception)\>  
}

I would like to parse my file and add a ‘catch’ with std::exception  
automatically,  
to get the next example.

try {  
  
}  
catch (Exception &pException) {  
\<BODY OF CATCH (for Exception)\>  
}  
/// New catch added by a ruby script.  
catch (std::exception &pException) {  
\<Fill the body of this catch with the body of catch (for Exception).\>  
}

How can I do it ? Because I find some problems ☹

1. Try to parse a lot of line with Regexp.
2. In the body of a ‘catch’, it can contains brackets

Best Regards,

Stephane

---

<div class="post-metadata">

**Author:** ![matrixise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@matrixise](https://www.ruby-forum.com/u/matrixise)\
**Post date:** [January 11, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/ruby-and-parsing/52149/2 "2006-01-11T16:46:24Z")

</div>

Stephane W. wrote:

> }
> 
> Best Regards,
> 
> Stephane

I don’t think Regexp is a viable alternative for this one - you might  
want to implement a trivial recursive-descent parser for C++, counting  
the opening and closing brackets after each catch clause. Unless the  
bodies of the exception handlers contain unmatched brackets in strings,  
that should work.

David V.

---

<div class="post-metadata">

**Author:** ![matrixise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@matrixise](https://www.ruby-forum.com/u/matrixise)\
**Post date:** [January 12, 2006, 8:39am UTC](https://www.ruby-forum.com/t/ruby-and-parsing/52149/3 "2006-01-12T08:39:56Z")

</div>

> I don’t think Regexp is a viable alternative for this one - you might  
> want to implement a trivial recursive-descent parser for C++, counting  
> the opening and closing brackets after each catch clause. Unless the  
> bodies of the exception handlers contain unmatched brackets in strings,  
> that should work.  
> Thanks David,

I will try this solution during my week.

Best Regards,

Stephane
