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