psym_else.c revision 1.4
1/* $NetBSD: psym_else.c,v 1.4 2022/04/24 10:36:37 rillig Exp $ */
2
3/*
4 * Tests for the parser symbol psym_else, which represents the keyword 'else'
5 * that is being shifted on the parser stack.
6 *
7 * This parser symbol never ends up on the stack itself.
8 */
9
10/*
11 * When parsing nested incomplete 'if' statements, the problem of the
12 * 'dangling else' occurs.  It is resolved by binding the 'else' to the
13 * innermost incomplete 'if' statement.
14 *
15 * In 'parse', an if_expr_stmt is reduced to a simple statement, unless the
16 * next token is 'else'. The comment does not influence this since it never
17 * reaches 'parse'.
18 */
19//indent input
20void
21example(bool cond)
22{
23	if (cond)
24	if (cond)
25	if (cond)
26	stmt();
27	else
28	stmt();
29	/* comment */
30	else
31	stmt();
32}
33//indent end
34
35//indent run
36void
37example(bool cond)
38{
39	if (cond)
40		if (cond)
41			if (cond)
42				stmt();
43			else
44				stmt();
45	/* comment */
46		else
47			stmt();
48}
49//indent end
50
51
52/*
53 * The keyword 'else' is followed by an expression, as opposed to 'if', which
54 * is followed by a parenthesized expression.
55 */
56//indent input
57void
58function(void)
59{
60	if(var>0)var=0;else(var=3);
61}
62//indent end
63
64//indent run
65void
66function(void)
67{
68	if (var > 0)
69		var = 0;
70	else
71		(var = 3);
72}
73//indent end
74