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

📄 ss6c

📁 UNIX v6源代码 这几乎是最经典的unix版本 unix操作系统设计和莱昂氏unix源代码分析都是用的该版
💻
字号:
.SHSection 6C: The C Language Yacc Environment.PPThe default mode of operation in Yacc is towrite actions and the lexicalanalyzer in C.This has a number of advantages; primarily,it is easier to write character handlingroutines, such as the lexical analyzer, in a languagewhich supports character-by-character I/O, and hasshifting and masking operators..PPWhen the user inputs a specificationto Yacc, the output is a file of C programs, called``y.tab.c''.These are then compiled, and loaded with a library;the library has default versions of a number of usefulroutines.This section discusses these routines, andhow the user can write his own routines if desired.The name of the Yacc library is system dependent; see Appendix B..PPThe subroutine produced by Yacc is called ``yyparse'';it is an integer valued function.When it is called, it in turn repeatedly calls ``yylex'', the lexical analyzersupplied by the user (see Section 3),to obtain input tokens.Eventually, either an error is detected, in which case(if no error recovery is possible)yyparse returns the value 1,or the lexical analyzer returns the endmarker token(type number 0), and the parser accepts.In this case, yyparse returns the value 0..PPThree of the routines on the Yacc library are concerned withthe ``external'' environment of yyparse.There is a default ``main'' program, a default ``initialization'' routine,and a default ``accept'' routine, respectively.They are so simple that they will be given here in their entirety:.DSmain( argc, argv )int argc;char *argv[ ]{	yyinit( argc, argv );	if( yyparse( ) )		return;	yyaccpt( );}yyinit( ) { }yyaccpt( ) { }.DEBy supplying his own versions of yyinit and/or yyaccpt,the user can get control either before the parser is called(to set options, open input files, etc.)or after the accept action has been done(to close files, call the next pass of the compiler, etc.).Note that yyinit is called with the two ``command line'' argumentswhich have been passed into the main program.If neither of these routines is redefined,the default situation simply looks like a callto the parser, followed by the termination of the program.Of course, in many cases the user will wish to supply his ownmain program; for example, this is necessary if theparser is to be called more than once..PPThe other major routine on the libraryis called ``yyerror''; its main purpose is to write out a messagewhen a syntax error is detected.It has a number of hooks and handles which attempt tomake this error message general and easy tounderstand.This routine is somewhat more complex, but still approachable:.DSextern int yyline;  /* input line number */yyerror(s)char *s;{	extern int yychar;	extern char *yysterm[ ];	printf("\en%s", s );	if( yyline )		printf(", line %d,", yyline );	printf(" on input: ");	if( yychar >= 0400 )		printf("%s\en", yysterm[yychar\-0400] );	else switch ( yychar ) {	case \'\et\': printf( "\e\et\en" ); return;	case \'\en\': printf( "\e\en\en" ); return;	case \'\e0\': printf( "$end\en" ); return;	default: printf( "%c\en" , yychar ); return;	}}.DEThe argument to yyerror is a string containing anerror message; most usually, it is ``syntax error''.yyerror also uses the external variablesyyline, yychar, and yysterm.yyline is a line number which,if set by the user to a nonzero number, willbe printed out as part of the error message.yychar is a variable which contains the type number of the currenttoken.yysterm has the names, supplied by the user, for all the tokenswhich have names.Thus, the routine spends most of its timetrying to print out a reasonable name for the input token.The biggest problem with the routine as given is that,on Unix, the error message does not go out on theerror file (file 2).This is hard to arrange in such a way that it works with boththe portable I/O library and the system I/O library;if a way can be worked out, the routine will be changedto do this..ulBeware:This routine will not work if any token nameshave been given redefined type numbers.In this case, the user must supply his own yyerror routine.Hopefully, this ``feature'' will disappearsoon..PPFinally, there is another feature which the C user of Yacc might wish to use.The integer variable yydebug is normally set to 0.If it is set to 1, the parser will output averbose description of its actions, includinga discussion of which input symbols have been read, andwhat the parser actions are.Depending on the operating environment,it may be possible to set this variable by using a debugging system.

⌨️ 快捷键说明

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