aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/rc/syntax.y
blob: a580cb07fb0f3d2294c367f8de8a180304f6b163 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
%token Tword Tif Telse Tbang
%token Targs
%token Tbasic Tparen Tblock

%define parse.error verbose

%{
    #include "rc.h"

    int  yylex(void);
    void yyerror(const 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
| assign cmd %prec Tbang  { $$ = hangchild1($1, $2, 2); }
| 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'
%%