Mathematica's core tree-oriented symbolic language makes it well suited to working with a hierarchical view of C code as Mathematica expressions. This supports the use of the Mathematica language for the creation, manipulation, and optimization of C code.

It is used extensively for Mathematica's code generation tools. In addition, you can use SymbolicC for your own code manipulation purposes.

To use SymbolicC you first need to load the package.

Click for copyable input

Now you can start to build up elements of a C program. The following represents a variable declaration.

Click for copyable input

An important feature of SymbolicC expressions is that they are inert; they evaluate to themselves, staying in an unevaluated form.

Here is an assignment and an entire function.

Click for copyable input

You can keep the function as an expression, or you can convert it into a string that shows the C code.

Click for copyable input

Importing from C Programs

At present Mathematica has no feature for importing from C programs. This has a number of challenges, such as how to work with the C preprocessor and dealing with context-sensitive parsing. Despite this, SymbolicC is very useful as a way to create C programs.

The C preprocessor adds difficulties, because it is appealing to represent it in SymbolicC, but there are programs that are hard to represent in a strict tree fashion. An example is shown below.

#ifdef __cplusplus
extern "C" {
int function( int arg);
#ifdef __cplusplus

To parse the C language you need to understand the meaning and context of the input. For example, in the following it is not clear whether this is a declaration of b, which is a pointer to type a, or a multiplication of two variables, a and b.

a * b


At present, SymbolicC only supports C syntax.

New to Mathematica? Find your learning path »
Have a question? Ask support »