⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gen.c

📁 C++版 词法分析、语法分析器
💻 C
📖 第 1 页 / 共 3 页
字号:
    {
    indent_put2s( "register YY_CHAR yy_c = %s;", char_map );

    /* save the backtracking info \before/ computing the next state
     * because we always compute one more state than needed - we
     * always proceed until we reach a jam state
     */
    gen_backtracking();

    indent_puts(
    "while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )" );
    indent_up();
    indent_puts( "{" );
    indent_puts( "yy_current_state = yy_def[yy_current_state];" );

    if ( usemecs )
	{
	/* we've arrange it so that templates are never chained
	 * to one another.  This means we can afford make a
	 * very simple test to see if we need to convert to
	 * yy_c's meta-equivalence class without worrying
	 * about erroneously looking up the meta-equivalence
	 * class twice
	 */
	do_indent();
	/* lastdfa + 2 is the beginning of the templates */
	printf( "if ( yy_current_state >= %d )\n", lastdfa + 2 );

	indent_up();
	indent_puts( "yy_c = yy_meta[yy_c];" );
	indent_down();
	}

    indent_puts( "}" );
    indent_down();

    indent_puts(
	"yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c];" );
    }


/* generate the code to find the next match */

void gen_next_match()

    {
    /* NOTE - changes in here should be reflected in gen_next_state() and
     * gen_NUL_trans()
     */
    char *char_map = useecs ? "yy_ec[*yy_cp]" : "*yy_cp";
    char *char_map_2 = useecs ? "yy_ec[*++yy_cp]" : "*++yy_cp";
    
    if ( fulltbl )
	{
	indent_put2s(
	    "while ( (yy_current_state = yy_nxt[yy_current_state][%s]) > 0 )",
		char_map );

	indent_up();

	if ( num_backtracking > 0 )
	    {
	    indent_puts( "{" );
	    gen_backtracking();
	    putchar( '\n' );
	    }

	indent_puts( "++yy_cp;" );

	if ( num_backtracking > 0 )
	    indent_puts( "}" );

	indent_down();

	putchar( '\n' );
	indent_puts( "yy_current_state = -yy_current_state;" );
	}

    else if ( fullspd )
	{
	indent_puts( "{" );
	indent_puts( "register const struct yy_trans_info *yy_trans_info;\n" );
	indent_puts( "register YY_CHAR yy_c;\n" );
	indent_put2s( "for ( yy_c = %s;", char_map );
	indent_puts(
	"      (yy_trans_info = &yy_current_state[yy_c])->yy_verify == yy_c;" );
	indent_put2s( "      yy_c = %s )", char_map_2 );

	indent_up();

	if ( num_backtracking > 0 )
	    indent_puts( "{" );

	indent_puts( "yy_current_state += yy_trans_info->yy_nxt;" );

	if ( num_backtracking > 0 )
	    {
	    putchar( '\n' );
	    gen_backtracking();
	    indent_puts( "}" );
	    }

	indent_down();
	indent_puts( "}" );
	}

    else
	{ /* compressed */
	indent_puts( "do" );

	indent_up();
	indent_puts( "{" );

	gen_next_state( false );

	indent_puts( "++yy_cp;" );

	indent_puts( "}" );
	indent_down();

	do_indent();

	if ( interactive )
	    printf( "while ( yy_base[yy_current_state] != %d );\n", jambase );
	else
	    printf( "while ( yy_current_state != %d );\n", jamstate );

	if ( ! reject && ! interactive )
	    {
	    /* do the guaranteed-needed backtrack to figure out the match */
	    indent_puts( "yy_cp = yy_last_accepting_cpos;" );
	    indent_puts( "yy_current_state = yy_last_accepting_state;" );
	    }
	}
    }


/* generate the code to find the next state */

