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