-
Notifications
You must be signed in to change notification settings - Fork 7
/
LGPrintGrammar.cpp
171 lines (132 loc) · 3.68 KB
/
LGPrintGrammar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#ifdef DFASTAR
#include "ComGlobal.h"
#include "LGGlobal.h"
#include "LGCheckGrammar.h"
///////////////////////////////////////////////////////////////////////////////
// //
void LGCheckGrammar::PrintGrammar ()
{
int line_length, left;
int a, i, h, p, t, L, s, n;
// if (n_errors) return;
if (!optn [LG_GRAMMAR])
{
option_grammar = 1;
prt_grm ("\nGRAMMAR LISTING:\n\n");
prt_grm ("'g' option was not specified.\n");
return;
}
prt_grm ("\nGRAMMAR LISTING:\n\n");
/* NONTERMINALS. */
for (h = 0; h < n_heads; h++)
{
if (head_type != NULL && head_type[h] & SETNAME)
prt_grm ("$%5d ", h);
else prt_grm ( "%6d ", h);
prt_head (h, "");
// printf ("\n%s\n", head_name[h]);
for (p = f_prod [h]; p < l_prod [h]; p++)
{
// printf ("-> ");
line_length = 16;
if (prod_type != NULL)
{
if (prod_type[p] == PRODARROW) prt_grm ("\n %6d -> ", p);
else if (prod_type[p] == PRODADD ) prt_grm (" | ");
else if (prod_type[p] == PRODSUB ) prt_grm (" - ");
else if (prod_type[p] == PRODTO ) prt_grm ("..");
}
else prt_grm ("\n %6d -> ", p);
for (t = f_tail [p]; t < l_tail [p]; t++)
{
s = tail [t];
// if (s < 0) printf ("%s ", head_name[-s]);
// else printf ("%s ", term_name[ s]);
char* ch = " ";
if (t == l_tail[p]-1) ch = "";
line_length += prt_sym2 (s, ch);
if (line_length > 100 && t+1 < l_tail[p])
{
prt_grm ("\n ");
line_length = 16;
}
}
if (h == 1)
{
prt_grm (" (%d)", ret_numb[p]);
}
// printf ("\n");
}
prt_grm ("\n\n");
}
}
///////////////////////////////////////////////////////////////////////////////
// //
void LGCheckGrammar::PRT_SYMB (int s)
{
int L;
if (s == -32767)
{
prt_grm (" (default) ");
L = 10;
}
else
{
prt_grm (" ");
L = prt_sym2 (s, " ");
}
L = max_headl-L+1;
if (L < 0) L = 0;
spaces [L] = 0;
prt_grm ("%s", spaces);
spaces [L] = ' ';
}
int LGCheckGrammar::prt_sym2 (int s, char *after)
{
char *p;
if (s >= 0)
{
if (s >= n_terms) p = "???";
else p = term_name[s];
prt_grm ("%s%s", p, after);
}
else
{
if (-s >= n_heads) p = "???";
else p = head_name[-s];
prt_grm ("%s%s", p, after);
}
return (strlen(p) + strlen(after));
}
///////////////////////////////////////////////////////////////////////////////
// //
void LGCheckGrammar::PRT_HEAD (int s)
{
int L;
prt_grm (" ");
L = PRT_HEAD (s, " ");
L = max_headl-L+1;
if (L < 0) L = 0;
spaces [L] = 0;
prt_grm ("%s", spaces);
spaces [L] = ' ';
}
int LGCheckGrammar::PRT_HEAD (int s, char *after)
{
char *p;
if (s < 0) p = "(ERROR)";
else p = head_name[s];
prt_grm ("%s%s", p, after);
return (strlen(p) + strlen(after));
}
int LGCheckGrammar::prt_head (int s, char *after)
{
char *p;
if (s < 0) p = "(ERROR)";
else p = head_name[s];
prt_grm ("%s%s", p, after);
return (strlen(p) + strlen(after));
}
#endif
//
////////////////////////////////////////////////////////////////////////////////