-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysh.y
78 lines (63 loc) · 1.34 KB
/
mysh.y
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
%{
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include "myshlex.h"
# include "commands.h"
# include "execcmd.h"
# include "state.h"
extern int yylineno;
%}
%union {
char *str;
struct cmd *command;
struct cmdgrp *group;
struct cmdpipe *pipeline;
}
%token <str> TEXT
%token NL
%token SEMICOLON
%token PIPE
%token REDIR_R
%token REDIR_A
%token REDIR_L
%token UNSUP
%type <pipeline> pipeln
%type <group> grp
%type <command> comtokens com
%define parse.error verbose
%destructor { free_group($$); } grp
%{
int yyerror() {
fprintf(stderr,
"error:%d: syntax error near unexpected token '%s'\n",
yylineno, yytext);
return (1);
};
%}
%%
cmdline
:
| cmdline NL
| cmdline grp NL { exec_group($2); free_group($2); }
;
grp
: pipeln { $$ = new_group(); push_pipe($$, $1); }
| pipeln SEMICOLON { $$ = new_group(); push_pipe($$, $1); }
| pipeln SEMICOLON grp { $$ = $3; push_pipe($$, $1); }
;
pipeln
: com { $$ = new_pipeline(); push_command($$, $1); }
| com PIPE pipeln { $$ = $3; push_command($$, $1); }
;
com
: comtokens
| com REDIR_R TEXT { $$ = $1; add_out($$, $3, false); }
| com REDIR_L TEXT { $$ = $1; add_in($$, $3); }
| com REDIR_A TEXT { $$ = $1; add_out($$, $3, true); }
;
comtokens
: TEXT { $$ = new_command(); add_path($$, $1); }
| comtokens TEXT { $$ = $1; add_arg($$, $2); }
;
%%