Hi all,

I'm writing a code generator that needs to parse a YACC-like input file (including partially parsing the C/C++ parts of the head and tail sections), as well as a LEX-like input file.

I figured I would start with existing YACC and LEX grammars, and modify them to add my new features. Unfortunately, I'm having trouble finding suitable Perl tools for parsing YACC, LEX, C, and C++. (I know the latter two are hard--I only need a partial parsing.) Do anyone know where I can find grammars for these languages?

Here's what my research has uncovered:

Parsing a YACC file

Parsing a LEX file

  • I can't find a suitable grammar anywhere. :(
Parsing C/C++

  • I could try Inline::C::ParseRegExp or Inline::C::ParseRecDescent for C, and Inline::CPP::grammar for C++.
  • Parse::RecDescent has a pretty functional demo_Cgrammar.pl, but the demo_cpp.pl seems a bit limited.
  • PERCEPS (http://starship.python.net/crew/tbryan/PERCEPS/) is a Perl header file parser. Not sure how well it would work for the head section of a YACC or LEX input file.
In the worst case, both the bison and flex distributions have grammars, which I could adapt to suitable input for Parse::RecDescent or Parse::Yapp. But I can't help but think that someone has already done all this work.

I need to fully parse the YACC and LEX input files, but for the C and C++ code I just need to locate any definitions so that I can move them to an implementation file and replace them with "extern" declarations. (I'm generating multiple source files from the YACC/LEX files, instead of the normal one big implementation file.)

Thanks for the help!
David


In reply to In search of grammars for parsing by coppit

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.