2 /* Computation of FIRST stets */
4 #include "pgenheaders.h"
8 extern int Py_DebugFlag
;
11 static void calcfirstset(grammar
*, dfa
*);
14 addfirstsets(grammar
*g
)
20 printf("Adding FIRST sets ...\n");
21 for (i
= 0; i
< g
->g_ndfas
; i
++) {
23 if (d
->d_first
== NULL
)
29 calcfirstset(grammar
*g
, dfa
*d
)
44 printf("Calculate FIRST set for '%s'\n", d
->d_name
);
48 if (d
->d_first
== dummy
) {
49 fprintf(stderr
, "Left-recursion for '%s'\n", d
->d_name
);
52 if (d
->d_first
!= NULL
) {
53 fprintf(stderr
, "Re-calculating FIRST set for '%s' ???\n",
58 l0
= g
->g_ll
.ll_label
;
59 nbits
= g
->g_ll
.ll_nlabels
;
60 result
= newbitset(nbits
);
62 sym
= (int *)PyObject_MALLOC(sizeof(int));
64 Py_FatalError("no mem for new sym in calcfirstset");
66 sym
[0] = findlabel(&g
->g_ll
, d
->d_type
, (char *)NULL
);
68 s
= &d
->d_state
[d
->d_initial
];
69 for (i
= 0; i
< s
->s_narcs
; i
++) {
71 for (j
= 0; j
< nsyms
; j
++) {
72 if (sym
[j
] == a
->a_lbl
)
75 if (j
>= nsyms
) { /* New label */
76 sym
= (int *)PyObject_REALLOC(sym
,
77 sizeof(int) * (nsyms
+ 1));
80 "no mem to resize sym in calcfirstset");
81 sym
[nsyms
++] = a
->a_lbl
;
82 type
= l0
[a
->a_lbl
].lb_type
;
83 if (ISNONTERMINAL(type
)) {
84 d1
= PyGrammar_FindDFA(g
, type
);
85 if (d1
->d_first
== dummy
) {
87 "Left-recursion below '%s'\n",
91 if (d1
->d_first
== NULL
)
97 else if (ISTERMINAL(type
)) {
98 addbit(result
, a
->a_lbl
);
104 printf("FIRST set for '%s': {", d
->d_name
);
105 for (i
= 0; i
< nbits
; i
++) {
106 if (testbit(result
, i
))
107 printf(" %s", PyGrammar_LabelRepr(&l0
[i
]));