Hi All,

I am working on a C code parser, in which one of my requirements is to parse the C source and header files and calculate the Lines of Code. Though there are tools to do so, all of them have a problem wherein they treat function definitions and declarations with argument list on multiple lines as multiple lines rather than single line . For example

- 1-

/* Definition */
int Test ( int x,
int y


/* Declaration */
int Test( int x,
int y

is treated as 5 and 3 lines respectively rather than 3 and 1 line, i.e it should be treated as

- 2 -
/* Definition */
int Test ( int x, int y)

/* Declaration */
int Test( int x, int y );

To fix this I plan to make some modifications in my perl tool. I wish to use RecDescent to parse the input file, identify such constucts and then use perl script to convert these multiple line construct into single line contructs, so if construct - 1 - is given as input to the script then the output should be - 2 -. I found a script by Damian Conway, Helmut Jarausch and Teodor Zlatanov which uses the RecDescent to seperate comments from the c code.(also attached with the mail) The grammar used is

C_code : m{(
[^"/]+      # one or more non-delimiters
(            # then (optionally)...
/            # a potential comment delimiter
[^*/]       # which is not an actual delimiter
)?          #
)+          # all repeated once or more
{ $Code .= $item[1] }

comment : m{   \s*              # optional whitespace
//                # comment delimiter
[^\n]*           # anything except a newline
\n               # then a newline
{  $Code .= "\n"; $Comments .= $item[1] }
m{\s*                   # optional whitespace
/\*                     # comment opener
(?:[^*]+|\*(?!/))*   # anything except */
\*/                     # comment closer
([ \t]*)?              # trailing blanks or tabs
{ $Code .= " "; $Comments .= $item[1] }

I want to use the same metodology but rather than seperating the comments from the C code I want to use a grammar to identify such constructs and if any such construct is found covert them into the required output.

So please could someone help me with the grammar than can be used to identify the constructs and the way I can convert it into a single line.

Thanks in advance,

Rahul Jain
HCL Technologies Ltd.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprecdescent @
postedOct 9, '09 at 12:12p
activeOct 9, '09 at 12:12p

1 user in discussion

Rahul Jain: 1 post



site design / logo © 2019 Grokbase