packcc

PackCC

Overview

PackCC is a parser generator for C. Its main features are as follows:

The grammar of your parser can be described in a PEG (Parsing Expression Grammar). The PEG is a top-down parsing language, and is similar to the regular-expression grammar. Compared with a bottom-up parsing language, like Yacc’s one, the PEG is much more intuitive and cannot be ambiguous. The PEG does not require tokenization to be a separate step, and tokenization rules can be written in the same way as any other grammar rules.

Your generated parser can parse inputs very efficiently by packrat parsing. The packrat parsing is the recursive descent parsing algorithm that is accelerated using memoization. By using packrat parsing, any input can be parsed in linear time. Without it, however, the resulting parser could exhibit exponential time performance in the worst case due to the unlimited look-ahead capability.

Unlike common packrat parsers, PackCC can support direct and indirect left-recursive grammar rules. This powerful feature enables you to describe your language grammar in a much simpler way. (The algorithm is based on the paper “Packrat Parsers Can Support Left Recursion” authored by A. Warth, J. R. Douglass, and T. Millstein.)

Some additional features are as follows:

The generated code is beautified and as ease-of-understanding as possible. Actually, it uses lots of goto statements, but the control flows are much more traceable than goto spaghetti storms generated by Yacc or other parser generators. This feature is irrelevant to common users, but helpful for PackCC developers to debug it.

PackCC itself is under MIT license, but you can distribute your generated code under any license you like.

Installation

You can obtain the executable packcc by compiling src/packcc.c using your favorite C compiler. For convenience, the build environments using GCC, Clang, and Microsoft Visual Studio are prepared under build directory.

Using GCC

Other than MinGW

packcc will be built in both directories build/gcc/debug/bin and build/gcc/release/bin using gcc by executing the following commands:

cd build/gcc
make
make check  # bats-core and uncrustify are required (see tests/README.md)

packcc in the directory build/gcc/release/bin is suitable for practical use.

MinGW

packcc will be built in both directories build/mingw-gcc/debug/bin and build/mingw-gcc/release/bin using gcc by executing the following commands:

cd build/mingw-gcc
make
make check  # bats-core and uncrustify are required (see tests/README.md)

packcc in the directory build/mingw-gcc/release/bin is suitable for practical use.

Using Clang

Other than MinGW

packcc will be built in both directories build/clang/debug/bin and build/clang/release/bin using clang by executing the following commands:

cd build/clang
make
make check  # bats-core and uncrustify are required (see tests/README.md)

packcc in the directory build/clang/release/bin is suitable for practical use.

MinGW

packcc will be built in both directories build/mingw-clang/debug/bin and build/mingw-clang/release/bin using clang by executing the following commands:

cd build/mingw-clang
make
make check  # bats-core and uncrustify are required (see tests/README.md)

packcc in the directory build/mingw-clang/release/bin is suitable for practical use.

Using Microsoft Visual Studio

You have to install Microsoft Visual Studio 2019 in advance. After that, you can build packcc.exe by the following instructions:

packcc.exe will appear in build\msvc\XXX\YYY directory. Here, XXX is x64 or x86, and YYY is Debug or Release. packcc.exe in the directory build\msvc\XXX\Release is suitable for practical use.

Usage

Command

You must prepare a PEG source file in advance. For details of the PEG source syntax, see the section “Syntax”. Here, let the file name example.peg for example.

packcc example.peg

By running this, the parser source example.h and example.c are generated.

If no PEG file name is specified, the PEG source is read from the standard input, and -.h and -.c will be generated.

The base name of the parser source files can be changed by -o option.

packcc -o parser example.peg

By running this, the parser source parser.h and parser.c are generated. This option can be specified only once.

A directory to search for import files can be added by -I option (version 2.0.0 or later). This option can be specified as many times as needed. The firstly specified directory will be searched first, the secondly specified directory will be searched next, and so on.

