vak: (бэсм-6)
[personal profile] vak
Парсер теперь можно вызывать из командной строки. Фактически это первый проход будущего компилятора. Сейчас он умеет выгружать синтаксическое дерево AST в форматах YAML или Grapviz DOT.

Соберём:
$ git clone https://github.com/besm6/c-compiler.git
$ cd c-compiler
$ make
$ cd build
$ ./cast
Usage:
    cast [options] input-filename [output-filename]
Options:
    --ast            Emit AST in binary format (default)
    --yaml           Emit YAML format
    --dot            Emit Graphviz DOT script
    -v, --verbose    Enable verbose mode
    -D, --debug      Print debug information
    -h, --help       Show this help message
Возьмём классический Hello World и перепрём в YAML:
$ cat hello.c
int main()
{
    printf("Hello, World!\n");
}
$ ./cast --yaml hello.c -
program:
  - external_decl:
    kind: function
    type:
      kind: function
      return_type:
        kind: int
        signedness: signed
      variadic: false
    name: main
    body:
      kind: compound
      body:
        - stmt:
          kind: expr
          expr:
            - expr:
              kind: call
              func:
                - expr:
                  kind: var
                  name: printf
              args:
                - expr:
                  kind: literal
                  kind: string
                  value: "Hello, World!\n"
Нарисуем картинку:
$ ./cast --dot hello.c hello.dot
$ dot hello.dot -Tpdf -ohello.pdf
Смотрим hello.pdf:

Date: 2025-05-21 10:01 (UTC)
x86128: (Default)
From: [personal profile] x86128
забавно, что есть узел типа DeclOrStmt, без семантического анализа получается не понять сразу однозначно?
OSZAR »