|
|
1.1 ! root 1: .\" @(#)ss8 6.1 (Berkeley) 5/8/86 ! 2: .\" ! 3: .SH ! 4: 8: The Yacc Environment ! 5: .PP ! 6: When the user inputs a specification ! 7: to Yacc, the output is a file of C programs, called ! 8: .I y.tab.c ! 9: on most ! 10: systems ! 11: (due to local file system conventions, the names may differ from ! 12: installation to installation). ! 13: The function produced by Yacc is called ! 14: .I yyparse \|; ! 15: it is an integer valued function. ! 16: When it is called, it in turn repeatedly calls ! 17: .I yylex , ! 18: the lexical analyzer ! 19: supplied by the user (see Section 3) ! 20: to obtain input tokens. ! 21: Eventually, either an error is detected, in which case ! 22: (if no error recovery is possible) ! 23: .I yyparse ! 24: returns the value 1, ! 25: or the lexical analyzer returns the endmarker token ! 26: and the parser accepts. ! 27: In this case, ! 28: .I yyparse ! 29: returns the value 0. ! 30: .PP ! 31: The user must provide a certain amount of environment for this ! 32: parser in order to obtain a working program. ! 33: For example, as with every C program, a program called ! 34: .I main ! 35: must be defined, that eventually calls ! 36: .I yyparse . ! 37: In addition, a routine called ! 38: .I yyerror ! 39: prints a message ! 40: when a syntax error is detected. ! 41: .PP ! 42: These two routines must be supplied in one form or another by the ! 43: user. ! 44: To ease the initial effort of using Yacc, a library has been ! 45: provided with default versions of ! 46: .I main ! 47: and ! 48: .I yyerror . ! 49: The name of this library is system dependent; ! 50: on many systems the library is accessed by a ! 51: .B \-ly ! 52: argument to the loader. ! 53: To show the triviality of these default programs, the source is ! 54: given below: ! 55: .DS ! 56: main(){ ! 57: return( yyparse() ); ! 58: } ! 59: .DE ! 60: and ! 61: .DS ! 62: # include <stdio.h> ! 63: ! 64: yyerror(s) char *s; { ! 65: fprintf( stderr, "%s\en", s ); ! 66: } ! 67: .DE ! 68: The argument to ! 69: .I yyerror ! 70: is a string containing an error message, usually ! 71: the string ``syntax error''. ! 72: The average application will want to do better than this. ! 73: Ordinarily, the program should keep track of the input line number, and print it ! 74: along with the message when a syntax error is detected. ! 75: The external integer variable ! 76: .I yychar ! 77: contains the lookahead token number at the time the error was detected; ! 78: this may be of some interest in giving better diagnostics. ! 79: Since the ! 80: .I main ! 81: program is probably supplied by the user (to read arguments, etc.) ! 82: the Yacc library is useful only in small ! 83: projects, or in the earliest stages of larger ones. ! 84: .PP ! 85: The external integer variable ! 86: .I yydebug ! 87: is normally set to 0. ! 88: If it is set to a nonzero value, the parser will output a ! 89: verbose description of its actions, including ! 90: a discussion of which input symbols have been read, and ! 91: what the parser actions are. ! 92: Depending on the operating environment, ! 93: it may be possible to set this variable by using a debugging system.
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.