# Code Parsing

**URL:** <https://www.ruby-forum.com/t/code-parsing/135850>\
**Category:** Ruby\
**Created:** [April 25, 2008, 5:58pm UTC](https://www.ruby-forum.com/t/code-parsing/135850 "2008-04-25T17:58:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbermejo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@rbermejo](https://www.ruby-forum.com/u/rbermejo)\
**Post date:** [April 25, 2008, 5:58pm UTC](https://www.ruby-forum.com/t/code-parsing/135850/1 "2008-04-25T17:58:48Z")

</div>

Dear ruby-list /.

## I have a text I file where I need to parse out all Table data-text similar to the one below.

more code  
…  
Table MyTable (real a, real b) {

```
settings="default";
a= 0 {
  max= { 0,0,0,0}
  mix = { 0,0,0,0}
}
b= 1 {
  max= { 0,0,0,0}
  mix = { 0,0,0,0}
}
c= 2 {
  max= { 0,0,0,0}
  mix = { 0,0,0,0}
}
d= 3 {
  max= { 0,0,0,0}
  mix = { 0,0,0,0}
}

```

}

## … more code

The table code could have different number of elements (a,b,c,d).  
I know one approach would be to count the number of brackets found until  
you match your 1st bracket.  
I suspect there can be many elegant solutions to this problem , so any  
charming idea/solution is very welcomed.

-ronnie bermejo.

---

<div class="post-metadata">

**Author:** ![rbermejo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@rbermejo](https://www.ruby-forum.com/u/rbermejo)\
**Post date:** [April 25, 2008, 7:41pm UTC](https://www.ruby-forum.com/t/code-parsing/135850/2 "2008-04-25T19:41:13Z")

</div>

On Fri, 25 Apr 2008 10:58:49 -0500, Rodrigo B. wrote:

> ```
> a= 0 {
> }
> 
> ```
> 
> The table code could have different number of elements (a,b,c,d). I know  
> one approach would be to count the number of brackets found until you  
> match your 1st bracket.  
> I suspect there can be many elegant solutions to this problem , so any  
> charming idea/solution is very welcomed.
> 
> -ronnie bermejo.

Ruby Q. #155 Parsing JSON ([Ruby Quiz - Parsing JSON (#155)](http://www.rubyquiz.com/quiz155.html)) dealt  
with this. Look there to see whether there are any good ideas there. If  
this isn’t JSON, then your best bet may be to learn how to use a CFG  
parser.

---

<div class="post-metadata">

**Author:** ![rbermejo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@rbermejo](https://www.ruby-forum.com/u/rbermejo)\
**Post date:** [April 26, 2008, 1:50am UTC](https://www.ruby-forum.com/t/code-parsing/135850/3 "2008-04-26T01:50:32Z")

</div>

Ken B. wrote:

> On Fri, 25 Apr 2008 10:58:49 -0500, Rodrigo B. wrote:
> 
> > ```
> > a= 0 {
> > }
> > 
> > ```
> > 
> > The table code could have different number of elements (a,b,c,d). I know  
> > one approach would be to count the number of brackets found until you  
> > match your 1st bracket.  
> > I suspect there can be many elegant solutions to this problem , so any  
> > charming idea/solution is very welcomed.
> > 
> > -ronnie bermejo.
> 
> Ruby Q. #155 Parsing JSON ([Ruby Quiz - Parsing JSON (#155)](http://www.rubyquiz.com/quiz155.html)) dealt  
> with this. Look there to see whether there are any good ideas there. If  
> this isn’t JSON, then your best bet may be to learn how to use a CFG  
> parser.

Thanks for the pointers Ken.  
I found strscan …so my 1st implementation is:  
require ‘strscan’

table\_file = StringScanner.new( File.read(“code”) )  
while line = table\_file.scan\_until(/\w+|\W+/)  
table=line  
if ( line =~ /Table/ ) and  
( /\w+.+(.+)/ =~ line = table\_file.scan\_until(/{/) )  
left\_brackets=1  
right\_brackets=0  
table \<\< line  
temp=“”  
until left\_brackets == right\_brackets do  
temp = table\_file.getbyte  
case temp  
when “}”  
right\_brackets+=1  
when “{”  
left\_brackets+=1  
end  
table \<\< temp  
end  
puts table  
puts “\*\*\*\*\*\*\*”  
end  
end
