Parsing C header files to extract information about data types, functions and function arguments
up vote
1
down vote
favorite
I have a C header file. I want to parse it and extract information about data types, functions and functions arguments. Who can help me? I need some example in C.
Thank you very much.
c parsing header-files
add a comment |
up vote
1
down vote
favorite
I have a C header file. I want to parse it and extract information about data types, functions and functions arguments. Who can help me? I need some example in C.
Thank you very much.
c parsing header-files
Related: stackoverflow.com/questions/2721071/…
– Nemo
Jul 13 '11 at 1:37
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a C header file. I want to parse it and extract information about data types, functions and functions arguments. Who can help me? I need some example in C.
Thank you very much.
c parsing header-files
I have a C header file. I want to parse it and extract information about data types, functions and functions arguments. Who can help me? I need some example in C.
Thank you very much.
c parsing header-files
c parsing header-files
edited Jul 13 '11 at 2:53
Vinicius Kamakura
6,70312137
6,70312137
asked Jul 13 '11 at 1:30
user828975
1213
1213
Related: stackoverflow.com/questions/2721071/…
– Nemo
Jul 13 '11 at 1:37
add a comment |
Related: stackoverflow.com/questions/2721071/…
– Nemo
Jul 13 '11 at 1:37
Related: stackoverflow.com/questions/2721071/…
– Nemo
Jul 13 '11 at 1:37
Related: stackoverflow.com/questions/2721071/…
– Nemo
Jul 13 '11 at 1:37
add a comment |
6 Answers
6
active
oldest
votes
up vote
3
down vote
You could try Clang. In special The Lexer and Preprocessor Library.
1
Recently, the most important and useful feature of Clang, namely its XML printer, was removed for some weird reasons. But it is still possible to either use and older version of Clang or to re-apply that removed patch to the current version.
– SK-logic
Jul 13 '11 at 7:23
@SK-logic Why was the XML printer the most important and useful feature? It was removed because using it was usually slower than the direct API calls (you're parsing one file, generating an AST, re-parsing the AST in a language, XML, which is slow to parse, and then operating on it).
– kirbyfan64sos
Mar 25 '15 at 19:40
@kirbyfan64sos, because a usablelibclang
did not exist back then.
– SK-logic
Mar 28 '15 at 0:27
add a comment |
up vote
3
down vote
Use ANTLR. There's a decent grammar for C already written for you, and ANTLR will generate C code (or some other languages if you prefer), which you can then traverse to get what you want.
+1 for parser generators generally, I've never used ANTLR, but Lex + Yacc (or equivalently: Flex + Bison) have been useful to me
– tobyodavies
Jul 13 '11 at 1:42
I've used ANTLR (v2 though, not v3), in production, in two or three projects. It's very good.
– John Zwinck
Jul 13 '11 at 1:44
1
A parser will not extract type information. You need symbol table support implemented for that. As a practical matter, you have to run the preprocessor, too. So an ANTLR grammar by itself won't help you. There may be an ANTLR grammar which parses and build symbol tables; I'm not specificially familiar with one.
– Ira Baxter
Jul 13 '11 at 4:04
... the specific C grammar referenced in this answer says it only keeps track of type definition names, that is, it does not do symbol table construction, let alone type analysis.
– Ira Baxter
Jul 13 '11 at 4:18
1
@RadLexus: Thanks for the note--I've fixed the link.
– John Zwinck
Jan 23 '17 at 3:04
|
show 1 more comment
up vote
0
down vote
The DMS Software Reengineering Toolkit with its C Front End can do this.
DMS provides general purpose parsing, symbol table construction, flow analysis, and program transformations, parameterized by a language definition. Using DMS's C front end, DMS will parse any of a variety of C dialects, builds ASTs for the code elements, builds full symbol tables doing complete name and type resolution of all symbols (including parameter lists in function headers); you can stop there and dump those out. DMS can also do control and data flow analysis on the C code; you can use othe DMS facilities to further analyze or transform the code. (The C front end has a full C preprocessor built-in).
The EDG front end can also be used for parsing and symbol tables, but does not have the other capabilities of DMS.
add a comment |
up vote
0
down vote
Yet another option is to use the c2xml tool from "sparse". Its C parser isn't 100% standard-compliant (e.g. it won't parse K&R-style declarations), but for reasonably modern C code it works quite well.
add a comment |
up vote
0
down vote
There is also srcml.
Similar to c2xml it uses source code directly.
c2xml starts from preprocessor output.
Assume good C coding rules (as opposed to arbitrary use of preprocessing) this has been an advantage for my re-engineering tasks, as it preserves the names of #defines and being able to process selected macros in a specific way.
add a comment |
up vote
0
down vote
If you need a human-readable output (e.g. in html or PDF), then you can use doxygene/doxywizard. In doxywizard "All entities" has to be selected.
add a comment |
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
You could try Clang. In special The Lexer and Preprocessor Library.
1
Recently, the most important and useful feature of Clang, namely its XML printer, was removed for some weird reasons. But it is still possible to either use and older version of Clang or to re-apply that removed patch to the current version.
– SK-logic
Jul 13 '11 at 7:23
@SK-logic Why was the XML printer the most important and useful feature? It was removed because using it was usually slower than the direct API calls (you're parsing one file, generating an AST, re-parsing the AST in a language, XML, which is slow to parse, and then operating on it).
– kirbyfan64sos
Mar 25 '15 at 19:40
@kirbyfan64sos, because a usablelibclang
did not exist back then.
– SK-logic
Mar 28 '15 at 0:27
add a comment |
up vote
3
down vote
You could try Clang. In special The Lexer and Preprocessor Library.
1
Recently, the most important and useful feature of Clang, namely its XML printer, was removed for some weird reasons. But it is still possible to either use and older version of Clang or to re-apply that removed patch to the current version.
– SK-logic
Jul 13 '11 at 7:23
@SK-logic Why was the XML printer the most important and useful feature? It was removed because using it was usually slower than the direct API calls (you're parsing one file, generating an AST, re-parsing the AST in a language, XML, which is slow to parse, and then operating on it).
– kirbyfan64sos
Mar 25 '15 at 19:40
@kirbyfan64sos, because a usablelibclang
did not exist back then.
– SK-logic
Mar 28 '15 at 0:27
add a comment |
up vote
3
down vote
up vote
3
down vote
You could try Clang. In special The Lexer and Preprocessor Library.
You could try Clang. In special The Lexer and Preprocessor Library.
answered Jul 13 '11 at 2:50
Vinicius Kamakura
6,70312137
6,70312137
1
Recently, the most important and useful feature of Clang, namely its XML printer, was removed for some weird reasons. But it is still possible to either use and older version of Clang or to re-apply that removed patch to the current version.
– SK-logic
Jul 13 '11 at 7:23
@SK-logic Why was the XML printer the most important and useful feature? It was removed because using it was usually slower than the direct API calls (you're parsing one file, generating an AST, re-parsing the AST in a language, XML, which is slow to parse, and then operating on it).
– kirbyfan64sos
Mar 25 '15 at 19:40
@kirbyfan64sos, because a usablelibclang
did not exist back then.
– SK-logic
Mar 28 '15 at 0:27
add a comment |
1
Recently, the most important and useful feature of Clang, namely its XML printer, was removed for some weird reasons. But it is still possible to either use and older version of Clang or to re-apply that removed patch to the current version.
– SK-logic
Jul 13 '11 at 7:23
@SK-logic Why was the XML printer the most important and useful feature? It was removed because using it was usually slower than the direct API calls (you're parsing one file, generating an AST, re-parsing the AST in a language, XML, which is slow to parse, and then operating on it).
– kirbyfan64sos
Mar 25 '15 at 19:40
@kirbyfan64sos, because a usablelibclang
did not exist back then.
– SK-logic
Mar 28 '15 at 0:27
1
1
Recently, the most important and useful feature of Clang, namely its XML printer, was removed for some weird reasons. But it is still possible to either use and older version of Clang or to re-apply that removed patch to the current version.
– SK-logic
Jul 13 '11 at 7:23
Recently, the most important and useful feature of Clang, namely its XML printer, was removed for some weird reasons. But it is still possible to either use and older version of Clang or to re-apply that removed patch to the current version.
– SK-logic
Jul 13 '11 at 7:23
@SK-logic Why was the XML printer the most important and useful feature? It was removed because using it was usually slower than the direct API calls (you're parsing one file, generating an AST, re-parsing the AST in a language, XML, which is slow to parse, and then operating on it).
– kirbyfan64sos
Mar 25 '15 at 19:40
@SK-logic Why was the XML printer the most important and useful feature? It was removed because using it was usually slower than the direct API calls (you're parsing one file, generating an AST, re-parsing the AST in a language, XML, which is slow to parse, and then operating on it).
– kirbyfan64sos
Mar 25 '15 at 19:40
@kirbyfan64sos, because a usable
libclang
did not exist back then.– SK-logic
Mar 28 '15 at 0:27
@kirbyfan64sos, because a usable
libclang
did not exist back then.– SK-logic
Mar 28 '15 at 0:27
add a comment |
up vote
3
down vote
Use ANTLR. There's a decent grammar for C already written for you, and ANTLR will generate C code (or some other languages if you prefer), which you can then traverse to get what you want.
+1 for parser generators generally, I've never used ANTLR, but Lex + Yacc (or equivalently: Flex + Bison) have been useful to me
– tobyodavies
Jul 13 '11 at 1:42
I've used ANTLR (v2 though, not v3), in production, in two or three projects. It's very good.
– John Zwinck
Jul 13 '11 at 1:44
1
A parser will not extract type information. You need symbol table support implemented for that. As a practical matter, you have to run the preprocessor, too. So an ANTLR grammar by itself won't help you. There may be an ANTLR grammar which parses and build symbol tables; I'm not specificially familiar with one.
– Ira Baxter
Jul 13 '11 at 4:04
... the specific C grammar referenced in this answer says it only keeps track of type definition names, that is, it does not do symbol table construction, let alone type analysis.
– Ira Baxter
Jul 13 '11 at 4:18
1
@RadLexus: Thanks for the note--I've fixed the link.
– John Zwinck
Jan 23 '17 at 3:04
|
show 1 more comment
up vote
3
down vote
Use ANTLR. There's a decent grammar for C already written for you, and ANTLR will generate C code (or some other languages if you prefer), which you can then traverse to get what you want.
+1 for parser generators generally, I've never used ANTLR, but Lex + Yacc (or equivalently: Flex + Bison) have been useful to me
– tobyodavies
Jul 13 '11 at 1:42
I've used ANTLR (v2 though, not v3), in production, in two or three projects. It's very good.
– John Zwinck
Jul 13 '11 at 1:44
1
A parser will not extract type information. You need symbol table support implemented for that. As a practical matter, you have to run the preprocessor, too. So an ANTLR grammar by itself won't help you. There may be an ANTLR grammar which parses and build symbol tables; I'm not specificially familiar with one.
– Ira Baxter
Jul 13 '11 at 4:04
... the specific C grammar referenced in this answer says it only keeps track of type definition names, that is, it does not do symbol table construction, let alone type analysis.
– Ira Baxter
Jul 13 '11 at 4:18
1
@RadLexus: Thanks for the note--I've fixed the link.
– John Zwinck
Jan 23 '17 at 3:04
|
show 1 more comment
up vote
3
down vote
up vote
3
down vote
Use ANTLR. There's a decent grammar for C already written for you, and ANTLR will generate C code (or some other languages if you prefer), which you can then traverse to get what you want.
Use ANTLR. There's a decent grammar for C already written for you, and ANTLR will generate C code (or some other languages if you prefer), which you can then traverse to get what you want.
edited Jan 23 '17 at 3:03
answered Jul 13 '11 at 1:35
John Zwinck
148k16175286
148k16175286
+1 for parser generators generally, I've never used ANTLR, but Lex + Yacc (or equivalently: Flex + Bison) have been useful to me
– tobyodavies
Jul 13 '11 at 1:42
I've used ANTLR (v2 though, not v3), in production, in two or three projects. It's very good.
– John Zwinck
Jul 13 '11 at 1:44
1
A parser will not extract type information. You need symbol table support implemented for that. As a practical matter, you have to run the preprocessor, too. So an ANTLR grammar by itself won't help you. There may be an ANTLR grammar which parses and build symbol tables; I'm not specificially familiar with one.
– Ira Baxter
Jul 13 '11 at 4:04
... the specific C grammar referenced in this answer says it only keeps track of type definition names, that is, it does not do symbol table construction, let alone type analysis.
– Ira Baxter
Jul 13 '11 at 4:18
1
@RadLexus: Thanks for the note--I've fixed the link.
– John Zwinck
Jan 23 '17 at 3:04
|
show 1 more comment
+1 for parser generators generally, I've never used ANTLR, but Lex + Yacc (or equivalently: Flex + Bison) have been useful to me
– tobyodavies
Jul 13 '11 at 1:42
I've used ANTLR (v2 though, not v3), in production, in two or three projects. It's very good.
– John Zwinck
Jul 13 '11 at 1:44
1
A parser will not extract type information. You need symbol table support implemented for that. As a practical matter, you have to run the preprocessor, too. So an ANTLR grammar by itself won't help you. There may be an ANTLR grammar which parses and build symbol tables; I'm not specificially familiar with one.
– Ira Baxter
Jul 13 '11 at 4:04
... the specific C grammar referenced in this answer says it only keeps track of type definition names, that is, it does not do symbol table construction, let alone type analysis.
– Ira Baxter
Jul 13 '11 at 4:18
1
@RadLexus: Thanks for the note--I've fixed the link.
– John Zwinck
Jan 23 '17 at 3:04
+1 for parser generators generally, I've never used ANTLR, but Lex + Yacc (or equivalently: Flex + Bison) have been useful to me
– tobyodavies
Jul 13 '11 at 1:42
+1 for parser generators generally, I've never used ANTLR, but Lex + Yacc (or equivalently: Flex + Bison) have been useful to me
– tobyodavies
Jul 13 '11 at 1:42
I've used ANTLR (v2 though, not v3), in production, in two or three projects. It's very good.
– John Zwinck
Jul 13 '11 at 1:44
I've used ANTLR (v2 though, not v3), in production, in two or three projects. It's very good.
– John Zwinck
Jul 13 '11 at 1:44
1
1
A parser will not extract type information. You need symbol table support implemented for that. As a practical matter, you have to run the preprocessor, too. So an ANTLR grammar by itself won't help you. There may be an ANTLR grammar which parses and build symbol tables; I'm not specificially familiar with one.
– Ira Baxter
Jul 13 '11 at 4:04
A parser will not extract type information. You need symbol table support implemented for that. As a practical matter, you have to run the preprocessor, too. So an ANTLR grammar by itself won't help you. There may be an ANTLR grammar which parses and build symbol tables; I'm not specificially familiar with one.
– Ira Baxter
Jul 13 '11 at 4:04
... the specific C grammar referenced in this answer says it only keeps track of type definition names, that is, it does not do symbol table construction, let alone type analysis.
– Ira Baxter
Jul 13 '11 at 4:18
... the specific C grammar referenced in this answer says it only keeps track of type definition names, that is, it does not do symbol table construction, let alone type analysis.
– Ira Baxter
Jul 13 '11 at 4:18
1
1
@RadLexus: Thanks for the note--I've fixed the link.
– John Zwinck
Jan 23 '17 at 3:04
@RadLexus: Thanks for the note--I've fixed the link.
– John Zwinck
Jan 23 '17 at 3:04
|
show 1 more comment
up vote
0
down vote
The DMS Software Reengineering Toolkit with its C Front End can do this.
DMS provides general purpose parsing, symbol table construction, flow analysis, and program transformations, parameterized by a language definition. Using DMS's C front end, DMS will parse any of a variety of C dialects, builds ASTs for the code elements, builds full symbol tables doing complete name and type resolution of all symbols (including parameter lists in function headers); you can stop there and dump those out. DMS can also do control and data flow analysis on the C code; you can use othe DMS facilities to further analyze or transform the code. (The C front end has a full C preprocessor built-in).
The EDG front end can also be used for parsing and symbol tables, but does not have the other capabilities of DMS.
add a comment |
up vote
0
down vote
The DMS Software Reengineering Toolkit with its C Front End can do this.
DMS provides general purpose parsing, symbol table construction, flow analysis, and program transformations, parameterized by a language definition. Using DMS's C front end, DMS will parse any of a variety of C dialects, builds ASTs for the code elements, builds full symbol tables doing complete name and type resolution of all symbols (including parameter lists in function headers); you can stop there and dump those out. DMS can also do control and data flow analysis on the C code; you can use othe DMS facilities to further analyze or transform the code. (The C front end has a full C preprocessor built-in).
The EDG front end can also be used for parsing and symbol tables, but does not have the other capabilities of DMS.
add a comment |
up vote
0
down vote
up vote
0
down vote
The DMS Software Reengineering Toolkit with its C Front End can do this.
DMS provides general purpose parsing, symbol table construction, flow analysis, and program transformations, parameterized by a language definition. Using DMS's C front end, DMS will parse any of a variety of C dialects, builds ASTs for the code elements, builds full symbol tables doing complete name and type resolution of all symbols (including parameter lists in function headers); you can stop there and dump those out. DMS can also do control and data flow analysis on the C code; you can use othe DMS facilities to further analyze or transform the code. (The C front end has a full C preprocessor built-in).
The EDG front end can also be used for parsing and symbol tables, but does not have the other capabilities of DMS.
The DMS Software Reengineering Toolkit with its C Front End can do this.
DMS provides general purpose parsing, symbol table construction, flow analysis, and program transformations, parameterized by a language definition. Using DMS's C front end, DMS will parse any of a variety of C dialects, builds ASTs for the code elements, builds full symbol tables doing complete name and type resolution of all symbols (including parameter lists in function headers); you can stop there and dump those out. DMS can also do control and data flow analysis on the C code; you can use othe DMS facilities to further analyze or transform the code. (The C front end has a full C preprocessor built-in).
The EDG front end can also be used for parsing and symbol tables, but does not have the other capabilities of DMS.
edited Jul 13 '11 at 4:15
answered Jul 13 '11 at 4:08
Ira Baxter
79.5k10128257
79.5k10128257
add a comment |
add a comment |
up vote
0
down vote
Yet another option is to use the c2xml tool from "sparse". Its C parser isn't 100% standard-compliant (e.g. it won't parse K&R-style declarations), but for reasonably modern C code it works quite well.
add a comment |
up vote
0
down vote
Yet another option is to use the c2xml tool from "sparse". Its C parser isn't 100% standard-compliant (e.g. it won't parse K&R-style declarations), but for reasonably modern C code it works quite well.
add a comment |
up vote
0
down vote
up vote
0
down vote
Yet another option is to use the c2xml tool from "sparse". Its C parser isn't 100% standard-compliant (e.g. it won't parse K&R-style declarations), but for reasonably modern C code it works quite well.
Yet another option is to use the c2xml tool from "sparse". Its C parser isn't 100% standard-compliant (e.g. it won't parse K&R-style declarations), but for reasonably modern C code it works quite well.
answered Jul 28 '11 at 7:12
Alon Ziv
1
1
add a comment |
add a comment |
up vote
0
down vote
There is also srcml.
Similar to c2xml it uses source code directly.
c2xml starts from preprocessor output.
Assume good C coding rules (as opposed to arbitrary use of preprocessing) this has been an advantage for my re-engineering tasks, as it preserves the names of #defines and being able to process selected macros in a specific way.
add a comment |
up vote
0
down vote
There is also srcml.
Similar to c2xml it uses source code directly.
c2xml starts from preprocessor output.
Assume good C coding rules (as opposed to arbitrary use of preprocessing) this has been an advantage for my re-engineering tasks, as it preserves the names of #defines and being able to process selected macros in a specific way.
add a comment |
up vote
0
down vote
up vote
0
down vote
There is also srcml.
Similar to c2xml it uses source code directly.
c2xml starts from preprocessor output.
Assume good C coding rules (as opposed to arbitrary use of preprocessing) this has been an advantage for my re-engineering tasks, as it preserves the names of #defines and being able to process selected macros in a specific way.
There is also srcml.
Similar to c2xml it uses source code directly.
c2xml starts from preprocessor output.
Assume good C coding rules (as opposed to arbitrary use of preprocessing) this has been an advantage for my re-engineering tasks, as it preserves the names of #defines and being able to process selected macros in a specific way.
answered Nov 11 '17 at 10:57
ngong
196
196
add a comment |
add a comment |
up vote
0
down vote
If you need a human-readable output (e.g. in html or PDF), then you can use doxygene/doxywizard. In doxywizard "All entities" has to be selected.
add a comment |
up vote
0
down vote
If you need a human-readable output (e.g. in html or PDF), then you can use doxygene/doxywizard. In doxywizard "All entities" has to be selected.
add a comment |
up vote
0
down vote
up vote
0
down vote
If you need a human-readable output (e.g. in html or PDF), then you can use doxygene/doxywizard. In doxywizard "All entities" has to be selected.
If you need a human-readable output (e.g. in html or PDF), then you can use doxygene/doxywizard. In doxywizard "All entities" has to be selected.
answered Nov 9 at 21:53
user3694063
12
12
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6673148%2fparsing-c-header-files-to-extract-information-about-data-types-functions-and-fu%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Related: stackoverflow.com/questions/2721071/…
– Nemo
Jul 13 '11 at 1:37