]>
git.saurik.com Git - bison.git/blob - src/gram.c
1 /* Allocate input grammar variables for bison,
2 Copyright (C) 1984, 1986, 1989, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
29 /* comments for these variables are in gram.h */
31 item_number_t
*ritem
= NULL
;
32 unsigned int nritems
= 0;
37 symbol_t
**symbols
= NULL
;
42 symbol_number_t
*token_translations
= NULL
;
44 int max_user_token_number
= 256;
49 /*--------------------------------------.
50 | Return the number of symbols in RHS. |
51 `--------------------------------------*/
54 rule_rhs_length (rule_t
*rule
)
58 for (rhsp
= rule
->rhs
; *rhsp
>= 0; ++rhsp
)
64 /*------------------------.
65 | Dump RITEM for traces. |
66 `------------------------*/
69 ritem_print (FILE *out
)
72 fputs ("RITEM\n", out
);
73 for (i
= 0; i
< nritems
; ++i
)
75 fprintf (out
, " %s", quotearg_style (escape_quoting_style
,
76 symbols
[ritem
[i
]]->tag
));
78 fprintf (out
, " (rule %d)\n", -ritem
[i
] - 1);
83 /*------------------------------------------.
84 | Return the size of the longest rule RHS. |
85 `------------------------------------------*/
88 ritem_longest_rhs (void)
93 for (i
= 1; i
< nrules
+ 1; ++i
)
95 int length
= rule_rhs_length (&rules
[i
]);
104 /*-------------------.
105 | Dump the grammar. |
106 `-------------------*/
109 grammar_dump (FILE *out
, const char *title
)
114 fprintf (out
, "%s\n\n", title
);
116 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
117 ntokens
, nvars
, nsyms
, nrules
, nritems
);
118 fprintf (out
, "Variables\n---------\n\n");
119 fprintf (out
, "Value Sprec Sassoc Tag\n");
120 for (i
= ntokens
; i
< nsyms
; i
++)
121 fprintf (out
, "%5d %5d %5d %s\n",
123 symbols
[i
]->prec
, symbols
[i
]->assoc
,
124 quotearg_style (escape_quoting_style
, symbols
[i
]->tag
));
125 fprintf (out
, "\n\n");
126 fprintf (out
, "Rules\n-----\n\n");
127 fprintf (out
, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
128 for (i
= 1; i
< nrules
+ nuseless_productions
+ 1; i
++)
131 /* Find the last RHS index in ritems. */
132 for (r
= rules
[i
].rhs
; *r
>= 0; ++r
)
134 fprintf (out
, "%3d (%2d, %2d, %2d, %2d-%2d) %2d ->",
136 rules
[i
].prec
? rules
[i
].prec
->prec
: 0,
137 rules
[i
].prec
? rules
[i
].prec
->assoc
: 0,
139 rules
[i
].rhs
- ritem
,
140 rules
[i
].rhs
- ritem
+ rhs_count
- 1,
141 rules
[i
].lhs
->number
);
142 /* Dumped the RHS. */
143 for (r
= rules
[i
].rhs
; *r
>= 0; r
++)
144 fprintf (out
, " %3d", *r
);
145 fprintf (out
, " [%d]\n", -(*r
) - 1);
147 fprintf (out
, "\n\n");
148 fprintf (out
, "Rules interpreted\n-----------------\n\n");
149 for (i
= 1; i
< nrules
+ nuseless_productions
+ 1; i
++)
151 fprintf (out
, "%-5d %s :",
152 i
, quotearg_style (escape_quoting_style
, rules
[i
].lhs
->tag
));
153 for (r
= rules
[i
].rhs
; *r
>= 0; r
++)
155 quotearg_style (escape_quoting_style
, symbols
[*r
]->tag
));
158 fprintf (out
, "\n\n");
167 XFREE (token_translations
);
168 /* Free the symbol table data structure. */