ssd

来自「UNIX版本6的源代码」· 代码 · 共 131 行

TXT
131
字号
.SHAppendix D: An Example of Bundling.PPThe following program is an example of the technique ofbundling; this example is discussed in Section 7..LP/* warnings:.IP 1.This works on Unix; the handling of functions with avariable number of arguments is different on different systems..IP 2.A number of checks for array bounds have been left outto avoid obscuring the basic ideas, but shouldbe there in a practical program..PP  */.LD%token NAME%right \'=\'%left \'+\' \'\-\'%left \'*\' \'/\'%%lines :	= /* empty */	{		bclear( ) ;	} |	lines expr \'\en\' =	{		bprint( $2 ) ;		printf( "\en" ) ;		bclear( ) ;	} |	lines error \'\en\' =	{		bclear( ) ;		yyerrok;	} ;expr :	expr \'+\' expr =	{		$$ = bundle( "add(", $1, ",", $3, ")" );	} |	expr \'\-\' expr =	{		$$ = bundle( "sub(", $1, ",", $3, ")" );	} |	expr \'*\' expr =	{		$$ = bundle( "mul(", $1, ",", $3, ")" );	} |	expr \'/\' expr =	{		$$ = bundle( "div(", $1, ",", $3, ")" );	} |	\'(\' expr \')\' =	{		$$ = $2;	} |	NAME \'=\' expr =		$$ = bundle( "assign(", $1, ",", $3, ")" );	} |	NAME ;%%#define	nsize 200char	names[nsize], *nptr { names };#define	bsize 500int	bspace[bsize], *bptr { bspace };yylex( ){	int c;	c = getchar( );	while( c == \' \' )		c = getchar( );	if( c>=\'a\' && c<=\'z\' ) {		yylval = nptr;		for( ; c>=\'a\' && c<=\'z\'; c=getchar( ) )			*nptr++ = c;		ungetc( c );		*nptr++ = \'\e0\';		return( NAME );	}	return( c );}bclear( ){	nptr = names;	bptr = bspace;}bundle( a1,a2,a3,a4,a5 ){	int i, j, *p, *obp;	p = &a1;	i = nargs( );	obp = bptr;	for( j=0; j<i; ++j )		*bptr++ = *p++;	*bptr++ = 0;	return( obp );}bprint( p )int *p;{	if( p>=bspace && p< &bspace[bsize] ) /* bundle */		while( *p != 0 )			bprint( *p++ );	else printf( "%s",  p );}.DE

⌨️ 快捷键说明

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