🤖

Writing-A-C-Complier

Last edited time
Jun 5, 2025 06:35 AM
Created
May 21, 2025 02:24 AM
Tags

编译指令

C源码→汇编
gcc -S -O -fno-asynchronous-unwind-tables -fcf-protection=none xxx.c

汇编→执行文件
gcc xxx.s -o xxx

C源码->预处理文件
gcc -E -P xxx.c -O xxx.i
 

The lexer

int A keyword
main An identifier, whose value is “main”
( An open parenthesis
void A keyword
) A close parenthesis
{ An open brace
return A keyword
2 A constant, whose value is “2; A semicolon
} A close brace
identifier(标识符): ascii字母或下划线开头的,跟着一堆 字母/下划线/数字;大小写敏感
integer constant(整形常量):一个或多个数字
 
 
each token type with a regular expression, or regex
Token IRegular expression
Identifier [a-zA-Z_]\w*\b
Constant [0-9]+\b
int keyword int\b
void keyword void\b
return keyword return\b
Open parenthesis \(
Close parenthesis \)
Open brace {
Close brace }
Semicolon ;

Loading Comments...