Eliminate tabs and trailing spaces

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@21441 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Jeff Cohen 2005-04-22 04:13:13 +00:00
parent 3c94497ec7
commit ea3e5e56fd
29 changed files with 3524 additions and 3524 deletions

View File

@ -94,7 +94,7 @@ public:
/// are to match.
///
bool debugPassMiscompilation(const PassInfo *ThePass,
const std::string &ReferenceOutput);
const std::string &ReferenceOutput);
/// compileSharedObject - This method creates a SharedObject from a given
/// BytecodeFile for debugging a code generator.
@ -227,7 +227,7 @@ public:
///
bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
std::string &OutputFilename, bool DeleteOutput = false,
bool Quiet = false) const;
bool Quiet = false) const;
/// writeProgramToFile - This writes the current "Program" to the named
/// bytecode file. If an error occurs, true is returned.

View File

@ -564,7 +564,7 @@ bool BugDriver::debugMiscompilation() {
// Make sure something was miscompiled...
if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun)) {
std::cerr << "*** Optimized program matches reference output! No problem "
<< "detected...\nbugpoint can't help you with your problem!\n";
<< "detected...\nbugpoint can't help you with your problem!\n";
return false;
}

View File

@ -43,7 +43,7 @@ using namespace llvm;
/// file. If an error occurs, true is returned.
///
bool BugDriver::writeProgramToFile(const std::string &Filename,
Module *M) const {
Module *M) const {
std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
std::ios::binary;
std::ofstream Out(Filename.c_str(), io_mode);
@ -127,7 +127,7 @@ static void RunChild(Module *Program,const std::vector<const PassInfo*> &Passes,
///
bool BugDriver::runPasses(const std::vector<const PassInfo*> &Passes,
std::string &OutputFilename, bool DeleteOutput,
bool Quiet) const{
bool Quiet) const{
std::cout << std::flush;
sys::Path uniqueFilename("bugpoint-output.bc");
uniqueFilename.makeUnique();

View File

@ -1,6 +1,6 @@
/* $Id$ */
#define MAX_ARITY 2
#define MAX_ARITY 2
typedef int ItemSetNum;
typedef int OperatorNum;
@ -9,11 +9,11 @@ typedef int RuleNum;
typedef int ArityNum;
typedef int ERuleNum;
extern NonTerminalNum last_user_nonterminal;
extern NonTerminalNum max_nonterminal;
extern RuleNum max_rule;
extern ERuleNum max_erule_num;
extern int max_arity;
extern NonTerminalNum last_user_nonterminal;
extern NonTerminalNum max_nonterminal;
extern RuleNum max_rule;
extern ERuleNum max_erule_num;
extern int max_arity;
#ifdef __STDC__
#define ARGS(x) x
@ -22,7 +22,7 @@ extern int max_arity;
#endif
#ifndef NOLEX
#define DELTAWIDTH 4
#define DELTAWIDTH 4
typedef short DeltaCost[DELTAWIDTH];
typedef short *DeltaPtr;
extern void ASSIGNCOST ARGS((DeltaPtr, DeltaPtr));
@ -31,179 +31,179 @@ extern void MINUSCOST ARGS((DeltaPtr, DeltaPtr));
extern void ZEROCOST ARGS((DeltaPtr));
extern int LESSCOST ARGS((DeltaPtr, DeltaPtr));
extern int EQUALCOST ARGS((DeltaPtr, DeltaPtr));
#define PRINCIPLECOST(x) (x[0])
#define PRINCIPLECOST(x) (x[0])
#else
#define DELTAWIDTH 1
#define DELTAWIDTH 1
typedef int DeltaCost;
typedef int DeltaPtr;
#define ASSIGNCOST(l, r) ((l) = (r))
#define ADDCOST(l, r) ((l) += (r))
#define MINUSCOST(l, r) ((l) -= (r))
#define ZEROCOST(x) ((x) = 0)
#define LESSCOST(l, r) ((l) < (r))
#define EQUALCOST(l, r) ((l) == (r))
#define PRINCIPLECOST(x) (x)
#define ASSIGNCOST(l, r) ((l) = (r))
#define ADDCOST(l, r) ((l) += (r))
#define MINUSCOST(l, r) ((l) -= (r))
#define ZEROCOST(x) ((x) = 0)
#define LESSCOST(l, r) ((l) < (r))
#define EQUALCOST(l, r) ((l) == (r))
#define PRINCIPLECOST(x) (x)
#endif /* NOLEX */
#define NODIVERGE(c,state,nt,base) if (prevent_divergence > 0) CHECKDIVERGE(c,state,nt,base);
#define NODIVERGE(c,state,nt,base) if (prevent_divergence > 0) CHECKDIVERGE(c,state,nt,base);
struct list {
void *x;
struct list *next;
void *x;
struct list *next;
};
typedef struct list *List;
typedef struct list *List;
struct intlist {
int x;
struct intlist *next;
int x;
struct intlist *next;
};
typedef struct intlist *IntList;
typedef struct intlist *IntList;
struct operator {
char *name;
unsigned int ref:1;
OperatorNum num;
ItemSetNum baseNum;
ItemSetNum stateCount;
ArityNum arity;
struct table *table;
char *name;
unsigned int ref:1;
OperatorNum num;
ItemSetNum baseNum;
ItemSetNum stateCount;
ArityNum arity;
struct table *table;
};
typedef struct operator *Operator;
typedef struct operator *Operator;
struct nonterminal {
char *name;
NonTerminalNum num;
ItemSetNum baseNum;
ItemSetNum ruleCount;
struct plankMap *pmap;
char *name;
NonTerminalNum num;
ItemSetNum baseNum;
ItemSetNum ruleCount;
struct plankMap *pmap;
struct rule *sampleRule; /* diagnostic---gives "a" rule that with this lhs */
struct rule *sampleRule; /* diagnostic---gives "a" rule that with this lhs */
};
typedef struct nonterminal *NonTerminal;
typedef struct nonterminal *NonTerminal;
struct pattern {
NonTerminal normalizer;
Operator op; /* NULL if NonTerm -> NonTerm */
NonTerminal children[MAX_ARITY];
NonTerminal normalizer;
Operator op; /* NULL if NonTerm -> NonTerm */
NonTerminal children[MAX_ARITY];
};
typedef struct pattern *Pattern;
typedef struct pattern *Pattern;
struct rule {
DeltaCost delta;
ERuleNum erulenum;
RuleNum num;
RuleNum newNum;
NonTerminal lhs;
Pattern pat;
unsigned int used:1;
DeltaCost delta;
ERuleNum erulenum;
RuleNum num;
RuleNum newNum;
NonTerminal lhs;
Pattern pat;
unsigned int used:1;
};
typedef struct rule *Rule;
typedef struct rule *Rule;
struct item {
DeltaCost delta;
Rule rule;
DeltaCost delta;
Rule rule;
};
typedef struct item Item;
typedef struct item Item;
typedef short *Relevant; /* relevant non-terminals */
typedef short *Relevant; /* relevant non-terminals */
typedef Item *ItemArray;
typedef Item *ItemArray;
struct item_set { /* indexed by NonTerminal */
ItemSetNum num;
ItemSetNum newNum;
Operator op;
struct item_set *kids[2];
struct item_set *representative;
Relevant relevant;
ItemArray virgin;
ItemArray closed;
struct item_set { /* indexed by NonTerminal */
ItemSetNum num;
ItemSetNum newNum;
Operator op;
struct item_set *kids[2];
struct item_set *representative;
Relevant relevant;
ItemArray virgin;
ItemArray closed;
};
typedef struct item_set *Item_Set;
typedef struct item_set *Item_Set;
#define DIM_MAP_SIZE (1 << 8)
#define GLOBAL_MAP_SIZE (1 << 15)
#define DIM_MAP_SIZE (1 << 8)
#define GLOBAL_MAP_SIZE (1 << 15)
struct mapping { /* should be a hash table for TS -> int */
List *hash;
int hash_size;
int max_size;
ItemSetNum count;
Item_Set *set; /* map: int <-> Item_Set */
struct mapping { /* should be a hash table for TS -> int */
List *hash;
int hash_size;
int max_size;
ItemSetNum count;
Item_Set *set; /* map: int <-> Item_Set */
};
typedef struct mapping *Mapping;
typedef struct mapping *Mapping;
struct index_map {
ItemSetNum max_size;
Item_Set *class;
ItemSetNum max_size;
Item_Set *class;
};
typedef struct index_map Index_Map;
typedef struct index_map Index_Map;
struct dimension {
Relevant relevant;
Index_Map index_map;
Mapping map;
ItemSetNum max_size;
struct plankMap *pmap;
Relevant relevant;
Index_Map index_map;
Mapping map;
ItemSetNum max_size;
struct plankMap *pmap;
};
typedef struct dimension *Dimension;
typedef struct dimension *Dimension;
struct table {
Operator op;
List rules;
Relevant relevant;
Dimension dimen[MAX_ARITY]; /* 1 for each dimension */
Item_Set *transition; /* maps local indices to global
itemsets */
Operator op;
List rules;
Relevant relevant;
Dimension dimen[MAX_ARITY]; /* 1 for each dimension */
Item_Set *transition; /* maps local indices to global
itemsets */
};
typedef struct table *Table;
typedef struct table *Table;
struct relation {
Rule rule;
DeltaCost chain;
NonTerminalNum nextchain;
DeltaCost sibling;
int sibFlag;
int sibComputed;
Rule rule;
DeltaCost chain;
NonTerminalNum nextchain;
DeltaCost sibling;
int sibFlag;
int sibComputed;
};
typedef struct relation *Relation;
typedef struct relation *Relation;
struct queue {
List head;
List tail;
List head;
List tail;
};
typedef struct queue *Queue;
typedef struct queue *Queue;
struct plank {
char *name;
List fields;
int width;
char *name;
List fields;
int width;
};
typedef struct plank *Plank;
typedef struct plank *Plank;
struct except {
short index;
short value;
short index;
short value;
};
typedef struct except *Exception;
typedef struct except *Exception;
struct plankMap {
List exceptions;
int offset;
struct stateMap *values;
List exceptions;
int offset;
struct stateMap *values;
};
typedef struct plankMap *PlankMap;
typedef struct plankMap *PlankMap;
struct stateMap {
char *fieldname;
Plank plank;
int width;
short *value;
char *fieldname;
Plank plank;
int width;
short *value;
};
typedef struct stateMap *StateMap;
typedef struct stateMap *StateMap;
struct stateMapTable {
List maps;
List maps;
};
extern void CHECKDIVERGE ARGS((DeltaPtr, Item_Set, int, int));
@ -232,7 +232,7 @@ extern Item_Set encode ARGS((Mapping, Item_Set, int *));
extern void build ARGS((void));
extern Item_Set *transLval ARGS((Table, int, int));
typedef void * (*ListFn) ARGS((void *));
typedef void * (*ListFn) ARGS((void *));
extern void foreachList ARGS((ListFn, List));
extern void reveachList ARGS((ListFn, List));
@ -247,23 +247,23 @@ extern void addRelevant ARGS((Relevant, NonTerminalNum));
extern void *zalloc ARGS((unsigned int));
extern void zfree ARGS((void *));
extern NonTerminal start;
extern List rules;
extern List chainrules;
extern List operators;
extern List leaves;
extern List nonterminals;
extern List grammarNts;
extern Queue globalQ;
extern Mapping globalMap;
extern int exceptionTolerance;
extern int prevent_divergence;
extern int principleCost;
extern int lexical;
extern struct rule stub_rule;
extern Relation *allpairs;
extern Item_Set *sortedStates;
extern Item_Set errorState;
extern NonTerminal start;
extern List rules;
extern List chainrules;
extern List operators;
extern List leaves;
extern List nonterminals;
extern List grammarNts;
extern Queue globalQ;
extern Mapping globalMap;
extern int exceptionTolerance;
extern int prevent_divergence;
extern int principleCost;
extern int lexical;
extern struct rule stub_rule;
extern Relation *allpairs;
extern Item_Set *sortedStates;
extern Item_Set errorState;
extern void dumpRelevant ARGS((Relevant));
extern void dumpOperator ARGS((Operator, int));
@ -289,14 +289,14 @@ extern void dumpSortedRules ARGS((void));
extern int debugTrim;
#ifdef DEBUG
#define debug(a,b) if (a) b
#define debug(a,b) if (a) b
#else
#define debug(a,b)
#endif
extern int debugTables;
#define TABLE_INCR 8
#define STATES_INCR 64
#define TABLE_INCR 8
#define STATES_INCR 64
#ifdef NDEBUG
#define assert(c) ((void) 0)

File diff suppressed because it is too large Load Diff

View File

@ -9,63 +9,63 @@ static void doLeaf ARGS((Operator));
static void
doLeaf(leaf) Operator leaf;
{
int new;
List pl;
Item_Set ts;
Item_Set tmp;
int new;
List pl;
Item_Set ts;
Item_Set tmp;
assert(leaf->arity == 0);
assert(leaf->arity == 0);
ts = newItem_Set(leaf->table->relevant);
ts = newItem_Set(leaf->table->relevant);
for (pl = rules; pl; pl = pl->next) {
Rule p = (Rule) pl->x;
if (p->pat->op == leaf) {
if (!ts->virgin[p->lhs->num].rule || p->delta < ts->virgin[p->lhs->num].delta) {
ts->virgin[p->lhs->num].rule = p;
ASSIGNCOST(ts->virgin[p->lhs->num].delta, p->delta);
ts->op = leaf;
}
}
}
trim(ts);
zero(ts);
tmp = encode(globalMap, ts, &new);
if (new) {
closure(ts);
leaf->table->transition[0] = ts;
addQ(globalQ, ts);
} else {
leaf->table->transition[0] = tmp;
freeItem_Set(ts);
}
for (pl = rules; pl; pl = pl->next) {
Rule p = (Rule) pl->x;
if (p->pat->op == leaf) {
if (!ts->virgin[p->lhs->num].rule || p->delta < ts->virgin[p->lhs->num].delta) {
ts->virgin[p->lhs->num].rule = p;
ASSIGNCOST(ts->virgin[p->lhs->num].delta, p->delta);
ts->op = leaf;
}
}
}
trim(ts);
zero(ts);
tmp = encode(globalMap, ts, &new);
if (new) {
closure(ts);
leaf->table->transition[0] = ts;
addQ(globalQ, ts);
} else {
leaf->table->transition[0] = tmp;
freeItem_Set(ts);
}
}
void
build()
{
int new;
List ol;
Item_Set ts;
int new;
List ol;
Item_Set ts;
globalQ = newQ();
globalMap = newMapping(GLOBAL_MAP_SIZE);
globalQ = newQ();
globalMap = newMapping(GLOBAL_MAP_SIZE);
ts = newItem_Set(0);
errorState = encode(globalMap, ts, &new);
ts->closed = ts->virgin;
addQ(globalQ, ts);
ts = newItem_Set(0);
errorState = encode(globalMap, ts, &new);
ts->closed = ts->virgin;
addQ(globalQ, ts);
foreachList((ListFn) doLeaf, leaves);
foreachList((ListFn) doLeaf, leaves);
debug(debugTables, printf("---initial set of states ---\n"));
debug(debugTables, dumpMapping(globalMap));
debug(debugTables, foreachList((ListFn) dumpItem_Set, globalQ->head));
for (ts = popQ(globalQ); ts; ts = popQ(globalQ)) {
for (ol = operators; ol; ol = ol->next) {
Operator op = (Operator) ol->x;
addToTable(op->table, ts);
}
}
debug(debugTables, printf("---initial set of states ---\n"));
debug(debugTables, dumpMapping(globalMap));
debug(debugTables, foreachList((ListFn) dumpItem_Set, globalQ->head));
for (ts = popQ(globalQ); ts; ts = popQ(globalQ)) {
for (ol = operators; ol; ol = ol->next) {
Operator op = (Operator) ol->x;
addToTable(op->table, ts);
}
}
}

View File

@ -10,86 +10,86 @@ List chainrules;
void
findChainRules()
{
List pl;
List pl;
assert(!chainrules);
assert(!chainrules);
for (pl = rules; pl; pl = pl->next) {
Rule p = (Rule) pl->x;
if (!p->pat->op) {
chainrules = newList(p, chainrules);
} else {
p->pat->op->table->rules = newList(p, p->pat->op->table->rules);
addRelevant(p->pat->op->table->relevant, p->lhs->num);
}
}
for (pl = rules; pl; pl = pl->next) {
Rule p = (Rule) pl->x;
if (!p->pat->op) {
chainrules = newList(p, chainrules);
} else {
p->pat->op->table->rules = newList(p, p->pat->op->table->rules);
addRelevant(p->pat->op->table->relevant, p->lhs->num);
}
}
}
void
zero(t) Item_Set t;
{
int i;
DeltaCost base;
int exists;
int base_nt = 0;
int i;
DeltaCost base;
int exists;
int base_nt = 0;
assert(!t->closed);
assert(!t->closed);
ZEROCOST(base);
exists = 0;
for (i = 0; i < max_nonterminal; i++) {
if (t->virgin[i].rule) {
if (exists) {
if (LESSCOST(t->virgin[i].delta, base)) {
ASSIGNCOST(base, t->virgin[i].delta);
base_nt = i;
}
} else {
ASSIGNCOST(base, t->virgin[i].delta);
exists = 1;
base_nt = i;
}
}
}
if (!exists) {
return;
}
for (i = 0; i < max_nonterminal; i++) {
if (t->virgin[i].rule) {
MINUSCOST(t->virgin[i].delta, base);
}
NODIVERGE(t->virgin[i].delta, t, i, base_nt);
}
ZEROCOST(base);
exists = 0;
for (i = 0; i < max_nonterminal; i++) {
if (t->virgin[i].rule) {
if (exists) {
if (LESSCOST(t->virgin[i].delta, base)) {
ASSIGNCOST(base, t->virgin[i].delta);
base_nt = i;
}
} else {
ASSIGNCOST(base, t->virgin[i].delta);
exists = 1;
base_nt = i;
}
}
}
if (!exists) {
return;
}
for (i = 0; i < max_nonterminal; i++) {
if (t->virgin[i].rule) {
MINUSCOST(t->virgin[i].delta, base);
}
NODIVERGE(t->virgin[i].delta, t, i, base_nt);
}
}
void
closure(t) Item_Set t;
{
int changes;
List pl;
int changes;
List pl;
assert(!t->closed);
t->closed = itemArrayCopy(t->virgin);
assert(!t->closed);
t->closed = itemArrayCopy(t->virgin);
changes = 1;
while (changes) {
changes = 0;
for (pl = chainrules; pl; pl = pl->next) {
Rule p = (Rule) pl->x;
register Item *rhs_item = &t->closed[p->pat->children[0]->num];
changes = 1;
while (changes) {
changes = 0;
for (pl = chainrules; pl; pl = pl->next) {
Rule p = (Rule) pl->x;
register Item *rhs_item = &t->closed[p->pat->children[0]->num];
if (rhs_item->rule) { /* rhs is active */
DeltaCost dc;
register Item *lhs_item = &t->closed[p->lhs->num];
if (rhs_item->rule) { /* rhs is active */
DeltaCost dc;
register Item *lhs_item = &t->closed[p->lhs->num];
ASSIGNCOST(dc, rhs_item->delta);
ADDCOST(dc, p->delta);
if (LESSCOST(dc, lhs_item->delta) || !lhs_item->rule) {
ASSIGNCOST(lhs_item->delta, dc);
lhs_item->rule = p;
changes = 1;
}
}
}
}
ASSIGNCOST(dc, rhs_item->delta);
ADDCOST(dc, p->delta);
if (LESSCOST(dc, lhs_item->delta) || !lhs_item->rule) {
ASSIGNCOST(lhs_item->delta, dc);
lhs_item->rule = p;
changes = 1;
}
}
}
}
}

View File

@ -11,133 +11,133 @@ int lexical = 0;
void
ASSIGNCOST(l, r) DeltaPtr l; DeltaPtr r;
{
int i;
int i;
if (lexical) {
for (i = 0; i < DELTAWIDTH; i++) {
l[i] = r[i];
}
} else {
l[0] = r[0];
}
if (lexical) {
for (i = 0; i < DELTAWIDTH; i++) {
l[i] = r[i];
}
} else {
l[0] = r[0];
}
}
void
ADDCOST(l, r) DeltaPtr l; DeltaPtr r;
{
int i;
int i;
if (lexical) {
for (i = 0; i < DELTAWIDTH; i++) {
l[i] += r[i];
}
} else {
l[0] += r[0];
}
if (lexical) {
for (i = 0; i < DELTAWIDTH; i++) {
l[i] += r[i];
}
} else {
l[0] += r[0];
}
}
void
MINUSCOST(l, r) DeltaPtr l; DeltaPtr r;
{
int i;
int i;
if (lexical) {
for (i = 0; i < DELTAWIDTH; i++) {
l[i] -= r[i];
}
} else {
l[0] -= r[0];
}
if (lexical) {
for (i = 0; i < DELTAWIDTH; i++) {
l[i] -= r[i];
}
} else {
l[0] -= r[0];
}
}
void
ZEROCOST(x) DeltaPtr x;
{
int i;
int i;
if (lexical) {
for (i = 0; i < DELTAWIDTH; i++) {
x[i] = 0;
}
} else {
x[0] = 0;
}
if (lexical) {
for (i = 0; i < DELTAWIDTH; i++) {
x[i] = 0;
}
} else {
x[0] = 0;
}
}
int
LESSCOST(l, r) DeltaPtr l; DeltaPtr r;
{
int i;
int i;
if (lexical) {
for (i = 0; i < DELTAWIDTH; i++) {
if (l[i] < r[i]) {
return 1;
} else if (l[i] > r[i]) {
return 0;
}
}
return 0;
} else {
return l[0] < r[0];
}
if (lexical) {
for (i = 0; i < DELTAWIDTH; i++) {
if (l[i] < r[i]) {
return 1;
} else if (l[i] > r[i]) {
return 0;
}
}
return 0;
} else {
return l[0] < r[0];
}
}
int
EQUALCOST(l, r) DeltaPtr l; DeltaPtr r;
{
int i;
int i;
if (lexical) {
for (i = 0; i < DELTAWIDTH; i++) {
if (l[i] != r[i]) {
return 0;
}
}
return 1;
} else {
return l[0] == r[0];
}
if (lexical) {
for (i = 0; i < DELTAWIDTH; i++) {
if (l[i] != r[i]) {
return 0;
}
}
return 1;
} else {
return l[0] == r[0];
}
}
#endif /* NOLEX */
void
CHECKDIVERGE(c, its, nt, base) DeltaPtr c; Item_Set its; int nt; int base;
{
int i;
int i;
if (prevent_divergence <= 0) {
return;
}
if (lexical) {
if (prevent_divergence <= 0) {
return;
}
if (lexical) {
#ifndef NOLEX
for (i = 0; i < DELTAWIDTH; i++) {
if (c[i] > prevent_divergence) {
char ntname[100];
char basename[100];
nonTerminalName(ntname, nt);
nonTerminalName(basename, base);
fprintf(stderr, "ERROR: The grammar appears to diverge\n");
fprintf(stderr, "\tRelative Costs: %s(0), %s(%d)\n", basename, ntname, c[i]);
fprintf(stderr, "\tOffending Operator: %s\n", its->op->name);
fprintf(stderr, "\tOffending Tree: ");
printRepresentative(stderr, its);
fprintf(stderr, "\n");
exit(1);
}
}
for (i = 0; i < DELTAWIDTH; i++) {
if (c[i] > prevent_divergence) {
char ntname[100];
char basename[100];
nonTerminalName(ntname, nt);
nonTerminalName(basename, base);
fprintf(stderr, "ERROR: The grammar appears to diverge\n");
fprintf(stderr, "\tRelative Costs: %s(0), %s(%d)\n", basename, ntname, c[i]);
fprintf(stderr, "\tOffending Operator: %s\n", its->op->name);
fprintf(stderr, "\tOffending Tree: ");
printRepresentative(stderr, its);
fprintf(stderr, "\n");
exit(1);
}
}
#endif /*NOLEX*/
} else if (PRINCIPLECOST(c) > prevent_divergence) {
char ntname[100];
char basename[100];
nonTerminalName(ntname, nt);
nonTerminalName(basename, base);
fprintf(stderr, "ERROR: The grammar appears to diverge\n");
fprintf(stderr, "\tRelative Costs: %s(0), %s(%d)\n", basename, ntname, PRINCIPLECOST(c));
fprintf(stderr, "\tOffending Operator: %s\n", its->op->name);
fprintf(stderr, "\tOffending Tree: ");
printRepresentative(stderr, its);
fprintf(stderr, "\n");
exit(1);
}
} else if (PRINCIPLECOST(c) > prevent_divergence) {
char ntname[100];
char basename[100];
nonTerminalName(ntname, nt);
nonTerminalName(basename, base);
fprintf(stderr, "ERROR: The grammar appears to diverge\n");
fprintf(stderr, "\tRelative Costs: %s(0), %s(%d)\n", basename, ntname, PRINCIPLECOST(c));
fprintf(stderr, "\tOffending Operator: %s\n", its->op->name);
fprintf(stderr, "\tOffending Tree: ");
printRepresentative(stderr, its);
fprintf(stderr, "\n");
exit(1);
}
}

View File

@ -9,8 +9,8 @@ int grammarflag;
static int arity;
List ruleASTs;
List grammarNts;
List ruleASTs;
List grammarNts;
static void doBinding ARGS((Binding));
static void doDecl ARGS((Arity));
@ -23,29 +23,29 @@ static void doTable ARGS((Operator));
static void
doBinding(b) Binding b;
{
int new;
Symbol s;
int new;
Symbol s;
s = enter(b->name, &new);
if (!new) {
fprintf(stderr, "Non-unique name: %s\n", b->name);
exit(1);
}
s->tag = OPERATOR;
s->u.op = newOperator(b->name, b->opnum, arity);
if (arity == 0) {
leaves = newList(s->u.op, leaves);
}
s = enter(b->name, &new);
if (!new) {
fprintf(stderr, "Non-unique name: %s\n", b->name);
exit(1);
}
s->tag = OPERATOR;
s->u.op = newOperator(b->name, b->opnum, arity);
if (arity == 0) {
leaves = newList(s->u.op, leaves);
}
}
static void
doDecl(a) Arity a;
{
if (!a) {
return;
}
arity = a->arity;
foreachList((ListFn) doBinding, a->bindings);
if (!a) {
return;
}
arity = a->arity;
foreachList((ListFn) doBinding, a->bindings);
}
@ -55,263 +55,263 @@ static int tcount;
static NonTerminal
lookup(p) Pattern p;
{
char buf[10];
char *s;
List l;
NonTerminal n;
DeltaCost dummy;
char buf[10];
char *s;
List l;
NonTerminal n;
DeltaCost dummy;
for (l = xpatterns; l; l = l->next) {
Pattern x = (Pattern) l->x;
if (x->op == p->op
&& x->children[0] == p->children[0]
&& x->children[1] == p->children[1]) {
return x->normalizer;
}
}
sprintf(buf, "n%%%d", tcount++);
s = (char *) zalloc(strlen(buf)+1);
strcpy(s, buf);
n = newNonTerminal(s);
p->normalizer = n;
xpatterns = newList(p, xpatterns);
ZEROCOST(dummy);
(void) newRule(dummy, 0, n, p);
return n;
for (l = xpatterns; l; l = l->next) {
Pattern x = (Pattern) l->x;
if (x->op == p->op
&& x->children[0] == p->children[0]
&& x->children[1] == p->children[1]) {
return x->normalizer;
}
}
sprintf(buf, "n%%%d", tcount++);
s = (char *) zalloc(strlen(buf)+1);
strcpy(s, buf);
n = newNonTerminal(s);
p->normalizer = n;
xpatterns = newList(p, xpatterns);
ZEROCOST(dummy);
(void) newRule(dummy, 0, n, p);
return n;
}
static NonTerminal
normalize(ast, nt, patt) PatternAST ast; NonTerminal nt; Pattern *patt;
{
Symbol s;
int new;
Pattern dummy;
Symbol s;
int new;
Pattern dummy;
s = enter(ast->op, &new);
ast->sym = s;
if (new) {
fprintf(stderr, "Illegal use of %s --- undefined symbol\n", s->name);
exit(1);
return 0; /* shut up compilers */
} else if (s->tag == NONTERMINAL) {
if (ast->children) {
fprintf(stderr, "Illegal use of %s, a non-terminal, as a terminal\n", s->name);
exit(1);
}
*patt = newPattern(0);
(*patt)->children[0] = s->u.nt;
return s->u.nt;
} else {
s->u.op->ref = 1;
*patt = newPattern(s->u.op);
if (s->u.op->arity == -1) {
if (!ast->children) {
s->u.op->arity = 0;
leaves = newList(s->u.op, leaves);
} else if (!ast->children->next) {
s->u.op->arity = 1;
} else if (!ast->children->next->next) {
s->u.op->arity = 2;
} else {
fprintf(stderr, "ERROR: Too many children (max = 2) for \"%s\"\n", s->name);
exit(1);
}
if (s->u.op->arity > max_arity) {
max_arity = s->u.op->arity;
}
}
switch (s->u.op->arity) {
default:
assert(0);
break;
case 0:
if (ast->children) {
fprintf(stderr, "ERROR: Incorrect number of children for leaf operator, \"%s\"\n", s->name);
exit(1);
}
break;
case 1:
if (!ast->children || ast->children->next) {
fprintf(stderr, "ERROR: Incorrect number of children for unary operator, \"%s\"\n", s->name);
exit(1);
}
(*patt)->children[0] = normalize((PatternAST) ast->children->x, 0, &dummy);
break;
case 2:
if (!ast->children || !ast->children->next) {
fprintf(stderr, "ERROR: Incorrect number of children for binary operator, \"%s\"\n", s->name);
exit(1);
}
(*patt)->children[0] = normalize((PatternAST) ast->children->x, 0, &dummy);
(*patt)->children[1] = normalize((PatternAST) ast->children->next->x, 0, &dummy);
break;
}
if (nt) {
(*patt)->normalizer = nt;
return nt;
} else {
return lookup(*patt);
}
}
s = enter(ast->op, &new);
ast->sym = s;
if (new) {
fprintf(stderr, "Illegal use of %s --- undefined symbol\n", s->name);
exit(1);
return 0; /* shut up compilers */
} else if (s->tag == NONTERMINAL) {
if (ast->children) {
fprintf(stderr, "Illegal use of %s, a non-terminal, as a terminal\n", s->name);
exit(1);
}
*patt = newPattern(0);
(*patt)->children[0] = s->u.nt;
return s->u.nt;
} else {
s->u.op->ref = 1;
*patt = newPattern(s->u.op);
if (s->u.op->arity == -1) {
if (!ast->children) {
s->u.op->arity = 0;
leaves = newList(s->u.op, leaves);
} else if (!ast->children->next) {
s->u.op->arity = 1;
} else if (!ast->children->next->next) {
s->u.op->arity = 2;
} else {
fprintf(stderr, "ERROR: Too many children (max = 2) for \"%s\"\n", s->name);
exit(1);
}
if (s->u.op->arity > max_arity) {
max_arity = s->u.op->arity;
}
}
switch (s->u.op->arity) {
default:
assert(0);
break;
case 0:
if (ast->children) {
fprintf(stderr, "ERROR: Incorrect number of children for leaf operator, \"%s\"\n", s->name);
exit(1);
}
break;
case 1:
if (!ast->children || ast->children->next) {
fprintf(stderr, "ERROR: Incorrect number of children for unary operator, \"%s\"\n", s->name);
exit(1);
}
(*patt)->children[0] = normalize((PatternAST) ast->children->x, 0, &dummy);
break;
case 2:
if (!ast->children || !ast->children->next) {
fprintf(stderr, "ERROR: Incorrect number of children for binary operator, \"%s\"\n", s->name);
exit(1);
}
(*patt)->children[0] = normalize((PatternAST) ast->children->x, 0, &dummy);
(*patt)->children[1] = normalize((PatternAST) ast->children->next->x, 0, &dummy);
break;
}
if (nt) {
(*patt)->normalizer = nt;
return nt;
} else {
return lookup(*patt);
}
}
}
static void
doEnterNonTerm(ast) RuleAST ast;
{
int new;
Symbol s;
DeltaCost delta;
int i;
IntList p;
int new;
Symbol s;
DeltaCost delta;
int i;
IntList p;
s = enter(ast->lhs, &new);
if (new) {
s->u.nt = newNonTerminal(s->name);
s->tag = NONTERMINAL;
} else {
if (s->tag != NONTERMINAL) {
fprintf(stderr, "Illegal use of %s as a non-terminal\n", s->name);
exit(1);
}
}
ZEROCOST(delta);
for (p = ast->cost, i = 0; p; p = p->next, i++) {
int x = p->x;
s = enter(ast->lhs, &new);
if (new) {
s->u.nt = newNonTerminal(s->name);
s->tag = NONTERMINAL;
} else {
if (s->tag != NONTERMINAL) {
fprintf(stderr, "Illegal use of %s as a non-terminal\n", s->name);
exit(1);
}
}
ZEROCOST(delta);
for (p = ast->cost, i = 0; p; p = p->next, i++) {
int x = p->x;
#ifndef NOLEX
if (lexical) {
if (i < DELTAWIDTH) {
delta[i] = x;
}
} else
if (lexical) {
if (i < DELTAWIDTH) {
delta[i] = x;
}
} else
#endif /* NOLEX */
{
if (i == principleCost) {
PRINCIPLECOST(delta) = x;
}
}
}
ast->rule = newRule(delta, ast->erulenum, s->u.nt, 0);
{
if (i == principleCost) {
PRINCIPLECOST(delta) = x;
}
}
}
ast->rule = newRule(delta, ast->erulenum, s->u.nt, 0);
}
static void
doRule(ast) RuleAST ast;
{
Pattern pat;
Pattern pat;
(void) normalize(ast->pat, ast->rule->lhs, &pat);
ast->rule->pat = pat;
(void) normalize(ast->pat, ast->rule->lhs, &pat);
ast->rule->pat = pat;
}
static void
doTable(op) Operator op;
{
op->table = newTable(op);
op->table = newTable(op);
}
void
void
doSpec(decls, rules) List decls; List rules;
{
foreachList((ListFn) doDecl, decls);
debug(debugTables, foreachList((ListFn) dumpOperator_l, operators));
foreachList((ListFn) doDecl, decls);
debug(debugTables, foreachList((ListFn) dumpOperator_l, operators));
ruleASTs = rules;
reveachList((ListFn) doEnterNonTerm, rules);
ruleASTs = rules;
reveachList((ListFn) doEnterNonTerm, rules);
last_user_nonterminal = max_nonterminal;
last_user_nonterminal = max_nonterminal;
reveachList((ListFn) doRule, rules);
debug(debugTables, foreachList((ListFn) dumpRule, rules));
reveachList((ListFn) doRule, rules);
debug(debugTables, foreachList((ListFn) dumpRule, rules));
foreachList((ListFn) doTable, operators);
foreachList((ListFn) doTable, operators);
}
void
doStart(name) char *name;
{
Symbol s;
int new;
Symbol s;
int new;
if (start) {
yyerror1("Redeclaration of start symbol to be ");
fprintf(stderr, "\"%s\"\n", name);
exit(1);
}
s = enter(name, &new);
if (new) {
s->u.nt = newNonTerminal(s->name);
s->tag = NONTERMINAL;
} else {
if (s->tag != NONTERMINAL) {
fprintf(stderr, "Illegal use of %s as a non-terminal\n", s->name);
exit(1);
}
}
if (start) {
yyerror1("Redeclaration of start symbol to be ");
fprintf(stderr, "\"%s\"\n", name);
exit(1);
}
s = enter(name, &new);
if (new) {
s->u.nt = newNonTerminal(s->name);
s->tag = NONTERMINAL;
} else {
if (s->tag != NONTERMINAL) {
fprintf(stderr, "Illegal use of %s as a non-terminal\n", s->name);
exit(1);
}
}
}
void
doGrammarNts()
{
List l;
int new;
List l;
int new;
for (l = grammarNts; l; l = l->next) {
char *n = (char*) l->x;
Symbol s;
for (l = grammarNts; l; l = l->next) {
char *n = (char*) l->x;
Symbol s;
s = enter(n, &new);
if (new) {
fprintf(stderr, "ERROR: %%gram, unused non-terminal: \"%s\"\n", n);
exit(1);
}
if (s->tag != NONTERMINAL) {
fprintf(stderr, "ERROR: %%gram, Not a non-terminal: \"%s\"\n", n);
exit(1);
}
l->x = s;
}
s = enter(n, &new);
if (new) {
fprintf(stderr, "ERROR: %%gram, unused non-terminal: \"%s\"\n", n);
exit(1);
}
if (s->tag != NONTERMINAL) {
fprintf(stderr, "ERROR: %%gram, Not a non-terminal: \"%s\"\n", n);
exit(1);
}
l->x = s;
}
}
void
doGram(nts) List nts;
{
if (grammarNts) {
yyerror1("Redeclaration of %%gram\n");
exit(1);
}
grammarNts = nts;
if (grammarNts) {
yyerror1("Redeclaration of %%gram\n");
exit(1);
}
grammarNts = nts;
}
Arity
Arity
newArity(ar, b) int ar; List b;
{
Arity a = (Arity) zalloc(sizeof(struct arity));
a->arity = ar;
a->bindings = b;
return a;
Arity a = (Arity) zalloc(sizeof(struct arity));
a->arity = ar;
a->bindings = b;
return a;
}
Binding
Binding
newBinding(name, opnum) char *name; int opnum;
{
Binding b = (Binding) zalloc(sizeof(struct binding));
if (opnum == 0) {
yyerror1("ERROR: Non-positive external symbol number, ");
fprintf(stderr, "%d", opnum);
exit(1);
}
b->name = name;
b->opnum = opnum;
return b;
Binding b = (Binding) zalloc(sizeof(struct binding));
if (opnum == 0) {
yyerror1("ERROR: Non-positive external symbol number, ");
fprintf(stderr, "%d", opnum);
exit(1);
}
b->name = name;
b->opnum = opnum;
return b;
}
PatternAST
newPatternAST(op, children) char *op; List children;
{
PatternAST p = (PatternAST) zalloc(sizeof(struct patternAST));
p->op = op;
p->children = children;
return p;
PatternAST p = (PatternAST) zalloc(sizeof(struct patternAST));
p->op = op;
p->children = children;
return p;
}
int max_ruleAST;
@ -319,85 +319,85 @@ int max_ruleAST;
RuleAST
newRuleAST(lhs, pat, erulenum, cost) char *lhs; PatternAST pat; int erulenum; IntList cost;
{
RuleAST p = (RuleAST) zalloc(sizeof(struct ruleAST));
p->lhs = lhs;
p->pat = pat;
if (erulenum <= 0) {
yyerror1("External Rulenumber ");
fprintf(stderr, "(%d) <= 0\n", erulenum);
exit(1);
}
p->erulenum = erulenum;
p->cost = cost;
max_ruleAST++;
return p;
RuleAST p = (RuleAST) zalloc(sizeof(struct ruleAST));
p->lhs = lhs;
p->pat = pat;
if (erulenum <= 0) {
yyerror1("External Rulenumber ");
fprintf(stderr, "(%d) <= 0\n", erulenum);
exit(1);
}
p->erulenum = erulenum;
p->cost = cost;
max_ruleAST++;
return p;
}
void
dumpBinding(b) Binding b;
{
printf("%s=%d ", b->name, b->opnum);
printf("%s=%d ", b->name, b->opnum);
}
void
dumpArity(a) Arity a;
{
List l;
List l;
printf("Arity(%d) ", a->arity);
for (l = a->bindings; l; l = l->next) {
Binding b = (Binding) l->x;
dumpBinding(b);
}
printf("\n");
printf("Arity(%d) ", a->arity);
for (l = a->bindings; l; l = l->next) {
Binding b = (Binding) l->x;
dumpBinding(b);
}
printf("\n");
}
void
dumpPatternAST(p) PatternAST p;
{
List l;
List l;
printf("%s", p->op);
if (p->children) {
printf("(");
for (l = p->children; l; l = l->next) {
PatternAST past = (PatternAST) l->x;
dumpPatternAST(past);
if (l->next) {
printf(", ");
}
}
printf(")");
}
printf("%s", p->op);
if (p->children) {
printf("(");
for (l = p->children; l; l = l->next) {
PatternAST past = (PatternAST) l->x;
dumpPatternAST(past);
if (l->next) {
printf(", ");
}
}
printf(")");
}
}
void
dumpRuleAST(p) RuleAST p;
{
printf("%s : ", p->lhs);
dumpPatternAST(p->pat);
printf(" = %d (%ld)\n", p->erulenum, (long) p->cost);
printf("%s : ", p->lhs);
dumpPatternAST(p->pat);
printf(" = %d (%ld)\n", p->erulenum, (long) p->cost);
}
void
dumpDecls(decls) List decls;
{
List l;
List l;
for (l = decls; l; l = l->next) {
Arity a = (Arity) l->x;
dumpArity(a);
}
for (l = decls; l; l = l->next) {
Arity a = (Arity) l->x;
dumpArity(a);
}
}
void
dumpRules(rules) List rules;
{
List l;
List l;
for (l = rules; l; l = l->next) {
RuleAST p = (RuleAST) l->x;
dumpRuleAST(p);
}
for (l = rules; l; l = l->next) {
RuleAST p = (RuleAST) l->x;
dumpRuleAST(p);
}
}

View File

@ -1,62 +1,62 @@
/* $Id$ */
struct binding {
char *name;
int opnum;
char *name;
int opnum;
};
typedef struct binding *Binding;
typedef struct binding *Binding;
struct arity {
int arity;
List bindings;
int arity;
List bindings;
};
typedef struct arity *Arity;
typedef struct arity *Arity;
struct patternAST {
struct symbol *sym;
char *op;
List children;
struct symbol *sym;
char *op;
List children;
};
typedef struct patternAST *PatternAST;
typedef struct patternAST *PatternAST;
struct ruleAST {
char *lhs;
PatternAST pat;
int erulenum;
IntList cost;
struct rule *rule;
struct strTableElement *kids;
struct strTableElement *nts;
char *lhs;
PatternAST pat;
int erulenum;
IntList cost;
struct rule *rule;
struct strTableElement *kids;
struct strTableElement *nts;
};
typedef struct ruleAST *RuleAST;
typedef struct ruleAST *RuleAST;
typedef enum {
UNKNOWN,
OPERATOR,
NONTERMINAL
UNKNOWN,
OPERATOR,
NONTERMINAL
} TagType;
struct symbol {
char *name;
TagType tag;
union {
NonTerminal nt;
Operator op;
} u;
char *name;
TagType tag;
union {
NonTerminal nt;
Operator op;
} u;
};
typedef struct symbol *Symbol;
typedef struct symbol *Symbol;
struct strTableElement {
char *str;
IntList erulenos;
char *ename;
char *str;
IntList erulenos;
char *ename;
};
typedef struct strTableElement *StrTableElement;
typedef struct strTableElement *StrTableElement;
struct strTable {
List elems;
List elems;
};
typedef struct strTable *StrTable;
typedef struct strTable *StrTable;
extern void doGrammarNts ARGS((void));
void makeRuleDescArray ARGS((void));
@ -122,11 +122,11 @@ extern void dumpStrTable ARGS((StrTable));
extern int yylex ARGS((void));
extern int yyparse ARGS((void));
extern int max_ruleAST;
extern List ruleASTs;
extern int max_ruleAST;
extern List ruleASTs;
extern FILE *outfile;
extern FILE *outfile;
extern const char *prefix;
extern int trimflag;
extern int speedflag;
extern int grammarflag;
extern int trimflag;
extern int speedflag;
extern int grammarflag;

View File

@ -10,124 +10,124 @@ static Item_Set fptr;
ItemArray
newItemArray()
{
ItemArray ia;
ia = (ItemArray) zalloc(max_nonterminal *sizeof(*ia));
return ia;
ItemArray ia;
ia = (ItemArray) zalloc(max_nonterminal *sizeof(*ia));
return ia;
}
ItemArray
itemArrayCopy(src) ItemArray src;
{
ItemArray dst;
ItemArray dst;
dst = newItemArray();
memcpy(dst, src, max_nonterminal * sizeof(*dst));
return dst;
dst = newItemArray();
memcpy(dst, src, max_nonterminal * sizeof(*dst));
return dst;
}
Item_Set
newItem_Set(relevant) Relevant relevant;
{
Item_Set ts;
if (fptr) {
ts = fptr;
fptr = 0;
memset(ts->virgin, 0, max_nonterminal * sizeof(struct item));
if (ts->closed) {
zfree(ts->closed);
ts->closed = 0;
}
ts->num = 0;
ts->op = 0;
} else {
ts = (Item_Set) zalloc(sizeof(struct item_set));
ts->virgin = newItemArray();
}
ts->relevant = relevant;
return ts;
Item_Set ts;
if (fptr) {
ts = fptr;
fptr = 0;
memset(ts->virgin, 0, max_nonterminal * sizeof(struct item));
if (ts->closed) {
zfree(ts->closed);
ts->closed = 0;
}
ts->num = 0;
ts->op = 0;
} else {
ts = (Item_Set) zalloc(sizeof(struct item_set));
ts->virgin = newItemArray();
}
ts->relevant = relevant;
return ts;
}
void
freeItem_Set(ts) Item_Set ts;
{
assert(!fptr);
fptr = ts;
assert(!fptr);
fptr = ts;
}
int
equivSet(a, b) Item_Set a; Item_Set b;
{
register Relevant r;
register int nt;
register Item *aa = a->virgin;
register Item *ba = b->virgin;
register Relevant r;
register int nt;
register Item *aa = a->virgin;
register Item *ba = b->virgin;
/*
return !bcmp(a->virgin, b->virgin, max_nonterminal * sizeof(Item));
*/
/*
return !bcmp(a->virgin, b->virgin, max_nonterminal * sizeof(Item));
*/
r = a->relevant ? a->relevant : b->relevant;
assert(r);
r = a->relevant ? a->relevant : b->relevant;
assert(r);
if (a->op && b->op && a->op != b->op) {
return 0;
}
for (; (nt = *r) != 0; r++) {
if (aa[nt].rule != ba[nt].rule || !EQUALCOST(aa[nt].delta, ba[nt].delta)) {
return 0;
}
}
return 1;
if (a->op && b->op && a->op != b->op) {
return 0;
}
for (; (nt = *r) != 0; r++) {
if (aa[nt].rule != ba[nt].rule || !EQUALCOST(aa[nt].delta, ba[nt].delta)) {
return 0;
}
}
return 1;
}
void
printRepresentative(f, s) FILE *f; Item_Set s;
{
if (!s) {
return;
}
fprintf(f, "%s", s->op->name);
switch (s->op->arity) {
case 1:
fprintf(f, "(");
printRepresentative(f, s->kids[0]);
fprintf(f, ")");
break;
case 2:
fprintf(f, "(");
printRepresentative(f, s->kids[0]);
fprintf(f, ", ");
printRepresentative(f, s->kids[1]);
fprintf(f, ")");
break;
}
if (!s) {
return;
}
fprintf(f, "%s", s->op->name);
switch (s->op->arity) {
case 1:
fprintf(f, "(");
printRepresentative(f, s->kids[0]);
fprintf(f, ")");
break;
case 2:
fprintf(f, "(");
printRepresentative(f, s->kids[0]);
fprintf(f, ", ");
printRepresentative(f, s->kids[1]);
fprintf(f, ")");
break;
}
}
void
dumpItem(t) Item *t;
{
printf("[%s #%d]", t->rule->lhs->name, t->rule->num);
dumpCost(t->delta);
printf("[%s #%d]", t->rule->lhs->name, t->rule->num);
dumpCost(t->delta);
}
void
dumpItem_Set(ts) Item_Set ts;
{
int i;
int i;
printf("Item_Set #%d: [", ts->num);
for (i = 1; i < max_nonterminal; i++) {
if (ts->virgin[i].rule) {
printf(" %d", i);
dumpCost(ts->virgin[i].delta);
}
}
printf(" ]\n");
printf("Item_Set #%d: [", ts->num);
for (i = 1; i < max_nonterminal; i++) {
if (ts->virgin[i].rule) {
printf(" %d", i);
dumpCost(ts->virgin[i].delta);
}
}
printf(" ]\n");
}
void
dumpCost(dc) DeltaCost dc;
{
printf("(%ld)", (long) dc);
printf("(%ld)", (long) dc);
}

View File

@ -23,237 +23,237 @@ static void ReadOldComment ARGS((ReadFn));
static char *
StrCopy(s) char *s;
{
char *t = (char *)zalloc(strlen(s) + 1);
strcpy(t,s);
return t;
char *t = (char *)zalloc(strlen(s) + 1);
strcpy(t,s);
return t;
}
static int
simple_get()
{
int ch;
if ((ch = getchar()) == '\n') {
yyline++;
}
return ch;
int ch;
if ((ch = getchar()) == '\n') {
yyline++;
}
return ch;
}
static int
code_get()
{
int ch;
if ((ch = getchar()) == '\n') {
yyline++;
}
if (ch != EOF) {
fputc(ch, outfile);
}
return ch;
int ch;
if ((ch = getchar()) == '\n') {
yyline++;
}
if (ch != EOF) {
fputc(ch, outfile);
}
return ch;
}
void
yypurge()
{
while (code_get() != EOF) ;
while (code_get() != EOF) ;
}
static void
ReadCharString(rdfn, which) ReadFn rdfn; int which;
{
int ch;
int backslash = 0;
int firstline = yyline;
int ch;
int backslash = 0;
int firstline = yyline;
while ((ch = rdfn()) != EOF) {
if (ch == which && !backslash) {
return;
}
if (ch == '\\' && !backslash) {
backslash = 1;
} else {
backslash = 0;
}
}
yyerror1("Unexpected EOF in string on line ");
fprintf(stderr, "%d\n", firstline);
exit(1);
while ((ch = rdfn()) != EOF) {
if (ch == which && !backslash) {
return;
}
if (ch == '\\' && !backslash) {
backslash = 1;
} else {
backslash = 0;
}
}
yyerror1("Unexpected EOF in string on line ");
fprintf(stderr, "%d\n", firstline);
exit(1);
}
static void
ReadOldComment(rdfn) ReadFn rdfn;
{
/* will not work for comments delimiter in string */
/* will not work for comments delimiter in string */
int ch;
int starred = 0;
int firstline = yyline;
int ch;
int starred = 0;
int firstline = yyline;
while ((ch = rdfn()) != EOF) {
if (ch == '*') {
starred = 1;
} else if (ch == '/' && starred) {
return;
} else {
starred = 0;
}
}
yyerror1("Unexpected EOF in comment on line ");
fprintf(stderr, "%d\n", firstline);
exit(1);
while ((ch = rdfn()) != EOF) {
if (ch == '*') {
starred = 1;
} else if (ch == '/' && starred) {
return;
} else {
starred = 0;
}
}
yyerror1("Unexpected EOF in comment on line ");
fprintf(stderr, "%d\n", firstline);
exit(1);
}
static void
ReadCodeBlock()
{
int ch;
int firstline = yyline;
int ch;
int firstline = yyline;
while ((ch = getchar()) != EOF) {
if (ch == '%') {
ch = getchar();
if (ch != '}') {
yyerror("bad %%");
}
return;
}
fputc(ch, outfile);
if (ch == '\n') {
yyline++;
}
if (ch == '"' || ch == '\'') {
ReadCharString(code_get, ch);
} else if (ch == '/') {
ch = getchar();
if (ch == '*') {
fputc(ch, outfile);
ReadOldComment(code_get);
continue;
} else {
ungetc(ch, stdin);
}
}
}
yyerror1("Unclosed block of C code started on line ");
fprintf(stderr, "%d\n", firstline);
exit(1);
while ((ch = getchar()) != EOF) {
if (ch == '%') {
ch = getchar();
if (ch != '}') {
yyerror("bad %%");
}
return;
}
fputc(ch, outfile);
if (ch == '\n') {
yyline++;
}
if (ch == '"' || ch == '\'') {
ReadCharString(code_get, ch);
} else if (ch == '/') {
ch = getchar();
if (ch == '*') {
fputc(ch, outfile);
ReadOldComment(code_get);
continue;
} else {
ungetc(ch, stdin);
}
}
}
yyerror1("Unclosed block of C code started on line ");
fprintf(stderr, "%d\n", firstline);
exit(1);
}
static int done;
void
yyfinished()
{
done = 1;
done = 1;
}
int
yylex()
{
int ch;
char *ptr = buf;
int ch;
char *ptr = buf;
if (done) return 0;
while ((ch = getchar()) != EOF) {
switch (ch) {
case ' ':
case '\f':
case '\t':
continue;
case '\n':
yyline++;
continue;
case '(':
case ')':
case ',':
case ':':
case ';':
case '=':
return(ch);
case '/':
ch = getchar();
if (ch == '*') {
ReadOldComment(simple_get);
continue;
} else {
ungetc(ch, stdin);
yyerror("illegal char /");
continue;
}
case '%':
ch = getchar();
switch (ch) {
case '%':
return (K_PPERCENT);
case '{':
ReadCodeBlock();
continue;
case 's':
case 'g':
case 't':
do {
if (ptr >= &buf[BUFSIZ]) {
yyerror("ID too long");
return(ERROR);
} else {
*ptr++ = ch;
}
ch = getchar();
} while (isalpha(ch) || isdigit(ch) || ch == '_');
ungetc(ch, stdin);
*ptr = '\0';
if (!strcmp(buf, "term")) return K_TERM;
if (!strcmp(buf, "start")) return K_START;
if (!strcmp(buf, "gram")) return K_GRAM;
yyerror("illegal character after %%");
continue;
default:
yyerror("illegal character after %%");
continue;
}
default:
if (isalpha(ch) ) {
do {
if (ptr >= &buf[BUFSIZ]) {
yyerror("ID too long");
return(ERROR);
} else {
*ptr++ = ch;
}
ch = getchar();
} while (isalpha(ch) || isdigit(ch) || ch == '_');
ungetc(ch, stdin);
*ptr = '\0';
yylval.y_string = StrCopy(buf);
return(ID);
}
if (isdigit(ch)) {
int val=0;
do {
val *= 10;
val += (ch - '0');
ch = getchar();
} while (isdigit(ch));
ungetc(ch, stdin);
yylval.y_int = val;
return(INT);
}
yyerror1("illegal char ");
fprintf(stderr, "(\\%03o)\n", ch);
exit(1);
}
}
return(0);
if (done) return 0;
while ((ch = getchar()) != EOF) {
switch (ch) {
case ' ':
case '\f':
case '\t':
continue;
case '\n':
yyline++;
continue;
case '(':
case ')':
case ',':
case ':':
case ';':
case '=':
return(ch);
case '/':
ch = getchar();
if (ch == '*') {
ReadOldComment(simple_get);
continue;
} else {
ungetc(ch, stdin);
yyerror("illegal char /");
continue;
}
case '%':
ch = getchar();
switch (ch) {
case '%':
return (K_PPERCENT);
case '{':
ReadCodeBlock();
continue;
case 's':
case 'g':
case 't':
do {
if (ptr >= &buf[BUFSIZ]) {
yyerror("ID too long");
return(ERROR);
} else {
*ptr++ = ch;
}
ch = getchar();
} while (isalpha(ch) || isdigit(ch) || ch == '_');
ungetc(ch, stdin);
*ptr = '\0';
if (!strcmp(buf, "term")) return K_TERM;
if (!strcmp(buf, "start")) return K_START;
if (!strcmp(buf, "gram")) return K_GRAM;
yyerror("illegal character after %%");
continue;
default:
yyerror("illegal character after %%");
continue;
}
default:
if (isalpha(ch) ) {
do {
if (ptr >= &buf[BUFSIZ]) {
yyerror("ID too long");
return(ERROR);
} else {
*ptr++ = ch;
}
ch = getchar();
} while (isalpha(ch) || isdigit(ch) || ch == '_');
ungetc(ch, stdin);
*ptr = '\0';
yylval.y_string = StrCopy(buf);
return(ID);
}
if (isdigit(ch)) {
int val=0;
do {
val *= 10;
val += (ch - '0');
ch = getchar();
} while (isdigit(ch));
ungetc(ch, stdin);
yylval.y_int = val;
return(INT);
}
yyerror1("illegal char ");
fprintf(stderr, "(\\%03o)\n", ch);
exit(1);
}
}
return(0);
}
void yyerror1(const char *str)
{
fprintf(stderr, "line %d: %s", yyline, str);
fprintf(stderr, "line %d: %s", yyline, str);
}
void
yyerror(const char *str)
{
yyerror1(str);
fprintf(stderr, "\n");
exit(1);
yyerror1(str);
fprintf(stderr, "\n");
exit(1);
}

View File

@ -5,71 +5,71 @@ char rcsid_list[] = "$Id$";
IntList
newIntList(x, next) int x; IntList next;
{
IntList l;
IntList l;
l = (IntList) zalloc(sizeof(*l));
assert(l);
l->x = x;
l->next = next;
l = (IntList) zalloc(sizeof(*l));
assert(l);
l->x = x;
l->next = next;
return l;
return l;
}
List
newList(x, next) void *x; List next;
{
List l;
List l;
l = (List) zalloc(sizeof(*l));
assert(l);
l->x = x;
l->next = next;
l = (List) zalloc(sizeof(*l));
assert(l);
l->x = x;
l->next = next;
return l;
return l;
}
List
appendList(x, l) void *x; List l;
{
List last;
List p;
List last;
List p;
last = 0;
for (p = l; p; p = p->next) {
last = p;
}
if (last) {
last->next = newList(x, 0);
return l;
} else {
return newList(x, 0);
}
last = 0;
for (p = l; p; p = p->next) {
last = p;
}
if (last) {
last->next = newList(x, 0);
return l;
} else {
return newList(x, 0);
}
}
void
foreachList(f, l) ListFn f; List l;
{
for (; l; l = l->next) {
(*f)(l->x);
}
for (; l; l = l->next) {
(*f)(l->x);
}
}
void
reveachList(f, l) ListFn f; List l;
{
if (l) {
reveachList(f, l->next);
(*f)(l->x);
}
if (l) {
reveachList(f, l->next);
(*f)(l->x);
}
}
int
length(l) List l;
{
int c = 0;
int c = 0;
for(; l; l = l->next) {
c++;
}
return c;
for(; l; l = l->next) {
c++;
}
return c;
}

View File

@ -20,163 +20,163 @@ extern int main ARGS((int argc, char **argv));
int
main(argc, argv) int argc; char **argv;
{
int i;
extern int atoi ARGS((const char *));
int i;
extern int atoi ARGS((const char *));
for (i = 1; argv[i]; i++) {
char **needStr = 0;
int *needInt = 0;
for (i = 1; argv[i]; i++) {
char **needStr = 0;
int *needInt = 0;
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'V':
fprintf(stderr, "%s\n", version);
break;
case 'p':
needStr = (char**)&prefix;
break;
case 'o':
needStr = &outFileName;
break;
case 'I':
internals = 1;
break;
case 'T':
simpleTables = 1;
break;
case '=':
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'V':
fprintf(stderr, "%s\n", version);
break;
case 'p':
needStr = (char**)&prefix;
break;
case 'o':
needStr = &outFileName;
break;
case 'I':
internals = 1;
break;
case 'T':
simpleTables = 1;
break;
case '=':
#ifdef NOLEX
fprintf(stderr, "'%s' was not compiled to support lexicographic ordering\n", argv[0]);
fprintf(stderr, "'%s' was not compiled to support lexicographic ordering\n", argv[0]);
#else
lexical = 1;
lexical = 1;
#endif /* NOLEX */
break;
case 'O':
needInt = &principleCost;
break;
case 'c':
needInt = &prevent_divergence;
break;
case 'e':
needInt = &exceptionTolerance;
break;
case 'd':
diagnostics = 1;
break;
case 'S':
speedflag = 1;
break;
case 't':
trimflag = 1;
break;
case 'G':
grammarflag = 1;
break;
default:
fprintf(stderr, "Bad option (%s)\n", argv[i]);
exit(1);
}
} else {
if (inFileName) {
fprintf(stderr, "Unexpected Filename (%s) after (%s)\n", argv[i], inFileName);
exit(1);
}
inFileName = argv[i];
}
if (needInt || needStr) {
char *v;
char *opt = argv[i];
break;
case 'O':
needInt = &principleCost;
break;
case 'c':
needInt = &prevent_divergence;
break;
case 'e':
needInt = &exceptionTolerance;
break;
case 'd':
diagnostics = 1;
break;
case 'S':
speedflag = 1;
break;
case 't':
trimflag = 1;
break;
case 'G':
grammarflag = 1;
break;
default:
fprintf(stderr, "Bad option (%s)\n", argv[i]);
exit(1);
}
} else {
if (inFileName) {
fprintf(stderr, "Unexpected Filename (%s) after (%s)\n", argv[i], inFileName);
exit(1);
}
inFileName = argv[i];
}
if (needInt || needStr) {
char *v;
char *opt = argv[i];
if (argv[i][2]) {
v = &argv[i][2];
} else {
v = argv[++i];
if (!v) {
fprintf(stderr, "Expection argument after %s\n", opt);
exit(1);
}
}
if (needInt) {
*needInt = atoi(v);
} else if (needStr) {
*needStr = v;
}
}
}
if (argv[i][2]) {
v = &argv[i][2];
} else {
v = argv[++i];
if (!v) {
fprintf(stderr, "Expection argument after %s\n", opt);
exit(1);
}
}
if (needInt) {
*needInt = atoi(v);
} else if (needStr) {
*needStr = v;
}
}
}
if (inFileName) {
if(freopen(inFileName, "r", stdin)==NULL) {
fprintf(stderr, "Failed opening (%s)", inFileName);
exit(1);
}
}
if (inFileName) {
if(freopen(inFileName, "r", stdin)==NULL) {
fprintf(stderr, "Failed opening (%s)", inFileName);
exit(1);
}
}
if (outFileName) {
if ((outfile = fopen(outFileName, "w")) == NULL) {
fprintf(stderr, "Failed opening (%s)", outFileName);
exit(1);
}
} else {
outfile = stdout;
}
if (outFileName) {
if ((outfile = fopen(outFileName, "w")) == NULL) {
fprintf(stderr, "Failed opening (%s)", outFileName);
exit(1);
}
} else {
outfile = stdout;
}
yyparse();
yyparse();
if (!rules) {
fprintf(stderr, "ERROR: No rules present\n");
exit(1);
}
if (!rules) {
fprintf(stderr, "ERROR: No rules present\n");
exit(1);
}
findChainRules();
findAllPairs();
doGrammarNts();
build();
findChainRules();
findAllPairs();
doGrammarNts();
build();
debug(debugTables, foreachList((ListFn) dumpOperator_l, operators));
debug(debugTables, printf("---final set of states ---\n"));
debug(debugTables, dumpMapping(globalMap));
debug(debugTables, foreachList((ListFn) dumpOperator_l, operators));
debug(debugTables, printf("---final set of states ---\n"));
debug(debugTables, dumpMapping(globalMap));
startBurm();
makeNts();
if (simpleTables) {
makeSimple();
} else {
makePlanks();
}
startBurm();
makeNts();
if (simpleTables) {
makeSimple();
} else {
makePlanks();
}
startOptional();
makeLabel();
makeKids();
startOptional();
makeLabel();
makeKids();
if (internals) {
makeChild();
makeOpLabel();
makeStateLabel();
}
endOptional();
if (internals) {
makeChild();
makeOpLabel();
makeStateLabel();
}
endOptional();
makeOperatorVector();
makeNonterminals();
if (internals) {
makeOperators();
makeStringArray();
makeRuleDescArray();
makeCostArray();
makeDeltaCostArray();
makeStateStringArray();
makeNonterminalArray();
/*
makeLHSmap();
*/
}
makeClosureArray();
makeOperatorVector();
makeNonterminals();
if (internals) {
makeOperators();
makeStringArray();
makeRuleDescArray();
makeCostArray();
makeDeltaCostArray();
makeStateStringArray();
makeNonterminalArray();
/*
makeLHSmap();
*/
}
makeClosureArray();
if (diagnostics) {
reportDiagnostics();
}
if (diagnostics) {
reportDiagnostics();
}
yypurge();
exit(0);
yypurge();
exit(0);
}

View File

@ -13,123 +13,123 @@ static int hash ARGS((Item_Set, int));
Mapping
newMapping(size) int size;
{
Mapping m;
Mapping m;
m = (Mapping) zalloc(sizeof(struct mapping));
assert(m);
m = (Mapping) zalloc(sizeof(struct mapping));
assert(m);
m->count = 0;
m->hash = (List*) zalloc(size * sizeof(List));
m->hash_size = size;
m->max_size = STATES_INCR;
m->set = (Item_Set*) zalloc(m->max_size * sizeof(Item_Set));
assert(m->set);
m->count = 0;
m->hash = (List*) zalloc(size * sizeof(List));
m->hash_size = size;
m->max_size = STATES_INCR;
m->set = (Item_Set*) zalloc(m->max_size * sizeof(Item_Set));
assert(m->set);
return m;
return m;
}
static void
growMapping(m) Mapping m;
{
Item_Set *tmp;
Item_Set *tmp;
m->max_size += STATES_INCR;
tmp = (Item_Set*) zalloc(m->max_size * sizeof(Item_Set));
memcpy(tmp, m->set, m->count * sizeof(Item_Set));
zfree(m->set);
m->set = tmp;
m->max_size += STATES_INCR;
tmp = (Item_Set*) zalloc(m->max_size * sizeof(Item_Set));
memcpy(tmp, m->set, m->count * sizeof(Item_Set));
zfree(m->set);
m->set = tmp;
}
static int
hash(ts, mod) Item_Set ts; int mod;
{
register Item *p = ts->virgin;
register int v;
register Relevant r = ts->relevant;
register int nt;
register Item *p = ts->virgin;
register int v;
register Relevant r = ts->relevant;
register int nt;
if (!ts->op) {
return 0;
}
if (!ts->op) {
return 0;
}
v = 0;
for (; (nt = *r) != 0; r++) {
v ^= ((long)p[nt].rule) + (PRINCIPLECOST(p[nt].delta)<<4);
}
v >>= 4;
v &= (mod-1);
return v;
v = 0;
for (; (nt = *r) != 0; r++) {
v ^= ((long)p[nt].rule) + (PRINCIPLECOST(p[nt].delta)<<4);
}
v >>= 4;
v &= (mod-1);
return v;
}
Item_Set
encode(m, ts, new) Mapping m; Item_Set ts; int *new;
{
int h;
List l;
int h;
List l;
assert(m);
assert(ts);
assert(m->count <= m->max_size);
assert(m);
assert(ts);
assert(m->count <= m->max_size);
if (grammarNts && errorState && m == globalMap) {
List l;
int found;
if (grammarNts && errorState && m == globalMap) {
List l;
int found;
found = 0;
for (l = grammarNts; l; l = l->next) {
Symbol s;
s = (Symbol) l->x;
found = 0;
for (l = grammarNts; l; l = l->next) {
Symbol s;
s = (Symbol) l->x;
if (ts->virgin[s->u.nt->num].rule) {
found = 1;
break;
}
}
if (!found) {
*new = 0;
return errorState;
}
}
if (ts->virgin[s->u.nt->num].rule) {
found = 1;
break;
}
}
if (!found) {
*new = 0;
return errorState;
}
}
*new = 0;
h = hash(ts, m->hash_size);
for (l = m->hash[h]; l; l = l->next) {
Item_Set s = (Item_Set) l->x;
if (ts->op == s->op && equivSet(ts, s)) {
ts->num = s->num;
return s;
}
}
if (m->count >= m->max_size) {
growMapping(m);
}
assert(m->count < m->max_size);
m->set[m->count] = ts;
ts->num = m->count++;
*new = 1;
m->hash[h] = newList(ts, m->hash[h]);
return ts;
*new = 0;
h = hash(ts, m->hash_size);
for (l = m->hash[h]; l; l = l->next) {
Item_Set s = (Item_Set) l->x;
if (ts->op == s->op && equivSet(ts, s)) {
ts->num = s->num;
return s;
}
}
if (m->count >= m->max_size) {
growMapping(m);
}
assert(m->count < m->max_size);
m->set[m->count] = ts;
ts->num = m->count++;
*new = 1;
m->hash[h] = newList(ts, m->hash[h]);
return ts;
}
Item_Set
decode(m, t) Mapping m; ItemSetNum t;
{
assert(m);
assert(t);
assert(m->count < m->max_size);
assert(t < m->count);
assert(m);
assert(t);
assert(m->count < m->max_size);
assert(t < m->count);
return m->set[t];
return m->set[t];
}
void
dumpMapping(m) Mapping m;
{
int i;
int i;
printf("BEGIN Mapping: Size=%d\n", m->count);
for (i = 0; i < m->count; i++) {
dumpItem_Set(m->set[i]);
}
printf("END Mapping\n");
printf("BEGIN Mapping: Size=%d\n", m->count);
for (i = 0; i < m->count; i++) {
dumpItem_Set(m->set[i]);
}
printf("END Mapping\n");
}

View File

@ -4,46 +4,46 @@ char rcsid_nonterminal[] = "$Id$";
#include <stdio.h>
#include <string.h>
NonTerminal start;
NonTerminalNum max_nonterminal = 1;
NonTerminalNum last_user_nonterminal;
List nonterminals;
NonTerminal start;
NonTerminalNum max_nonterminal = 1;
NonTerminalNum last_user_nonterminal;
List nonterminals;
NonTerminal
newNonTerminal(name) char *name;
{
NonTerminal nt;
NonTerminal nt;
nt = (NonTerminal) zalloc(sizeof(struct nonterminal));
assert(nt);
if (max_nonterminal == 1) {
start = nt;
}
nt->name = name;
nt->num = max_nonterminal++;
nonterminals = newList(nt, nonterminals);
nt = (NonTerminal) zalloc(sizeof(struct nonterminal));
assert(nt);
if (max_nonterminal == 1) {
start = nt;
}
nt->name = name;
nt->num = max_nonterminal++;
nonterminals = newList(nt, nonterminals);
return nt;
return nt;
}
int
nonTerminalName(buf, i) char *buf; int i;
{
List l;
List l;
for (l = nonterminals; l; l = l->next) {
NonTerminal nt = (NonTerminal) l->x;
if (nt->num == i) {
strcpy(buf, nt->name);
return 1;
}
}
strcpy(buf, "(Unknown NonTerminal)");
return 0;
for (l = nonterminals; l; l = l->next) {
NonTerminal nt = (NonTerminal) l->x;
if (nt->num == i) {
strcpy(buf, nt->name);
return 1;
}
}
strcpy(buf, "(Unknown NonTerminal)");
return 0;
}
void
dumpNonTerminal(n) NonTerminal n;
{
printf("%s(%d)", n->name, n->num);
printf("%s(%d)", n->name, n->num);
}

View File

@ -11,38 +11,38 @@ List leaves;
Operator
newOperator(name, num, arity) char *name; OperatorNum num; ArityNum arity;
{
Operator op;
Operator op;
assert(arity <= MAX_ARITY);
op = (Operator) zalloc(sizeof(struct operator));
assert(op);
op->name = name;
op->num = num;
op->arity = arity;
assert(arity <= MAX_ARITY);
op = (Operator) zalloc(sizeof(struct operator));
assert(op);
op->name = name;
op->num = num;
op->arity = arity;
operators = newList(op, operators);
operators = newList(op, operators);
return op;
return op;
}
void
dumpOperator_s(op) Operator op;
{
printf("Op: %s(%d)=%d\n", op->name, op->arity, op->num);
printf("Op: %s(%d)=%d\n", op->name, op->arity, op->num);
}
void
dumpOperator(op, full) Operator op; int full;
{
dumpOperator_s(op);
if (full) {
dumpTable(op->table, 0);
}
dumpOperator_s(op);
if (full) {
dumpTable(op->table, 0);
}
}
void
dumpOperator_l(op) Operator op;
{
dumpOperator(op, 1);
dumpOperator(op, 1);
}

View File

@ -6,33 +6,33 @@ char rcsid_pattern[] = "$Id$";
Pattern
newPattern(op) Operator op;
{
Pattern p;
Pattern p;
p = (Pattern) zalloc(sizeof(struct pattern));
p->op = op;
return p;
p = (Pattern) zalloc(sizeof(struct pattern));
p->op = op;
return p;
}
void
dumpPattern(p) Pattern p;
{
int i;
int i;
if (!p) {
printf("[no-pattern]");
return;
}
if (!p) {
printf("[no-pattern]");
return;
}
if (p->op) {
printf("%s", p->op->name);
if (p->op->arity > 0) {
printf("(");
for (i = 0; i < p->op->arity; i++) {
printf("%s ", p->children[i]->name);
}
printf(")");
}
} else {
printf("%s", p->children[0]->name);
}
if (p->op) {
printf("%s", p->op->name);
if (p->op->arity > 0) {
printf("(");
for (i = 0; i < p->op->arity; i++) {
printf("%s ", p->children[i]->name);
}
printf(")");
}
} else {
printf("%s", p->children[0]->name);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -8,57 +8,57 @@ Queue globalQ;
Queue
newQ()
{
Queue q;
Queue q;
q = (Queue) zalloc(sizeof(struct queue));
assert(q);
q->head = 0;
q->tail = 0;
q = (Queue) zalloc(sizeof(struct queue));
assert(q);
q->head = 0;
q->tail = 0;
return q;
return q;
}
void
addQ(q, ts) Queue q; Item_Set ts;
{
List qe;
List qe;
assert(q);
assert(ts);
assert(q);
assert(ts);
qe = newList(ts, 0);
if (q->head) {
assert(q->tail);
q->tail->next = qe;
q->tail = qe;
} else {
q->head = q->tail = qe;
}
qe = newList(ts, 0);
if (q->head) {
assert(q->tail);
q->tail->next = qe;
q->tail = qe;
} else {
q->head = q->tail = qe;
}
}
Item_Set
popQ(q) Queue q;
{
List qe;
Item_Set ts;
List qe;
Item_Set ts;
assert(q);
assert(q);
if (q->head) {
qe = q->head;
q->head = q->head->next;
ts = (Item_Set) qe->x;
zfree(qe);
return ts;
} else {
return 0;
}
if (q->head) {
qe = q->head;
q->head = q->head->next;
ts = (Item_Set) qe->x;
zfree(qe);
return ts;
} else {
return 0;
}
}
void
dumpQ(q) Queue q;
{
printf("Begin Queue\n");
foreachList((ListFn)dumpItem_Set, q->head);
printf("End Queue\n");
printf("Begin Queue\n");
foreachList((ListFn)dumpItem_Set, q->head);
printf("End Queue\n");
}

View File

@ -13,37 +13,37 @@ List rules;
Rule
newRule(delta, erulenum, lhs, pat) DeltaPtr delta; ERuleNum erulenum; NonTerminal lhs; Pattern pat;
{
Rule p;
Rule p;
p = (Rule) zalloc(sizeof(struct rule));
assert(p);
ASSIGNCOST(p->delta, delta);
p->erulenum = erulenum;
if (erulenum > max_erule_num) {
max_erule_num = erulenum;
}
p->num = max_rule++;
p->lhs = lhs;
p->pat = pat;
p = (Rule) zalloc(sizeof(struct rule));
assert(p);
ASSIGNCOST(p->delta, delta);
p->erulenum = erulenum;
if (erulenum > max_erule_num) {
max_erule_num = erulenum;
}
p->num = max_rule++;
p->lhs = lhs;
p->pat = pat;
rules = newList(p, rules);
rules = newList(p, rules);
return p;
return p;
}
void
dumpRule(p) Rule p;
{
dumpNonTerminal(p->lhs);
printf(" : ");
dumpPattern(p->pat);
printf(" ");
dumpCost(p->delta);
printf("\n");
dumpNonTerminal(p->lhs);
printf(" : ");
dumpPattern(p->pat);
printf(" ");
dumpCost(p->delta);
printf("\n");
}
void
dumpRuleList(l) List l;
{
foreachList((ListFn)dumpRule, l);
foreachList((ListFn)dumpRule, l);
}

View File

@ -10,56 +10,56 @@ static StrTableElement newStrTableElement ARGS((void));
StrTable
newStrTable()
{
return (StrTable) zalloc(sizeof(struct strTable));
return (StrTable) zalloc(sizeof(struct strTable));
}
static StrTableElement
newStrTableElement()
{
return (StrTableElement) zalloc(sizeof(struct strTableElement));
return (StrTableElement) zalloc(sizeof(struct strTableElement));
}
void
dumpStrTable(t) StrTable t;
{
List e;
IntList r;
{
List e;
IntList r;
printf("Begin StrTable\n");
for (e = t->elems; e; e = e->next) {
StrTableElement el = (StrTableElement) e->x;
printf("%s: ", el->str);
for (r = el->erulenos; r; r = r->next) {
int i = r->x;
printf("(%d)", i);
}
printf("\n");
}
printf("End StrTable\n");
printf("Begin StrTable\n");
for (e = t->elems; e; e = e->next) {
StrTableElement el = (StrTableElement) e->x;
printf("%s: ", el->str);
for (r = el->erulenos; r; r = r->next) {
int i = r->x;
printf("(%d)", i);
}
printf("\n");
}
printf("End StrTable\n");
}
StrTableElement
addString(t, s, eruleno, new) StrTable t; char *s; int eruleno; int *new;
{
List l;
StrTableElement ste;
List l;
StrTableElement ste;
assert(t);
for (l = t->elems; l; l = l->next) {
StrTableElement e = (StrTableElement) l->x;
assert(t);
for (l = t->elems; l; l = l->next) {
StrTableElement e = (StrTableElement) l->x;
assert(e);
if (!strcmp(s, e->str)) {
e->erulenos = newIntList(eruleno, e->erulenos);
*new = 0;
return e;
}
}
ste = newStrTableElement();
ste->erulenos = newIntList(eruleno, 0);
ste->str = (char *) zalloc(strlen(s) + 1);
strcpy(ste->str, s);
t->elems = newList(ste, t->elems);
*new = 1;
return ste;
assert(e);
if (!strcmp(s, e->str)) {
e->erulenos = newIntList(eruleno, e->erulenos);
*new = 0;
return e;
}
}
ste = newStrTableElement();
ste->erulenos = newIntList(eruleno, 0);
ste->str = (char *) zalloc(strlen(s) + 1);
strcpy(ste->str, s);
t->elems = newList(ste, t->elems);
*new = 1;
return ste;
}

View File

@ -10,29 +10,29 @@ static List symtab;
Symbol
newSymbol(name) char *name;
{
Symbol s;
Symbol s;
s = (Symbol) zalloc(sizeof(struct symbol));
assert(s);
s->name = name;
return s;
s = (Symbol) zalloc(sizeof(struct symbol));
assert(s);
s->name = name;
return s;
}
Symbol
enter(name, new) char *name; int *new;
{
List l;
Symbol s;
List l;
Symbol s;
*new = 0;
for (l = symtab; l; l = l->next) {
s = (Symbol) l->x;
if (!strcmp(name, s->name)) {
return s;
}
}
*new = 1;
s = newSymbol(name);
symtab = newList(s, symtab);
return s;
*new = 0;
for (l = symtab; l; l = l->next) {
s = (Symbol) l->x;
if (!strcmp(name, s->name)) {
return s;
}
}
*new = 1;
s = newSymbol(name);
symtab = newList(s, symtab);
return s;
}

View File

@ -20,533 +20,533 @@ static void addHyperPlane ARGS((Table, int, Item_Set));
static void
growIndex_Map(r) Index_Map *r;
{
Index_Map new;
Index_Map new;
new.max_size = r->max_size + STATES_INCR;
new.class = (Item_Set*) zalloc(new.max_size * sizeof(Item_Set));
assert(new.class);
memcpy(new.class, r->class, r->max_size * sizeof(Item_Set));
zfree(r->class);
*r = new;
new.max_size = r->max_size + STATES_INCR;
new.class = (Item_Set*) zalloc(new.max_size * sizeof(Item_Set));
assert(new.class);
memcpy(new.class, r->class, r->max_size * sizeof(Item_Set));
zfree(r->class);
*r = new;
}
static Relevant
newRelevant()
{
Relevant r = (Relevant) zalloc(max_nonterminal * sizeof(*r));
return r;
Relevant r = (Relevant) zalloc(max_nonterminal * sizeof(*r));
return r;
}
void
addRelevant(r, nt) Relevant r; NonTerminalNum nt;
{
int i;
int i;
for (i = 0; r[i]; i++) {
if (r[i] == nt) {
break;
}
}
if (!r[i]) {
r[i] = nt;
}
for (i = 0; r[i]; i++) {
if (r[i] == nt) {
break;
}
}
if (!r[i]) {
r[i] = nt;
}
}
static Dimension
newDimension(op, index) Operator op; ArityNum index;
{
Dimension d;
List pl;
Relevant r;
Dimension d;
List pl;
Relevant r;
assert(op);
assert(index >= 0 && index < op->arity);
d = (Dimension) zalloc(sizeof(struct dimension));
assert(d);
assert(op);
assert(index >= 0 && index < op->arity);
d = (Dimension) zalloc(sizeof(struct dimension));
assert(d);
r = d->relevant = newRelevant();
for (pl = rules; pl; pl = pl->next) {
Rule pr = (Rule) pl->x;
if (pr->pat->op == op) {
addRelevant(r, pr->pat->children[index]->num);
}
}
r = d->relevant = newRelevant();
for (pl = rules; pl; pl = pl->next) {
Rule pr = (Rule) pl->x;
if (pr->pat->op == op) {
addRelevant(r, pr->pat->children[index]->num);
}
}
d->index_map.max_size = STATES_INCR;
d->index_map.class = (Item_Set*)
zalloc(d->index_map.max_size * sizeof(Item_Set));
d->map = newMapping(DIM_MAP_SIZE);
d->max_size = TABLE_INCR;
d->index_map.max_size = STATES_INCR;
d->index_map.class = (Item_Set*)
zalloc(d->index_map.max_size * sizeof(Item_Set));
d->map = newMapping(DIM_MAP_SIZE);
d->max_size = TABLE_INCR;
return d;
return d;
}
Table
newTable(op) Operator op;
{
Table t;
int i, size;
Table t;
int i, size;
assert(op);
assert(op);
t = (Table) zalloc(sizeof(struct table));
assert(t);
t = (Table) zalloc(sizeof(struct table));
assert(t);
t->op = op;
t->op = op;
for (i = 0; i < op->arity; i++) {
t->dimen[i] = newDimension(op, i);
}
for (i = 0; i < op->arity; i++) {
t->dimen[i] = newDimension(op, i);
}
size = 1;
for (i = 0; i < op->arity; i++) {
size *= t->dimen[i]->max_size;
}
t->transition = (Item_Set*) zalloc(size * sizeof(Item_Set));
t->relevant = newRelevant();
assert(t->transition);
size = 1;
for (i = 0; i < op->arity; i++) {
size *= t->dimen[i]->max_size;
}
t->transition = (Item_Set*) zalloc(size * sizeof(Item_Set));
t->relevant = newRelevant();
assert(t->transition);
return t;
return t;
}
static void
GT_1(t) Table t;
{
Item_Set *ts;
ItemSetNum oldsize = t->dimen[0]->max_size;
ItemSetNum newsize = t->dimen[0]->max_size + TABLE_INCR;
Item_Set *ts;
ItemSetNum oldsize = t->dimen[0]->max_size;
ItemSetNum newsize = t->dimen[0]->max_size + TABLE_INCR;
t->dimen[0]->max_size = newsize;
t->dimen[0]->max_size = newsize;
ts = (Item_Set*) zalloc(newsize * sizeof(Item_Set));
assert(ts);
memcpy(ts, t->transition, oldsize * sizeof(Item_Set));
zfree(t->transition);
t->transition = ts;
ts = (Item_Set*) zalloc(newsize * sizeof(Item_Set));
assert(ts);
memcpy(ts, t->transition, oldsize * sizeof(Item_Set));
zfree(t->transition);
t->transition = ts;
}
static void
GT_2_0(t) Table t;
{
Item_Set *ts;
ItemSetNum oldsize = t->dimen[0]->max_size;
ItemSetNum newsize = t->dimen[0]->max_size + TABLE_INCR;
int size;
Item_Set *ts;
ItemSetNum oldsize = t->dimen[0]->max_size;
ItemSetNum newsize = t->dimen[0]->max_size + TABLE_INCR;
int size;
t->dimen[0]->max_size = newsize;
t->dimen[0]->max_size = newsize;
size = newsize * t->dimen[1]->max_size;
size = newsize * t->dimen[1]->max_size;
ts = (Item_Set*) zalloc(size * sizeof(Item_Set));
assert(ts);
memcpy(ts, t->transition, oldsize*t->dimen[1]->max_size * sizeof(Item_Set));
zfree(t->transition);
t->transition = ts;
ts = (Item_Set*) zalloc(size * sizeof(Item_Set));
assert(ts);
memcpy(ts, t->transition, oldsize*t->dimen[1]->max_size * sizeof(Item_Set));
zfree(t->transition);
t->transition = ts;
}
static void
GT_2_1(t) Table t;
{
Item_Set *ts;
ItemSetNum oldsize = t->dimen[1]->max_size;
ItemSetNum newsize = t->dimen[1]->max_size + TABLE_INCR;
int size;
Item_Set *from;
Item_Set *to;
int i1, i2;
Item_Set *ts;
ItemSetNum oldsize = t->dimen[1]->max_size;
ItemSetNum newsize = t->dimen[1]->max_size + TABLE_INCR;
int size;
Item_Set *from;
Item_Set *to;
int i1, i2;
t->dimen[1]->max_size = newsize;
t->dimen[1]->max_size = newsize;
size = newsize * t->dimen[0]->max_size;
size = newsize * t->dimen[0]->max_size;
ts = (Item_Set*) zalloc(size * sizeof(Item_Set));
assert(ts);
ts = (Item_Set*) zalloc(size * sizeof(Item_Set));
assert(ts);
from = t->transition;
to = ts;
for (i1 = 0; i1 < t->dimen[0]->max_size; i1++) {
for (i2 = 0; i2 < oldsize; i2++) {
to[i2] = from[i2];
}
to += newsize;
from += oldsize;
}
zfree(t->transition);
t->transition = ts;
from = t->transition;
to = ts;
for (i1 = 0; i1 < t->dimen[0]->max_size; i1++) {
for (i2 = 0; i2 < oldsize; i2++) {
to[i2] = from[i2];
}
to += newsize;
from += oldsize;
}
zfree(t->transition);
t->transition = ts;
}
static void
growTransition(t, dim) Table t; ArityNum dim;
{
assert(t);
assert(t->op);
assert(dim < t->op->arity);
assert(t);
assert(t->op);
assert(dim < t->op->arity);
switch (t->op->arity) {
default:
assert(0);
break;
case 1:
GT_1(t);
return;
case 2:
switch (dim) {
default:
assert(0);
break;
case 0:
GT_2_0(t);
return;
case 1:
GT_2_1(t);
return;
}
}
switch (t->op->arity) {
default:
assert(0);
break;
case 1:
GT_1(t);
return;
case 2:
switch (dim) {
default:
assert(0);
break;
case 0:
GT_2_0(t);
return;
case 1:
GT_2_1(t);
return;
}
}
}
static Item_Set
restrict(d, ts) Dimension d; Item_Set ts;
{
DeltaCost base;
Item_Set r;
int found;
register Relevant r_ptr = d->relevant;
register Item *ts_current = ts->closed;
register Item *r_current;
register int i;
register int nt;
DeltaCost base;
Item_Set r;
int found;
register Relevant r_ptr = d->relevant;
register Item *ts_current = ts->closed;
register Item *r_current;
register int i;
register int nt;
ZEROCOST(base);
found = 0;
r = newItem_Set(d->relevant);
r_current = r->virgin;
for (i = 0; (nt = r_ptr[i]) != 0; i++) {
if (ts_current[nt].rule) {
r_current[nt].rule = &stub_rule;
if (!found) {
found = 1;
ASSIGNCOST(base, ts_current[nt].delta);
} else {
if (LESSCOST(ts_current[nt].delta, base)) {
ASSIGNCOST(base, ts_current[nt].delta);
}
}
}
}
ZEROCOST(base);
found = 0;
r = newItem_Set(d->relevant);
r_current = r->virgin;
for (i = 0; (nt = r_ptr[i]) != 0; i++) {
if (ts_current[nt].rule) {
r_current[nt].rule = &stub_rule;
if (!found) {
found = 1;
ASSIGNCOST(base, ts_current[nt].delta);
} else {
if (LESSCOST(ts_current[nt].delta, base)) {
ASSIGNCOST(base, ts_current[nt].delta);
}
}
}
}
/* zero align */
for (i = 0; (nt = r_ptr[i]) != 0; i++) {
if (r_current[nt].rule) {
ASSIGNCOST(r_current[nt].delta, ts_current[nt].delta);
MINUSCOST(r_current[nt].delta, base);
}
}
assert(!r->closed);
r->representative = ts;
return r;
/* zero align */
for (i = 0; (nt = r_ptr[i]) != 0; i++) {
if (r_current[nt].rule) {
ASSIGNCOST(r_current[nt].delta, ts_current[nt].delta);
MINUSCOST(r_current[nt].delta, base);
}
}
assert(!r->closed);
r->representative = ts;
return r;
}
static void
addHP_1(t, ts) Table t; Item_Set ts;
{
List pl;
Item_Set e;
Item_Set tmp;
int new;
List pl;
Item_Set e;
Item_Set tmp;
int new;
e = newItem_Set(t->relevant);
assert(e);
e->kids[0] = ts->representative;
for (pl = t->rules; pl; pl = pl->next) {
Rule p = (Rule) pl->x;
if (t->op == p->pat->op && ts->virgin[p->pat->children[0]->num].rule) {
DeltaCost dc;
ASSIGNCOST(dc, ts->virgin[p->pat->children[0]->num].delta);
ADDCOST(dc, p->delta);
if (!e->virgin[p->lhs->num].rule || LESSCOST(dc, e->virgin[p->lhs->num].delta)) {
e->virgin[p->lhs->num].rule = p;
ASSIGNCOST(e->virgin[p->lhs->num].delta, dc);
e->op = t->op;
}
}
}
trim(e);
zero(e);
tmp = encode(globalMap, e, &new);
assert(ts->num < t->dimen[0]->map->max_size);
t->transition[ts->num] = tmp;
if (new) {
closure(e);
addQ(globalQ, tmp);
} else {
freeItem_Set(e);
}
e = newItem_Set(t->relevant);
assert(e);
e->kids[0] = ts->representative;
for (pl = t->rules; pl; pl = pl->next) {
Rule p = (Rule) pl->x;
if (t->op == p->pat->op && ts->virgin[p->pat->children[0]->num].rule) {
DeltaCost dc;
ASSIGNCOST(dc, ts->virgin[p->pat->children[0]->num].delta);
ADDCOST(dc, p->delta);
if (!e->virgin[p->lhs->num].rule || LESSCOST(dc, e->virgin[p->lhs->num].delta)) {
e->virgin[p->lhs->num].rule = p;
ASSIGNCOST(e->virgin[p->lhs->num].delta, dc);
e->op = t->op;
}
}
}
trim(e);
zero(e);
tmp = encode(globalMap, e, &new);
assert(ts->num < t->dimen[0]->map->max_size);
t->transition[ts->num] = tmp;
if (new) {
closure(e);
addQ(globalQ, tmp);
} else {
freeItem_Set(e);
}
}
static void
addHP_2_0(t, ts) Table t; Item_Set ts;
{
List pl;
register Item_Set e;
Item_Set tmp;
int new;
int i2;
List pl;
register Item_Set e;
Item_Set tmp;
int new;
int i2;
assert(t->dimen[1]->map->count <= t->dimen[1]->map->max_size);
for (i2 = 0; i2 < t->dimen[1]->map->count; i2++) {
e = newItem_Set(t->relevant);
assert(e);
e->kids[0] = ts->representative;
e->kids[1] = t->dimen[1]->map->set[i2]->representative;
for (pl = t->rules; pl; pl = pl->next) {
register Rule p = (Rule) pl->x;
assert(t->dimen[1]->map->count <= t->dimen[1]->map->max_size);
for (i2 = 0; i2 < t->dimen[1]->map->count; i2++) {
e = newItem_Set(t->relevant);
assert(e);
e->kids[0] = ts->representative;
e->kids[1] = t->dimen[1]->map->set[i2]->representative;
for (pl = t->rules; pl; pl = pl->next) {
register Rule p = (Rule) pl->x;
if (t->op == p->pat->op
&& ts->virgin[p->pat->children[0]->num].rule
&& t->dimen[1]->map->set[i2]->virgin[p->pat->children[1]->num].rule){
DeltaCost dc;
ASSIGNCOST(dc, p->delta);
ADDCOST(dc, ts->virgin[p->pat->children[0]->num].delta);
ADDCOST(dc, t->dimen[1]->map->set[i2]->virgin[p->pat->children[1]->num].delta);
if (t->op == p->pat->op
&& ts->virgin[p->pat->children[0]->num].rule
&& t->dimen[1]->map->set[i2]->virgin[p->pat->children[1]->num].rule){
DeltaCost dc;
ASSIGNCOST(dc, p->delta);
ADDCOST(dc, ts->virgin[p->pat->children[0]->num].delta);
ADDCOST(dc, t->dimen[1]->map->set[i2]->virgin[p->pat->children[1]->num].delta);
if (!e->virgin[p->lhs->num].rule || LESSCOST(dc, e->virgin[p->lhs->num].delta)) {
e->virgin[p->lhs->num].rule = p;
ASSIGNCOST(e->virgin[p->lhs->num].delta, dc);
e->op = t->op;
}
}
}
trim(e);
zero(e);
tmp = encode(globalMap, e, &new);
assert(ts->num < t->dimen[0]->map->max_size);
t->transition[ts->num * t->dimen[1]->max_size + i2] = tmp;
if (new) {
closure(e);
addQ(globalQ, tmp);
} else {
freeItem_Set(e);
}
}
if (!e->virgin[p->lhs->num].rule || LESSCOST(dc, e->virgin[p->lhs->num].delta)) {
e->virgin[p->lhs->num].rule = p;
ASSIGNCOST(e->virgin[p->lhs->num].delta, dc);
e->op = t->op;
}
}
}
trim(e);
zero(e);
tmp = encode(globalMap, e, &new);
assert(ts->num < t->dimen[0]->map->max_size);
t->transition[ts->num * t->dimen[1]->max_size + i2] = tmp;
if (new) {
closure(e);
addQ(globalQ, tmp);
} else {
freeItem_Set(e);
}
}
}
static void
addHP_2_1(t, ts) Table t; Item_Set ts;
{
List pl;
register Item_Set e;
Item_Set tmp;
int new;
int i1;
List pl;
register Item_Set e;
Item_Set tmp;
int new;
int i1;
assert(t->dimen[0]->map->count <= t->dimen[0]->map->max_size);
for (i1 = 0; i1 < t->dimen[0]->map->count; i1++) {
e = newItem_Set(t->relevant);
assert(e);
e->kids[0] = t->dimen[0]->map->set[i1]->representative;
e->kids[1] = ts->representative;
for (pl = t->rules; pl; pl = pl->next) {
register Rule p = (Rule) pl->x;
assert(t->dimen[0]->map->count <= t->dimen[0]->map->max_size);
for (i1 = 0; i1 < t->dimen[0]->map->count; i1++) {
e = newItem_Set(t->relevant);
assert(e);
e->kids[0] = t->dimen[0]->map->set[i1]->representative;
e->kids[1] = ts->representative;
for (pl = t->rules; pl; pl = pl->next) {
register Rule p = (Rule) pl->x;
if (t->op == p->pat->op
&& ts->virgin[p->pat->children[1]->num].rule
&& t->dimen[0]->map->set[i1]->virgin[p->pat->children[0]->num].rule){
DeltaCost dc;
ASSIGNCOST(dc, p->delta );
ADDCOST(dc, ts->virgin[p->pat->children[1]->num].delta);
ADDCOST(dc, t->dimen[0]->map->set[i1]->virgin[p->pat->children[0]->num].delta);
if (!e->virgin[p->lhs->num].rule || LESSCOST(dc, e->virgin[p->lhs->num].delta)) {
e->virgin[p->lhs->num].rule = p;
ASSIGNCOST(e->virgin[p->lhs->num].delta, dc);
e->op = t->op;
}
}
}
trim(e);
zero(e);
tmp = encode(globalMap, e, &new);
assert(ts->num < t->dimen[1]->map->max_size);
t->transition[i1 * t->dimen[1]->max_size + ts->num] = tmp;
if (new) {
closure(e);
addQ(globalQ, tmp);
} else {
freeItem_Set(e);
}
}
if (t->op == p->pat->op
&& ts->virgin[p->pat->children[1]->num].rule
&& t->dimen[0]->map->set[i1]->virgin[p->pat->children[0]->num].rule){
DeltaCost dc;
ASSIGNCOST(dc, p->delta );
ADDCOST(dc, ts->virgin[p->pat->children[1]->num].delta);
ADDCOST(dc, t->dimen[0]->map->set[i1]->virgin[p->pat->children[0]->num].delta);
if (!e->virgin[p->lhs->num].rule || LESSCOST(dc, e->virgin[p->lhs->num].delta)) {
e->virgin[p->lhs->num].rule = p;
ASSIGNCOST(e->virgin[p->lhs->num].delta, dc);
e->op = t->op;
}
}
}
trim(e);
zero(e);
tmp = encode(globalMap, e, &new);
assert(ts->num < t->dimen[1]->map->max_size);
t->transition[i1 * t->dimen[1]->max_size + ts->num] = tmp;
if (new) {
closure(e);
addQ(globalQ, tmp);
} else {
freeItem_Set(e);
}
}
}
static void
addHyperPlane(t, i, ts) Table t; ArityNum i; Item_Set ts;
{
switch (t->op->arity) {
default:
assert(0);
break;
case 1:
addHP_1(t, ts);
return;
case 2:
switch (i) {
default:
assert(0);
break;
case 0:
addHP_2_0(t, ts);
return;
case 1:
addHP_2_1(t, ts);
return;
}
}
switch (t->op->arity) {
default:
assert(0);
break;
case 1:
addHP_1(t, ts);
return;
case 2:
switch (i) {
default:
assert(0);
break;
case 0:
addHP_2_0(t, ts);
return;
case 1:
addHP_2_1(t, ts);
return;
}
}
}
void
addToTable(t, ts) Table t; Item_Set ts;
{
ArityNum i;
ArityNum i;
assert(t);
assert(ts);
assert(t->op);
assert(t);
assert(ts);
assert(t->op);
for (i = 0; i < t->op->arity; i++) {
Item_Set r;
Item_Set tmp;
int new;
for (i = 0; i < t->op->arity; i++) {
Item_Set r;
Item_Set tmp;
int new;
r = restrict(t->dimen[i], ts);
tmp = encode(t->dimen[i]->map, r, &new);
if (t->dimen[i]->index_map.max_size <= ts->num) {
growIndex_Map(&t->dimen[i]->index_map);
}
assert(ts->num < t->dimen[i]->index_map.max_size);
t->dimen[i]->index_map.class[ts->num] = tmp;
if (new) {
if (t->dimen[i]->max_size <= r->num) {
growTransition(t, i);
}
addHyperPlane(t, i, r);
} else {
freeItem_Set(r);
}
}
r = restrict(t->dimen[i], ts);
tmp = encode(t->dimen[i]->map, r, &new);
if (t->dimen[i]->index_map.max_size <= ts->num) {
growIndex_Map(&t->dimen[i]->index_map);
}
assert(ts->num < t->dimen[i]->index_map.max_size);
t->dimen[i]->index_map.class[ts->num] = tmp;
if (new) {
if (t->dimen[i]->max_size <= r->num) {
growTransition(t, i);
}
addHyperPlane(t, i, r);
} else {
freeItem_Set(r);
}
}
}
Item_Set *
transLval(t, row, col) Table t; int row; int col;
{
switch (t->op->arity) {
case 0:
assert(row == 0);
assert(col == 0);
return t->transition;
case 1:
assert(col == 0);
return t->transition + row;
case 2:
return t->transition + row * t->dimen[1]->max_size + col;
default:
assert(0);
}
return 0;
switch (t->op->arity) {
case 0:
assert(row == 0);
assert(col == 0);
return t->transition;
case 1:
assert(col == 0);
return t->transition + row;
case 2:
return t->transition + row * t->dimen[1]->max_size + col;
default:
assert(0);
}
return 0;
}
void
dumpRelevant(r) Relevant r;
{
for (; *r; r++) {
printf("%4d", *r);
}
for (; *r; r++) {
printf("%4d", *r);
}
}
void
dumpIndex_Map(r) Index_Map *r;
{
int i;
int i;
printf("BEGIN Index_Map: MaxSize (%d)\n", r->max_size);
for (i = 0; i < globalMap->count; i++) {
printf("\t#%d: -> %d\n", i, r->class[i]->num);
}
printf("END Index_Map:\n");
printf("BEGIN Index_Map: MaxSize (%d)\n", r->max_size);
for (i = 0; i < globalMap->count; i++) {
printf("\t#%d: -> %d\n", i, r->class[i]->num);
}
printf("END Index_Map:\n");
}
void
dumpDimension(d) Dimension d;
{
printf("BEGIN Dimension:\n");
printf("Relevant: ");
dumpRelevant(d->relevant);
printf("\n");
dumpIndex_Map(&d->index_map);
dumpMapping(d->map);
printf("MaxSize of dimension = %d\n", d->max_size);
printf("END Dimension\n");
printf("BEGIN Dimension:\n");
printf("Relevant: ");
dumpRelevant(d->relevant);
printf("\n");
dumpIndex_Map(&d->index_map);
dumpMapping(d->map);
printf("MaxSize of dimension = %d\n", d->max_size);
printf("END Dimension\n");
}
void
dumpTable(t, full) Table t; int full;
{
int i;
int i;
if (!t) {
printf("NO Table yet.\n");
return;
}
printf("BEGIN Table:\n");
if (full) {
dumpOperator(t->op, 0);
}
for (i = 0; i < t->op->arity; i++) {
printf("BEGIN dimension(%d)\n", i);
dumpDimension(t->dimen[i]);
printf("END dimension(%d)\n", i);
}
dumpTransition(t);
printf("END Table:\n");
if (!t) {
printf("NO Table yet.\n");
return;
}
printf("BEGIN Table:\n");
if (full) {
dumpOperator(t->op, 0);
}
for (i = 0; i < t->op->arity; i++) {
printf("BEGIN dimension(%d)\n", i);
dumpDimension(t->dimen[i]);
printf("END dimension(%d)\n", i);
}
dumpTransition(t);
printf("END Table:\n");
}
void
dumpTransition(t) Table t;
{
int i,j;
int i,j;
switch (t->op->arity) {
case 0:
printf("{ %d }", t->transition[0]->num);
break;
case 1:
printf("{");
for (i = 0; i < t->dimen[0]->map->count; i++) {
if (i > 0) {
printf(",");
}
printf("%5d", t->transition[i]->num);
}
printf("}");
break;
case 2:
printf("{");
for (i = 0; i < t->dimen[0]->map->count; i++) {
if (i > 0) {
printf(",");
}
printf("\n");
printf("{");
for (j = 0; j < t->dimen[1]->map->count; j++) {
Item_Set *ts = transLval(t, i, j);
if (j > 0) {
printf(",");
}
printf("%5d", (*ts)->num);
}
printf("}");
}
printf("\n}\n");
break;
default:
assert(0);
}
switch (t->op->arity) {
case 0:
printf("{ %d }", t->transition[0]->num);
break;
case 1:
printf("{");
for (i = 0; i < t->dimen[0]->map->count; i++) {
if (i > 0) {
printf(",");
}
printf("%5d", t->transition[i]->num);
}
printf("}");
break;
case 2:
printf("{");
for (i = 0; i < t->dimen[0]->map->count; i++) {
if (i > 0) {
printf(",");
}
printf("\n");
printf("{");
for (j = 0; j < t->dimen[1]->map->count; j++) {
Item_Set *ts = transLval(t, i, j);
if (j > 0) {
printf(",");
}
printf("%5d", (*ts)->num);
}
printf("}");
}
printf("\n}\n");
break;
default:
assert(0);
}
}

View File

@ -16,397 +16,397 @@ static Relation *newAllPairs ARGS((void));
static void
siblings(i, j) int i; int j;
{
int k;
List pl;
DeltaCost Max;
int foundmax;
int k;
List pl;
DeltaCost Max;
int foundmax;
allpairs[i][j].sibComputed = 1;
allpairs[i][j].sibComputed = 1;
if (i == 1) {
return; /* never trim start symbol */
}
if (i==j) {
return;
}
if (i == 1) {
return; /* never trim start symbol */
}
if (i==j) {
return;
}
ZEROCOST(Max);
foundmax = 0;
ZEROCOST(Max);
foundmax = 0;
for (k = 1; k < max_nonterminal; k++) {
DeltaCost tmp;
for (k = 1; k < max_nonterminal; k++) {
DeltaCost tmp;
if (k==i || k==j) {
continue;
}
if (!allpairs[k][i].rule) {
continue;
}
if (!allpairs[k][j].rule) {
return;
}
ASSIGNCOST(tmp, allpairs[k][j].chain);
MINUSCOST(tmp, allpairs[k][i].chain);
if (foundmax) {
if (LESSCOST(Max, tmp)) {
ASSIGNCOST(Max, tmp);
}
} else {
foundmax = 1;
ASSIGNCOST(Max, tmp);
}
}
if (k==i || k==j) {
continue;
}
if (!allpairs[k][i].rule) {
continue;
}
if (!allpairs[k][j].rule) {
return;
}
ASSIGNCOST(tmp, allpairs[k][j].chain);
MINUSCOST(tmp, allpairs[k][i].chain);
if (foundmax) {
if (LESSCOST(Max, tmp)) {
ASSIGNCOST(Max, tmp);
}
} else {
foundmax = 1;
ASSIGNCOST(Max, tmp);
}
}
for (pl = rules; pl; pl = pl->next) {
Rule p = (Rule) pl->x;
Operator op = p->pat->op;
List oprule;
DeltaCost Min;
int foundmin;
if (!op) {
continue;
}
switch (op->arity) {
case 0:
continue;
case 1:
if (!allpairs[p->pat->children[0]->num ][ i].rule) {
continue;
}
foundmin = 0;
for (oprule = op->table->rules; oprule; oprule = oprule->next) {
Rule s = (Rule) oprule->x;
DeltaPtr Cx;
DeltaPtr Csj;
DeltaPtr Cpi;
DeltaCost tmp;
for (pl = rules; pl; pl = pl->next) {
Rule p = (Rule) pl->x;
Operator op = p->pat->op;
List oprule;
DeltaCost Min;
int foundmin;
if (!allpairs[p->lhs->num ][ s->lhs->num].rule
|| !allpairs[s->pat->children[0]->num ][ j].rule) {
continue;
}
Cx = allpairs[p->lhs->num ][ s->lhs->num].chain;
Csj= allpairs[s->pat->children[0]->num ][ j].chain;
Cpi= allpairs[p->pat->children[0]->num ][ i].chain;
ASSIGNCOST(tmp, Cx);
ADDCOST(tmp, s->delta);
ADDCOST(tmp, Csj);
MINUSCOST(tmp, Cpi);
MINUSCOST(tmp, p->delta);
if (foundmin) {
if (LESSCOST(tmp, Min)) {
ASSIGNCOST(Min, tmp);
}
} else {
foundmin = 1;
ASSIGNCOST(Min, tmp);
}
}
if (!foundmin) {
return;
}
if (foundmax) {
if (LESSCOST(Max, Min)) {
ASSIGNCOST(Max, Min);
}
} else {
foundmax = 1;
ASSIGNCOST(Max, Min);
}
break;
case 2:
/* do first dimension */
if (allpairs[p->pat->children[0]->num ][ i].rule) {
foundmin = 0;
for (oprule = op->table->rules; oprule; oprule = oprule->next) {
Rule s = (Rule) oprule->x;
DeltaPtr Cx;
DeltaPtr Cb;
DeltaPtr Csj;
DeltaPtr Cpi;
DeltaCost tmp;
if (!op) {
continue;
}
switch (op->arity) {
case 0:
continue;
case 1:
if (!allpairs[p->pat->children[0]->num ][ i].rule) {
continue;
}
foundmin = 0;
for (oprule = op->table->rules; oprule; oprule = oprule->next) {
Rule s = (Rule) oprule->x;
DeltaPtr Cx;
DeltaPtr Csj;
DeltaPtr Cpi;
DeltaCost tmp;
if (allpairs[p->lhs->num ][ s->lhs->num].rule
&& allpairs[s->pat->children[0]->num ][ j].rule
&& allpairs[s->pat->children[1]->num ][ p->pat->children[1]->num].rule) {
Cx = allpairs[p->lhs->num ][ s->lhs->num].chain;
Csj= allpairs[s->pat->children[0]->num ][ j].chain;
Cpi= allpairs[p->pat->children[0]->num ][ i].chain;
Cb = allpairs[s->pat->children[1]->num ][ p->pat->children[1]->num].chain;
ASSIGNCOST(tmp, Cx);
ADDCOST(tmp, s->delta);
ADDCOST(tmp, Csj);
ADDCOST(tmp, Cb);
MINUSCOST(tmp, Cpi);
MINUSCOST(tmp, p->delta);
if (foundmin) {
if (LESSCOST(tmp, Min)) {
ASSIGNCOST(Min, tmp);
}
} else {
foundmin = 1;
ASSIGNCOST(Min, tmp);
}
}
}
if (!foundmin) {
return;
}
if (foundmax) {
if (LESSCOST(Max, Min)) {
ASSIGNCOST(Max, Min);
}
} else {
foundmax = 1;
ASSIGNCOST(Max, Min);
}
}
/* do second dimension */
if (allpairs[p->pat->children[1]->num ][ i].rule) {
foundmin = 0;
for (oprule = op->table->rules; oprule; oprule = oprule->next) {
Rule s = (Rule) oprule->x;
DeltaPtr Cx;
DeltaPtr Cb;
DeltaPtr Csj;
DeltaPtr Cpi;
DeltaCost tmp;
if (!allpairs[p->lhs->num ][ s->lhs->num].rule
|| !allpairs[s->pat->children[0]->num ][ j].rule) {
continue;
}
Cx = allpairs[p->lhs->num ][ s->lhs->num].chain;
Csj= allpairs[s->pat->children[0]->num ][ j].chain;
Cpi= allpairs[p->pat->children[0]->num ][ i].chain;
ASSIGNCOST(tmp, Cx);
ADDCOST(tmp, s->delta);
ADDCOST(tmp, Csj);
MINUSCOST(tmp, Cpi);
MINUSCOST(tmp, p->delta);
if (foundmin) {
if (LESSCOST(tmp, Min)) {
ASSIGNCOST(Min, tmp);
}
} else {
foundmin = 1;
ASSIGNCOST(Min, tmp);
}
}
if (!foundmin) {
return;
}
if (foundmax) {
if (LESSCOST(Max, Min)) {
ASSIGNCOST(Max, Min);
}
} else {
foundmax = 1;
ASSIGNCOST(Max, Min);
}
break;
case 2:
/* do first dimension */
if (allpairs[p->pat->children[0]->num ][ i].rule) {
foundmin = 0;
for (oprule = op->table->rules; oprule; oprule = oprule->next) {
Rule s = (Rule) oprule->x;
DeltaPtr Cx;
DeltaPtr Cb;
DeltaPtr Csj;
DeltaPtr Cpi;
DeltaCost tmp;
if (allpairs[p->lhs->num ][ s->lhs->num].rule
&& allpairs[s->pat->children[1]->num ][ j].rule
&& allpairs[s->pat->children[0]->num ][ p->pat->children[0]->num].rule) {
Cx = allpairs[p->lhs->num ][ s->lhs->num].chain;
Csj= allpairs[s->pat->children[1]->num ][ j].chain;
Cpi= allpairs[p->pat->children[1]->num ][ i].chain;
Cb = allpairs[s->pat->children[0]->num ][ p->pat->children[0]->num].chain;
ASSIGNCOST(tmp, Cx);
ADDCOST(tmp, s->delta);
ADDCOST(tmp, Csj);
ADDCOST(tmp, Cb);
MINUSCOST(tmp, Cpi);
MINUSCOST(tmp, p->delta);
if (foundmin) {
if (LESSCOST(tmp, Min)) {
ASSIGNCOST(Min, tmp);
}
} else {
foundmin = 1;
ASSIGNCOST(Min, tmp);
}
}
}
if (!foundmin) {
return;
}
if (foundmax) {
if (LESSCOST(Max, Min)) {
ASSIGNCOST(Max, Min);
}
} else {
foundmax = 1;
ASSIGNCOST(Max, Min);
}
}
break;
default:
assert(0);
}
}
allpairs[i ][ j].sibFlag = foundmax;
ASSIGNCOST(allpairs[i ][ j].sibling, Max);
if (allpairs[p->lhs->num ][ s->lhs->num].rule
&& allpairs[s->pat->children[0]->num ][ j].rule
&& allpairs[s->pat->children[1]->num ][ p->pat->children[1]->num].rule) {
Cx = allpairs[p->lhs->num ][ s->lhs->num].chain;
Csj= allpairs[s->pat->children[0]->num ][ j].chain;
Cpi= allpairs[p->pat->children[0]->num ][ i].chain;
Cb = allpairs[s->pat->children[1]->num ][ p->pat->children[1]->num].chain;
ASSIGNCOST(tmp, Cx);
ADDCOST(tmp, s->delta);
ADDCOST(tmp, Csj);
ADDCOST(tmp, Cb);
MINUSCOST(tmp, Cpi);
MINUSCOST(tmp, p->delta);
if (foundmin) {
if (LESSCOST(tmp, Min)) {
ASSIGNCOST(Min, tmp);
}
} else {
foundmin = 1;
ASSIGNCOST(Min, tmp);
}
}
}
if (!foundmin) {
return;
}
if (foundmax) {
if (LESSCOST(Max, Min)) {
ASSIGNCOST(Max, Min);
}
} else {
foundmax = 1;
ASSIGNCOST(Max, Min);
}
}
/* do second dimension */
if (allpairs[p->pat->children[1]->num ][ i].rule) {
foundmin = 0;
for (oprule = op->table->rules; oprule; oprule = oprule->next) {
Rule s = (Rule) oprule->x;
DeltaPtr Cx;
DeltaPtr Cb;
DeltaPtr Csj;
DeltaPtr Cpi;
DeltaCost tmp;
if (allpairs[p->lhs->num ][ s->lhs->num].rule
&& allpairs[s->pat->children[1]->num ][ j].rule
&& allpairs[s->pat->children[0]->num ][ p->pat->children[0]->num].rule) {
Cx = allpairs[p->lhs->num ][ s->lhs->num].chain;
Csj= allpairs[s->pat->children[1]->num ][ j].chain;
Cpi= allpairs[p->pat->children[1]->num ][ i].chain;
Cb = allpairs[s->pat->children[0]->num ][ p->pat->children[0]->num].chain;
ASSIGNCOST(tmp, Cx);
ADDCOST(tmp, s->delta);
ADDCOST(tmp, Csj);
ADDCOST(tmp, Cb);
MINUSCOST(tmp, Cpi);
MINUSCOST(tmp, p->delta);
if (foundmin) {
if (LESSCOST(tmp, Min)) {
ASSIGNCOST(Min, tmp);
}
} else {
foundmin = 1;
ASSIGNCOST(Min, tmp);
}
}
}
if (!foundmin) {
return;
}
if (foundmax) {
if (LESSCOST(Max, Min)) {
ASSIGNCOST(Max, Min);
}
} else {
foundmax = 1;
ASSIGNCOST(Max, Min);
}
}
break;
default:
assert(0);
}
}
allpairs[i ][ j].sibFlag = foundmax;
ASSIGNCOST(allpairs[i ][ j].sibling, Max);
}
static void
findAllNexts()
{
int i,j;
int last;
int i,j;
int last;
for (i = 1; i < max_nonterminal; i++) {
last = 0;
for (j = 1; j < max_nonterminal; j++) {
if (allpairs[i ][j].rule) {
allpairs[i ][ last].nextchain = j;
last = j;
}
}
}
/*
for (i = 1; i < max_nonterminal; i++) {
last = 0;
for (j = 1; j < max_nonterminal; j++) {
if (allpairs[i ][j].sibFlag) {
allpairs[i ][ last].nextsibling = j;
last = j;
}
}
}
*/
for (i = 1; i < max_nonterminal; i++) {
last = 0;
for (j = 1; j < max_nonterminal; j++) {
if (allpairs[i ][j].rule) {
allpairs[i ][ last].nextchain = j;
last = j;
}
}
}
/*
for (i = 1; i < max_nonterminal; i++) {
last = 0;
for (j = 1; j < max_nonterminal; j++) {
if (allpairs[i ][j].sibFlag) {
allpairs[i ][ last].nextsibling = j;
last = j;
}
}
}
*/
}
static Relation *
newAllPairs()
{
int i;
Relation *rv;
int i;
Relation *rv;
rv = (Relation*) zalloc(max_nonterminal * sizeof(Relation));
for (i = 0; i < max_nonterminal; i++) {
rv[i] = (Relation) zalloc(max_nonterminal * sizeof(struct relation));
}
return rv;
rv = (Relation*) zalloc(max_nonterminal * sizeof(Relation));
for (i = 0; i < max_nonterminal; i++) {
rv[i] = (Relation) zalloc(max_nonterminal * sizeof(struct relation));
}
return rv;
}
void
findAllPairs()
{
List pl;
int changes;
int j;
List pl;
int changes;
int j;
allpairs = newAllPairs();
for (pl = chainrules; pl; pl = pl->next) {
Rule p = (Rule) pl->x;
NonTerminalNum rhs = p->pat->children[0]->num;
NonTerminalNum lhs = p->lhs->num;
Relation r = &allpairs[lhs ][ rhs];
allpairs = newAllPairs();
for (pl = chainrules; pl; pl = pl->next) {
Rule p = (Rule) pl->x;
NonTerminalNum rhs = p->pat->children[0]->num;
NonTerminalNum lhs = p->lhs->num;
Relation r = &allpairs[lhs ][ rhs];
if (LESSCOST(p->delta, r->chain)) {
ASSIGNCOST(r->chain, p->delta);
r->rule = p;
}
}
for (j = 1; j < max_nonterminal; j++) {
Relation r = &allpairs[j ][ j];
ZEROCOST(r->chain);
r->rule = &stub_rule;
}
changes = 1;
while (changes) {
changes = 0;
for (pl = chainrules; pl; pl = pl->next) {
Rule p = (Rule) pl->x;
NonTerminalNum rhs = p->pat->children[0]->num;
NonTerminalNum lhs = p->lhs->num;
int i;
if (LESSCOST(p->delta, r->chain)) {
ASSIGNCOST(r->chain, p->delta);
r->rule = p;
}
}
for (j = 1; j < max_nonterminal; j++) {
Relation r = &allpairs[j ][ j];
ZEROCOST(r->chain);
r->rule = &stub_rule;
}
changes = 1;
while (changes) {
changes = 0;
for (pl = chainrules; pl; pl = pl->next) {
Rule p = (Rule) pl->x;
NonTerminalNum rhs = p->pat->children[0]->num;
NonTerminalNum lhs = p->lhs->num;
int i;
for (i = 1; i < max_nonterminal; i++) {
Relation r = &allpairs[rhs ][ i];
Relation s = &allpairs[lhs ][ i];
DeltaCost dc;
if (!r->rule) {
continue;
}
ASSIGNCOST(dc, p->delta);
ADDCOST(dc, r->chain);
if (!s->rule || LESSCOST(dc, s->chain)) {
s->rule = p;
ASSIGNCOST(s->chain, dc);
changes = 1;
}
}
}
}
findAllNexts();
for (i = 1; i < max_nonterminal; i++) {
Relation r = &allpairs[rhs ][ i];
Relation s = &allpairs[lhs ][ i];
DeltaCost dc;
if (!r->rule) {
continue;
}
ASSIGNCOST(dc, p->delta);
ADDCOST(dc, r->chain);
if (!s->rule || LESSCOST(dc, s->chain)) {
s->rule = p;
ASSIGNCOST(s->chain, dc);
changes = 1;
}
}
}
}
findAllNexts();
}
void
trim(t) Item_Set t;
{
int m,n;
static short *vec = 0;
int last;
int m,n;
static short *vec = 0;
int last;
assert(!t->closed);
debug(debugTrim, printf("Begin Trim\n"));
debug(debugTrim, dumpItem_Set(t));
assert(!t->closed);
debug(debugTrim, printf("Begin Trim\n"));
debug(debugTrim, dumpItem_Set(t));
last = 0;
if (!vec) {
vec = (short*) zalloc(max_nonterminal * sizeof(*vec));
}
for (m = 1; m < max_nonterminal; m++) {
if (t->virgin[m].rule) {
vec[last++] = m;
}
}
for (m = 0; m < last; m++) {
DeltaCost tmp;
int j;
int i;
last = 0;
if (!vec) {
vec = (short*) zalloc(max_nonterminal * sizeof(*vec));
}
for (m = 1; m < max_nonterminal; m++) {
if (t->virgin[m].rule) {
vec[last++] = m;
}
}
for (m = 0; m < last; m++) {
DeltaCost tmp;
int j;
int i;
i = vec[m];
i = vec[m];
for (j = allpairs[i ][ 0].nextchain; j; j = allpairs[i ][ j].nextchain) {
for (j = allpairs[i ][ 0].nextchain; j; j = allpairs[i ][ j].nextchain) {
if (i == j) {
continue;
}
if (!t->virgin[j].rule) {
continue;
}
ASSIGNCOST(tmp, t->virgin[j].delta);
ADDCOST(tmp, allpairs[i ][ j].chain);
if (!LESSCOST(t->virgin[i].delta, tmp)) {
t->virgin[i].rule = 0;
ZEROCOST(t->virgin[i].delta);
debug(debugTrim, printf("Trimmed Chain (%d,%d)\n", i,j));
goto outer;
}
}
if (!trimflag) {
continue;
}
for (n = 0; n < last; n++) {
j = vec[n];
if (i == j) {
continue;
}
if (i == j) {
continue;
}
if (!t->virgin[j].rule) {
continue;
}
ASSIGNCOST(tmp, t->virgin[j].delta);
ADDCOST(tmp, allpairs[i ][ j].chain);
if (!LESSCOST(t->virgin[i].delta, tmp)) {
t->virgin[i].rule = 0;
ZEROCOST(t->virgin[i].delta);
debug(debugTrim, printf("Trimmed Chain (%d,%d)\n", i,j));
goto outer;
}
if (!t->virgin[j].rule) {
continue;
}
}
if (!trimflag) {
continue;
}
for (n = 0; n < last; n++) {
j = vec[n];
if (i == j) {
continue;
}
if (!allpairs[i][j].sibComputed) {
siblings(i,j);
}
if (!allpairs[i][j].sibFlag) {
continue;
}
ASSIGNCOST(tmp, t->virgin[j].delta);
ADDCOST(tmp, allpairs[i ][ j].sibling);
if (!LESSCOST(t->virgin[i].delta, tmp)) {
t->virgin[i].rule = 0;
ZEROCOST(t->virgin[i].delta);
goto outer;
}
}
if (!t->virgin[j].rule) {
continue;
}
outer: ;
}
if (!allpairs[i][j].sibComputed) {
siblings(i,j);
}
if (!allpairs[i][j].sibFlag) {
continue;
}
ASSIGNCOST(tmp, t->virgin[j].delta);
ADDCOST(tmp, allpairs[i ][ j].sibling);
if (!LESSCOST(t->virgin[i].delta, tmp)) {
t->virgin[i].rule = 0;
ZEROCOST(t->virgin[i].delta);
goto outer;
}
}
debug(debugTrim, dumpItem_Set(t));
debug(debugTrim, printf("End Trim\n"));
outer: ;
}
debug(debugTrim, dumpItem_Set(t));
debug(debugTrim, printf("End Trim\n"));
}
void
dumpRelation(r) Relation r;
{
printf("{ %d %ld %d %ld }", r->rule->erulenum, (long) r->chain, r->sibFlag, (long) r->sibling);
printf("{ %d %ld %d %ld }", r->rule->erulenum, (long) r->chain, r->sibFlag, (long) r->sibling);
}
void
dumpAllPairs()
{
int i,j;
int i,j;
printf("Dumping AllPairs\n");
for (i = 1; i < max_nonterminal; i++) {
for (j = 1; j < max_nonterminal; j++) {
dumpRelation(&allpairs[i ][j]);
}
printf("\n");
}
printf("Dumping AllPairs\n");
for (i = 1; i < max_nonterminal; i++) {
for (j = 1; j < max_nonterminal; j++) {
dumpRelation(&allpairs[i ][j]);
}
printf("\n");
}
}

View File

@ -8,25 +8,25 @@ char rcsid_zalloc[] = "$Id$";
int
fatal(const char *name, int line)
{
fprintf(stderr, "assertion failed: file %s, line %d\n", name, line);
exit(1);
return 0;
fprintf(stderr, "assertion failed: file %s, line %d\n", name, line);
exit(1);
return 0;
}
void *
zalloc(size) unsigned int size;
{
void *t = (void *) malloc(size);
if (!t) {
fprintf(stderr, "Malloc failed---PROGRAM ABORTED\n");
exit(1);
}
memset(t, 0, size);
return t;
void *t = (void *) malloc(size);
if (!t) {
fprintf(stderr, "Malloc failed---PROGRAM ABORTED\n");
exit(1);
}
memset(t, 0, size);
return t;
}
void
zfree(p) void *p;
{
free(p);
free(p);
}

View File

@ -90,7 +90,7 @@ Init *BitsRecTy::convertValue(TypedInit *VI) {
if (BRT->Size == Size) {
BitsInit *Ret = new BitsInit(Size);
for (unsigned i = 0; i != Size; ++i)
Ret->setBit(i, new VarBitInit(VI, i));
Ret->setBit(i, new VarBitInit(VI, i));
return Ret;
}
if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
@ -768,13 +768,13 @@ std::ostream &llvm::operator<<(std::ostream &OS, const RecordKeeper &RK) {
OS << "------------- Classes -----------------\n";
const std::map<std::string, Record*> &Classes = RK.getClasses();
for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
E = Classes.end(); I != E; ++I)
E = Classes.end(); I != E; ++I)
OS << "class " << *I->second;
OS << "------------- Defs -----------------\n";
const std::map<std::string, Record*> &Defs = RK.getDefs();
for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
E = Defs.end(); I != E; ++I)
E = Defs.end(); I != E; ++I)
OS << "def " << *I->second;
return OS;
}

View File

@ -944,7 +944,7 @@ public:
bool isSubClassOf(Record *R) const {
for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
if (SuperClasses[i] == R)
return true;
return true;
return false;
}
@ -1031,10 +1031,10 @@ class RecordKeeper {
public:
~RecordKeeper() {
for (std::map<std::string, Record*>::iterator I = Classes.begin(),
E = Classes.end(); I != E; ++I)
E = Classes.end(); I != E; ++I)
delete I->second;
for (std::map<std::string, Record*>::iterator I = Defs.begin(),
E = Defs.end(); I != E; ++I)
E = Defs.end(); I != E; ++I)
delete I->second;
}