packcc -I foo -I bar/baz example.peg

By running this, the directory foo is searched first, and the directory bar/baz is searched next. The directories specified by this option have higher priority than those specified in the environment variable PCC_IMPORT_PATH and the default directories. For more details of import, see the explanation of %import written in the section “Syntax”.

If you want to disable UTF-8 support, specify the command line option -a or --ascii (version 1.4.0 or later).

If you want to insert #line directives in the generated source and header files, specify the command line option -l or --lines (version 1.7.0 or later). It is helpful to trace compilation errors of the generated source and header files back to the codes written in the PEG source file.

If you want to confirm the version of the packcc command, execute the below.

packcc -v

Syntax

A grammar consists of a set of named rules. A rule definition can be split into multiple lines.

rulename <- pattern

The rulename is the name of the rule to define. The pattern is a text pattern that contains one or more of the following elements.

rulename

The element stands for the entire pattern in the rule with the name given by rulename.

variable:rulename

The element stands for the entire pattern in the rule with the name given by rulename. The variable is an identifier associated with the semantic value returned from the rule by assigning to $$ in its action. The identifier can be referred to in subsequent actions as a variable. The example is shown below.

term <- l:term _ '+' _ r:factor { $$ = l + r; }

A variable identifier must consist of alphabets (both uppercase and lowercase letters), digits, and underscores. The first letter must be an alphabet. The reserved keywords in C cannot be used.

sequence1 / sequence2 // sequenceN

Each sequence is tried in turn until one of them matches, at which time matching for the overall pattern succeeds. If no sequence matches then the matching for the overall pattern fails. The operator slash (/) has the least priority. The example is shown below.

'foo' rule1 / 'bar'+ [0-9]? / rule2

This pattern tries matching of the first sequence ('foo' rule1). If it succeeds, then the overall pattern matching succeeds and ends without evaluating the subsequent sequences. Otherwise, it tries matching of the next sequence ('bar'+ [0-9]?). If it succeeds, then the overall pattern matching succeeds and ends without evaluating the subsequent sequence. Finally, it tries matching of the last sequence (rule2). If it succeeds, then the overall pattern matching succeeds. Otherwise, the overall pattern matching fails.

'string'

A character or string enclosed in single quotes is matched literally. The ANSI C escape sequences are recognized within the characters. The UNICODE escape sequences (ex. \u20AC) are also recognized including surrogate pairs, if the command line option -a is not specified (version 1.4.0 or later). The example is shown below.

'foo bar'

"string"

A character or string enclosed in double quotes is matched literally. The ANSI C escape sequences are recognized within the characters. The UNICODE escape sequences (ex. \u20AC) are also recognized including surrogate pairs, if the command line option -a is not specified (version 1.4.0 or later). The example is shown below.

"foo bar"

[character class]

A set of characters enclosed in square brackets matches any single character from the set. The ANSI C escape sequences are recognized within the characters. The UNICODE escape sequences (ex. \u20AC) are also recognized including surrogate pairs, if the command line option -a is not specified (version 1.4.0 or later). If the set begins with an up-arrow (^), the set is negated (the element matches any character not in the set). Any pair of characters separated with a dash (-) represents the range of characters from the first to the second, inclusive. The examples are shown below.

[abc]
[^abc]
[a-zA-Z0-9_]

.

A dot (.) matches any single character. Note that the only time this fails is at the end of input, where there is no character to match.

element ?

The element is optional. If present on the input, it is consumed and the match succeeds. If not present on the input, no text is consumed and the match succeeds anyway.

element *

The element is optional and repeatable. If present on the input, one or more occurrences of the element are consumed and the match succeeds. If no occurrence of the element is present on the input, the match succeeds anyway.

element +

The element is repeatable. If present on the input, one or more occurrences of the element are consumed and the match succeeds. If no occurrence of the element is present on the input, the match fails.

& element

