aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/rc/syntax.y
diff options
context:
space:
mode:
Diffstat (limited to 'sys/cmd/rc/syntax.y')
-rw-r--r--sys/cmd/rc/syntax.y104
1 files changed, 104 insertions, 0 deletions
diff --git a/sys/cmd/rc/syntax.y b/sys/cmd/rc/syntax.y
new file mode 100644
index 0000000..247cc8a
--- /dev/null
+++ b/sys/cmd/rc/syntax.y
@@ -0,0 +1,104 @@
+%token Tword Tif Telse Tbang
+%token Targs
+%token Tbasic Tparen Tblock
+
+%{
+ #include "rc.h"
+
+ int yylex(void);
+ void yyerror(char *);
+%}
+
+/* operator precendence: lowest first */
+%right Telse
+%left Twhile
+%left '\n'
+%left Tbang
+%right '$'
+
+/* semantic types */
+%union{
+ struct Tree *tree;
+}
+%type<tree> line cmds cmdsln body paren block ifbody assign;
+%type<tree> cmd basic executable nonkeyword keyword word atom arg args;
+%type<tree> Tbang;
+%type<tree> Tword Tif Telse;
+
+/* grammar */
+
+%start rc
+
+%%
+rc:
+/*empty*/ { return 0; }
+| line '\n' { return compile($1); }
+
+line:
+ cmd
+| cmds line { $$ = maketree2(';', $1, $2); }
+
+body:
+ cmd
+| cmdsln body { $$ = maketree2(';', $1, $2); }
+
+paren:
+ '(' body ')' { $$ = maketree1(Tparen, $2); }
+
+block:
+ '{' body '}' { $$ = maketree1(Tblock, $2); }
+
+cmds:
+ cmd ';'
+| cmd '&' { $$ = maketree1('&', $1); }
+
+cmdsln:
+ cmds
+| cmd '\n'
+
+ifbody:
+ cmd %prec Telse { $$ = maketree2(Tif, nil, $1); }
+| block Telse nl cmd { $$ = maketree3(Tif, nil, $1, $2); }
+
+assign:
+ executable '=' word { $$ = maketree2('=', $1, $3); }
+
+cmd:
+/* empty */ %prec Twhile { $$ = nil; }
+| basic { $$ = maketree1(Tbasic, $1); }
+| block
+| Tif paren nl ifbody { $$ = hangchild1($2, $1, 0); }
+
+basic:
+ executable
+| basic word { $$ = maketree2(Targs, $1, $2); }
+
+arg: word
+
+args:
+ arg
+| args arg { $$ = maketree2(Targs, $1, $2); }
+
+atom:
+ nonkeyword
+| keyword { $$ = maketree1(Tword, $1); }
+
+word:
+ atom
+| word '^' atom { $$ = maketree2('^', $1, $3); }
+
+executable:
+ nonkeyword
+| executable '^' atom { $$ = maketree2('^', $1, $3); }
+
+nonkeyword:
+ Tword
+| '$' atom { $$ = maketree1('$', $2); }
+
+keyword:
+ Tif|Telse
+
+nl:
+/*empty*/
+| nl '\n'
+%%