From c7c8e47f74ede2f5861e83b15e4d46d25d6931cc Mon Sep 17 00:00:00 2001 From: Alex Elder Date: Fri, 1 Oct 2021 18:23:16 -0500 Subject: [PATCH] parser: only one lookahead byte is required The scratch_buf[] array is used to hold characters supplied to unput(). The only time unput() is called is in yylex(), when a character returned by input() isn't in the character set appropriate for the symbol being parsed. And in that case unput() is called only once. We will not call unput() again until input() has been called at least once, and that will consume the only character in the scratch buffer. Therefore, for our purposes, only one character is required for the lookahead buffer. We never accept a NUL byte on input, so it will never be used as a lookahead character. So we can use 0 as a special lookahead value that indicates "no lookahead present." So replace the scratch_buf[] and scratch_pos with a single character lookahead, which is considered invalid if its value is 0. Signed-off-by: Alex Elder Message-Id: <20211001232338.769309-13-elder@linaro.org> Signed-off-by: Bjorn Andersson --- parser.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/parser.c b/parser.c index 4ede708..0148cba 100644 --- a/parser.c +++ b/parser.c @@ -41,8 +41,7 @@ struct token { struct qmi_struct *qmi_struct; }; -static char scratch_buf[128]; -static unsigned scratch_pos; +static char lookahead; static int yyline = 1; @@ -69,8 +68,9 @@ static char input() int ret; char ch; - if (scratch_pos) { - ch = scratch_buf[--scratch_pos]; + if (lookahead) { + ch = lookahead; + lookahead = 0; goto out; } @@ -102,8 +102,8 @@ out: static void unput(int ch) { - assert(scratch_pos < 128); - scratch_buf[scratch_pos++] = ch; + assert(!lookahead); + lookahead = ch; if (ch == '\n') yyline--;