From 47d4646eebac34c0b94951cfcf1b81ed2ca513e1 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Mon, 4 May 2020 18:34:19 -0400 Subject: rc: add recursive descent parser MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/cmd/rc/pcmd.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) (limited to 'src/cmd/rc/pcmd.c') diff --git a/src/cmd/rc/pcmd.c b/src/cmd/rc/pcmd.c index 8caf60a2..26bd883b 100644 --- a/src/cmd/rc/pcmd.c +++ b/src/cmd/rc/pcmd.c @@ -145,3 +145,118 @@ pcmd(io *f, tree *t) break; } } + +void +pcmdu(io *f, tree *t) /* unambiguous */ +{ + if(t==0) { + pfmt(f, ""); + return; + } + + switch(t->type){ + default: pfmt(f, "(bad %d %p %p %p)", t->type, c0, c1, c2); + break; + case '$': pfmt(f, "($ %u)", c0); + break; + case '"': pfmt(f, "($\" %u)", c0); + break; + case '&': pfmt(f, "(& %u)", c0); + break; + case '^': pfmt(f, "(^ %u %u)", c0, c1); + break; + case '`': pfmt(f, "(` %u)", c0); + break; + case ANDAND: pfmt(f, "(&& %u %u)", c0, c1); + break; + case BANG: pfmt(f, "(! %u)", c0); + break; + case BRACE: pfmt(f, "(brace %u)", c0); + break; + case COUNT: pfmt(f, "($# %u)", c0); + break; + case FN: pfmt(f, "(fn %u %u)", c0, c1); + break; + case IF: pfmt(f, "(if %u %u)", c0, c1); + break; + case NOT: pfmt(f, "(if not %u)", c0); + break; + case OROR: pfmt(f, "(|| %u %u)", c0, c1); + break; + case PCMD: + case PAREN: pfmt(f, "(paren %u)", c0); + break; + case SUB: pfmt(f, "($sub %u %u)", c0, c1); + break; + case SIMPLE: pfmt(f, "(simple %u)", c0); + break; + case SUBSHELL: pfmt(f, "(@ %u)", c0); + break; + case SWITCH: pfmt(f, "(switch %u %u)", c0, c1); + break; + case TWIDDLE: pfmt(f, "(~ %u %u)", c0, c1); + break; + case WHILE: pfmt(f, "(while %u %u)", c0, c1); + break; + case ARGLIST: + pfmt(f, "(arglist %u %u)", c0, c1); + break; + case ';': + pfmt(f, "(; %u %u)", c0, c1); + break; + case WORDS: + pfmt(f, "(words %u %u)", c0, c1); + break; + case FOR: + pfmt(f, "(for %u %u %u)", c0, c1, c2); + break; + case WORD: + if(t->quoted) + pfmt(f, "%Q", t->str); + else pdeglob(f, t->str); + break; + case DUP: + if(t->rtype==DUPFD) + pfmt(f, "(>[%d=%d]", t->fd1, t->fd0); /* yes, fd1, then fd0; read lex.c */ + else + pfmt(f, "(>[%d=]", t->fd0); /*)*/ + pfmt(f, " %u)", c1); + break; + case PIPEFD: + case REDIR: + pfmt(f, "("); + switch(t->rtype){ + case HERE: + pchr(f, '<'); + case READ: + case RDWR: + pchr(f, '<'); + if(t->rtype==RDWR) + pchr(f, '>'); + if(t->fd0!=0) + pfmt(f, "[%d]", t->fd0); + break; + case APPEND: + pchr(f, '>'); + case WRITE: + pchr(f, '>'); + if(t->fd0!=1) + pfmt(f, "[%d]", t->fd0); + break; + } + pfmt(f, "%u %u)", c0, c1); + break; + case '=': + pfmt(f, "(%u=%u %u)", c0, c1, c2); + break; + case PIPE: + pfmt(f, "(|"); + if(t->fd1==0){ + if(t->fd0!=1) + pfmt(f, "[%d]", t->fd0); + } + else pfmt(f, "[%d=%d]", t->fd0, t->fd1); + pfmt(f, " %u %u", c0, c1); + break; + } +} -- cgit v1.2.3 From 3caf5c238a886d06b438ec6d42f2609b8625463f Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Mon, 4 May 2020 22:52:27 -0400 Subject: rc: move newline handling into parser --- src/cmd/rc/pcmd.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/cmd/rc/pcmd.c') diff --git a/src/cmd/rc/pcmd.c b/src/cmd/rc/pcmd.c index 26bd883b..cae84737 100644 --- a/src/cmd/rc/pcmd.c +++ b/src/cmd/rc/pcmd.c @@ -244,7 +244,10 @@ pcmdu(io *f, tree *t) /* unambiguous */ pfmt(f, "[%d]", t->fd0); break; } - pfmt(f, "%u %u)", c0, c1); + if(t->rtype == HERE) + pfmt(f, "HERE %u)", c1); + else + pfmt(f, "%u %u)", c0, c1); break; case '=': pfmt(f, "(%u=%u %u)", c0, c1, c2); -- cgit v1.2.3