Commit | Line | Data |
---|---|---|
c9528a00 C |
1 | .SH |
2 | Appendix B: Yacc Input Syntax | |
3 | .PP | |
4 | This Appendix has a description of the Yacc input syntax, as a Yacc specification. | |
5 | Context dependencies, etc., are not considered. | |
6 | Ironically, the Yacc input specification language | |
7 | is most naturally specified as an LR(2) grammar; the sticky | |
8 | part comes when an identifier is seen in a rule, immediately | |
9 | following an action. | |
10 | If this identifier is followed by a colon, it is the start of the | |
11 | next rule; otherwise | |
12 | it is a continuation of the current rule, which just happens to have | |
13 | an action embedded in it. | |
14 | As implemented, the lexical analyzer looks | |
15 | ahead after seeing an identifier, and | |
16 | decide whether the next token (skipping blanks, newlines, comments, etc.) | |
17 | is a colon. | |
18 | If so, it returns the token C_IDENTIFIER. | |
19 | Otherwise, it returns IDENTIFIER. | |
20 | Literals (quoted strings) are also returned as IDENTIFIERS, | |
21 | but never as part of C_IDENTIFIERs. | |
22 | .sp | |
23 | .nf | |
24 | .ta .6i 1.2i 1.8i 2.4i 3i 3.6i | |
25 | ||
26 | /* grammar for the input to Yacc */ | |
27 | ||
28 | /* basic entities */ | |
29 | %token IDENTIFIER /* includes identifiers and literals */ | |
30 | %token C_IDENTIFIER /* identifier (but not literal) followed by colon */ | |
31 | %token NUMBER /* [0-9]+ */ | |
32 | ||
33 | /* reserved words: %type => TYPE, %left => LEFT, etc. */ | |
34 | ||
35 | %token LEFT RIGHT NONASSOC TOKEN PREC TYPE START UNION | |
36 | ||
37 | %token MARK /* the %% mark */ | |
38 | %token LCURL /* the %{ mark */ | |
39 | %token RCURL /* the %} mark */ | |
40 | ||
41 | /* ascii character literals stand for themselves */ | |
42 | ||
43 | %start spec | |
44 | ||
45 | %% | |
46 | ||
47 | spec : defs MARK rules tail | |
48 | ; | |
49 | ||
50 | tail : MARK { \fIIn this action, eat up the rest of the file\fR } | |
51 | | /* empty: the second MARK is optional */ | |
52 | ; | |
53 | ||
54 | defs : /* empty */ | |
55 | | defs def | |
56 | ; | |
57 | ||
58 | def : START IDENTIFIER | |
59 | | UNION { \fICopy union definition to output\fR } | |
60 | | LCURL { \fICopy C code to output file\fR } RCURL | |
61 | | ndefs rword tag nlist | |
62 | ; | |
63 | ||
64 | rword : TOKEN | |
65 | | LEFT | |
66 | | RIGHT | |
67 | | NONASSOC | |
68 | | TYPE | |
69 | ; | |
70 | ||
71 | tag : /* empty: union tag is optional */ | |
72 | | \'<\' IDENTIFIER \'>\' | |
73 | ; | |
74 | ||
75 | nlist : nmno | |
76 | | nlist nmno | |
77 | | nlist \',\' nmno | |
78 | ; | |
79 | ||
80 | nmno : IDENTIFIER /* NOTE: literal illegal with %type */ | |
81 | | IDENTIFIER NUMBER /* NOTE: illegal with %type */ | |
82 | ; | |
83 | ||
84 | /* rules section */ | |
85 | ||
86 | rules : C_IDENTIFIER rbody prec | |
87 | | rules rule | |
88 | ; | |
89 | ||
90 | rule : C_IDENTIFIER rbody prec | |
91 | | '|' rbody prec | |
92 | ; | |
93 | ||
94 | rbody : /* empty */ | |
95 | | rbody IDENTIFIER | |
96 | | rbody act | |
97 | ; | |
98 | ||
99 | act : \'{\' { \fICopy action, translate $$, etc.\fR } \'}\' | |
100 | ; | |
101 | ||
102 | prec : /* empty */ | |
103 | | PREC IDENTIFIER | |
104 | | PREC IDENTIFIER act | |
105 | | prec \';\' | |
106 | ; | |
107 | .fi | |
108 | .bp |