rewritting egrep.y is should be easier than understanding and refactoring the original

This commit is contained in:
anon 2024-11-14 17:54:43 +01:00
parent d0ec6f3ecb
commit feaf54e3a8

View File

@ -80,8 +80,8 @@ static void cgotofn(void);
static int cstate(int v);
static int member(int symb, int set, int torf);
static int notin(int n);
static void synerror(void);
static void overflo(void);
static void syntax_error(void);
static void table_overflow(void);
static void add(int *array, int n);
static void follow(unsigned int v);
static int unary(int x, int d);
@ -208,7 +208,7 @@ int yylex(void) {
}
do {
if (c == '\0')
synerror();
syntax_error();
if ( (c == '-')
&& (cclcnt > 0)
&& (chars[nxtchar-1] != 0)) {
@ -216,7 +216,7 @@ int yylex(void) {
c = chars[nxtchar-1];
while ((unsigned int)c < (unsigned int)d) {
if (nxtchar >= MAXLIN)
overflo();
table_overflow();
chars[nxtchar++] = ++c;
cclcnt++;
}
@ -224,7 +224,7 @@ int yylex(void) {
} /* if() */
} /* if() */
if (nxtchar >= MAXLIN)
overflo();
table_overflow();
chars[nxtchar++] = c;
cclcnt++;
} while ((c = nextch()) != ']');
@ -232,7 +232,7 @@ int yylex(void) {
return (x);
case '\\':
if ((c = nextch()) == '\0')
synerror();
syntax_error();
yylval = c;
return (CHAR);
case '$':
@ -247,14 +247,14 @@ int yylex(void) {
}
static
void synerror(void) {
void syntax_error(void) {
yyerror("Syntax error");
}
static
unsigned int enter(int x) {
if(line >= MAXLIN)
overflo();
table_overflow();
name[line] = x;
left[line] = 0;
right[line] = 0;
@ -271,7 +271,7 @@ unsigned int cclenter(int x) {
static
int node(int x, int l, int r) {
if(line >= MAXLIN)
overflo();
table_overflow();
name[line] = x;
left[line] = l;
right[line] = r;
@ -283,7 +283,7 @@ int node(int x, int l, int r) {
static
int unary(int x, int d) {
if(line >= MAXLIN)
overflo();
table_overflow();
name[line] = x;
left[line] = d;
right[line] = 0;
@ -292,7 +292,7 @@ int unary(int x, int d) {
}
static
void overflo(void) {
void table_overflow(void) {
yyerror("internal table overflow");
}
@ -408,7 +408,7 @@ void cgotofn(void) {
} /* end nextstate */
if (notin(n)) {
if (n >= NSTATES)
overflo();
table_overflow();
add(state, ++n);
if (tmpstat[line] == 1)
out[n] = 1;
@ -464,11 +464,11 @@ int member(int symb, int set, int torf) {
static
int notin(int n) {
int i, j, pos;
for (i=0; i<=n; i++) {
int pos;
for (int i = 0; i <= n; i++) {
if (positions[state[i]] == count) {
pos = state[i] + 1;
for (j=0; j < count; j++)
for (int j = 0; j < count; j++)
if (tmpstat[positions[pos++]] != 1) goto nxt;
xstate = i;
return (0);
@ -480,13 +480,11 @@ int notin(int n) {
static
void add(int *array, int n) {
unsigned int i;
if (nxtpos + count > MAXPOS)
overflo();
table_overflow();
array[n] = nxtpos;
positions[nxtpos++] = count;
for (i=3; i <= line; i++) {
for (unsigned i = 3; i <= line; i++) {
if (tmpstat[i] == 1) {
positions[nxtpos++] = i;
}
@ -495,36 +493,32 @@ void add(int *array, int n) {
static
void follow(unsigned int v) {
unsigned int p;
if (v == line) { return; }
if (v == line)
return;
p = parent[v];
unsigned int p = parent[v];
switch(name[p]) {
case STAR:
case PLUS: cstate(v);
follow(p);
return;
case OR:
case QUEST: follow(p);
return;
case CAT:
if (v == left[p]) {
if (cstate(right[p]) == 0) {
follow(p);
return;
}
} else
follow(p);
return;
case FINAL:
if (tmpstat[line] != 1) {
tmpstat[line] = 1;
count++;
}
return;
case STAR:
case PLUS:
cstate(v);
follow(p);
break;
case OR:
case QUEST:
follow(p);
break;
case CAT:
if (v == left[p]
&& cstate(right[p]) != 0) {
return;
}
follow(p);
return;
case FINAL:
if (tmpstat[line] != 1) {
tmpstat[line] = 1;
count++;
}
break;
}
}