Introduction

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.

In[1]:=
Click for copyable input

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

In[2]:=
Click for copyable input
Out[2]=

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.

In[3]:=
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.

In[5]:=
Click for copyable input
Out[5]=

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" {
#endif
int function( int arg);
#ifdef __cplusplus
}
#endif

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

C++

At present, SymbolicC only supports C syntax.

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