BSD 4_2 development
[unix-history] / usr / doc / yacc / ssb
CommitLineData
c9528a00
C
1.SH
2Appendix B: Yacc Input Syntax
3.PP
4This Appendix has a description of the Yacc input syntax, as a Yacc specification.
5Context dependencies, etc., are not considered.
6Ironically, the Yacc input specification language
7is most naturally specified as an LR(2) grammar; the sticky
8part comes when an identifier is seen in a rule, immediately
9following an action.
10If this identifier is followed by a colon, it is the start of the
11next rule; otherwise
12it is a continuation of the current rule, which just happens to have
13an action embedded in it.
14As implemented, the lexical analyzer looks
15ahead after seeing an identifier, and
16decide whether the next token (skipping blanks, newlines, comments, etc.)
17is a colon.
18If so, it returns the token C_IDENTIFIER.
19Otherwise, it returns IDENTIFIER.
20Literals (quoted strings) are also returned as IDENTIFIERS,
21but 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
47spec : defs MARK rules tail
48 ;
49
50tail : MARK { \fIIn this action, eat up the rest of the file\fR }
51 | /* empty: the second MARK is optional */
52 ;
53
54defs : /* empty */
55 | defs def
56 ;
57
58def : 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
64rword : TOKEN
65 | LEFT
66 | RIGHT
67 | NONASSOC
68 | TYPE
69 ;
70
71tag : /* empty: union tag is optional */
72 | \'<\' IDENTIFIER \'>\'
73 ;
74
75nlist : nmno
76 | nlist nmno
77 | nlist \',\' nmno
78 ;
79
80nmno : IDENTIFIER /* NOTE: literal illegal with %type */
81 | IDENTIFIER NUMBER /* NOTE: illegal with %type */
82 ;
83
84 /* rules section */
85
86rules : C_IDENTIFIER rbody prec
87 | rules rule
88 ;
89
90rule : C_IDENTIFIER rbody prec
91 | '|' rbody prec
92 ;
93
94rbody : /* empty */
95 | rbody IDENTIFIER
96 | rbody act
97 ;
98
99act : \'{\' { \fICopy action, translate $$, etc.\fR } \'}\'
100 ;
101
102prec : /* empty */
103 | PREC IDENTIFIER
104 | PREC IDENTIFIER act
105 | prec \';\'
106 ;
107.fi
108.bp