Skip to main content

Online c parser

That is simple parser of C language code. which work in valid C code. And generates a syntax tree to find the expression result.

Examples

Here are some examples to give an idea of what kind of results this tool produces.

Simple expression

C code which define some simple statement, such as variable declaration and display some text etc.

C Code

#include <stdio.h>
int main()
{
    // Main logic here
    int a = 0;
    int b = a + 10;
    printf("%d", b);
    return 0;
}

Result

{
  "source": [
    {
      "fundecl": {
        "name": "main",
        "param": [],
        "kind": 4,
        "type": "int",
        "signed": false,
        "body": [
          {
            "compound": [
              {
                "comment": [
                  "// Main logic here"
                ]
              },
              {
                "decl": {
                  "name": "a",
                  "kind": 4,
                  "type": "int",
                  "signed": false,
                  "init": true,
                  "assing": [
                    {
                      "0": "constant",
                      "value": 0,
                      "type": "int"
                    }
                  ]
                }
              },
              {
                "decl": {
                  "name": "b",
                  "kind": 4,
                  "type": "int",
                  "signed": false,
                  "init": true,
                  "assing": [
                    {
                      "0": "binary",
                      "left": {
                        "exp": [
                          {
                            "0": "identifier",
                            "name": "a",
                            "kind": 4,
                            "type": "int",
                            "signed": false
                          }
                        ],
                        "kind": 4,
                        "type": "int",
                        "signed": false
                      },
                      "op": "+",
                      "right": {
                        "exp": [
                          {
                            "0": "constant",
                            "value": 10.3,
                            "type": "double"
                          }
                        ],
                        "kind": 8,
                        "type": "double",
                        "signed": false
                      },
                      "kind": 8,
                      "type": "double",
                      "signed": false
                    }
                  ]
                }
              },
              {
                "0": "funcall",
                "name": "printf",
                "kind": 4,
                "type": "int",
                "signed": false,
                "param": [
                  [
                    {
                      "0": "constant",
                      "value": "%d",
                      "type": "string"
                    }
                  ],
                  [
                    {
                      "0": "identifier",
                      "name": "b",
                      "kind": 4,
                      "type": "int",
                      "signed": false
                    }
                  ]
                ]
              },
              [
                "return",
                [
                  {
                    "0": "constant",
                    "value": 0,
                    "type": "int"
                  }
                ]
              ]
            ]
          }
        ]
      }
    },
    [
      "include",
      [
        "stdio.h"
      ]
    ],
    [
      "localfun",
      [
        "main"
      ]
    ]
  ]
}

Struct example

C code

/*
    C program for
    Insert linked list node at beginning of linked list
*/
#include <stdio.h>
#include <stdlib.h>

// Linked List LinkNode
struct LinkNode
{
    int data;
    struct LinkNode *next;
};
// Singly linked list 
struct SingleLL
{
    struct LinkNode *head;
};
// Returns the new linked list
struct SingleLL *newLinkedList()
{
    // Create memory of head and tail Nodes
    struct SingleLL *sll = (struct SingleLL *)
    malloc(sizeof(struct SingleLL));
    if (sll == NULL)
    {
        printf("Memory overflow\n");
    }
    else
    {
        sll->head = NULL;
    }
    return sll;
}
// Handles the request of adding new node at beginning of linked list
void addNode(struct SingleLL *sll, int data)
{
    // Create dynamic node
    struct LinkNode *node = (struct LinkNode *)
    malloc(sizeof(struct LinkNode));
    if (node == NULL)
    {
        printf("Memory overflow to Create LinkNode\n");
        return;
    }
    else
    {
        // Set initial node value
        node->data = data;
        node->next = sll->head;
    }
    sll->head = node;
}
// Display linked list element
void display(struct LinkNode *node)
{
    if (node == NULL)
    {
        return;
    }
    struct LinkNode *temp = node;
    // iterating linked list elements
    while (temp != NULL)
    {
        printf(" %d →", temp->data);
        // Visit to next node
        temp = temp->next;
    }
    printf(" NULL\n");
}
int main()
{
    struct SingleLL *sll = newLinkedList();
    // Linked list
    // 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
    addNode(sll, 8);
    addNode(sll, 7);
    addNode(sll, 6);
    addNode(sll, 5);
    addNode(sll, 4);
    addNode(sll, 3);
    addNode(sll, 2);
    addNode(sll, 1);
    printf(" Linked List \n");
    display(sll->head);
    return 0;
}