void gen_next_state( worry_about_NULs )
int worry_about_NULs;

    { /* NOTE - changes in here should be reflected in get_next_match() */
    char char_map[256];

    if ( worry_about_NULs && ! nultrans )
	{
	if ( useecs )
	    (void) sprintf( char_map, "(*yy_cp ? yy_ec[*yy_cp] : %d)", NUL_ec );
	else
	    (void) sprintf( char_map, "(*yy_cp ? *yy_cp : %d)", NUL_ec );
	}

    else
	(void) strcpy( char_map, useecs ? "yy_ec[*yy_cp]" : "*yy_cp" );

    if ( worry_about_NULs && nultrans )
	{
	if ( ! fulltbl && ! fullspd )
	    /* compressed tables backtrack *before* they match */
	    gen_backtracking();

	indent_puts( "if ( *yy_cp )" );
	indent_up();
	indent_puts( "{" );
	}
   
    if ( fulltbl )
	indent_put2s( "yy_current_state = yy_nxt[yy_current_state][%s];", 
		char_map );
    
    else if ( fullspd )
	indent_put2s( "yy_current_state += yy_current_state[%s].yy_nxt;",
		    char_map );

    else
	gen_next_compressed_state( char_map );

    if ( worry_about_NULs && nultrans )
	{
	indent_puts( "}" );
	indent_down();
	indent_puts( "else" );
	indent_up();
	indent_puts( "yy_current_state = yy_NUL_trans[yy_current_state];" );
	indent_down();
	}
    
    if ( fullspd || fulltbl )
	gen_backtracking();

    if ( reject )
	indent_puts( "*yy_state_ptr++ = yy_current_state;" );
    }


/* generate the code to make a NUL transition */

void gen_NUL_trans()

    { /* NOTE - changes in here should be reflected in get_next_match() */
    int need_backtracking = (num_backtracking > 0 && ! reject);

    if ( need_backtracking )
	/* we'll need yy_cp lying around for the gen_backtracking() */
	indent_puts( "register YY_CHAR *yy_cp = yy_c_buf_p;" );

    putchar( '\n' );

    if ( nultrans )
	{
	indent_puts( "yy_current_state = yy_NUL_trans[yy_current_state];" );
	indent_puts( "yy_is_jam = (yy_current_state == 0);" );
	}

    else if ( fulltbl )
	{
	do_indent();
	printf( "yy_current_state = yy_nxt[yy_current_state][%d];\n",
		NUL_ec );
	indent_puts( "yy_is_jam = (yy_current_state <= 0);" );
	}

    else if ( fullspd )
	{
	do_indent();
	printf( "register int yy_c = %d;\n", NUL_ec );

	indent_puts(
	    "register const struct yy_trans_info *yy_trans_info;\n" );
	indent_puts( "yy_trans_info = &yy_current_state[yy_c];" );
	indent_puts( "yy_current_state += yy_trans_info->yy_nxt;" );

	indent_puts( "yy_is_jam = (yy_trans_info->yy_verify != yy_c);" );
	}

    else
	{
	char NUL_ec_str[20];

	(void) sprintf( NUL_ec_str, "%d", NUL_ec );
	gen_next_compressed_state( NUL_ec_str );

	if ( reject )
	    indent_puts( "*yy_state_ptr++ = yy_current_state;" );

	do_indent();

 	printf( "yy_is_jam = (yy_current_state == %d);\n", jamstate );
	}

    /* if we've entered an accepting state, backtrack; note that
     * compressed tables have *already* done such backtracking, so
     * we needn't bother with it again
     */
    if ( need_backtracking && (fullspd || fulltbl) )
	{
	putchar( '\n' );
	indent_puts( "if ( ! yy_is_jam )" );
	indent_up();
	indent_puts( "{" );
	gen_backtracking();
	indent_puts( "}" );
	indent_down();
	}
    }


/* generate the code to find the start state */

void gen_start_state()

    {
    if ( fullspd )
	indent_put2s( "yy_current_state = yy_start_state_list[yy_start%s];",
		bol_needed ? " + (yy_bp[-1] == '\\n' ? 1 : 0)" : "" );

    else
	{
	indent_puts( "yy_current_state = yy_start;" );

	if ( bol_needed )
	    {
	    indent_puts( "if ( yy_bp[-1] == '\\n' )" );
	    indent_up();
	    indent_puts( "++yy_current_state;" );
	    indent_down();
	    }

	if ( reject )
	    {
	    /* set up for storing up states */
	    indent_puts( "yy_state_ptr = yy_state_buf;" );
	    indent_puts( "*yy_state_ptr++ = yy_current_state;" );
	    }
	}
    }


