From dc701a62719d0c460d3306c65e9e0457c7bbd4a7 Mon Sep 17 00:00:00 2001 From: Quentin Carbonneaux Date: Mon, 2 Aug 2021 18:52:39 +0200 Subject: support /* */ comments --- yacc.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/yacc.c b/yacc.c index 8ea5137..926ceaa 100644 --- a/yacc.c +++ b/yacc.c @@ -1,4 +1,4 @@ -/*% clang -g -Wall -Wextra % -o # +/*% clang --pedantic -std=c89 -g -Wall -Wextra % -o # * miniyacc - LALR(1) grammars for C * See LICENSE for copyright and license details. */ @@ -800,12 +800,27 @@ istok(int c) int nexttk() { - int n; + char stm[4][4] = { + {0,4,1,4}, + {5,5,5,2}, /* '/' */ + {2,2,2,3}, /* '/' '*' */ + {2,2,0,3} /* '/' '*' ... '*' */ + }; + int st, n; char c, *p; - while (isspace(c=fgetc(fin))) - if (c == '\n') - lineno++; + for (st=0; st<4;) + switch (c=fgetc(fin)) { + default: + if (c=='\n') + lineno++; + st = stm[st][!isspace(c)]; + break; + case '/': st = stm[st][2]; break; + case '*': st = stm[st][3]; break; + } + if (st==5) + die("invalid token"); switch (c) { case '<': return TLangle; @@ -837,8 +852,8 @@ nexttk() die("identifier too long"); c = fgetc(fin); } - if (p == idnt) - die("unknown token"); + if (p==idnt) + die("invalid token"); *p = 0; if (strcmp(idnt, "%")==0) if (c=='{') -- cgit v1.2.3