Rectangle 27 5

Take a look at Structure and Interpretation of Computer Programs by Abelson and Sussman. While it doesn't show how to do it in C, it does demonstrate how to create types at run time and how to build an object system on top of a language that doesn't provide native support. Once you understand the basic ideas, you should be able to use structs and function pointers to create an implementation. Of course, looking at the source code for a C++ preprocessor will also be instructive. At one time, C++ was just a preprocessor for a C compiler.

Interesting answer! +1, I'm gonna read up on that too

I will have to buy it and give it a look myself. @Santiago I think you may find Nick's link (posted above) pretty useful. I've been looking over it for the last few hours and have had many of my questions answered.

design - how to program high level concepts (i.e. Object class, and Ge...

c design programming-languages assembly
Rectangle 27 4

They are in some regards as they enable some similar semantics. The C preprocessor has been used to enable generic data structures and algorithms (See token Concatination). However without considering any other features of C++ templates, it makes the whole generic programming game a LOT CLEARER to read and implement.

If anyone wants to see hardcore C only generic programming in action read the libevent sourcecode -- this is also mentioned here. A vast collection of container/algorithms are implemented, and its done in SINGLE header file (very readable). I really admire this, C++ template code (which I prefer for its other attributes) is VERY verbose.

Are C++ Templates just Macros in disguise? - Stack Overflow

c++ templates macros
Rectangle 27 4

They are in some regards as they enable some similar semantics. The C preprocessor has been used to enable generic data structures and algorithms (See token Concatination). However without considering any other features of C++ templates, it makes the whole generic programming game a LOT CLEARER to read and implement.

If anyone wants to see hardcore C only generic programming in action read the libevent sourcecode -- this is also mentioned here. A vast collection of container/algorithms are implemented, and its done in SINGLE header file (very readable). I really admire this, C++ template code (which I prefer for its other attributes) is VERY verbose.

Are C++ Templates just Macros in disguise? - Stack Overflow

c++ templates macros
Rectangle 27 2

There are very few tools that can parse and transform C and C++ code. As a first step, parsing of these languages is considered hard all by itself. Another key problem is the preprocessor, which is used pretty abusively (e.g., not in a structured way) in C programs and makes it difficult for a parser to see the program structure, and consequently difficult for an analyzer (needed to decide when refactorings are legal) to do its analysis correctly.

Modulo these glitches, our DMS Software Reengineering Toolkit can be configured to carry out refactorings on C and C++ code. DMS has industrial strength parsers for C and C++ and uniquely can capture most preprocessor uses as part of the internal code structures (typically people trying to parse C/C++ expand the preprocessor directives away, and that isn't an option for refactoring).

We've used it to carry out massive code reorganizations on C++ code (where preprocessor abuse is minimal because C++ has a variety of other ways to configure code). We also done some automated reengineering of C systems, but with somewhat more effort to handle the abusive preprocessor uses.

What it is not is interactive. You have to plan the refactoring transformations and specify them with a pattern matching language. But it is reliable.

c++ - Code refactoring - Stack Overflow

c++ refactoring automated-refactoring
Rectangle 27 0

Take a look at Structure and Interpretation of Computer Programs by Abelson and Sussman. While it doesn't show how to do it in C, it does demonstrate how to create types at run time and how to build an object system on top of a language that doesn't provide native support. Once you understand the basic ideas, you should be able to use structs and function pointers to create an implementation. Of course, looking at the source code for a C++ preprocessor will also be instructive. At one time, C++ was just a preprocessor for a C compiler.

Interesting answer! +1, I'm gonna read up on that too

I will have to buy it and give it a look myself. @Santiago I think you may find Nick's link (posted above) pretty useful. I've been looking over it for the last few hours and have had many of my questions answered.

design - how to program high level concepts (i.e. Object class, and Ge...

c design programming-languages assembly
Rectangle 27 0

If you already have a Boost dependency, you can use BOOST_FUSION_ADAPT_STRUCT and use Fusion to iterate over the members. This also allows you to work with types, which is not possible in a pure preprocessor approach.

All in all: Just write a constructor.

Thanks for the response pmr; we don't have a boost dependency yet, but I'll look into it anyway.

c++ - Use C preprocessor to iterate over structure fields - Stack Over...

c++ templates macros preprocessor
Rectangle 27 0

The preprocessor was originally necessary for C for several reasons. If I remember correctly, originally C did not have constants, so #define was needed to avoid magic numbers. Prior to 1999 C did not have inline functions, so again #define was used to create macros or "pseudo-functions" to save the overhead of a function call, while keeping code structured. C also doesn't have run-time or compile-time polymorphism, so #ifdefs were needed for conditional compilation. Compilers were typically not smart enough to optimize away unreachable code, so, again, #ifdefs were used to insert debugging or diagnostic code.

Using the preprocessor in C++ is a throwback to C, and is generally frowned upon. Language features, such as constants, inline functions, and templates can be used in most situations where in C you would have used the preprocessor.

The few cases where the use of a pre-processor in C++ is acceptable or even necessary include the guards for the header files, to prevent the same header from being included multiple times, #ifdef __cplusplus to use the same header for both C and C++, __FILE__ and __LINE__ for logging, and a few others.

The preprocessor is also often used for platform-specific definitions, although C++ Gotchas by Stephen Dewhurst advises having separate include directories for the platform specific definitions, and using them in separate build configurations for each platform.

That's not the only time preprocessor use in C++ is considered OK. Other uses widely (if not universally) considered OK include outputting __FILE__ and __LINE__ in logging, the Boost FOREACH macro, checking compiler feature support (including the classic #ifdef __cplusplus // extern "C" {// #endif), platform-specific definitions (e.g. where you need __declspec in MSVC and __attribute__ in GCC), defining NDEBUG before including <cassert>,...

c# - Why is preprocessor usage less common in languages other than C/C...

c# java c preprocessor
Rectangle 27 0

CScout version 2.8 offers a command-line option (-C) that will create a vim-compatible tags file for the C source code it will process. CScout is a source code analyzer and refactoring browser for collections of C programs. It can process workspaces of multiple projects mapping the complexity introduced by the C preprocessor back into the original C source code files. Consequently, the generated tags file contains correct information for entities, like functions, variables, and structures, that are created through preprocessor macros. CScout will process include files, using the include file paths specified in the code's project configuration file (the equivalent of a Makefile). To try it out, download the package matching your setup, go into the example directory containing the awk source code and run

../bin/cscout -Cc awk.cs

You can see some types of preprocessor constructs that CScout can handle in this page. For example, if you process the following code

#define typefun(name, type, op) \
type type ## _ ## name(type a, type b) { return a op b; }

typefun(add, int, +)
typefun(sub, int, -)
typefun(mul, int, *)
typefun(div, int, /)
typefun(add, double, +)
typefun(sub, double, -)
typefun(mul, double, *)
typefun(div, double, /)

main()
{
        printf("%d\n", int_add(5, 4));
        printf("%g\n", double_mul(3.14, 2.0));
}

CScout will generate a tags file with the following entries.

double_add  test.c  8       ;"  f
double_div  test.c  11      ;"  f
double_mul  test.c  10      ;"  f
double_sub  test.c  9       ;"  f
int_add test.c  4       ;"  f
int_div test.c  7       ;"  f
int_mul test.c  6       ;"  f
int_sub test.c  5       ;"  f
main    test.c  13      ;"  f
typefun test.c  1       ;"  d

You can try it out yourself by adding a few code-generating macros in the example source code to see the tags CScout will create.

Does CScout only work with C or does it work with C++ as well? Would be an interesting option, though C++ is a far more complex grammar than C.

Currently C (with gcc and Microsoft C extensions) and yacc/bison. As you say, C++ is a lot more complex.

vim - Is there an alternative to ctags that works better? - Stack Over...

vim ctags
Rectangle 27 0

Being less than 100 lines doesn't help you much; people can write a program that uses every feature of C language (and the compiler extensions!) in a program that size. So you are saying you have to deal with the entire C language (statements, expressions, functions, structures, typedefs, macros, preprocessor conditionals, ...). If this is a school project, I think that's too hard; you will need to limit your project to a interesting subset, say, functions, assignments, while statements and function calls.

  • The ability to climb over the AST
  • The ability to patch the AST
  • The ability to emit the AST as source text

Program transformations system are nearly ideal engines to accomplish this kind of work. These allow you to additionally do pattern-directed changes to the AST, which makes this much easier to implement.

You can probably do this with TXL or Stratego; I'm sure both have C parsers that will handle the limited subset of C that you should be willing to do.

You could do with this our DMS Software Reengineering Toolkit, although this is probably not suited for student use in short period of time. (TXL and Stratego are free, but might have steep learning curves, too).

But you will find this document on instrumenting code to build test coverage tools probably an ideal introduction to what you need to do to accomplish your task with a transformation system.

I think he wants a tracer not a profiler.

He can do that using instrumentation, but he'll have a lot of instrumentation to insert because it looks like the goal is tracing every action.

In that case, it would be easier if he had an interpreter, but he needs one that keeps the code structure around so that he can report the code structure as it runs (so CINT which compiles to a pcode wouldn't be the right answer).

What he needs is full C parsing to AST with symbol tables, an interpreter that executes the ASTs, and a prettyprinter so that he can convert appropriate parts of the AST he is currently executing back into text to report as progress.

DMS is still a very good foundation for this, as it has all the machinery needed to support this task too. Parse the C text, build the symbol table (all built into DMS for C) and then run a custom interpreter. This is easily coded as an interpreter over the AST; a big case statement over the tree node types would be the interpreter core, with node-type specific cases carrying out the action implied by the AST node (including climbing up or down the tree, updating symbol table entries with new values, computing expression intermediate results, and of course reporting progress.

ANTLR might work; it has a C parser but I'm not so sure about a prettyprinter. The interpreter would work pretty much the same way.

TXL and Stratego would likely be awkward for this, as it isn't clear how you'd use their pure-transformational style to alternatate interpreting and printing trace data.

For examples of short, complex code one might look at codegolf.SE (and the [code-golf] tag on Stack Overflow), other code golfing sites, or ioccc.

@ira thank u, will check that out... and ya the subset which u mentioned is actually enough.. Will try those tools and write back..

void main() {    int h=2,j=5,k=1;     while (h>j)     {         h++;         for (k=100;k>0;k/=2)         {             if (i<=5)             {                         d=n*10;                 p=n/10;             }         }     } }

sorry for the output, its vertical, i think it gives every1 a clear idea ! :)

@Hari: you should put additional information about the question, into your original question. You should be able to do that using the EDIT hyperlink directly under your question. I did it for you this time (maybe incorrectly?); yours to do in the future.

How to write a C or C++ program to detect modules in another C program...

c++ c parsing abstract-syntax-tree
Rectangle 27 0

#define DEPRECATE( var , explanation )   var __attribute__((availability(myFramework,introduced=1,deprecated=2.1,obsoleted=3.0, message= explanation)));

DEPRECATE ( typedef old_name new_name, please use new_name);

c preprocessor - How to generate a warning when a deprecated variable ...

warnings c-preprocessor gcc-warning
Rectangle 27 0

There are very few tools that can parse and transform C and C++ code. As a first step, parsing of these languages is considered hard all by itself. Another key problem is the preprocessor, which is used pretty abusively (e.g., not in a structured way) in C programs and makes it difficult for a parser to see the program structure, and consequently difficult for an analyzer (needed to decide when refactorings are legal) to do its analysis correctly.

Modulo these glitches, our DMS Software Reengineering Toolkit can be configured to carry out refactorings on C and C++ code. DMS has industrial strength parsers for C and C++ and uniquely can capture most preprocessor uses as part of the internal code structures (typically people trying to parse C/C++ expand the preprocessor directives away, and that isn't an option for refactoring).

We've used it to carry out massive code reorganizations on C++ code (where preprocessor abuse is minimal because C++ has a variety of other ways to configure code). We also done some automated reengineering of C systems, but with somewhat more effort to handle the abusive preprocessor uses.

What it is not is interactive. You have to plan the refactoring transformations and specify them with a pattern matching language. But it is reliable.

c++ - Code refactoring - Stack Overflow

c++ refactoring automated-refactoring
Rectangle 27 0

See this answer, which shows how to iterate over the members of a class. Then using those macros, the two classes can be reflectable like this:

struct S
{
    REFLECTABLE
    (
        (double) a,
        (double) b,
        (double) c
    )
};
class C
{
private:
    REFLECTABLE
    (
        (double) a,
        (double) b,
        (double) c
    )
public: 
    void set_a(double a);
    void set_b(double b);
    void set_c(double c); 
};

Then to create a generic assignment, based on the name of the member variable:

struct assign_fields_visitor
{
    template<class FieldData1, class FieldData2>
    void operator()(FieldData1 fd1, FieldData2 fd2)
    {
        if (strcmp(fd1.name(), fd2.name()) == 0)
        {
            fd1.get() = fd2.get();
        }
    }
};

struct assign_fields
{
    template<class X, class FieldData>
    void operator()(X & x, FieldData f)
    {
        visit_each(x, boost::bind(assign_fields_visitor(), f, _1));
    }
};

template<class L, class R>
void assign(L & lhs, const R& rhs)
{
    visit_each(rhs, boost::bind(assign_fields(), boost::ref(lhs), _1));
}

Finally, it can be called like this:

S s; C c;
assign(c, s);

c++ - Use C preprocessor to iterate over structure fields - Stack Over...

c++ templates macros preprocessor
Rectangle 27 0

They are in some regards as they enable some similar semantics. The C preprocessor has been used to enable generic data structures and algorithms (See token Concatination). However without considering any other features of C++ templates, it makes the whole generic programming game a LOT CLEARER to read and implement.

If anyone wants to see hardcore C only generic programming in action read the libevent sourcecode -- this is also mentioned here. A vast collection of container/algorithms are implemented, and its done in SINGLE header file (very readable). I really admire this, C++ template code (which I prefer for its other attributes) is VERY verbose.

Are C++ Templates just Macros in disguise? - Stack Overflow

c++ templates macros
Rectangle 27 0

In C language, you can use a preprocessor directive #pragma to check sizeof structure without padding..

#pragma pack(1)
sizeof

This pragma must come before the structure declaration, I don't want to affect actual code.

#pragma pack

c++ - How to check the size of struct w/o padding? - Stack Overflow

c++ c gcc