Re: Writing a C++ Style Checker
- From: ids <ishan.desilva@xxxxxxxxx>
- Date: Thu, 20 Sep 2007 04:52:07 -0000
On Sep 19, 9:18 pm, Ben Morrow <b...@xxxxxxxxxxxx> wrote:
This is going to be seriously hard work. What you need is a parser for
C++, and as C++ is a *very* complex language this is not going to be
easy to get right, unless you are content to only recognize simple
constructions without parsing the code properly.
There is a Parse::RecDescent grammar for some subset of C++ included in
the Inline::CPP distribution. You may find it useful to start there.
Alternatively, you may be able to persuade your compiler to do the
parsing for you. While some of your criteria above (such as blocks on
ifs) will be lost by such an approach, you may be able to handle these
with a relatively simple parser, leaving the hard work of 'is this
identifier a local or member variable' to the compiler.
What you need to do is either persuade your compiler to produce some
intermediate parsed form of output (such as from gcc's -fdump-* and -d*
options), or compile objects with debugging info and then parse that.
One example of a (now very old) program that does this is c2ph in the
Perl distribution, which was intended to allow access to C structures by
parsing stabs debugging information.
Are there any existing Perl based style checkers?
There is Perl::Critic, for style-checking Perl, but that is based on the
excellent PPI, a Perl module that parses Perl, which took a *lot* of
work to produce.
Basically: you have set yourself an *extremely* hard problem :(. You can
either produce a very 'shallow' and rather incomplete solution, or
produce a proper solution only after a lot of work. OTOH, a proper C++
parser for Perl would probably be a good thing... :)
Well, I guess what I need is something in between the two ends that
two of you proposed.
What BenB suggested is not sufficient. Applying pattern matchings on a
line by line basis is not going help. For example, it won't allow me
to recognize a function implementation.
In a BNF grammar we can define a function using something similar to
the following.
func_impl : type_spec IDENTIFIER '(' opt_param_list ')' block ;
block : '{' opt_statement_list '}' ;
// define the other non terminals here
In order to do this, you need to *build state* as and when you scan
through. i.e. you need to remember that you saw the opening brace in a
previous line and you find the matching end brace in another line
below. This gets even difficult because of nested blocks. So you need
to keep pushing and popping braces.
Building of state requires some well organized data structures. I can
visualize how this is to be done with a language like C++. What you
need is a set of classes with a suited inheritance structure. I don't
know how to do it with Perl. That's why asked about a mental model. I
suppose Perl does support the OO paradigm, but I didn't find any
material to read about it. I mean, I would like to read "OOA/D with
Perl" sort of thing.
The alternative here is to use Flex/Bison with C++. The problem is the
complexity of the grammar to handle C++. Why I thought I would try
with Perl is because of the powerful pattern matching ability. But,
whether I use Flex/Bison or I use Perl, the need to parse the grammar
is still there. That's what BenM has said.
I will try starting off with a very simple thing and then expanding
it.
Thanks for your help.
Cheers,
Ishan.
.
- Follow-Ups:
- Re: Writing a C++ Style Checker
- From: Ben Morrow
- Re: Writing a C++ Style Checker
- From: Ted Zlatanov
- Re: Writing a C++ Style Checker
- From: Ben Bullock
- Re: Writing a C++ Style Checker
- References:
- Writing a C++ Style Checker
- From: ids
- Re: Writing a C++ Style Checker
- From: Ben Morrow
- Writing a C++ Style Checker
- Prev by Date: Re: Invoking a CGI method in external program trashes form params
- Next by Date: Re: Writing a C++ Style Checker
- Previous by thread: Re: Writing a C++ Style Checker
- Next by thread: Re: Writing a C++ Style Checker
- Index(es):
Relevant Pages
|