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 <elder@linaro.org>
Message-Id: <20211001232338.769309-13-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
This commit is contained in:
Alex Elder 2021-10-01 18:23:16 -05:00 committed by Bjorn Andersson
parent fad43b91d2
commit c7c8e47f74

View File

@ -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--;