从零开始的C语言编译器(1) lexer

289 阅读1分钟

github.com/yyyxxq/SZU_…

C语言词法分析

主要作用:将文件拆分成token

image.png 构造DFA自动机

常量

数字常量

整型

进制字符串形式默认形式后缀l后缀ll后缀u后缀ul后缀ull
十进制[0-9]+[u]{1,0}[l]{0,1,2})intlonglong longunsigned intunsigned longunsigned long long
十六进制0x[0-9a-zA-Z]+[u]{1,0}[l]{0,1,2})intlonglong longunsigned intunsigned longunsigned long long

浮点数

表达形式字符串形式默认形式后缀f后缀l
实数[0-9]+.[0-9]*[lf]或.[0-9]+[lf]doublefloatlong double
科学计数法[0-9]+.[0-9]*[lf]e[+-]?[0-9]+或.[0-9]+[lf]e[+-]?[0-9]+doublefloatlong double

字符常量

表达形式形式默认形式
单字符'char'char
转义字符'{'"?\ abfnrtv}'char
十六进制表示'\x十六进制'char
八进制表示'\八进制'char

wchar_t暂时不支持,仅支持ASCII。

字符串常量

符号

[] () {} . ->

++ -- & * + - ~ !

/ % << >> < > <= >= == != ^ | && ||

? : ; ...

= *= /= %= += -= <<= >>= &= ^= |=

, # ##

<: :> <% %> %: %:%:

关键字

auto break case char const continue default do double

else enum extern float for goto if inline int long

register restrict return short signed sizeof static struct switch

typedef union unsigned void volatile while

_Bool _Complex _Imaginary

效果

可对自身完成自检

image.png