Result

{
  "source": [
    {
      "struct": {
        "name": "LinkNode",
        "fields": [
          {
            "name": "data",
            "kind": 4,
            "type": "int",
            "signed": false
          },
          {
            "name": "next",
            "kind": 12,
            "type": "LinkNode",
            "ptr": "*"
          }
        ]
      }
    },
    {
      "comment": [
        "// Singly linked list"
      ]
    },
    {
      "struct": {
        "name": "SingleLL",
        "fields": [
          {
            "name": "head",
            "kind": 12,
            "type": "LinkNode",
            "ptr": "*"
          }
        ]
      }
    },
    {
      "comment": [
        "// Returns the new linked list"
      ]
    },
    {
      "fundecl": {
        "name": "newLinkedList",
        "param": [],
        "kind": 12,
        "type": "SingleLL",
        "ptr": "*",
        "body": [
          {
            "comment": [
              "// Create memory of head and tail Nodes",
              "// Handles the request of adding new node at beginning of linked list"
            ]
          },
          {
            "compound": [
              {
                "decl": {
                  "name": "sll",
                  "kind": 12,
                  "type": "SingleLL",
                  "ptr": "*",
                  "init": true,
                  "assing": [
                    {
                      "0": "cast",
                      "type": "SingleLL",
                      "kind": 12,
                      "ptr": "*",
                      "operand": {
                        "0": "funcall",
                        "name": "malloc",
                        "kind": 12,
                        "type": "void",
                        "ptr": "*",
                        "param": [
                          [
                            {
                              "0": "constant",
                              "value": 8,
                              "type": "long"
                            }
                          ]
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "if": {
                  "cond": [
                    {
                      "0": "binary",
                      "left": {
                        "exp": [
                          {
                            "0": "identifier",
                            "name": "sll",
                            "kind": 12,
                            "type": "SingleLL",
                            "ptr": "*"
                          }
                        ],
                        "kind": 12,
                        "type": "SingleLL",
                        "ptr": "*"
                      },
                      "op": "==",
                      "right": {
                        "exp": [
                          {
                            "0": "cast",
                            "type": "void",
                            "kind": 12,
                            "ptr": "*",
                            "operand": {
                              "0": "constant",
                              "value": 0,
                              "type": "int"
                            }
                          }
                        ],
                        "kind": 12,
                        "type": "void",
                        "ptr": "*"
                      },
                      "kind": 4,
                      "type": "int",
                      "signed": false
                    }
                  ],
                  "body": [
                    {
                      "compound": [
                        {
                          "0": "funcall",
                          "name": "printf",
                          "kind": 4,
                          "type": "int",
                          "signed": false,
                          "param": [
                            [
                              {
                                "0": "constant",
                                "value": "Memory overflow\n",
                                "type": "string"
                              }
                            ]
                          ]
                        }
                      ]
                    }
                  ]
                }
              },
              {
                "else": [
                  {
                    "compound": [
                      {
                        "0": "assing_var",
                        "left": {
                          "exp": [
                            {
                              "ref": {
                                "select": [
                                  {
                                    "deref": "*"
                                  },
                                  {
                                    "0": "identifier",
                                    "name": "sll",
                                    "kind": 12,
                                    "type": "SingleLL",
                                    "ptr": "*"
                                  }
                                ],
                                "field": {
                                  "name": "head",
                                  "kind": 12,
                                  "type": "LinkNode",
                                  "ptr": "*"
                                }
                              }
                            }
                          ],
                          "kind": 12,
                          "type": "LinkNode",
                          "ptr": "*"
                        },
                        "op": "=",
                        "right": {
                          "exp": [
                            {
                              "0": "cast",
                              "type": "void",
                              "kind": 12,
                              "ptr": "*",
                              "operand": {
                                "0": "constant",
                                "value": 0,
                                "type": "int"
                              }
                            }
                          ],
                          "kind": 12,
                          "type": "void",
                          "ptr": "*"
                        },
                        "kind": 12,
                        "type": "LinkNode",
                        "ptr": "*"
                      }
                    ]
                  }
                ]
              },
              [
                "return",
                [
                  {
                    "0": "identifier",
                    "name": "sll",
                    "kind": 12,
                    "type": "SingleLL",
                    "ptr": "*"
                  }
                ]
              ]
            ]
          }
        ]
      }
    },
    {
      "fundecl": {
        "name": "addNode",
        "param": [
          [
            {
              "0": "identifier",
              "name": "sll",
              "kind": 12,
              "type": "SingleLL",
              "ptr": "*"
            }
          ],
          [
            {
              "0": "identifier",
              "name": "data",
              "kind": 4,
              "type": "int",
              "signed": false
            }
          ]
        ],
        "kind": 0,
        "type": "void",
        "signed": false,
        "body": [
          {
            "comment": [
              "// Create dynamic node",
              "// Set initial node value",
              "// Display linked list element"
            ]
          },
          {
            "compound": [
              {
                "decl": {
                  "name": "node",
                  "kind": 12,
                  "type": "LinkNode",
                  "ptr": "*",
                  "init": true,
                  "assing": [
                    {
                      "0": "cast",
                      "type": "LinkNode",
                      "kind": 12,
                      "ptr": "*",
                      "operand": {
                        "0": "funcall",
                        "name": "malloc",
                        "kind": 12,
                        "type": "void",
                        "ptr": "*",
                        "param": [
                          [
                            {
                              "0": "constant",
                              "value": 16,
                              "type": "long"
                            }
                          ]
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "if": {
                  "cond": [
                    {
                      "0": "binary",
                      "left": {
                        "exp": [
                          {
                            "0": "identifier",
                            "name": "node",
                            "kind": 12,
                            "type": "LinkNode",
                            "ptr": "*"
                          }
                        ],
                        "kind": 12,
                        "type": "LinkNode",
                        "ptr": "*"
                      },
                      "op": "==",
                      "right": {
                        "exp": [
                          {
                            "0": "cast",
                            "type": "void",
                            "kind": 12,
                            "ptr": "*",
                            "operand": {
                              "0": "constant",
                              "value": 0,
                              "type": "int"
                            }
                          }
                        ],
                        "kind": 12,
                        "type": "void",
                        "ptr": "*"
                      },
                      "kind": 4,
                      "type": "int",
                      "signed": false
                    }
                  ],
                  "body": [
                    {
                      "compound": [
                        {
                          "0": "funcall",
                          "name": "printf",
                          "kind": 4,
                          "type": "int",
                          "signed": false,
                          "param": [
                            [
                              {
                                "0": "constant",
                                "value": "Memory overflow to Create LinkNode\n",
                                "type": "string"
                              }
                            ]
                          ]
                        },
                        [
                          "return",
                          []
                        ]
                      ]
                    }
                  ]
                }
              },
              {
                "else": [
                  {
                    "compound": [
                      {
                        "0": "assing_var",
                        "left": {
                          "exp": [
                            {
                              "ref": {
                                "select": [
                                  {
                                    "deref": "*"
                                  },
                                  {
                                    "0": "identifier",
                                    "name": "node",
                                    "kind": 12,
                                    "type": "LinkNode",
                                    "ptr": "*"
                                  }
                                ],
                                "field": {
                                  "name": "data",
                                  "kind": 4,
                                  "type": "int",
                                  "signed": false
                                }
                              }
                            }
                          ],
                          "kind": 4,
                          "type": "int",
                          "signed": false
                        },
                        "op": "=",
                        "right": {
                          "exp": [
                            {
                              "0": "identifier",
                              "name": "data",
                              "kind": 4,
                              "type": "int",
                              "signed": false
                            }
                          ],
                          "kind": 4,
                          "type": "int",
                          "signed": false
                        },
                        "kind": 4,
                        "type": "int",
                        "signed": false
                      },
                      {
                        "0": "assing_var",
                        "left": {
                          "exp": [
                            {
                              "ref": {
                                "select": [
                                  {
                                    "deref": "*"
                                  },
                                  {
                                    "0": "identifier",
                                    "name": "node",
                                    "kind": 12,
                                    "type": "LinkNode",
                                    "ptr": "*"
                                  }
                                ],
                                "field": {
                                  "name": "next",
                                  "kind": 12,
                                  "type": "LinkNode",
                                  "ptr": "*"
                                }
                              }
                            }
                          ],
                          "kind": 12,
                          "type": "LinkNode",
                          "ptr": "*"
                        },
                        "op": "=",
                        "right": {
                          "exp": [
                            {
                              "ref": {
                                "select": [
                                  {
                                    "deref": "*"
                                  },
                                  {
                                    "0": "identifier",
                                    "name": "sll",
                                    "kind": 12,
                                    "type": "SingleLL",
                                    "ptr": "*"
                                  }
                                ],
                                "field": {
                                  "name": "head",
                                  "kind": 12,
                                  "type": "LinkNode",
                                  "ptr": "*"
                                }
                              }
                            }
                          ],
                          "kind": 12,
                          "type": "LinkNode",
                          "ptr": "*"
                        },
                        "kind": 12,
                        "type": "LinkNode",
                        "ptr": "*"
                      }
                    ]
                  }
                ]
              },
              {
                "0": "assing_var",
                "left": {
                  "exp": [
                    {
                      "ref": {
                        "select": [
                          {
                            "deref": "*"
                          },
                          {
                            "0": "identifier",
                            "name": "sll",
                            "kind": 12,
                            "type": "SingleLL",
                            "ptr": "*"
                          }
                        ],
                        "field": {
                          "name": "head",
                          "kind": 12,
                          "type": "LinkNode",
                          "ptr": "*"
                        }
                      }
                    }
                  ],
                  "kind": 12,
                  "type": "LinkNode",
                  "ptr": "*"
                },
                "op": "=",
                "right": {
                  "exp": [
                    {
                      "0": "identifier",
                      "name": "node",
                      "kind": 12,
                      "type": "LinkNode",
                      "ptr": "*"
                    }
                  ],
                  "kind": 12,
                  "type": "LinkNode",
                  "ptr": "*"
                },
                "kind": 12,
                "type": "LinkNode",
                "ptr": "*"
              }
            ]
          }
        ]
      }
    },
    {
      "fundecl": {
        "name": "display",
        "param": [
          [
            {
              "0": "identifier",
              "name": "node",
              "kind": 12,
              "type": "LinkNode",
              "ptr": "*"
            }
          ]
        ],
        "kind": 0,
        "type": "void",
        "signed": false,
        "body": [
          {
            "comment": [
              "// iterating linked list elements",
              "// Visit to next node"
            ]
          },
          {
            "compound": [
              {
                "if": {
                  "cond": [
                    {
                      "0": "binary",
                      "left": {
                        "exp": [
                          {
                            "0": "identifier",
                            "name": "node",
                            "kind": 12,
                            "type": "LinkNode",
                            "ptr": "*"
                          }
                        ],
                        "kind": 12,
                        "type": "LinkNode",
                        "ptr": "*"
                      },
                      "op": "==",
                      "right": {
                        "exp": [
                          {
                            "0": "cast",
                            "type": "void",
                            "kind": 12,
                            "ptr": "*",
                            "operand": {
                              "0": "constant",
                              "value": 0,
                              "type": "int"
                            }
                          }
                        ],
                        "kind": 12,
                        "type": "void",
                        "ptr": "*"
                      },
                      "kind": 4,
                      "type": "int",
                      "signed": false
                    }
                  ],
                  "body": [
                    {
                      "compound": [
                        [
                          "return",
                          []
                        ]
                      ]
                    }
                  ]
                }
              },
              {
                "decl": {
                  "name": "temp",
                  "kind": 12,
                  "type": "LinkNode",
                  "ptr": "*",
                  "init": true,
                  "assing": [
                    {
                      "0": "identifier",
                      "name": "node",
                      "kind": 12,
                      "type": "LinkNode",
                      "ptr": "*"
                    }
                  ]
                }
              },
              {
                "compound": [
                  {
                    "0": "while",
                    "cond": [
                      {
                        "0": "binary",
                        "left": {
                          "exp": [
                            {
                              "0": "identifier",
                              "name": "temp",
                              "kind": 12,
                              "type": "LinkNode",
                              "ptr": "*"
                            }
                          ],
                          "kind": 12,
                          "type": "LinkNode",
                          "ptr": "*"
                        },
                        "op": "!=",
                        "right": {
                          "exp": [
                            {
                              "0": "cast",
                              "type": "void",
                              "kind": 12,
                              "ptr": "*",
                              "operand": {
                                "0": "constant",
                                "value": 0,
                                "type": "int"
                              }
                            }
                          ],
                          "kind": 12,
                          "type": "void",
                          "ptr": "*"
                        }
                      }
                    ],
                    "body": [
                      {
                        "compound": [
                          {
                            "0": "funcall",
                            "name": "printf",
                            "kind": 4,
                            "type": "int",
                            "signed": false,
                            "param": [
                              [
                                {
                                  "0": "constant",
                                  "value": " %d →",
                                  "type": "string"
                                }
                              ],
                              [
                                {
                                  "ref": {
                                    "select": [
                                      {
                                        "deref": "*"
                                      },
                                      {
                                        "0": "identifier",
                                        "name": "temp",
                                        "kind": 12,
                                        "type": "LinkNode",
                                        "ptr": "*"
                                      }
                                    ],
                                    "field": {
                                      "name": "data",
                                      "kind": 4,
                                      "type": "int",
                                      "signed": false
                                    }
                                  }
                                }
                              ]
                            ]
                          },
                          {
                            "0": "assing_var",
                            "left": {
                              "exp": [
                                {
                                  "0": "identifier",
                                  "name": "temp",
                                  "kind": 12,
                                  "type": "LinkNode",
                                  "ptr": "*"
                                }
                              ],
                              "kind": 12,
                              "type": "LinkNode",
                              "ptr": "*"
                            },
                            "op": "=",
                            "right": {
                              "exp": [
                                {
                                  "ref": {
                                    "select": [
                                      {
                                        "deref": "*"
                                      },
                                      {
                                        "0": "identifier",
                                        "name": "temp",
                                        "kind": 12,
                                        "type": "LinkNode",
                                        "ptr": "*"
                                      }
                                    ],
                                    "field": {
                                      "name": "next",
                                      "kind": 12,
                                      "type": "LinkNode",
                                      "ptr": "*"
                                    }
                                  }
                                }
                              ],
                              "kind": 12,
                              "type": "LinkNode",
                              "ptr": "*"
                            },
                            "kind": 12,
                            "type": "LinkNode",
                            "ptr": "*"
                          }
                        ]
                      }
                    ]
                  }
                ]
              },
              {
                "0": "funcall",
                "name": "printf",
                "kind": 4,
                "type": "int",
                "signed": false,
                "param": [
                  [
                    {
                      "0": "constant",
                      "value": " NULL\n",
                      "type": "string"
                    }
                  ]
                ]
              }
            ]
          }
        ]
      }
    },
    {
      "fundecl": {
        "name": "main",
        "param": [],
        "kind": 4,
        "type": "int",
        "signed": false,
        "body": [
          {
            "compound": [
              {
                "decl": {
                  "name": "sll",
                  "kind": 12,
                  "type": "SingleLL",
                  "ptr": "*",
                  "init": true,
                  "assing": [
                    {
                      "0": "funcall",
                      "name": "newLinkedList",
                      "kind": 12,
                      "type": "SingleLL",
                      "ptr": "*",
                      "param": []
                    }
                  ]
                }
              },
              {
                "comment": [
                  "// Linked list",
                  "// 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL"
                ]
              },
              {
                "0": "funcall",
                "name": "addNode",
                "kind": 0,
                "type": "void",
                "signed": false,
                "param": [
                  [
                    {
                      "0": "identifier",
                      "name": "sll",
                      "kind": 12,
                      "type": "SingleLL",
                      "ptr": "*"
                    }
                  ],
                  [
                    {
                      "0": "constant",
                      "value": 8,
                      "type": "int"
                    }
                  ]
                ]
              },
              {
                "0": "funcall",
                "name": "addNode",
                "kind": 0,
                "type": "void",
                "signed": false,
                "param": [
                  [
                    {
                      "0": "identifier",
                      "name": "sll",
                      "kind": 12,
                      "type": "SingleLL",
                      "ptr": "*"
                    }
                  ],
                  [
                    {
                      "0": "constant",
                      "value": 7,
                      "type": "int"
                    }
                  ]
                ]
              },
              {
                "0": "funcall",
                "name": "addNode",
                "kind": 0,
                "type": "void",
                "signed": false,
                "param": [
                  [
                    {
                      "0": "identifier",
                      "name": "sll",
                      "kind": 12,
                      "type": "SingleLL",
                      "ptr": "*"
                    }
                  ],
                  [
                    {
                      "0": "constant",
                      "value": 6,
                      "type": "int"
                    }
                  ]
                ]
              },
              {
                "0": "funcall",
                "name": "addNode",
                "kind": 0,
                "type": "void",
                "signed": false,
                "param": [
                  [
                    {
                      "0": "identifier",
                      "name": "sll",
                      "kind": 12,
                      "type": "SingleLL",
                      "ptr": "*"
                    }
                  ],
                  [
                    {
                      "0": "constant",
                      "value": 5,
                      "type": "int"
                    }
                  ]
                ]
              },
              {
                "0": "funcall",
                "name": "addNode",
                "kind": 0,
                "type": "void",
                "signed": false,
                "param": [
                  [
                    {
                      "0": "identifier",
                      "name": "sll",
                      "kind": 12,
                      "type": "SingleLL",
                      "ptr": "*"
                    }
                  ],
                  [
                    {
                      "0": "constant",
                      "value": 4,
                      "type": "int"
                    }
                  ]
                ]
              },
              {
                "0": "funcall",
                "name": "addNode",
                "kind": 0,
                "type": "void",
                "signed": false,
                "param": [
                  [
                    {
                      "0": "identifier",
                      "name": "sll",
                      "kind": 12,
                      "type": "SingleLL",
                      "ptr": "*"
                    }
                  ],
                  [
                    {
                      "0": "constant",
                      "value": 3,
                      "type": "int"
                    }
                  ]
                ]
              },
              {
                "0": "funcall",
                "name": "addNode",
                "kind": 0,
                "type": "void",
                "signed": false,
                "param": [
                  [
                    {
                      "0": "identifier",
                      "name": "sll",
                      "kind": 12,
                      "type": "SingleLL",
                      "ptr": "*"
                    }
                  ],
                  [
                    {
                      "0": "constant",
                      "value": 2,
                      "type": "int"
                    }
                  ]
                ]
              },
              {
                "0": "funcall",
                "name": "addNode",
                "kind": 0,
                "type": "void",
                "signed": false,
                "param": [
                  [
                    {
                      "0": "identifier",
                      "name": "sll",
                      "kind": 12,
                      "type": "SingleLL",
                      "ptr": "*"
                    }
                  ],
                  [
                    {
                      "0": "constant",
                      "value": 1,
                      "type": "int"
                    }
                  ]
                ]
              },
              {
                "0": "funcall",
                "name": "printf",
                "kind": 4,
                "type": "int",
                "signed": false,
                "param": [
                  [
                    {
                      "0": "constant",
                      "value": " Linked List \n",
                      "type": "string"
                    }
                  ]
                ]
              },
              {
                "0": "funcall",
                "name": "display",
                "kind": 0,
                "type": "void",
                "signed": false,
                "param": [
                  [
                    {
                      "ref": {
                        "select": [
                          {
                            "deref": "*"
                          },
                          {
                            "0": "identifier",
                            "name": "sll",
                            "kind": 12,
                            "type": "SingleLL",
                            "ptr": "*"
                          }
                        ],
                        "field": {
                          "name": "head",
                          "kind": 12,
                          "type": "LinkNode",
                          "ptr": "*"
                        }
                      }
                    }
                  ]
                ]
              },
              [
                "return",
                [
                  {
                    "0": "constant",
                    "value": 0,
                    "type": "int"
                  }
                ]
              ]
            ]
          }
        ]
      }
    },
    [
      "include",
      [
        "stdlib.h",
        "stdio.h"
      ]
    ],
    [
      "localfun",
      [
        "main",
        "display",
        "addNode",
        "newLinkedList"
      ]
    ]
  ]
}

Ternary operator example

C code

#include <stdio.h>

void main()
{
    int a = 10;
    int b = 20;
    int c = (a > b) ? a + 2 : b + 5;
    int d = (c > 2) ? ((c > b) ? c - 3 : c + 3) : 100;
    // 25
    printf("%d\n", c);
    // 22
    printf("%d\n", d);
}

Result

{
  "source": [
    {
      "fundecl": {
        "name": "main",
        "param": [],
        "kind": 0,
        "type": "void",
        "signed": false,
        "body": [
          {
            "compound": [
              {
                "decl": {
                  "name": "a",
                  "kind": 4,
                  "type": "int",
                  "signed": false,
                  "init": true,
                  "assing": [
                    {
                      "0": "constant",
                      "value": 10,
                      "type": "int"
                    }
                  ]
                }
              },
              {
                "decl": {
                  "name": "b",
                  "kind": 4,
                  "type": "int",
                  "signed": false,
                  "init": true,
                  "assing": [
                    {
                      "0": "constant",
                      "value": 20,
                      "type": "int"
                    }
                  ]
                }
              },
              {
                "decl": {
                  "name": "c",
                  "kind": 4,
                  "type": "int",
                  "signed": false,
                  "init": true,
                  "assing": [
                    {
                      "0": "ternary",
                      "cond": {
                        "0": "binary",
                        "left": {
                          "exp": [
                            {
                              "0": "identifier",
                              "name": "b",
                              "kind": 4,
                              "type": "int",
                              "signed": false
                            }
                          ],
                          "kind": 4,
                          "type": "int",
                          "signed": false
                        },
                        "op": "<",
                        "right": {
                          "exp": [
                            {
                              "0": "identifier",
                              "name": "a",
                              "kind": 4,
                              "type": "int",
                              "signed": false
                            }
                          ],
                          "kind": 4,
                          "type": "int",
                          "signed": false
                        },
                        "kind": 4,
                        "type": "int",
                        "signed": false
                      },
                      "then": {
                        "0": "binary",
                        "left": {
                          "exp": [
                            {
                              "0": "identifier",
                              "name": "a",
                              "kind": 4,
                              "type": "int",
                              "signed": false
                            }
                          ],
                          "kind": 4,
                          "type": "int",
                          "signed": false
                        },
                        "op": "+",
                        "right": {
                          "exp": [
                            {
                              "0": "constant",
                              "value": 2,
                              "type": "int"
                            }
                          ],
                          "kind": 4,
                          "type": "int",
                          "signed": false
                        },
                        "kind": 4,
                        "type": "int",
                        "signed": false
                      },
                      "else": {
                        "0": "binary",
                        "left": {
                          "exp": [
                            {
                              "0": "identifier",
                              "name": "b",
                              "kind": 4,
                              "type": "int",
                              "signed": false
                            }
                          ],
                          "kind": 4,
                          "type": "int",
                          "signed": false
                        },
                        "op": "+",
                        "right": {
                          "exp": [
                            {
                              "0": "constant",
                              "value": 5,
                              "type": "int"
                            }
                          ],
                          "kind": 4,
                          "type": "int",
                          "signed": false
                        },
                        "kind": 4,
                        "type": "int",
                        "signed": false
                      }
                    }
                  ]
                }
              },
              {
                "decl": {
                  "name": "d",
                  "kind": 4,
                  "type": "int",
                  "signed": false,
                  "init": true,
                  "assing": [
                    {
                      "0": "ternary",
                      "cond": {
                        "0": "binary",
                        "left": {
                          "exp": [
                            {
                              "0": "constant",
                              "value": 2,
                              "type": "int"
                            }
                          ],
                          "kind": 4,
                          "type": "int",
                          "signed": false
                        },
                        "op": "<",
                        "right": {
                          "exp": [
                            {
                              "0": "identifier",
                              "name": "c",
                              "kind": 4,
                              "type": "int",
                              "signed": false
                            }
                          ],
                          "kind": 4,
                          "type": "int",
                          "signed": false
                        },
                        "kind": 4,
                        "type": "int",
                        "signed": false
                      },
                      "then": {
                        "0": "ternary",
                        "cond": {
                          "0": "binary",
                          "left": {
                            "exp": [
                              {
                                "0": "identifier",
                                "name": "b",
                                "kind": 4,
                                "type": "int",
                                "signed": false
                              }
                            ],
                            "kind": 4,
                            "type": "int",
                            "signed": false
                          },
                          "op": "<",
                          "right": {
                            "exp": [
                              {
                                "0": "identifier",
                                "name": "c",
                                "kind": 4,
                                "type": "int",
                                "signed": false
                              }
                            ],
                            "kind": 4,
                            "type": "int",
                            "signed": false
                          },
                          "kind": 4,
                          "type": "int",
                          "signed": false
                        },
                        "then": {
                          "0": "binary",
                          "left": {
                            "exp": [
                              {
                                "0": "identifier",
                                "name": "c",
                                "kind": 4,
                                "type": "int",
                                "signed": false
                              }
                            ],
                            "kind": 4,
                            "type": "int",
                            "signed": false
                          },
                          "op": "-",
                          "right": {
                            "exp": [
                              {
                                "0": "constant",
                                "value": 3,
                                "type": "int"
                              }
                            ],
                            "kind": 4,
                            "type": "int",
                            "signed": false
                          },
                          "kind": 4,
                          "type": "int",
                          "signed": false
                        },
                        "else": {
                          "0": "binary",
                          "left": {
                            "exp": [
                              {
                                "0": "identifier",
                                "name": "c",
                                "kind": 4,
                                "type": "int",
                                "signed": false
                              }
                            ],
                            "kind": 4,
                            "type": "int",
                            "signed": false
                          },
                          "op": "+",
                          "right": {
                            "exp": [
                              {
                                "0": "constant",
                                "value": 3,
                                "type": "int"
                              }
                            ],
                            "kind": 4,
                            "type": "int",
                            "signed": false
                          },
                          "kind": 4,
                          "type": "int",
                          "signed": false
                        }
                      },
                      "else": {
                        "0": "constant",
                        "value": 100,
                        "type": "int"
                      }
                    }
                  ]
                }
              },
              {
                "comment": [
                  "// 25"
                ]
              },
              {
                "0": "funcall",
                "name": "printf",
                "kind": 4,
                "type": "int",
                "signed": false,
                "param": [
                  [
                    {
                      "0": "constant",
                      "value": "%d\n",
                      "type": "string"
                    }
                  ],
                  [
                    {
                      "0": "identifier",
                      "name": "c",
                      "kind": 4,
                      "type": "int",
                      "signed": false
                    }
                  ]
                ]
              },
              {
                "comment": [
                  "// 22"
                ]
              },
              {
                "0": "funcall",
                "name": "printf",
                "kind": 4,
                "type": "int",
                "signed": false,
                "param": [
                  [
                    {
                      "0": "constant",
                      "value": "%d\n",
                      "type": "string"
                    }
                  ],
                  [
                    {
                      "0": "identifier",
                      "name": "d",
                      "kind": 4,
                      "type": "int",
                      "signed": false
                    }
                  ]
                ]
              }
            ]
          }
        ]
      }
    },
    [
      "include",
      [
        "stdio.h"
      ]
    ],
    [
      "localfun",
      [
        "main"
      ]
    ]
  ]
}

Array example

C code

#include<stdio.h>
int main()
{
    int one_d[] = {1,2,4};
    int two_d[][3] = {{1,2,4},{3,4,6}};
    return 0;
}

Result

{
  "source": [
    {
      "fundecl": {
        "name": "main",
        "param": [],
        "kind": 4,
        "type": "int",
        "signed": false,
        "body": [
          {
            "compound": [
              {
                "decl": {
                  "name": "one_d",
                  "kind": 10,
                  "dim": [
                    {
                      "0": "index",
                      "value": 3
                    }
                  ],
                  "type": "int",
                  "bits": 12,
                  "init": true,
                  "assing": [
                    {
                      "0": "array_init",
                      "element": [
                        [
                          {
                            "0": "constant",
                            "value": 1,
                            "type": "int"
                          }
                        ],
                        [
                          {
                            "0": "constant",
                            "value": 2,
                            "type": "int"
                          }
                        ],
                        [
                          {
                            "0": "constant",
                            "value": 4,
                            "type": "int"
                          }
                        ]
                      ]
                    }
                  ]
                }
              },
              {
                "decl": {
                  "name": "two_d",
                  "kind": 10,
                  "dim": [
                    {
                      "0": "index",
                      "value": 2
                    },
                    {
                      "0": "index",
                      "value": 3
                    }
                  ],
                  "type": "int",
                  "bits": 24,
                  "init": true,
                  "assing": [
                    {
                      "0": "array_init",
                      "element": [
                        [
                          {
                            "0": "array_init",
                            "element": [
                              [
                                {
                                  "0": "constant",
                                  "value": 1,
                                  "type": "int"
                                }
                              ],
                              [
                                {
                                  "0": "constant",
                                  "value": 2,
                                  "type": "int"
                                }
                              ],
                              [
                                {
                                  "0": "constant",
                                  "value": 4,
                                  "type": "int"
                                }
                              ]
                            ]
                          }
                        ],
                        [
                          {
                            "0": "array_init",
                            "element": [
                              [
                                {
                                  "0": "constant",
                                  "value": 3,
                                  "type": "int"
                                }
                              ],
                              [
                                {
                                  "0": "constant",
                                  "value": 4,
                                  "type": "int"
                                }
                              ],
                              [
                                {
                                  "0": "constant",
                                  "value": 6,
                                  "type": "int"
                                }
                              ]
                            ]
                          }
                        ]
                      ]
                    }
                  ]
                }
              },
              [
                "return",
                [
                  {
                    "0": "constant",
                    "value": 0,
                    "type": "int"
                  }
                ]
              ]
            ]
          }
        ]
      }
    },
    [
      "include",
      [
        "stdio.h"
      ]
    ],
    [
      "localfun",
      [
        "main"
      ]
    ]
  ]
}

("bits" / sizeof(array element)) provides the size of array.

Note : This tool are designed to parse c code statically. It is in its infancy and does not provide 100% accuracy guarantee.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment