Subversion Repositories tendra.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
2
    		 Crown Copyright (c) 1997
3
 
4
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
12
 
13
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
15
 
16
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
19
 
20
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
22
        these conditions;
23
 
24
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
27
        it may be put.
28
*/
29
 
30
 
31
/*** rule-firsts.c --- Computation of rule first sets.
32
 *
33
 ** Author: Steve Folkes <smf@hermes.mod.uk>
34
 *
35
 *** Commentary:
36
 *
37
 * This file implements the SID first set calculation routines.
38
 *
39
 * The first set of a rule is the set of all terminals that may start that
40
 * rule.  As well as computing the first set of a rule, its priority and
41
 * whether or not it is see through are also computed.  A rule is see through
42
 * if there is an expansion of the rule that does not involve a basic or
43
 * predicate.  The priority is used during the factorisation phase to work out
44
 * which rules to expand.  The rule's priority is one more than the priority
45
 * of the rule with the highest priority in any of the rule's alternatives
46
 * that is not preceded by an action.  If there is no such rule, then the
47
 * rule's priority is one.
48
 *
49
 * As well as computing the first sets, these routines check that there are no
50
 * recursive calls that are not preceded by a basic or predicate.  They also
51
 * check that predicates are either the first item in an alternative, or are
52
 * preceded by a basic (or another predicate).  The same check is also made
53
 * for rules that start with a predicate.
54
 *
55
 *** Change Log:
56
 * $Log: rule-firsts.c,v $
57
 * Revision 1.1.1.1  1998/01/17  15:57:47  release
58
 * First version to be checked into rolling release.
59
 *
60
 * Revision 1.2  1994/12/15  09:58:39  smf
61
 * Brought into line with OSSG C Coding Standards Document, as per
62
 * "CR94_178.sid+tld-update".
63
 *
64
 * Revision 1.1.1.1  1994/07/25  16:04:38  smf
65
 * Initial import of SID 1.8 non shared files.
66
 *
67
**/
68
 
69
/****************************************************************************/
70
 
71
#include "rule.h"
72
#include "basic.h"
73
#include "gen-errors.h"
74
#include "table.h"
75
 
76
/*--------------------------------------------------------------------------*/
77
 
78
void
79
rule_compute_first_set_1 PROTO_N ((rule))
80
			 PROTO_T (RuleP rule)
81
{
82
    AltP     alt;
83
    unsigned priority = 0;
84
 
85
    if (rule_has_computed_first_set (rule)) {
86
	return;
87
    } else if (rule_is_computing_first_set (rule)) {
88
	E_cannot_compute_first_set (rule);
89
	return;
90
    }
91
    rule_computing_first_set (rule);
92
    for (alt = rule_alt_head (rule); alt; alt = alt_next (alt)) {
93
	BoolT see_through = TRUE;
94
	BoolT no_action   = TRUE;
95
	ItemP item        = alt_item_head (alt);
96
	ItemP initial     = item;
97
 
98
	for (; see_through && (item != NIL (ItemP)); item = item_next (item)) {
99
	    switch (item_type (item)) EXHAUSTIVE {
100
	      case ET_PREDICATE:
101
		if (item != initial) {
102
		    E_see_to_predicate (entry_key (item_entry (item)), rule);
103
		}
104
		entry_list_add_if_missing (rule_predicate_first (rule),
105
					   item_entry (item));
106
		see_through = FALSE;
107
		break;
108
	      case ET_RENAME:
109
	      case ET_ACTION:
110
		no_action = FALSE;
111
		break;
112
	      case ET_RULE: {
113
		  EntryP     entry      = item_entry (item);
114
		  RuleP      item_rule  = entry_get_rule (entry);
115
		  EntryListP pred_first = rule_predicate_first (item_rule);
116
		  unsigned   item_priority;
117
 
118
		  rule_compute_first_set_1 (item_rule);
119
		  if ((item != initial) &&
120
		      (!entry_list_is_empty (pred_first))) {
121
		      E_see_to_rule_predicate (item_rule, rule);
122
		  }
123
		  bitvec_or (rule_first_set (rule),
124
			     rule_first_set (item_rule));
125
		  entry_list_append (rule_predicate_first (rule), pred_first);
126
		  see_through   = rule_is_see_through (item_rule);
127
		  item_priority = rule_get_priority (item_rule);
128
		  if ((item_priority > priority) && no_action) {
129
		      priority = item_priority;
130
		  }
131
	      }
132
		break;
133
	      case ET_BASIC: {
134
		  BasicP basic = entry_get_basic (item_entry (item));
135
 
136
		  bitvec_set (rule_first_set (rule), basic_terminal (basic));
137
		  see_through = FALSE;
138
	      }
139
		break;
140
	      case ET_NON_LOCAL:
141
	      case ET_NAME:
142
	      case ET_TYPE:
143
		UNREACHED;
144
	    }
145
	}
146
	if (see_through) {
147
	    rule_see_through (rule);
148
	}
149
    }
150
    if (rule_has_empty_alt (rule)) {
151
	rule_see_through (rule);
152
    }
153
    rule_set_priority (rule, priority + 1);
154
    if (rule_is_see_through (rule) && bitvec_is_full (rule_first_set (rule))) {
155
	E_redundant_see_through_alt (rule);
156
    }
157
    rule_computed_first_set (rule);
158
}
159
 
160
void
161
rule_compute_first_set PROTO_N ((entry, gclosure))
162
		       PROTO_T (EntryP   entry X
163
				GenericP gclosure)
164
{
165
    UNUSED (gclosure);
166
    if (entry_is_rule (entry)) {
167
	RuleP rule = entry_get_rule (entry);
168
 
169
	rule_compute_first_set_1 (rule);
170
    }
171
}
172
 
173
/*
174
 * Local variables(smf):
175
 * eval: (include::add-path-entry "../os-interface" "../library")
176
 * eval: (include::add-path-entry "../generated")
177
 * end:
178
**/