src/ast/README

    A Yacc/Bison AST library

    This directory contains the implementation of an Abstract Syntax Tree (AST) library which can be used to generate and manipulate a tree representation of a code described by a Yacc/Bison context-free grammar.

    Within Basilisk, it is used by qcc to parse Basilisk C code and transform it into standard C99.

    The main files are:

    Example of syntax tree generation

    A graphical representation of the syntax tree can be obtained with the utility program expr.c.

    For example

    make expr
    ./expr 'int main() { printf ("Hello world!"); }'

    gives the (rather long) tree-representation

    root
    └─translation_unit
      └─external_declaration
        └─function_definition
          ├─function_declaration
          │ ├─declaration_specifiers
          │ │ └─type_specifier
          │ │   └─types
          │ │     └─INT int :1
          │ └─declarator
          │   └─direct_declarator
          │     ├─direct_declarator
          │     │ └─generic_identifier
          │     │   └─IDENTIFIER main :1
          │     ├─'(' ( :1
          │     └─')' ) :1
          └─compound_statement
            ├─'{' { :1
            ├─block_item_list
            │ └─block_item
            │   └─statement
            │     └─expression_statement
            │       ├─expression
            │       │ └─assignment_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
            │       │                         └─cast_expression
            │       │                           └─unary_expression
            │       │                             └─postfix_expression
            │       │                               └─function_call
            │       │                                 ├─postfix_expression
            │       │                                 │ └─primary_expression
            │       │                                 │   └─IDENTIFIER printf :1
            │       │                                 ├─'(' ( :1
            │       │                                 ├─argument_expression_list
            │       │                                 │ └─argument_expression_list_item
            │       │                                 │   └─assignment_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
            │       │                                 │                           └─cast_expression
            │       │                                 │                             └─unary_expression
            │       │                                 │                               └─postfix_expression
            │       │                                 │                                 └─primary_expression
            │       │                                 │                                   └─string
            │       │                                 │                                     └─STRING_LITERAL "Hello world!" :1
            │       │                                 └─')' ) :1
            │       └─';' ; :1
            └─'}' } :1
    

    See also

    A generic interpreter for Basilisk C