vak: (бэсм-6)
Serge Vakulenko ([personal profile] vak) wrote2025-05-21 02:32 am

Парсер из командной строки

Парсер теперь можно вызывать из командной строки. Фактически это первый проход будущего компилятора. Сейчас он умеет выгружать синтаксическое дерево 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:

Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org