Allow for empty right-hand side rules

This commit is contained in:
Devine Lu Linvega 2024-04-12 10:30:59 -07:00
parent 5249d7612d
commit ea9935babb
1 changed files with 12 additions and 10 deletions

View File

@ -19,7 +19,7 @@ walk(char *s)
{ {
char c; char c;
int depth = 0; int depth = 0;
if(s[0] == '(') { if(*s == '(') {
while((c = *s++)) { while((c = *s++)) {
if(c == '(') depth++; if(c == '(') depth++;
if(c == ')') --depth; if(c == ')') --depth;
@ -36,7 +36,7 @@ plode(char *s)
int i, depth = 0; int i, depth = 0;
char c, *ss; char c, *ss;
/* implode */ /* implode */
if(s[0] == '(') { if(*s == '(') {
ss = walk(s); ss = walk(s);
while(s < ss && (c = *s++)) while(s < ss && (c = *s++))
if(!spacer(c)) *outp_++ = c; if(!spacer(c)) *outp_++ = c;
@ -149,20 +149,22 @@ parse_frag(char *s)
{ {
char c, *ss; char c, *ss;
while((c = *s) && c <= ' ') s++; while((c = *s) && c <= ' ') s++;
ss = walk(s); if(*s != ')' && *s != '<' && s[1] != '>') {
if(*s == '(') s++, ss--; ss = walk(s);
while(s < ss) *dict_++ = *s++; if(*s == '(') s++, ss--;
while(s < ss) *dict_++ = *s++;
s++;
}
*dict_++ = 0; *dict_++ = 0;
s++;
return s; return s;
} }
static char * static char *
create_rule(Rule *r, int id, char *s) create_rule(Rule *r, int id, char *s)
{ {
char c;
r->id = id, s += 2; r->id = id, s += 2;
r->a = dict_, s = parse_frag(s), r->b = dict_, s = parse_frag(s); r->a = dict_, s = parse_frag(s);
r->b = dict_, s = parse_frag(s);
return s; return s;
} }
@ -174,13 +176,13 @@ rewrite(void)
while((c = *s)) { while((c = *s)) {
if(spacer(last)) { if(spacer(last)) {
Rule *r; Rule *r;
if(s[0] == '<' && s[1] == '>') { if(*s == '<' && s[1] == '>') {
r = rules_++; r = rules_++;
s = create_rule(r, rules_ - rules - 1, s); s = create_rule(r, rules_ - rules - 1, s);
while((c = *s) && c <= ' ') s++; while((c = *s) && c <= ' ') s++;
return commit_rule(r, s, 1); return commit_rule(r, s, 1);
} }
if(s[0] == '?' && s[1] == '(') { if(*s == '?' && s[1] == '(') {
r = &lambda, cap = walk(s + 1); r = &lambda, cap = walk(s + 1);
create_rule(&lambda, -1, s), s = cap; create_rule(&lambda, -1, s), s = cap;
while((c = *s) && c <= ' ') s++; while((c = *s) && c <= ' ') s++;