]> pd.if.org Git - pdutils/blob - posix/sh/gram.y
implemented pwd
[pdutils] / posix / sh / gram.y
1 /* -------------------------------------------------------
2    The Grammar
3    ------------------------------------------------------- */
4 /*
5  * added tokens
6  * Pipe '|'
7  * Lparen '('
8  * Rparen ')'
9  * Lessthan '<'
10  * Greaterthan '<'
11  * Ampersand '&'
12  * Semicolon ';'
13  */
14 %start_symbol complete_command
15 %token_prefix TOKEN_
16 %token_type {struct token *}
17 %extra_argument { struct parser_state *pstate }
18 %include {
19 #include "parser.h"
20 #include <assert.h>
21 }
22
23 complete_command ::= list separator.
24 complete_command ::= list.
25
26 list             ::= list separator_op and_or.
27 list             ::=                   and_or.
28
29 and_or           ::=                         pipeline.
30 and_or           ::= and_or AND_IF linebreak pipeline.
31 and_or           ::= and_or OR_IF  linebreak pipeline.
32
33 pipeline         ::=      pipe_sequence.
34 pipeline         ::= Bang pipe_sequence.
35
36 pipe_sequence    ::=                             command.
37 pipe_sequence    ::= pipe_sequence Pipe linebreak command.
38
39 command          ::= simple_command.
40 command          ::= compound_command.
41 command          ::= compound_command redirect_list.
42 command          ::= function_definition.
43
44 compound_command ::= brace_group.
45 compound_command ::= subshell.
46 compound_command ::= for_clause.
47 compound_command ::= case_clause.
48 compound_command ::= if_clause.
49 compound_command ::= while_clause.
50 compound_command ::= until_clause.
51
52 subshell         ::= Lparen compound_list Rparen.
53
54 compound_list    ::=              term. [NEWLINE]
55 compound_list    ::= newline_list term. [NEWLINE]
56 compound_list    ::=              term separator.
57 compound_list    ::= newline_list term separator.
58
59 term             ::= term separator and_or.
60 term             ::=                and_or.
61
62 for_clause       ::= For name linebreak                            do_group.
63 for_clause       ::= For name linebreak in          sequential_sep do_group.
64 for_clause       ::= For name linebreak in wordlist sequential_sep do_group.
65
66 name             ::= NAME                     /* Apply rule 5 */.
67
68 in               ::= In                       /* Apply rule 6 */.
69                  /* TODO start looking for pattern = Esac here */
70
71 wordlist         ::= wordlist WORD.
72 wordlist         ::=          WORD.
73
74 %fallback WORD
75         NAME Esac In
76 .
77
78 case_clause      ::= case_start case_list Esac.
79 case_clause      ::= case_start           Esac. { pstate->incase = 0; }
80
81 /* added to simplify */
82 case_start ::= Case WORD linebreak in linebreak.
83
84 case_list        ::= case_list case_item.
85 case_list        ::=           case_item.
86
87 %left NEWLINE.
88 %right DSEMI.
89 %right WORD.
90
91 //%left WORD.
92
93 case_item        ::= case_item_ns .
94 //case_item        ::= case_item_ns DSEMI linebreak.
95
96 case_item_ns     ::=        pattern Rparen linebreak. [DSEMI]
97 case_item_ns     ::=        pattern Rparen linebreak DSEMI linebreak.
98
99 case_item_ns     ::=        pattern Rparen compound_list linebreak. [DSEMI]
100 case_item_ns     ::=        pattern Rparen compound_list linebreak DSEMI linebreak.
101 //case_item_ns     ::= Lparen pattern Rparen linebreak. [DSEMI]
102 //case_item_ns     ::= Lparen pattern Rparen compound_list linebreak. [DSEMI]
103
104 //case_item_end ::= linebreak.
105 //case_item_end ::= linebreak DSEMI linebreak.
106
107 /* rule four implicitly done via %fallback */
108 pattern          ::=             WORD .         /* Apply rule 4 */
109 pattern          ::= pattern Pipe WORD .         /* Do not apply rule 4 */
110
111 %ifdef CASE_NS
112 case_clause      ::= case_start case_list_ns Esac.
113
114 case_list_ns     ::= case_list case_item_ns.
115 case_list_ns     ::=           case_item_ns.
116
117 %endif
118
119 if_clause        ::= If compound_list Then compound_list else_part Fi.
120 if_clause        ::= If compound_list Then compound_list           Fi.
121
122 else_part        ::= Elif compound_list Then compound_list.
123 else_part        ::= Elif compound_list Then compound_list else_part.
124 else_part        ::= Else compound_list.
125
126 while_clause     ::= While compound_list do_group.
127
128 until_clause     ::= Until compound_list do_group.
129
130 //function_definition ::= fname Lparen Rparen linebreak function_body.
131 function_definition ::= WORD Lparen Rparen linebreak function_body.
132
133 function_body    ::= compound_command                /* Apply rule 9 */.
134 function_body    ::= compound_command redirect_list  /* Apply rule 9 */.
135
136 /* we would need two tokens of look-ahead to prove this should be a name
137  * and not a word
138  */
139 //fname            ::= NAME                            /* Apply rule 8 */.
140 //fname ::= WORD.
141
142 brace_group      ::= Lbrace compound_list Rbrace.
143
144 do_group         ::= Do compound_list Done           /* Apply rule 6 */.
145
146 simple_command   ::= cmd_prefix cmd_word cmd_suffix.
147 simple_command   ::= cmd_prefix cmd_word.
148 simple_command   ::= cmd_prefix.
149 simple_command   ::= cmd_name cmd_suffix.
150 simple_command   ::= cmd_name.
151
152 cmd_name         ::= WORD                   /* Apply rule 7a */.
153
154 cmd_word         ::= WORD                   /* Apply rule 7b */.
155
156 cmd_prefix       ::=            io_redirect.
157 cmd_prefix       ::= cmd_prefix io_redirect.
158 cmd_prefix       ::=            ASSIGNMENT_WORD.
159 cmd_prefix       ::= cmd_prefix ASSIGNMENT_WORD.
160
161 cmd_suffix       ::=            io_redirect.
162 cmd_suffix       ::= cmd_suffix io_redirect.
163 cmd_suffix       ::=            WORD.
164 cmd_suffix       ::= cmd_suffix WORD.
165
166 redirect_list    ::=               io_redirect.
167 redirect_list    ::= redirect_list io_redirect.
168
169 io_redirect      ::=           io_file.
170 io_redirect      ::= IO_NUMBER io_file.
171 io_redirect      ::=           io_here.
172 io_redirect      ::= IO_NUMBER io_here.
173
174 io_file          ::= Lessthan    filename.
175 io_file          ::= LESSAND     filename.
176 io_file          ::= Greaterthan filename.
177 io_file          ::= GREATAND    filename.
178 io_file          ::= DGREAT      filename.
179 io_file          ::= LESSGREAT   filename.
180 io_file          ::= CLOBBER     filename.
181
182 filename         ::= WORD                      /* Apply rule 2 */.
183
184 io_here          ::= DLESS     here_end.
185 io_here          ::= DLESSDASH here_end.
186
187 here_end         ::= WORD                      /* Apply rule 3 */.
188
189 newline_list     ::= newline_list NEWLINE.
190 newline_list     ::=              NEWLINE. { pstate->linebreak = 1; }
191
192 linebreak        ::= newline_list. [NEWLINE]
193 linebreak        ::= /* empty */. [NEWLINE]
194
195 separator_op     ::= Ampersand.
196 separator_op     ::= Semicolon.
197
198 separator        ::= separator_op linebreak. [NEWLINE]
199 separator        ::= newline_list. [NEWLINE]
200
201 sequential_sep   ::= Semicolon linebreak.
202 sequential_sep   ::= newline_list.