Skip to content

Commit

Permalink
rc: add recursive descent parser
Browse files Browse the repository at this point in the history
The old yacc-based parser is available with the -Y flag,
which will probably be removed at some point.

The new -D flag dumps a parse tree of the input,
without executing it. This allows comparing the output
of rc -D and rc -DY on different scripts to see that the
two parsers behave the same.

The rc paper ends by saying:

	It is remarkable that in the four most recent editions of the UNIX
	system programmer’s manual the Bourne shell grammar described in the
	manual page does not admit the command who|wc. This is surely an
	oversight, but it suggests something darker: nobody really knows what
	the Bourne shell’s grammar is. Even examination of the source code is
	little help. The parser is implemented by recursive descent, but the
	routines corresponding to the syntactic categories all have a flag
	argument that subtly changes their operation depending on the context.
	Rc’s parser is implemented using yacc, so I can say precisely what the
	grammar is.

The new recursive descent parser here has no such flags.
It is a straightforward translation of the yacc.

The new parser will make it easier to handle free carats
in more generality as well as potentially allow the use of
unquoted = as a word character.

Going through this exercise has highlighted a few
dark corners here as well. For example, I was surprised to
find that

	x >f | y
	>f x | y

are different commands (the latter redirects y's output).

It is similarly surprising that

	a=b x | y

sets a during the execution of y.

It is also a bit counter-intuitive

	x | y | z
	x | if(c) y | z

are not both 3-phase pipelines.

These are certainly not things we should change, but they
are not entirely obvious from the man page description,
undercutting the quoted claim a bit.

On the other hand, who | wc is clearly accepted by the grammar
in the manual page, and the new parser still handles that test case.
  • Loading branch information
rsc committed May 5, 2020
1 parent c1c1b52 commit 47d4646
Show file tree
Hide file tree
Showing 13 changed files with 730 additions and 9 deletions.
14 changes: 14 additions & 0 deletions src/cmd/rc/checkparse
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

files="$@"
if [ $# = 0 ]; then
files=$(echo ./test.rc;
grep -l '^#!/usr/local/plan9/bin/rc' /usr/local/plan9/bin/{*,*/*} 2>/dev/null;
grep -l '^#!/bin/rc' $HOME/pub/plan9/rc/bin/{*,*/*} 2>/dev/null)
fi

for i in $files
do
echo '#' $i
diff <(./o.rc -DY $i 2>&1) <(./o.rc -D $i 2>&1)
done
10 changes: 10 additions & 0 deletions src/cmd/rc/code.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ stuffdot(int a)
int
compile(tree *t)
{
if(flag['D']) {
struct io *s;
s = openstr();
pfmt(s, "compile: %u\n", t);
write(2, s->strp, strlen(s->strp));
closeio(s);
if(eflagok) // made it out of rcmain - stop executing commands, just print them
t = nil;
}

ncode = 100;
codebuf = (code *)emalloc(ncode*sizeof codebuf[0]);
codep = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/rc/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ main(int argc, char *argv[])
/* needed for rcmain later */
putenv("PLAN9", unsharp("#9"));

argc = getflags(argc, argv, "SsrdiIlxepvVc:1m:1[command]", 1);
argc = getflags(argc, argv, "DSYsrdiIlxepvVc:1m:1[command]", 1);
if(argc==-1)
usage("[file [arg ...]]");
if(argv[0][0]=='-')
Expand Down Expand Up @@ -901,7 +901,7 @@ Xrdcmds(void)
promptstr="% ";
}
Noerror();
if(yyparse()){
if((flag['Y'] ? yyparse : parse)()){
if(!p->iflag || p->eof && !Eintr()){
if(p->cmdfile)
efree(p->cmdfile);
Expand Down
1 change: 1 addition & 0 deletions src/cmd/rc/fns.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ int wordchr(int);
void yyerror(char*);
int yylex(void);
int yyparse(void);
int parse(void);
5 changes: 4 additions & 1 deletion src/cmd/rc/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ pfmt(io *f, char *fmt, ...)
pstr(f, va_arg(ap, char *));
break;
case 't':
pcmd(f, va_arg(ap, struct tree *));
pcmd(f, va_arg(ap, tree *));
break;
case 'u':
pcmdu(f, va_arg(ap, tree *));
break;
case 'v':
pval(f, va_arg(ap, struct word *));
Expand Down
1 change: 1 addition & 0 deletions src/cmd/rc/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void pquo(io*, char*);
void pwrd(io*, char*);
void pstr(io*, char*);
void pcmd(io*, tree*);
void pcmdu(io*, tree*);
void pval(io*, word*);
void pfnc(io*, thread*);
void pfmt(io*, char*, ...);
7 changes: 6 additions & 1 deletion src/cmd/rc/lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ yylex(void)
{
int c, d = nextc();
char *w = tok;
struct tree *t;
tree *t;
yylval.tree = 0;
/*
* Embarassing sneakiness: if the last token read was a quoted or unquoted
Expand Down Expand Up @@ -331,6 +331,11 @@ yylex(void)
yylval.tree = t;
if(t->type==PIPE)
skipnl();
if(t->type==REDIR) {
skipwhite();
if(nextc() == '{')
t->type = REDIRW;
}
return t->type;
case '\'':
lastdol = 0;
Expand Down
1 change: 1 addition & 0 deletions src/cmd/rc/mkfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ OFILES=\
here.$O\
io.$O\
lex.$O\
parse.$O\
pcmd.$O\
pfnc.$O\
simple.$O\
Expand Down

0 comments on commit 47d4646

Please sign in to comment.