/* gentabs - generate data statements for the transition tables
 *
 * synopsis
 *    gentabs();
 */

void gentabs()

    {
    int i, j, k, *accset, nacc, *acc_array, total_states;
    int end_of_buffer_action = num_rules + 1;

    /* *everything* is done in terms of arrays starting at 1, so provide
     * a null entry for the zero element of all C arrays
     */
    static char C_char_decl[] =
	"static const YY_CHAR %s[%d] =\n    {   0,\n";

    acc_array = allocate_integer_array( current_max_dfas );
    nummt = 0;

    /* the compressed table format jams by entering the "jam state",
     * losing information about the previous state in the process.
     * In order to recover the previous state, we effectively need
     * to keep backtracking information.
     */
    ++num_backtracking;

    if ( reject )
	{
	/* write out accepting list and pointer list
	 *
	 * first we generate the "yy_acclist" array.  In the process, we compute
	 * the indices that will go into the "yy_accept" array, and save the
	 * indices in the dfaacc array
	 */
	int EOB_accepting_list[2];

	/* set up accepting structures for the End Of Buffer state */
	EOB_accepting_list[0] = 0;
	EOB_accepting_list[1] = end_of_buffer_action;
	accsiz[end_of_buffer_state] = 1;
	dfaacc[end_of_buffer_state].dfaacc_set = EOB_accepting_list;

	printf( C_short_decl, "yy_acclist", max( numas, 1 ) + 1 );

	j = 1;	/* index into "yy_acclist" array */

	for ( i = 1; i <= lastdfa; ++i )
	    {
	    acc_array[i] = j;

	    if ( accsiz[i] != 0 )
		{
		accset = dfaacc[i].dfaacc_set;
		nacc = accsiz[i];

		if ( trace )
		    fprintf( stderr, "state # %d accepts: ", i );

		for ( k = 1; k <= nacc; ++k )
		    {
		    int accnum = accset[k];

		    ++j;

		    if ( variable_trailing_context_rules &&
			 ! (accnum & YY_TRAILING_HEAD_MASK) &&
			 accnum > 0 && accnum <= num_rules &&
			 rule_type[accnum] == RULE_VARIABLE )
			{
			/* special hack to flag accepting number as part
			 * of trailing context rule
			 */
			accnum |= YY_TRAILING_MASK;
			}

		    mkdata( accnum );

		    if ( trace )
			{
			fprintf( stderr, "[%d]", accset[k] );

			if ( k < nacc )
			    fputs( ", ", stderr );
			else
			    putc( '\n', stderr );
			}
		    }
		}
	    }

	/* add accepting number for the "jam" state */
	acc_array[i] = j;

	dataend();
	}

    else
	{
	dfaacc[end_of_buffer_state].dfaacc_state = end_of_buffer_action;

	for ( i = 1; i <= lastdfa; ++i )
	    acc_array[i] = dfaacc[i].dfaacc_state;

	/* add accepting number for jam state */
	acc_array[i] = 0;
	}

    /* spit out "yy_accept" array.  If we're doing "reject", it'll be pointers
     * into the "yy_acclist" array.  Otherwise it's actual accepting numbers.
     * In either case, we just dump the numbers.
     */

    /* "lastdfa + 2" is the size of "yy_accept"; includes room for C arrays
     * beginning at 0 and for "jam" state
     */
    k = lastdfa + 2;

    if ( reject )
	/* we put a "cap" on the table associating lists of accepting
	 * numbers with state numbers.  This is needed because we tell
	 * where the end of an accepting list is by looking at where
	 * the list for the next state starts.
	 */
	++k;

    printf( C_short_decl, "yy_accept", k );

    for ( i = 1; i <= lastdfa; ++i )
	{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -