Re: help: using yacc, and my grammar needs to count items.

gneuner@dyn.com (George Neuner)
19 Apr 1999 14:58:16 -0400

          From comp.compilers

Related articles
help: using yacc, and my grammar needs to count items. madings@baladi.nmrfam.wisc.edu (1999-04-18)
Re: help: using yacc, and my grammar needs to count items. gneuner@dyn.com (1999-04-19)
Re: help: using yacc, and my grammar needs to count items. debray@CS.Arizona.EDU (1999-04-26)
| List of all articles for this month |

From: gneuner@dyn.com (George Neuner)
Newsgroups: comp.compilers
Date: 19 Apr 1999 14:58:16 -0400
Organization: Dynamic ReSolutions, Inc.
References: 99-04-057
Keywords: parse, comment

On 18 Apr 1999 02:16:07 -0400, madings@baladi.nmrfam.wisc.edu (Steve
Mading) wrote:


>I am running into something I can't express in BNF, and I can't figure
>out how to make lex/yacc parse this correctly. Can anyone offer
>advice on this? My problem is that my grammer needs to count items
>and group them into 'rows' in a table even though there are no
>delimiters.
>


You can use the lexical scanner interface directly to read your table
if you can determine that you are in the correct context.


Below is a clip showing a simple little function and grammar fragment
I have used to read (known size) tables of integer values. It assumes
a tag line that gives the size of the table in advance. You'll
obviously have to modify it so that it loops until your stop tag is
seen and you'll have to create a list dynamically as you read your
records unless there is a limit.




========= cut ==========


%union
{
int intval;
:
}


%%


data: tNUM_ENTRIES '=' INTEGER
                {
                int count = $3;
                int* table = (int*) calloc( count, sizeof(int) );


if ( count > 0 )
{
if ( !IntegerTable( table, count ) )
{
                                YYABORT;
                                }
else
{
// do something with table
}
}
}
                ;


%%
/*
  * read a table of integer values from the input: this function
  * bypasses the parser and gets input tokens directly from the
  * scanner so it can check that the number of input values is
  * correct
  */
static BOOLEAN
IntegerTable( int* tablePtr, int tableSize )
{
BOOLEAN retval;
int index;
YYSTYPE yylval;


        retval = TRUE;
        for ( index = 0; (retval && (index < tableSize)); ++index )
                if ( !(retval = ( YYLEX == INTEGER )) )
                        {
                        yyerror( "parse error, expecting INTEGER" );
                        }
                else
                        {
                        tablePtr[index] = yylval.intval;
                        }




        return ( retval );
}






George Neuner
Dynamic Resolutions, Inc.
[That's certainly doable, but I find that I prefer to do as much as
possible in the parser. It's more readable and often makes it easier
to change things down the road. -John]


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.