[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: gawk's "for" does not allow simple-statements.
From: |
Aharon Robbins |
Subject: |
Re: gawk's "for" does not allow simple-statements. |
Date: |
Fri, 11 Jan 2002 09:53:42 +0200 |
Greetings. Re this:
> To: address@hidden
> From: David Jones <address@hidden>
> Subject: gawk's "for" does not allow simple-statements.
> Date: Mon, 07 Jan 2002 13:46:33 +0000
>
> In a for statement:
>
> for(x;;y)
>
> x and y are both allowed to be simple_statement (in the single unix spec
> Version 2, but also in the IEEE P1003.1, Draft 7, Issue 6).
>
> That means the following ought to work (outputting "9\n").
>
> ./gawk 'BEGIN{for(print 9;0;);}'
>
> but it doesn't (I get parse error).
>
> Cheers,
> drj
>
> # versions etc
> $ ./gawk --version
> GNU Awk 3.1.0
> $ uname -a
> FreeBSD topcat.zoonami.com 4.3-RELEASE FreeBSD 4.3-RELEASE #0: Thu Nov 1
> 14:13:13 GMT 2001 address@hidden:/usr/src/sys/compile/GENERIC i386
Fortunately, this was fairly straightforward to fix. Here is an unofficial
patch.
This patch will cause a few innocuous errors in the test suite that will
be straightened out for gawk 3.1.1.
Thanks,
Arnold
-----------------------------------------------------------------
*** ../gawk-3.1.0/awkgram.y Mon Apr 23 10:25:58 2001
--- awkgram.y Mon Jan 7 22:27:17 2002
***************
*** 110,115 ****
--- 110,116 ----
%type <nodeval> rexpression_list opt_rexpression_list
%type <nodeval> expression_list opt_expression_list
%type <nodeval> statements statement if_statement opt_param_list
+ %type <nodeval> simple_stmt opt_simple_stmt
%type <nodeval> opt_exp opt_variable regexp
%type <nodeval> input_redir output_redir
%type <nodetypeval> print
***************
*** 419,429 ****
(NODE *) NULL, variable($5, CAN_FREE,
Node_var_array)));
}
}
! | LEX_FOR '(' opt_exp semi opt_nls exp semi opt_nls opt_exp r_paren
opt_nls statement
{
$$ = node($12, Node_K_for, (NODE *) make_for_loop($3, $6, $9));
}
! | LEX_FOR '(' opt_exp semi opt_nls semi opt_nls opt_exp r_paren opt_nls
statement
{
$$ = node($11, Node_K_for,
(NODE *) make_for_loop($3, (NODE *) NULL, $8));
--- 420,430 ----
(NODE *) NULL, variable($5, CAN_FREE,
Node_var_array)));
}
}
! | LEX_FOR '(' opt_simple_stmt semi opt_nls exp semi opt_nls
opt_simple_stmt r_paren opt_nls statement
{
$$ = node($12, Node_K_for, (NODE *) make_for_loop($3, $6, $9));
}
! | LEX_FOR '(' opt_simple_stmt semi opt_nls semi opt_nls opt_simple_stmt
r_paren opt_nls statement
{
$$ = node($11, Node_K_for,
(NODE *) make_for_loop($3, (NODE *) NULL, $8));
***************
*** 434,467 ****
| LEX_CONTINUE statement_term
/* similarly */
{ $$ = node((NODE *) NULL, Node_K_continue, (NODE *) NULL); }
- | print '(' expression_list r_paren output_redir statement_term
- {
- $$ = node($3, $1, $5);
- if ($$->type == Node_K_printf)
- count_args($$)
- }
- | print opt_rexpression_list output_redir statement_term
- {
- if ($1 == Node_K_print && $2 == NULL) {
- static int warned = FALSE;
-
- $2 = node(node(make_number(0.0),
- Node_field_spec,
- (NODE *) NULL),
- Node_expression_list,
- (NODE *) NULL);
-
- if (do_lint && ! io_allowed && ! warned) {
- warned = TRUE;
- lintwarn(
- _("plain `print' in BEGIN or END rule should probably be `print
\"\"'"));
- }
- }
-
- $$ = node($2, $1, $3);
- if ($$->type == Node_K_printf)
- count_args($$)
- }
| LEX_NEXT statement_term
{ NODETYPE type;
--- 435,440 ----
***************
*** 498,506 ****
}
opt_exp statement_term
{ $$ = node($3, Node_K_return, (NODE *) NULL); }
! | LEX_DELETE NAME '[' expression_list ']' statement_term
{ $$ = node(variable($2, CAN_FREE, Node_var_array),
Node_K_delete, $4); }
! | LEX_DELETE NAME statement_term
{
if (do_lint)
lintwarn(_("`delete array' is a gawk extension"));
--- 471,519 ----
}
opt_exp statement_term
{ $$ = node($3, Node_K_return, (NODE *) NULL); }
! | simple_stmt statement_term
! ;
!
! /*
! * A simple_stmt exists to satisfy a constraint in the POSIX
! * grammar allowing them to occur as the 1st and 3rd parts
! * in a `for (...;...;...)' loop. This is a historical oddity
! * inherited from Unix awk, not at all documented in the AK&W
! * awk book. We support it, as this was reported as a bug.
! * We don't bother to document it though. So there.
! */
! simple_stmt
! : print '(' expression_list r_paren output_redir
! {
! $$ = node($3, $1, $5);
! if ($$->type == Node_K_printf)
! count_args($$);
! }
! | print opt_rexpression_list output_redir
! {
! if ($1 == Node_K_print && $2 == NULL) {
! static int warned = FALSE;
!
! $2 = node(node(make_number(0.0),
! Node_field_spec,
! (NODE *) NULL),
! Node_expression_list,
! (NODE *) NULL);
!
! if (do_lint && ! io_allowed && ! warned) {
! warned = TRUE;
! lintwarn(
! _("plain `print' in BEGIN or END rule should probably be `print
\"\"'"));
! }
! }
!
! $$ = node($2, $1, $3);
! if ($$->type == Node_K_printf)
! count_args($$);
! }
! | LEX_DELETE NAME '[' expression_list ']'
{ $$ = node(variable($2, CAN_FREE, Node_var_array),
Node_K_delete, $4); }
! | LEX_DELETE NAME
{
if (do_lint)
lintwarn(_("`delete array' is a gawk extension"));
***************
*** 514,520 ****
}
$$ = node(variable($2, CAN_FREE, Node_var_array),
Node_K_delete, (NODE *) NULL);
}
! | exp statement_term
{ $$ = $1; }
;
--- 527,540 ----
}
$$ = node(variable($2, CAN_FREE, Node_var_array),
Node_K_delete, (NODE *) NULL);
}
! | exp
! { $$ = $1; }
! ;
!
! opt_simple_stmt
! : /* empty */
! { $$ = NULL; }
! | simple_stmt
{ $$ = $1; }
;