The question title might be misleading when read out of context. Let me first explain what I am trying to build.
I am building a script which will take 100s of very simple C programs written by my students and check for some very basic properties such as.
- Have they declared a variable called ‘x’, is it’s type ‘int’ and so on.
- what is the value of variable ‘z’ ?
If this was some kind of scripting programming language this could have been a lot easier. I could have simply used include or eval and then done the checks.
But when it comes to C programming I would say this is very tough. How can I do it ?
You can use ANTLR to do this. There is already a C grammar you can use with ANTLR so mainly all you have to do is pass the code into antlr then walk the syntax tree looking for various attributes….
you can use ANTLR from a number of languages. While it might seem daunting at first. It’s actually surprisingly easy to work with.