From 9fc687392c215a090b35d6966964990633f40acb Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Mon, 12 Sep 2011 15:52:29 -0400 Subject: [PATCH] gc: clean up if grammar Fixes #2248. R=ken2 CC=golang-dev https://golang.org/cl/4978064 --- src/cmd/gc/go.errors | 3 +++ src/cmd/gc/go.y | 38 ++++++++++++++++++++++---------------- test/syntax/else.go | 12 ++++++++++++ 3 files changed, 37 insertions(+), 16 deletions(-) create mode 100644 test/syntax/else.go diff --git a/src/cmd/gc/go.errors b/src/cmd/gc/go.errors index b5af4678c91..e29cfff5bd4 100644 --- a/src/cmd/gc/go.errors +++ b/src/cmd/gc/go.errors @@ -67,4 +67,7 @@ static struct { % loadsys package imports LFUNC LNAME '(' ')' '{' LFUNC LNAME "nested func not allowed", + + % loadsys package imports LFUNC LNAME '(' ')' '{' LIF if_header loop_body LELSE ';' + "else must be followed by if or statement block" }; diff --git a/src/cmd/gc/go.y b/src/cmd/gc/go.y index a5e92bd4d43..0c007f5f0b4 100644 --- a/src/cmd/gc/go.y +++ b/src/cmd/gc/go.y @@ -57,7 +57,7 @@ static void fixlbrace(int); %type compound_stmt dotname embed expr complitexpr %type expr_or_type %type fndcl fnliteral -%type for_body for_header for_stmt if_header if_stmt non_dcl_stmt +%type for_body for_header for_stmt if_header if_stmt else non_dcl_stmt %type interfacedcl keyval labelname name %type name_or_type non_expr_type %type new_name dcl_name oexpr typedclname @@ -640,6 +640,7 @@ if_header: $$->ntest = $3; } +/* IF cond body (ELSE IF cond body)* (ELSE block)? */ if_stmt: LIF { @@ -652,9 +653,27 @@ if_stmt: } loop_body { + $3->nbody = $5; + } + else + { + popdcl(); $$ = $3; - $$->nbody = $5; - // no popdcl; maybe there's an LELSE + if($7 != N) + $$->nelse = list1($7); + } + +else: + { + $$ = N; + } +| LELSE if_stmt + { + $$ = $2; + } +| LELSE compound_stmt + { + $$ = $2; } switch_stmt: @@ -1474,19 +1493,6 @@ non_dcl_stmt: | switch_stmt | select_stmt | if_stmt - { - popdcl(); - $$ = $1; - } -| if_stmt LELSE stmt - { - if($3->op != OIF && $3->op != OBLOCK) - yyerror("missing { } after else"); - - popdcl(); - $$ = $1; - $$->nelse = list1($3); - } | labelname ':' { $1 = nod(OLABEL, $1, N); diff --git a/test/syntax/else.go b/test/syntax/else.go new file mode 100644 index 00000000000..186d5959a8c --- /dev/null +++ b/test/syntax/else.go @@ -0,0 +1,12 @@ +// errchk $G $D/$F.go + +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +func main() { + if true { + } else ; // ERROR "else must be followed by if or statement block" +}