From 7e8b9219a8844896ce946f166da6f099412ecd7d Mon Sep 17 00:00:00 2001 From: Randy Heit Date: Sun, 20 Mar 2016 09:33:25 -0500 Subject: [PATCH] Lemon update 2010-01-07 03:53:04 on branch trunk - Another attempt at fixing the table generator in lemon. Again, this does not effect the SQLite grammar. (user: drh) --- tools/lemon/lemon.c | 56 +++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/tools/lemon/lemon.c b/tools/lemon/lemon.c index 102a86c27..ec16de534 100644 --- a/tools/lemon/lemon.c +++ b/tools/lemon/lemon.c @@ -412,7 +412,23 @@ char *arg; /* ** The state of the yy_action table under construction is an instance of -** the following structure +** the following structure. +** +** The yy_action table maps the pair (state_number, lookahead) into an +** action_number. The table is an array of integers pairs. The state_number +** determines an initial offset into the yy_action array. The lookahead +** value is then added to this initial offset to get an index X into the +** yy_action array. If the aAction[X].lookahead equals the value of the +** of the lookahead input, then the value of the action_number output is +** aAction[X].action. If the lookaheads do not match then the +** default action for the state_number is returned. +** +** All actions associated with a single state_number are first entered +** into aLookahead[] using multiple calls to acttab_action(). Then the +** actions for that single state_number are placed into the aAction[] +** array with a single call to acttab_insert(). The acttab_insert() call +** also resets the aLookahead[] array in preparation for the next +** state number. */ typedef struct acttab acttab; struct acttab { @@ -458,7 +474,10 @@ acttab *acttab_alloc(void){ return p; } -/* Add a new action to the current transaction set +/* Add a new action to the current transaction set. +** +** This routine is called once for each lookahead for a particular +** state. */ void acttab_action(acttab *p, int lookahead, int action){ if( p->nLookahead>=p->nLookaheadAlloc ){ @@ -495,7 +514,6 @@ void acttab_action(acttab *p, int lookahead, int action){ */ int acttab_insert(acttab *p){ int i, j, k, n; - int nActtab; /* Number of slots in the p->aAction[] table */ assert( p->nLookahead>0 ); /* Make sure we have enough space to hold the expanded action table @@ -503,8 +521,7 @@ int acttab_insert(acttab *p){ ** must be appended to the current action table */ n = p->mxLookahead + 1; - nActtab = p->nAction + n; - if( nActtab >= p->nActionAlloc ){ + if( p->nAction + n >= p->nActionAlloc ){ int oldAlloc = p->nActionAlloc; p->nActionAlloc = p->nAction + n + p->nActionAlloc + 20; p->aAction = realloc( p->aAction, @@ -519,16 +536,16 @@ int acttab_insert(acttab *p){ } } - /* Scan the existing action table looking for an offset where we can - ** insert the current transaction set. Fall out of the loop when that - ** offset is found. In the worst case, we fall out of the loop when - ** i reaches nActtab, which means we append the new transaction set. + /* Scan the existing action table looking for an offset that is a + ** duplicate of the current transaction set. Fall out of the loop + ** if and when the duplicate is found. ** ** i is the index in p->aAction[] where p->mnLookahead is inserted. */ - for(i=nActtab-1; i>=0; i--){ - /* First look for an existing action table entry that can be reused */ + for(i=p->nAction-1; i>=0; i--){ if( p->aAction[i].lookahead==p->mnLookahead ){ + /* All lookaheads and actions in the aLookahead[] transaction + ** must match against the candidate aAction[i] entry. */ if( p->aAction[i].action!=p->mnAction ) continue; for(j=0; jnLookahead; j++){ k = p->aLookahead[j].lookahead - p->mnLookahead + i; @@ -537,19 +554,30 @@ int acttab_insert(acttab *p){ if( p->aLookahead[j].action!=p->aAction[k].action ) break; } if( jnLookahead ) continue; + + /* No possible lookahead value that is not in the aLookahead[] + ** transaction is allowed to match aAction[i] */ n = 0; for(j=0; jnAction; j++){ if( p->aAction[j].lookahead<0 ) continue; if( p->aAction[j].lookahead==j+p->mnLookahead-i ) n++; } if( n==p->nLookahead ){ - break; /* Same as a prior transaction set */ + break; /* An exact match is found at offset i */ } } } + + /* If no existing offsets exactly match the current transaction, find an + ** an empty offset in the aAction[] table in which we can add the + ** aLookahead[] transaction. + */ if( i<0 ){ - /* If no reusable entry is found, look for an empty slot */ - for(i=0; inAction, which means the + ** transaction will be appended. */ + for(i=0; inActionAlloc - p->mxLookahead; i++){ if( p->aAction[i].lookahead<0 ){ for(j=0; jnLookahead; j++){ k = p->aLookahead[j].lookahead - p->mnLookahead + i;