Skip to content

reyu0722/c-compiler

Repository files navigation

c-compiler

C (subset) compiler written in C.

Syntax

program    = external*
external   = declaration "(" ("int" ident ("," "int" ident)*)? | "void" ")" (";" | "{" stmt* "}")
           | declaration ";"
           | "enum" ident "{" ident ("," ident)* "}" ";"
           | ("struct" | "union") ident? "{" declaration ("," declaration)* "}" ";"
           | ("struct" | "union") "{" declaration ("," declaration)* "}" ";"
           | "typedef" type_name ident ";"
           | preprocessor
label_stmt = label? stmt
label      = "case" expr ":"
           | "default" ":"
stmt       = expr ";"
           | "if" "(" expr ")" stmt ("else" stmt)?
           | "switch" "(" expr ")" stmt
           | "while "(" expr ")" stmt
           | "for" "(" expr? ";" expr? ";" expr? ")" stmt
           | "{" stmt* "}"
           | "return" expr? ";"
           | "break" ";"
           | "contunie" ";"
expr       = assign
assign     = equality (("=" | "+=" | "-=" | "*=" | "/=") assign)?
equality   = relational (("&&" | "||" | "==" | "!=") relational)*
relational = add ("<" add | "<=" add | ">" add | ">=" add)*
add        = mul ("+" mul | "-" mul)*
mul        = unary ("*" unary | "/" unary)*
unary      = ("+" | "-" | "&" | "*" | "sizeof" | "++" | "--")? postfix
postfix    = primary ("[" expr "]")*
           | primary ("." | "->") ident
           | primary ("++" | "--")
primary    = num | string | "(" expr ")"
           | declaration ("=" (assign | "{" expr_list? "}"))?
           | ident ("(" expr_list? ")")?

preprocessor = "#" "include" string

declaration = type_name nested_type
type_name   = "int" | "char" | "struct" ident
nested_type = "*"* ("(" nested_type ")" | ident) ("[" num "]")*
expr_list   = expr ("," expr_list)?

References

About

A self-compilable C compiler

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published