From ec4bdd2e748b84924b0b6bae6c37ff6f0ec8cc6c Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Sun, 9 Jul 2023 16:51:29 -0400 Subject: [PATCH] libcdecl: Combine scanner rules for punctuation. Use a single rule to match all punctuation, with a lookup table to return the correct token. This gives a small size reduction for the scanner. --- src/scan.l | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/scan.l b/src/scan.l index 1342e5f..7e3e365 100644 --- a/src/scan.l +++ b/src/scan.l @@ -131,14 +131,31 @@ INTEGER 0x[[:xdigit:]]+|0[0-7]+|[[:digit:]]+ } %} -"..." return T_ELLIPSIS; -";" return T_SEMICOLON; -"*" return T_ASTERISK; -"(" return T_LPAREN; -")" return T_RPAREN; -"[" return T_LBRACKET; -"]" return T_RBRACKET; -"," return T_COMMA; +"..."|[][;*(),] { + static const unsigned char tab[2][8] = { + "*[](),.;", + { + T_ASTERISK & 0xff, + T_LBRACKET & 0xff, + T_RBRACKET & 0xff, + T_LPAREN & 0xff, + T_RPAREN & 0xff, + T_COMMA & 0xff, + T_ELLIPSIS & 0xff, + T_SEMICOLON & 0xff + } + }; + + unsigned char *match; + int x; + + match = memchr(&tab, yytext[0], sizeof tab[0]); + x = match[sizeof tab[0]]; + + if (T_VOID >= 256) + x += 256; + return x; +} {INTEGER} { char *end; -- 2.43.2