View File

@ -93,10 +93,10 @@ static Init *getBit(Record *R, unsigned BitNo) {
for (unsigned i = 0, e = V.size(); i != e; ++i)
if (V[i].getPrefix()) {
assert(dynamic_cast<BitsInit*>(V[i].getValue()) &&
"Can only handle fields of bits<> type!");
"Can only handle fields of bits<> type!");
BitsInit *I = (BitsInit*)V[i].getValue();
if (BitNo < I->getNumBits())
return I->getBit(BitNo);
return I->getBit(BitNo);
BitNo -= I->getNumBits();
}
@ -111,7 +111,7 @@ static unsigned getNumBits(Record *R) {
for (unsigned i = 0, e = V.size(); i != e; ++i)
if (V[i].getPrefix()) {
assert(dynamic_cast<BitsInit*>(V[i].getValue()) &&
"Can only handle fields of bits<> type!");
"Can only handle fields of bits<> type!");
Num += ((BitsInit*)V[i].getValue())->getNumBits();
}
return Num;
@ -130,7 +130,7 @@ static bool BitsAreEqual(Record *I1, Record *I2, unsigned BitNo) {
}
static bool BitRangesEqual(Record *I1, Record *I2,
unsigned Start, unsigned End) {
unsigned Start, unsigned End) {
for (unsigned i = Start; i != End; ++i)
if (!BitsAreEqual(I1, I2, i))
return false;
@ -145,9 +145,9 @@ static unsigned getFirstFixedBit(Record *R, unsigned FirstFixedBit) {
}
static void FindInstDifferences(Record *I1, Record *I2,
unsigned FirstFixedBit, unsigned MaxBits,
unsigned &FirstVaryingBitOverall,
unsigned &LastFixedBitOverall) {
unsigned FirstFixedBit, unsigned MaxBits,
unsigned &FirstVaryingBitOverall,
unsigned &LastFixedBitOverall) {
// Compare the first instruction to the rest of the instructions, looking for
// fields that differ.
//
@ -179,16 +179,16 @@ struct BitComparator {
for (unsigned i = BitBegin; i != BitEnd; ++i) {
bool V1 = getBitValue(R1, i), V2 = getBitValue(R2, i);
if (V1 < V2)
return true;
return true;
else if (V2 < V1)
return false;
return false;
}
return false;
}
};
static void PrintRange(std::vector<Record*>::iterator I,
std::vector<Record*>::iterator E) {
std::vector<Record*>::iterator E) {
while (I != E) std::cerr << **I++;
}
@ -197,8 +197,8 @@ static bool getMemoryBit(unsigned char *M, unsigned i) {
}
static unsigned getFirstFixedBitInSequence(std::vector<Record*>::iterator IB,
std::vector<Record*>::iterator IE,
unsigned StartBit) {
std::vector<Record*>::iterator IE,
unsigned StartBit) {
unsigned FirstFixedBit = 0;
for (std::vector<Record*>::iterator I = IB; I != IE; ++I)
FirstFixedBit = std::max(FirstFixedBit, getFirstFixedBit(*I, StartBit));
@ -211,16 +211,16 @@ static unsigned getFirstFixedBitInSequence(std::vector<Record*>::iterator IB,
// down to zero or one instruction, in which case we have a match or failure.
//
static Record *ParseMachineCode(std::vector<Record*>::iterator InstsB,
std::vector<Record*>::iterator InstsE,
unsigned char *M) {
std::vector<Record*>::iterator InstsE,
unsigned char *M) {
assert(InstsB != InstsE && "Empty range?");
if (InstsB+1 == InstsE) {
// Only a single instruction, see if we match it...
Record *Inst = *InstsB;
for (unsigned i = 0, e = getNumBits(Inst); i != e; ++i)
if (BitInit *BI = dynamic_cast<BitInit*>(getBit(Inst, i)))
if (getMemoryBit(M, i) != BI->getValue())
throw std::string("Parse failed!\n");
if (getMemoryBit(M, i) != BI->getValue())
throw std::string("Parse failed!\n");
return Inst;
}
@ -235,16 +235,16 @@ static Record *ParseMachineCode(std::vector<Record*>::iterator InstsB,
LastFixedBit = ~0;
for (std::vector<Record*>::iterator I = InstsB+1; I != InstsE; ++I)
FindInstDifferences(*InstsB, *I, FirstFixedBit, MaxBits,
FirstVaryingBit, LastFixedBit);
FirstVaryingBit, LastFixedBit);
if (FirstVaryingBit == MaxBits) {
std::cerr << "ERROR: Could not find bit to distinguish between "
<< "the following entries!\n";
<< "the following entries!\n";
PrintRange(InstsB, InstsE);
}
#if 0
std::cerr << "FVB: " << FirstVaryingBit << " - " << LastFixedBit
<< ": " << InstsE-InstsB << "\n";
<< ": " << InstsE-InstsB << "\n";
#endif
FirstFixedBit = getFirstFixedBitInSequence(InstsB, InstsE, FirstVaryingBit);
@ -282,7 +282,7 @@ static Record *ParseMachineCode(std::vector<Record*>::iterator InstsB,
#if 0
std::cerr << "FVB: " << FirstVaryingBit << " - " << LastFixedBit
<< ": [" << RangeEnd-RangeBegin << "] - ";
<< ": [" << RangeEnd-RangeBegin << "] - ";
for (int i = LastFixedBit-1; i >= (int)FirstVaryingBit; --i)
std::cerr << (int)((BitInit*)getBit(*RangeBegin, i))->getValue() << " ";
std::cerr << "\n";
@ -290,8 +290,8 @@ static Record *ParseMachineCode(std::vector<Record*>::iterator InstsB,
if (Record *R = ParseMachineCode(RangeBegin, RangeEnd, M)) {
if (Match) {
std::cerr << "Error: Multiple matches found:\n";
PrintRange(InstsB, InstsE);
std::cerr << "Error: Multiple matches found:\n";
PrintRange(InstsB, InstsE);
}
assert(Match == 0 && "Multiple matches??");
@ -305,7 +305,7 @@ static Record *ParseMachineCode(std::vector<Record*>::iterator InstsB,
static void PrintValue(Record *I, unsigned char *Ptr, const RecordVal &Val) {
assert(dynamic_cast<BitsInit*>(Val.getValue()) &&
"Can only handle undefined bits<> types!");
"Can only handle undefined bits<> types!");
BitsInit *BI = (BitsInit*)Val.getValue();
assert(BI->getNumBits() <= 32 && "Can only handle fields up to 32 bits!");
@ -325,17 +325,17 @@ static void PrintValue(Record *I, unsigned char *Ptr, const RecordVal &Val) {
if (Vals[f].getPrefix()) {
BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
if (&Vals[f] == &Val) {
// Read the bits directly now...
for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
Value |= getMemoryBit(Ptr, Offset+i) << i;
break;
// Read the bits directly now...
for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
Value |= getMemoryBit(Ptr, Offset+i) << i;
break;
}
// Scan through the field looking for bit initializers of the current
// variable...
for (unsigned i = 0, e = FieldInitializer->getNumBits(); i != e; ++i)
if (VarBitInit *VBI =
dynamic_cast<VarBitInit*>(FieldInitializer->getBit(i))) {
if (VarBitInit *VBI =
dynamic_cast<VarBitInit*>(FieldInitializer->getBit(i))) {
TypedInit *TI = VBI->getVariable();
if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
if (VI->getName() == Val.getName())
@ -344,7 +344,7 @@ static void PrintValue(Record *I, unsigned char *Ptr, const RecordVal &Val) {
// FIXME: implement this!
std::cerr << "FIELD INIT not implemented yet!\n";
}
}
}
Offset += FieldInitializer->getNumBits();
}
@ -353,8 +353,8 @@ static void PrintValue(Record *I, unsigned char *Ptr, const RecordVal &Val) {
static void PrintInstruction(Record *I, unsigned char *Ptr) {
std::cout << "Inst " << getNumBits(I)/8 << " bytes: "
<< "\t" << I->getName() << "\t" << *I->getValue("Name")->getValue()
<< "\t";
<< "\t" << I->getName() << "\t" << *I->getValue("Name")->getValue()
<< "\t";
const std::vector<RecordVal> &Vals = I->getValues();
for (unsigned i = 0, e = Vals.size(); i != e; ++i)
@ -371,21 +371,21 @@ static void ParseMachineCode() {
// X86 code
unsigned char Buffer[] = {
0x55, // push EBP
0x89, 0xE5, // mov EBP, ESP
//0x83, 0xEC, 0x08, // sub ESP, 0x8
0xE8, 1, 2, 3, 4, // call +0x04030201
0x89, 0xEC, // mov ESP, EBP
0x5D, // pop EBP
0xC3, // ret
0x90, // nop
0xC9, // leave
0x89, 0xF6, // mov ESI, ESI
0x68, 1, 2, 3, 4, // push 0x04030201
0x5e, // pop ESI
0xFF, 0xD0, // call EAX
0xB8, 1, 2, 3, 4, // mov EAX, 0x04030201
0x85, 0xC0, // test EAX, EAX
0xF4, // hlt
0x89, 0xE5, // mov EBP, ESP
//0x83, 0xEC, 0x08, // sub ESP, 0x8
0xE8, 1, 2, 3, 4, // call +0x04030201
0x89, 0xEC, // mov ESP, EBP
0x5D, // pop EBP
0xC3, // ret
0x90, // nop
0xC9, // leave
0x89, 0xF6, // mov ESI, ESI
0x68, 1, 2, 3, 4, // push 0x04030201
0x5e, // pop ESI
0xFF, 0xD0, // call EAX
0xB8, 1, 2, 3, 4, // mov EAX, 0x04030201
0x85, 0xC0, // test EAX, EAX
0xF4, // hlt
};
#if 0