The predicate succeeds only if the element can be matched. The input text scanned while matching element is not consumed from the input and remains available for subsequent matching.

! element

The predicate succeeds only if the element cannot be matched. The input text scanned while matching element is not consumed from the input and remains available for subsequent matching. A popular idiom is the following, which matches the end of input, after the last character of the input has already been consumed.

!.

( pattern )

Parentheses are used for grouping (modifying the precedence of the pattern).

< pattern >

Angle brackets are used for grouping (modifying the precedence of the pattern) and text capturing. The captured text is numbered in evaluation order, and can be referred to later using $1, $2, etc.

$n

A dollar ($) followed by a positive integer represents a text previously captured. The positive integer corresponds to the order of capturing. A $1 represents the first captured text. The examples are shown below.

< [0-9]+ > 'foo' $1

This matches 0foo0, 123foo123, etc.

'[' < '='* > '[' ( !( ']' $1 ']' ) . )* ( ']' $1 ']' )

This matches [[]], [=[]=], [==[]==], etc.

{ c source code }

Curly braces surround an action. The action is arbitrary C source code to be executed at the end of matching. Any braces within the action must be properly nested. Note that braces in directive lines and in comments (/**/ and //…) are appropriately ignored. One or more actions can be inserted in any places between elements in the pattern. Actions are not executed where matching fails.

[0-9]+ 'foo' { puts("OK"); } 'bar' / [0-9]+ 'foo' 'baz'

In this example, if the input is 012foobar, the action { puts("OK"); } is to be executed, but if the input is 012foobaz, the action is not to be executed. All matched actions are guaranteed to be executed only once.

In the action, the C source code can use the predefined variables below.

An example is shown below.

term <- l:term _ '+' _ r:factor { $$ = l + r; }
factor <- < [0-9]+ >            { $$ = atoi($1); }
_ <- [ \t]*

Note that the string data held by $n and $0 are discarded immediately after evaluation of the action. If the string data are needed after the action, they must be copied in $$ or auxil. If they are required to be copied in $$, it is recommended to define a structure as the type of output data using %value, and to copy the necessary string data in its member variable. Similarly, if they are required to be copied in auxil, it is recommended to define a structure as the type of user-defined data using %auxil, and to copy the necessary string data in its member variable.

The position values are 0-based; that is, the first position is 0. The data type is size_t (before version 1.4.0, it was int).

element ~ { c source code }

Curly braces following tilde (~) surround an error action. The error action is arbitrary C source code to be executed at the end of matching only if the preceding element matching fails. Any braces within the error action must be properly nested. Note that braces in directive lines and in comments (/**/ and //…) are appropriately ignored. One or more error actions can be inserted in any places after elements in the pattern. The operator tilde (~) binds less tightly than any other operator except alternation (/) and sequencing. The error action is intended to make error handling and recovery code easier to write. In the error action, all predefined variables described above are available as well. The examples are shown below.

rule1 <- e1 e2 e3 ~{ error("e[12] ok; e3 has failed"); }
rule2 <- (e1 e2 e3) ~{ error("one of e[123] has failed"); }

%header { c source code }

The specified C source code is copied verbatim to the C header file before the generated parser API function declarations. Any braces in the C source code must be properly nested. Note that braces in directive lines and in comments (/**/ and //…) are appropriately ignored. When %header is used multiple times, the respective C source codes are copied in order of their appearance.

%source { c source code }

The specified C source code is copied verbatim to the C source file before the generated parser implementation code. Any braces in the C source code must be properly nested. Note that braces in directive lines and in comments (/**/ and //…) are appropriately ignored. When %source is used multiple times, the respective C source codes are copied in order of their appearance.

%common { c source code }

The specified C source code is copied verbatim to both of the C header file and the C source file before the generated parser API function declarations and the implementation code respectively. This has the same effect as %header { c source code } %source { c source code }. Any braces in the C source code must be properly nested. Note that braces in directive lines and in comments (/**/ and //…) are appropriately ignored.

%earlyheader { c source code }

%earlysource { c source code }

%earlycommon { c source code }

Same as %header, %source and %common, respectively. The only difference is that these directives place the code at the very beginning of the generated file, before any code or includes generated by PackCC. This can be useful for example when it is necessary to modify behavior of standard libraries via a macro definition.

%value "output data type"

The type of output data, which is output as $$ in each action and can be retrieved from the parser API function pcc_parse(), is changed to the specified one from the default int. This can be used only once and cannot be used in imported files.

%auxil "user-defined data type"

The type of user-defined data, which is passed to the parser API function pcc_create(), is changed to the specified one from the default void *. This can be used only once and cannot be used in imported files.

%prefix "prefix"

The prefix of the parser API functions is changed to the specified one from the default pcc. This can be used only once and cannot be used in imported files.

%import "import file name"

The content of the specified import file is expanded at the text location of %import (version 2.0.0 or later). This can be used multiple times anywhere and can be used also in imported files. The import file name can be a relative path to the current directory or an absolute path. If it is a relative path, the directories listed below are searched for the import file in the listed order.

  1. the directory where the file that imports the import file is located
  2. the directories specified with -I options
    • They are prioritized in order of their appearance in the command line.
  3. the directories specified by the environment variable PCC_IMPORT_PATH
    • They are prioritized in order of their appearance in the value of this variable.
    • The character used as a delimiter between directory names is the colon ':' if PackCC is built for a Unix-like platform such as Linux, macOS, and MinGW. The character is the semicolon ';' if PackCC is built as a native Windows executable. (This is exactly the same manner as the environment variable PATH.)
  4. the per-user default directory
    • This is the subdirectory .packcc/import in the home directory if PackCC is built for a Unix-like platform, and in the user profile directory, “C:\Users\username” for example, if PackCC is built as a native Windows executable.
  5. the system-wide default directory
    • This is the directory /usr/share/packcc/import if PackCC is built for a Unix-like platform, and is the subdirectory packcc/import in the common application data directory, “C:\ProgramData” for example.

Note that the file imported once is silently ignored when it is attempted to be imported again.

#comment

A comment can be inserted between # and the end of the line.

%%

A double percent %% terminates the section for rule definitions of the grammar. All text following %% is copied verbatim to the C source file after the generated parser implementation code.

(The specification is determined by referring to peg/leg developed by Ian Piumarta.)

Import Files

The following import files are currently bundled.

For details, see here.

Macros

Some macros are prepared to customize the parser. The macro definition should be in %source section in the PEG source.

%source {
#define PCC_GETCHAR(auxil) get_character((auxil)->input)
#define PCC_BUFFERSIZE 1024
}

The following macros are available.

PCC_GETCHAR(auxil)

The function macro to get a character from the input. The user-defined data passed to the API function pcc_create() can be retrieved from the argument auxil. It can be ignored if no user-defined data. This macro must return a character code as an int type, or -1 if the input ends.

The default is defined as below.

#define PCC_GETCHAR(auxil) getchar()

PCC_ERROR(auxil)

The function macro to handle a syntax error. The user-defined data passed to the API function pcc_create() can be retrieved from the argument auxil. It can be ignored if no user-defined data. This macro need not return a value. It may abort the process (by using exit() for example) when a fatal error occurs, and can also return normally to deal with warnings.

The default is defined as below.

#define PCC_ERROR(auxil) pcc_error()
static void pcc_error(void) {
    fprintf(stderr, "Syntax error\n");
    exit(1);
}

PCC_MALLOC(auxil,size)

The function macro to allocate a memory block. The user-defined data passed to the API function pcc_create() can be retrieved from the argument auxil. It can be ignored if no user-defined data. The argument size is the number of bytes to allocate. This macro must return a pointer to the allocated memory block, or NULL if no sufficient memory is available.

The default is defined as below.

#define PCC_MALLOC(auxil, size) pcc_malloc_e(size)
static void *pcc_malloc_e(size_t size) {
    void *p = malloc(size);
    if (p == NULL) {
        fprintf(stderr, "Out of memory\n");
        exit(1);
    }
    return p;
}

PCC_REALLOC(auxil,ptr,size)

The function macro to reallocate the existing memory block. The user-defined data passed to the API function pcc_create() can be retrieved from the argument auxil. It can be ignored if no user-defined data. The argument ptr is the pointer to the previously allocated memory block. The argument size is the new number of bytes to reallocate. This macro must return a pointer to the reallocated memory block, or NULL if no sufficient memory is available. The contents of the memory block should be left unchanged in any case even if the reallocation fails.

The default is defined as below.

#define PCC_REALLOC(auxil, ptr, size) pcc_realloc_e(ptr, size)
static void *pcc_realloc_e(void *ptr, size_t size) {
    void *p = realloc(ptr, size);
    if (p == NULL) {
        fprintf(stderr, "Out of memory\n");
        exit(1);
    }
    return p;
}

PCC_FREE(auxil,ptr)

The function macro to free the existing memory block. The user-defined data passed to the API function pcc_create() can be retrieved from the argument auxil. It can be ignored if no user-defined data. The argument ptr is the pointer to the previously allocated memory block. This macro need not return a value.

The default is defined as below.

#define PCC_FREE(auxil, ptr) free(ptr)

PCC_DEBUG(auxil,event,rule,level,pos,buffer,length)

The function macro for debugging (version 1.5.0 or later). Sometimes, especially for complex parsers, it is useful to see how exactly the parser processes the input. This macro is called on important events and allows to log or display the current state of the parser. The argument rule is a string that contains the name of the currently evaluated rule. The non-negative integer level specifies how deep in the rule hierarchy the parser currently is. The argument pos holds the position from the start of the current context in bytes. In case of event == PCC_DBG_MATCH, the argument buffer holds the matched input and length is its size. For other events, buffer and length indicate a part of the currently loaded input, which is used to evaluate the current rule.

Caution: Since version 1.6.0, the first argument auxil is added to this macro. The user-defined data passed to the API function pcc_create() can be retrieved from this argument.

There are currently three supported events:

A very simple implementation could look like this:

static const char *dbg_str[] = { "Evaluating rule", "Matched rule", "Abandoning rule" };
#define PCC_DEBUG(auxil, event, rule, level, pos, buffer, length) \
    fprintf(stderr, "%*s%s %s @%zu [%.*s]\n", (int)((level) * 2), "", dbg_str[event], rule, pos, (int)(length), buffer)

The default is to do nothing:

#define PCC_DEBUG(auxil, event, rule, level, pos, buffer, length) ((void)0)

PCC_BUFFERSIZE

The initial size (the number of characters) of the text buffer. The text buffer is expanded as needed. The default is 256.

PCC_ARRAYSIZE

The initial size (the number of elements) of the internal arrays other than the text buffer. The arrays are expanded as needed. The default is 2.

API

The parser API has only 3 simple functions below.

pcc_context_t *pcc_create(void *auxil);

Creates a parser context. This context needs to be passed to the functions below. The auxil can be used to pass user-defined data to be bound to the context. NULL can be specified if no user-defined data.

int pcc_parse(pcc_context_t *ctx, int *ret);

Parses an input text (from standard input by default) and returns the result in ret. The ret can be NULL if no output data is needed. This function returns 0 if no text is left to be parsed, or a nonzero value otherwise.

void pcc_destroy(pcc_context_t *ctx);

Destroys the parser context. All resources allocated in the parser context are released.

The type of output data ret can be changed. If you want change it to char *, specify %value "char *" in the PEG source. The default is int.

The type of user-defined data auxil can be changed. If you want change it to long, specify %auxil "long" in the PEG source. The default is void *.

The prefix pcc can be changed. If you want change it to foo, specify %prefix "foo" in the PEG source. The default is pcc.

After the above settings, the API functions change like below.

foo_context_t *foo_create(long auxil);
int foo_parse(foo_context_t *ctx, char **ret);
void foo_destroy(foo_context_t *ctx);

The typical usage of the API functions is shown below.

int ret;
pcc_context_t *ctx = pcc_create(NULL);
while (pcc_parse(ctx, &ret));
pcc_destroy(ctx);

Examples

Desktop Calculator

A simple example which provides interactive four arithmetic operations of integers is shown here. Note that left-recursive grammar rules are defined in this example.

%prefix "calc"

%source {
#include <stdio.h>
#include <stdlib.h>
}

statement <- _ e:expression _ EOL { printf("answer=%d\n", e); }
           / ( !EOL . )* EOL      { printf("error\n"); }

expression <- e:term { $$ = e; }

term <- l:term _ '+' _ r:factor { $$ = l + r; }
      / l:term _ '-' _ r:factor { $$ = l - r; }
      / e:factor                { $$ = e; }

factor <- l:factor _ '*' _ r:unary { $$ = l * r; }
        / l:factor _ '/' _ r:unary { $$ = l / r; }
        / e:unary                  { $$ = e; }

unary <- '+' _ e:unary { $$ = +e; }
       / '-' _ e:unary { $$ = -e; }
       / e:primary     { $$ = e; }

primary <- < [0-9]+ >               { $$ = atoi($1); }
         / '(' _ e:expression _ ')' { $$ = e; }

_      <- [ \t]*
EOL    <- '\n' / '\r\n' / '\r' / ';'

%%
int main() {
    calc_context_t *ctx = calc_create(NULL);
    while (calc_parse(ctx, NULL));
    calc_destroy(ctx);
    return 0;
}

An execution example is as follows.

$ ./calc↵
1+2*(3+4*(5+6))↵
answer=95
5*6*7*8/(1*2*3*4)↵
answer=70

Simple AST builder

An example which builds an AST (abstract syntax tree) and dumps it is shown here. This example accepts the same inputs as Desktop Calculator shown above.

%prefix "calc"

%value "pcc_ast_node_t *"    # <-- must be set

%auxil "pcc_ast_manager_t *" # <-- must be set

%header {
#define PCC_AST_NODE_CUSTOM_DATA_DEFINED /* <-- enables node custom data */

typedef struct text_data_tag { /* <-- node custom data type */
    char *text;
} pcc_ast_node_custom_data_t;
}

%source {
#include <stdio.h>
#include <string.h>
}

statement <- _ e:expression _ EOL { $$ = e; }
           / ( !EOL . )* EOL      { $$ = NULL; }

expression <- e:term { $$ = e; }

term <- l:term _ '+' _ r:factor { $$ = pcc_ast_node__create_2(l, r); $$->custom.text = strdup("+"); }
      / l:term _ '-' _ r:factor { $$ = pcc_ast_node__create_2(l, r); $$->custom.text = strdup("-"); }
      / e:factor                { $$ = e; }

factor <- l:factor _ '*' _ r:unary { $$ = pcc_ast_node__create_2(l, r); $$->custom.text = strdup("*"); }
        / l:factor _ '/' _ r:unary { $$ = pcc_ast_node__create_2(l, r); $$->custom.text = strdup("/"); }
        / e:unary                  { $$ = e; }

unary <- '+' _ e:unary { $$ = pcc_ast_node__create_1(e); $$->custom.text = strdup("+"); }
       / '-' _ e:unary { $$ = pcc_ast_node__create_1(e); $$->custom.text = strdup("-"); }
       / e:primary     { $$ = e; }

primary <- < [0-9]+ >               { $$ = pcc_ast_node__create_0(); $$->custom.text = strdup($1); }
         / '(' _ e:expression _ ')' { $$ = e; }

_      <- [ \t]*
EOL    <- '\n' / '\r\n' / '\r' / ';'

%import "code/pcc_ast.peg"   # <-- provides AST build functions

%%
void pcc_ast_node_custom_data__initialize(pcc_ast_node_custom_data_t *obj) { /* <-- must be implemented when enabling node custom data */
    obj->text = NULL;
}

void pcc_ast_node_custom_data__finalize(pcc_ast_node_custom_data_t *obj) {   /* <-- must be implemented when enabling node custom data */
    free(obj->text);
}

static void dump_ast(const pcc_ast_node_t *obj, int depth) {
    if (obj) {
        switch (obj->type) {
        case PCC_AST_NODE_TYPE_NULLARY:
            printf("%*s%s: \"%s\"\n", 2 * depth, "", "nullary", obj->custom.text);
            break;
        case PCC_AST_NODE_TYPE_UNARY:
            printf("%*s%s: \"%s\"\n", 2 * depth, "", "unary", obj->custom.text);
            dump_ast(obj->data.unary.node, depth + 1);
            break;
        case PCC_AST_NODE_TYPE_BINARY:
            printf("%*s%s: \"%s\"\n", 2 * depth, "", "binary", obj->custom.text);
            dump_ast(obj->data.binary.node[0], depth + 1);
            dump_ast(obj->data.binary.node[1], depth + 1);
            break;
        case PCC_AST_NODE_TYPE_TERNARY:
            printf("%*s%s: \"%s\"\n", 2 * depth, "", "ternary", obj->custom.text);
            dump_ast(obj->data.ternary.node[0], depth + 1);
            dump_ast(obj->data.ternary.node[1], depth + 1);
            dump_ast(obj->data.ternary.node[2], depth + 1);
            break;
        case PCC_AST_NODE_TYPE_VARIADIC:
            printf("%*s%s: \"%s\"\n", 2 * depth, "", "variadic", obj->custom.text);
            {
                size_t i;
                for (i = 0; i < obj->data.variadic.len; i++) {
                    dump_ast(obj->data.variadic.node[i], depth + 1);
                }
            }
            break;
        default:
            printf("%*s%s: \"%s\"\n", 2 * depth, "", "(unknown)", obj->custom.text);
            break;
        }
    }
    else {
        printf("%*s(null)\n", 2 * depth, "");
    }
}

int main(int argc, char **argv) {
    pcc_ast_manager_t mgr;
    pcc_ast_manager__initialize(&mgr);
    {
        calc_context_t *ctx = calc_create(&mgr);
        pcc_ast_node_t *ast = NULL;
        while (calc_parse(ctx, &ast)) {
            dump_ast(ast, 0);
            pcc_ast_node__destroy(ast);
        }
        calc_destroy(ctx);
    }
    pcc_ast_manager__finalize(&mgr);
    return 0;
}

The key point is the line %import "code/pcc_ast.peg". The import file code/pcc_ast.peg makes it easier to build ASTs. For more details, see here.

An execution example is as follows.

$ ./ast-calc↵
1+2*(3+4*(5+6))↵
binary: "+"
  nullary: "1"
  binary: "*"
    nullary: "2"
    binary: "+"
      nullary: "3"
      binary: "*"
        nullary: "4"
        binary: "+"
          nullary: "5"
          nullary: "6"
5*6*7*8/(1*2*3*4)↵
binary: "/"
  binary: "*"
    binary: "*"
      binary: "*"
        nullary: "5"
        nullary: "6"
      nullary: "7"
    nullary: "8"
  binary: "*"
    binary: "*"
      binary: "*"
        nullary: "1"
        nullary: "2"
      nullary: "3"
    nullary: "4"

AST Builder for Tiny-C

You can find the more practical example in the directory examples/ast-tinyc. It builds an AST from an input source file written in Tiny-C and dumps the AST.