how to analyze a C header file? [duplicate] - c

This question already exists:
Is it possible to have gcc analyze my code and give me info about it [closed]
Closed 2 years ago.
I want to write a script that analyzes a C header file.
The header contains a few declarations of functions
The scrip needs to understand how many arguments each function has, and what are their types.
can gcc (or other tool) do this?

It comes down to how accurate you want to be when parsing a C header file. You could implement the C grammar for a function declaration and be guaranteed to parse it 100% correctly, but it will be a lot of work.
Conversely, a simple regex matcher is the easiest approach, but could have false-positive matches. You can implement a regex parser using C++, python or even other scripting languages such as bash or powershell. I would suggest python as its beginner friendly.
If you don't know what regex is, it's a simple language used to parse strings and extract data from strings. So you could create a regex expression to find the c function declaration then use that expression to pull out all of the arguments. Should be straight forward.
Create a regex expression on https://regex101.com/ and test it against teat strings, then implement that expression in python to do the work.

Related

python "print" like function in c [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am new to C and I use to code in python, and I usually print too much variables and text to test my code and printf statement is irritating sometimes. I want to write a function in c which works exactly like print function in python.
I am facing two problem while writing the function.
I want to take n number of arguments as input to the function.
I want to take any data type as input to the function,
for Eg: print(12); print("hello"); print(123.12) should not raise error.
What have I done so Far
I found solution for taking n number of arguments using <stdarg.h>, but I have to specify the data type of the first argument which is not what I want.
"generic selection" Macros can be used to call different function on different data type of argument passed but not sure how it can be done, here is the link which I used for reference.
I want to write a function in c which works exactly like print function in python.
You simply cannot do that (because of type erasure : at runtime, type information is lost in C). Read Modern C then see this C reference.
In practice, you'll better write one function per datatype in C to print it. So it would be void print_int(int); to print an integer, void print_double(double); to print a double, etc. Once you have a collection of such functions you might use _Generic inside a macro (but that is rarely useful; it can handle a finite set of types).
Study for inspiration the source code of existing open source C software on github or elsewhere. Look for inspiration inside the source code of GNU bash, sqlite, GTK or GNU bison (and perhaps inside the source code of Python; half of the Python interpreter is coded in C)
Read also the documentation of your C compiler, e.g. GCC. So compile with all warnings and debug info: gcc -Wall -Wextra -g then use the GDB debugger.
Perhaps you want to implement some tagged union abstract data type in C. Then take inspiration from GNU guile or the runtime of Ocaml and dive inside their source code.
Once you are more familiar with C, read some C draft standard, e.g. n2176
Consider using Frama-C or the Clang static analyzer.
Consider also sometimes generating some C code, like SWIG does.
Budget weeks of work.

Can I use pattern matching in C program? [duplicate]

This question already has answers here:
Regular expressions in C: examples?
(5 answers)
Closed 6 years ago.
I am novice to C programming. Is it possible to match pattern inside the C string, like any built in functions?
I am using Red Hat Linux and I want to check if a string starts with abc: or def: followed by 10 digit numbers # chars. Something like : (abc|def):([0-9]{10})#([A-Za-z0-9]*).
Is there any C built in function which I can use to check this pattern matching.
Thanks for your help.
You can use POSIX regex matching in linux. See man 3 regex for more details.
If you are looking for a fast, safe and threaded library you can use re2 library from Google with support like pre-compiled regex.
(https://github.com/google/re2)
Linux and POSIX have regex(3) functions. You'll need to "compile" -that is prepare by transforming- your regular expression string using regcomp first.
In some simple cases, you might also use other string functions, like strstr, strchr, sscanf(3) (the %n format specifier might be handy, to know how much bytes you have scanned).
You could also consider some lexing utility, perhaps using flex to generate a tokenizer able to recognize your regexp.

How to write a C program for Unix that can process the Regular Expression ?

I want creat a C program for Unix that can porcess the Regular Expression,like a simple Perl interpreter. Do I have to personally write an regexp engine?
http://www.gnu.org/software/libc/manual/html_node/Regular-Expressions.html
You might like to look for a library with it already coded. C uses librarys to allow you to include other peoples code into your work. I assume there are many regular expression librarys for you to use already to go!

Write a compiler from scratch in C [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to code a compiler in C?
How would I start writing a compiler from scratch (no Flex or Bison or Lex or Yacc) in C? I have a language that I wrote an interpreter for, and it's kind of like Forth. Sort of. It takes in symbols and interprets them one at a time, using a stack.
How would I make a compiler?
That wasn't a particularly spammy bit; just to show people the syntax and simplicity.
http://github.com/tekknolagi/StackBased
Simple!
You tokenize the input.
You build a proper representation of it, generally this is an Abstract Syntax Tree, but that is not required.
You perform any tree transformations you may require (optional).
You generate the code by walking the tree.
You link any disparate portions together (optional)
Flex and Bison help with stage 1 and 2, everything else is up to you. If you're still stuck, I suggest going through "Programming Language Pragmatics" or The Dragon Book.

Parsing C header files to extract information about data types, functions and function arguments

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.
You could try Clang. In special The Lexer and Preprocessor Library.
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.
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.
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.
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.
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.

Resources