Demonstration

1. Hello, World!

To begin with, let's analyze the famous "Hello World!" program:

int
main(int argc, char** argv)
{
	std::cout << "Hello, World!" << std::endl;
	return 0;
}

1.1. Syntax Analysis

The Scalpel package contains a utility program named 'demo', which outputs a JSON representation of the syntax tree produced by Scalpel. Let's run 'demo' with the above source code as input:

{
  "declaration_seq":
  [
    {
      "function_definition":
      {
        "simple_function_definition":
        {
          "decl_specifier_seq":
          [
            {
              "type_specifier":
              {
                "simple_type_specifier":
                {
                  "fundamental_type_specifier":
                  {
                    "predefined text": "int"
                  }
                }
              }
            }
          ],
          "declarator":
          {
            "direct_declarator":
            {
              "direct_declarator_first_part":
              {
                "declarator_id":
                {
                  "nested_identifier_or_template_id":
                  {
                    "identifier_or_template_id":
                    {
                      "text": "main"
                    }
                  }
                }
              },
              "direct_declarator_last_part_seq":
              [
                {
                  "direct_declarator_function_part":
                  {
                    "predefined text": "(",
                    "parameter_declaration_clause":
                    {
                      "parameter_declaration_list":
                      [
                        {
                          "decl_specifier_seq":
                          [
                            {
                              "type_specifier":
                              {
                                "simple_type_specifier":
                                {
                                  "fundamental_type_specifier":
                                  {
                                    "predefined text": "int"
                                  }
                                }
                              }
                            }
                          ],
                          "declarator":
                          {
                            "direct_declarator":
                            {
                              "direct_declarator_first_part":
                              {
                                "declarator_id":
                                {
                                  "nested_identifier_or_template_id":
                                  {
                                    "identifier_or_template_id":
                                    {
                                      "text": "argc"
                                    }
                                  }
                                }
                              }
                            }
                          }
                        },
                        {
                          "decl_specifier_seq":
                          [
                            {
                              "type_specifier":
                              {
                                "simple_type_specifier":
                                {
                                  "fundamental_type_specifier":
                                  {
                                    "predefined text": "char"
                                  }
                                }
                              }
                            }
                          ],
                          "declarator":
                          {
                            "ptr_operator_seq":
                            [
                              {
                                "ptr_ptr_operator":
                                {
                                  "simple_ptr_ptr_operator":
                                  {
                                    "predefined text": "*"
                                  }
                                }
                              },
                              {
                                "ptr_ptr_operator":
                                {
                                  "simple_ptr_ptr_operator":
                                  {
                                    "predefined text": "*"
                                  }
                                }
                              }
                            ],
                            "direct_declarator":
                            {
                              "direct_declarator_first_part":
                              {
                                "declarator_id":
                                {
                                  "nested_identifier_or_template_id":
                                  {
                                    "identifier_or_template_id":
                                    {
                                      "text": "argv"
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      ]
                    },
                    "predefined text": ")"
                  }
                }
              ]
            }
          },
          "compound_statement":
          {
            "predefined text": "{",
            "statement_seq":
            [
              {
                "expression_statement":
                {
                  "expression":
                  [
                    {
                      "conditional_expression":
                      {
                        "logical_or_expression":
                        {
                          "logical_and_expression":
                          {
                            "inclusive_or_expression":
                            {
                              "exclusive_or_expression":
                              {
                                "and_expression":
                                {
                                  "equality_expression":
                                  {
                                    "relational_expression":
                                    {
                                      "shift_expression":
                                      {
                                        "additive_expression":
                                        {
                                          "multiplicative_expression":
                                          {
                                            "pm_expression":
                                            {
                                              "cast_expression":
                                              {
                                                "unary_expression":
                                                {
                                                  "postfix_expression":
                                                  {
                                                    "postfix_expression_first_part":
                                                    {
                                                      "primary_expression":
                                                      {
                                                        "id_expression":
                                                        {
                                                          "qualified_id":
                                                          {
                                                            "qualified_nested_id":
                                                            {
                                                              "nested_name_specifier":
                                                              {
                                                                "identifier_or_template_id":
                                                                {
                                                                  "text": "std"
                                                                },
                                                                "predefined text": "::"
                                                              },
                                                              "unqualified_id":
                                                              {
                                                                "text": "cout"
                                                              }
                                                            }
                                                          }
                                                        }
                                                      }
                                                    }
                                                  }
                                                }
                                              }
                                            }
                                          }
                                        },
                                        "shift_operator":
                                        {
                                          "predefined text": "<<"
                                        },
                                        "shift_expression":
                                        {
                                          "additive_expression":
                                          {
                                            "multiplicative_expression":
                                            {
                                              "pm_expression":
                                              {
                                                "cast_expression":
                                                {
                                                  "unary_expression":
                                                  {
                                                    "postfix_expression":
                                                    {
                                                      "postfix_expression_first_part":
                                                      {
                                                        "primary_expression":
                                                        {
                                                          "literal":
                                                          {
                                                            "string_literal":
                                                            [
                                                              {
                                                                "s_char_sequence":
                                                                [
                                                                  {
                                                                    "text": "H"
                                                                  },
                                                                  {
                                                                    "text": "e"
                                                                  },
                                                                  {
                                                                    "text": "l"
                                                                  },
                                                                  {
                                                                    "text": "l"
                                                                  },
                                                                  {
                                                                    "text": "o"
                                                                  },
                                                                  {
                                                                    "text": ","
                                                                  },
                                                                  {
                                                                    "text": " "
                                                                  },
                                                                  {
                                                                    "text": "W"
                                                                  },
                                                                  {
                                                                    "text": "o"
                                                                  },
                                                                  {
                                                                    "text": "r"
                                                                  },
                                                                  {
                                                                    "text": "l"
                                                                  },
                                                                  {
                                                                    "text": "d"
                                                                  },
                                                                  {
                                                                    "text": "!"
                                                                  }
                                                                ]
                                                              }
                                                            ]
                                                          }
                                                        }
                                                      }
                                                    }
                                                  }
                                                }
                                              }
                                            }
                                          },
                                          "shift_operator":
                                          {
                                            "predefined text": "<<"
                                          },
                                          "shift_expression":
                                          {
                                            "additive_expression":
                                            {
                                              "multiplicative_expression":
                                              {
                                                "pm_expression":
                                                {
                                                  "cast_expression":
                                                  {
                                                    "unary_expression":
                                                    {
                                                      "postfix_expression":
                                                      {
                                                        "postfix_expression_first_part":
                                                        {
                                                          "primary_expression":
                                                          {
                                                            "id_expression":
                                                            {
                                                              "qualified_id":
                                                              {
                                                                "qualified_nested_id":
                                                                {
                                                                  "nested_name_specifier":
                                                                  {
                                                                    "identifier_or_template_id":
                                                                    {
                                                                      "text": "std"
                                                                    },
                                                                    "predefined text": "::"
                                                                  },
                                                                  "unqualified_id":
                                                                  {
                                                                    "text": "endl"
                                                                  }
                                                                }
                                                              }
                                                            }
                                                          }
                                                        }
                                                      }
                                                    }
                                                  }
                                                }
                                              }
                                            }
                                          }
                                        }
                                      }
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  ],
                  "predefined text": ";"
                }
              },
              {
                "jump_statement":
                {
                  "return_statement":
                  {
                    "predefined text": "return",
                    "expression":
                    [
                      {
                        "conditional_expression":
                        {
                          "logical_or_expression":
                          {
                            "logical_and_expression":
                            {
                              "inclusive_or_expression":
                              {
                                "exclusive_or_expression":
                                {
                                  "and_expression":
                                  {
                                    "equality_expression":
                                    {
                                      "relational_expression":
                                      {
                                        "shift_expression":
                                        {
                                          "additive_expression":
                                          {
                                            "multiplicative_expression":
                                            {
                                              "pm_expression":
                                              {
                                                "cast_expression":
                                                {
                                                  "unary_expression":
                                                  {
                                                    "postfix_expression":
                                                    {
                                                      "postfix_expression_first_part":
                                                      {
                                                        "primary_expression":
                                                        {
                                                          "literal":
                                                          {
                                                            "integer_literal":
                                                            {
                                                              "integer_literal_no_suffix":
                                                              {
                                                                "text": "0"
                                                              }
                                                            }
                                                          }
                                                        }
                                                      }
                                                    }
                                                  }
                                                }
                                              }
                                            }
                                          }
                                        }
                                      }
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    ],
                    "predefined text": ";"
                  }
                }
              }
            ],
            "predefined text": "}"
          }
        }
      }
    }
  ]
}

Who said C++ was complex?

Fortunately for your sanity, you probably won't have to deal directly with that kind of object structures. The syntax tree is the object structure from which Scalpel generates the semantic graph, a much more readable structure for human beings.

1.2. Semantic Analysis

Here is the JSON representation of the semantic graph of our sample source code, still outputted by 'demo':

{
  "id": "namespace-0",
  "simple functions":
  [
    {
      "id": "simple-function-0",
      "name": "main",
      "return type":
      {
        "fundamental type": "int"
      },
      "parameters":
      [
        {
          "name": "argc",
          "type":
          {
            "fundamental type": "int"
          }
        },
        {
          "name": "argv",
          "type":
          {
            "pointer":
            {
              "type":
              {
                "pointer":
                {
                  "type":
                  {
                    "fundamental type": "char"
                  }
                }
              }
            }
          }
        }
      ]
    }
  ]
}

The root object represents the global namespace, which contains a single simple function (the main function).