开发者

Reflections for C language?

The qu开发者_Go百科estion 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.

  1. Have they declared a variable called 'x', is it's type 'int' and so on.
  2. 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.


I saw an initiative to make an XML dumper for the tree built by GCC, called gcc-xml. So you give it a file example1.cxx like:

struct EmptyClass {};

int a_function(float f, EmptyClass e)
{
}

int main(void)
{
    return 0;
}

You'll get back:

<?xml version="1.0"?>
<GCC_XML>
  <Namespace id="_1" name="::" members="_2 _3 _4 "/>
  <Function id="_2" name="main" returns="_5" context="_1" location="f0:8"/>
  <Function id="_3" name="a_function" returns="_5" context="_1" location="f0:4">
    <Argument name="f" type="_6"/>
    <Argument name="e" type="_4"/>
  </Function>
  <Struct id="_4" name="EmptyClass" context="_1" location="f0:1" members="_7 _8 " bases=""/>
  <FundamentalType id="_5" name="int"/>
  <FundamentalType id="_6" name="float"/>
  <Constructor id="_7" name="EmptyClass" context="_4" location="f0:1">
    <Argument name="_ctor_arg" type="_9"/>
  </Constructor>
  <Constructor id="_8" name="EmptyClass" context="_4" location="f0:1"/>
  <ReferenceType id="_9" type="_4c"/>
  <File id="f0" name="example1.cxx"/>
</GCC_XML>

Caveats would be that it only works with the subset of C compatible with C++, and the official project doesn't support the dumping of function bodies. I don't know how much progress the unofficial efforts have made:

http://www.djlauk.de/index.php/Projects/GccXmlFunctionBodies


An important distinction is what you mean by "checking" these programs. I assume you don't mean actually compiling/running them during runtime (which is what reflection means to me), because you can't check their program structure from a compiled program.

That means you're going to be text processing the source files, and while this is much easier in pretty much every scripting language, it's not that bad in C:

  1. Figure out how to get a list of all the files to be processed. Either put all the file names in an index file, and iterate over that file, OR get a list of files by using readdir or preferably a library (Boost) to do it for you.

  2. For each file, open it and read each line (Google, it's trivial)

  3. For each line, check it against your rules, and collect the necessary results.

  4. Store result in an array, or write it to a file, etc.

Edit-If you want to check to see if your students' programs run (aka, compile/run them during runtime), you'd probably have to execve off some gcc calls (if you want to compile), and then again to run the program. However, execve is only going to tell you if the command failed. Getting output from other programs would mean opening pipes with popen. If you find yourself ready to do this, turn back, you've gone too far.


Reflection from inside the language is always limited by the language designers (the designers of C simply don't allow any). If you use a tool that can inspect any part of the language from outside the language, you don't have the problem of being limited by what the language designers imagined. (All the current languages with reflection limit what what you can learn by writing code in the language. This seems to me to be a stupid constraint.)

The way to analyze a program is to an agent the understands the programming language from outside, and doesn't have any such limitations. See our DMS Software Reengineering Toolkit for a system that can provide the ultimate in program inspection. Using its C Front End you can arguably ask any well-formulated question about the C source code in a reliable way.

The specific questions you ask could be answered easily by DMS and its C front end (the one about the value of Z might be hard; you're reasoning about a Turing machine).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