# Parsing data using dhaka (http://dhaka.rubyforge.org/)

**URL:** <https://www.ruby-forum.com/t/parsing-data-using-dhaka-http-dhaka-rubyforge-org/91104>\
**Category:** Ruby\
**Created:** [April 4, 2007, 7:25pm UTC](https://www.ruby-forum.com/t/parsing-data-using-dhaka-http-dhaka-rubyforge-org/91104 "2007-04-04T19:25:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![emmanuel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3e96dc/32.png) [@emmanuel](https://www.ruby-forum.com/u/emmanuel)\
**Post date:** [April 4, 2007, 7:25pm UTC](https://www.ruby-forum.com/t/parsing-data-using-dhaka-http-dhaka-rubyforge-org/91104/1 "2007-04-04T19:25:37Z")

</div>

Hello! I requested help from Mushfeq, the author of dhaka, but he seems  
to be busy Is anyone familiar with dhaka? ([http://dhaka.rubyforge.org/](http://dhaka.rubyforge.org/))

My problem is:

I succesfully built my lexer, my grammar and a parser for the grammar:  
“NaturalParser \< Dhaka::CompiledParsed”.

Now i can do:

lexed\_program= NaturalLexer.lex(source\_code) #Natural is the  
language name  
parse\_result = NaturalParser.parse(lexed\_program)

And all is ok. Now i want to pretty print the source code: My objective  
is to beautify the source code, colorize keywords, and extract some info  
(ala-doxygen but for my language).

> > > > How do you sugest me to use the parse\_result object?\<\<\<\<

I think in this case a Dhaka::Evaluator would be very messy (i’m trying  
unseccessfully to built one NaturalPrettyPrinter \< Dhaka::Evaluator  
Object), the thing i’m thinking in is doing a tree walker for the  
parse\_result object and printing the nodes only if they are leafs, but i  
don’t get how to do it!

* * *

## Thank you very much! Here’s the original message i send to the author of dhaka:

Mushfeq:

Hello, is Emmanuel again. I have succesfully written a grammar for the  
actual “code-part” of the language i’m parsing. (my language is divided  
in a define-data part and a code part). For the define data part i  
followed your advice and use groups of regular expressions. For the code  
part, i have built a Dhaka::CompiledParser.

My goal is to build a doxygen-like documentor for my ugly,  
uglier-than-COBOL “enterprisey” language 😉 .

Now, I have to choose how to continue. Since i want to pretty-print the  
source code and extract some information while doing it, what would be  
your advice?:

Build a tree walker to the Dhaka::CompiledParser#parse result  
or  
use a Dhaka::Evaluator?

If i choose a Dhaka::Evaluator, how would you sugest i can use it for my  
task?  
Thank you verry much!!!

Emmanuel O.

[emmanueloga.blogspot.com](http://emmanueloga.blogspot.com)

----- Original Message -----  
From: Mushfeq K.  
To: Emmanuel O.  
Sent: Friday, March 23, 2007 2:10 AM  
Subject: Re: Dhaka Help

Emmanuel,

I don’t envy your situation. This doesn’t look like a pleasant language  
to have to deal with. 🙂

It seems that Natural uses integer prefixes to specify levels of  
nesting. This has serious problems. Basically you can’t detect nesting  
at the parsing state, which means that you have to figure it out at the  
evaluation stage.

This is only a sketch - I haven’t tried running this.

DEFINE, DATA, LOCAL and END\_DEFINE are keywords.

for\_symbol(‘data\_definition’) do  
definition\_block %w| DEFINE DATA LOCAL definitions END-DEFINE|  
end

for\_symbol(‘definitions’) do  
single\_definition %w| definition |  
multiple\_definitions %w| definitions opt\_newline definition |  
end

for\_symbol(‘definition’) do  
definition %w| int\_literal word\_literal opt\_type |  
end

for\_symbol(‘opt\_type’) do  
no\_type %||  
type %| ( word\_literal ) |  
end

You can certainly pursue this, but since you’re detecting nesting by  
looking at the value of the int\_literal in the expansion for definition  
(the parser doesn’t know that ‘2’ is a deeper level of nesting than  
‘1’), the evaluator does most of the work. At that point it seems that  
you might even be better off processing the thing line by line and using  
regexes and capture groups on each line (Ruby’s regexes are much faster  
than Dhaka’s regexes - the difference between a C implementation and a  
Ruby one) to extract the information.

Hope this helps.

Mushfeq.

On 3/22/07, Emmanuel O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
Hello Mushfeq, i wan’t to thank you for publishig such a usefull library  
(dhaka)!

I have a difficult parser to implement, and i wish you could helpme with  
some issues. Thank you in advance if you decide to read the whole thing  
🙂 I wanted to ask you if you have more example grammars, specially one  
that can parse a typed language.

I’m dealing with an horrible such-called 4th generation language that i  
want to parse,  
(softwareag’s Natural [www.softwareag.com](http://www.softwareag.com) if you are curious, some tells  
me that is similar to COBOL).

My objective is to program a doxygen-like code analyzer to help me  
navigate my one-and-a-half-million-of-natural-lines code base.

This is what a tipycal chunk of natural looks like:  
DEFINE DATA  
LOCAL  
1 CUSTOMER VIEW OF PRO\_CUSTOMER  
2 CUSTOMER\_NO  
2 METH\_OF\_DELIVERY  
2 NO\_COPIES  
2 UPD\_PROGRAM  
END-DEFINE

RC. READ CUSTOMER BY CUST\_STATUS\_NO  
IF UPD\_PROGRAM EQ ‘INVOICE’  
ESCAPE TOP  
END-IF

```
GC. GET CUSTOMER *ISN (RC.)
MOVE 'FACTURA' TO UPD_PROGRAM
SUBTRACT 1 FROM NO_COPIES
UPDATE (GC.)
END TRANSACTION

```

END  
I decided to fragment my grammar in two:  
one grammar for the DEFINE-DATA … END-DEFINE part  
other grammar for the body (the actual code).  
Do you think dhaka would be suitable for such a language?

How do you sugest i can implement the grammar for the structured  
variable definition? i.e. which grammar would parse this? :  
1 MY\_VAR (A5)  
2 MY VAR\_1 (A2)  
2 MY\_VAR\_2 (A3)  
In natural, this means:  
if MY\_VAR, an alphanumeric variable of length 4 has the string “HELLO”,  
MY\_VAR\_1 is ==“HE” and  
MYVAR\_2 is == “LLO”  
A can ve anything from A1 to A255

etc…

THANK YOU VERY MUCH IN ADVANCE 🙂

Emmanuel O.,  
[http://emmanueloga.blogspot.com](http://emmanueloga.blogspot.com)

---

<div class="post-metadata">

**Author:** ![emmanuel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3e96dc/32.png) [@emmanuel](https://www.ruby-forum.com/u/emmanuel)\
**Post date:** [April 5, 2007, 2:08am UTC](https://www.ruby-forum.com/t/parsing-data-using-dhaka-http-dhaka-rubyforge-org/91104/2 "2007-04-05T02:08:28Z")

</div>

Emmanuel,

Apologies for not replying sooner. I’m going to answer this question off  
the  
list.

Mushfeq.
