halley has asked for the wisdom of the Perl Monks concerning the following question:

I have searched Google and likely Perl sites in vain; I now ask my fellow Monks before I do some heavy manual lifting.

My task is to do a bunch of UCA (unfamiliar code analysis) on several huge repositories of Ada95 and GNAT source code files. In UCA cases, we often don't know what a given program does, or what it might depend on, or even if it will build successfully. Rarely is there documentation to be found.

A very helpful technique I've employed in UCA missions involving FORTRAN and C++ is to partially parse each source file, and generate a call-tree graph. I then pump that graph through a color-coded GraphViz template. Some hand-tweaking of that graph can bring order from chaos, at least enough to figure out the scope of complexity and interaction in a given codebase.

I am not very familiar with Ada code in practice, but most of it can be read for the basics after a one-day primer on the web. The real and complete Ada language appears to be pretty darn tricky to parse, however, when you get into all of the various historical tweaks. The best LALR grammar I've found is Ada Home's Ada95 LALR Grammar. It's not complete, though, since some of the rule names are not actually defined here.

Before I go through the trouble of converting that LALR grammar to something that Parse::RecDescent can parse, even partially, I ask if anyone else has something better than the aforementioned grammar, for use with Perl. Even if it's not RecDescent, anything Perl would help immensely.

I'll publish the working PRD version if I have to build it myself. I wish there were a central place where people could publish useful PRD grammars, besides maybe the CPAN Inline:: namespace.

--
[ e d @ h a l l e y . c c ]

Replies are listed 'Best First'.
Re: Parse::RecDescent Ada95 Grammar?
by Corion (Patriarch) on Mar 10, 2004 at 16:19 UTC

    I think the P::RD mailing list is the place to ask, but Parse::RecDescent is slow for anything a bit more complex.

    As you already have a yacc grammar, I'd opt for Parse::YAPP, because YAPP can use yacc grammars. You lose the nice "autotree" directive though and have to write that one yourself, but that's not so bad, as all grammar work is already done...

Re: Parse::RecDescent Ada95 Grammar?
by flyingmoose (Priest) on Mar 10, 2004 at 15:57 UTC
    To avoid converting the grammar, maybe (untested) Parse::YAPP would work better. I've struggled with converting a grammer to Parse::RecDescent format before and I didn't like the process. I have not tried YAPP yet, but it appears to accept yacc-grammars more or less raw, which would be a good thing. Unfortunately, the documentation is not as good, and it appears less widely used. So, YMMV...
Re: Parse::RecDescent Ada95 Grammar?
by stvn (Monsignor) on Mar 10, 2004 at 19:02 UTC
    ed,

    I would actually recommend against your approach. Not because it isn't a good one, but because Ada is a HUGE language, and (as you have found) tricky to parse. Also, there are alot of Ada tools out there already, and some that may just fit your needs. Here is what I found on Google:

    Again, I would really recomend using a pre-existing tool, your likely to get much more accurate results/information. And given that you are likely working for either a Defense contractor or Aerospace firm of some kind (who the hell else uses Ada), accuracy of information, I imagine, is of paramount importance.

    Again, let me re-iterate this is nothing wrong with you approach, I just don't recomend it for Ada. IMHO Ada is just to large and complex a language for this kind of approach.

    NOTE:
    The grammer you found is also not 100% standard Backus-Naur form either. This page describes the differences. So along with the conversion from a slightly off Backus-Naur you would need to massage it for either yacc, Parse::YAPP or Parse::RecDescent. I see alot of opportunity for loss of imformation.

    -stvn
Re: Parse::RecDescent Ada95 Grammar?
by Heretyk (Scribe) on Mar 11, 2004 at 00:28 UTC

    You may well find it significantly easier to use the output of gnatxref, rather than attempting to parse Ada95 yourself. This will mean that you need to be able to compile the code that you're analysing with gnat, in order to generate the cross-reference information, but since you mentioned gnat in your post, I'm assuming that you already have it. The gnat documentation contains a lot of potentially useful information.

    Good luck.

Re: Parse::RecDescent Ada95 Grammar?
by Anonymous Monk on Mar 10, 2004 at 23:58 UTC
    Here are more links.

    Graphical navigation of syntax:
    http://cui.unige.ch/db-research/Enseignement/analyseinfo/Ada95/BNFindex.html

    Left recursion issue discussion:
    http://www.cslab.ece.ntua.gr/~pappas/epfl/lgl/doc/adastruct.html