psym_else.c revision 1.4
11.4Srillig/* $NetBSD: psym_else.c,v 1.4 2022/04/24 10:36:37 rillig Exp $ */
21.1Srillig
31.1Srillig/*
41.1Srillig * Tests for the parser symbol psym_else, which represents the keyword 'else'
51.1Srillig * that is being shifted on the parser stack.
61.1Srillig *
71.1Srillig * This parser symbol never ends up on the stack itself.
81.1Srillig */
91.1Srillig
101.4Srillig/*
111.4Srillig * When parsing nested incomplete 'if' statements, the problem of the
121.4Srillig * 'dangling else' occurs.  It is resolved by binding the 'else' to the
131.4Srillig * innermost incomplete 'if' statement.
141.4Srillig *
151.4Srillig * In 'parse', an if_expr_stmt is reduced to a simple statement, unless the
161.4Srillig * next token is 'else'. The comment does not influence this since it never
171.4Srillig * reaches 'parse'.
181.4Srillig */
191.4Srillig//indent input
201.4Srilligvoid
211.4Srilligexample(bool cond)
221.4Srillig{
231.4Srillig	if (cond)
241.4Srillig	if (cond)
251.4Srillig	if (cond)
261.4Srillig	stmt();
271.4Srillig	else
281.4Srillig	stmt();
291.4Srillig	/* comment */
301.4Srillig	else
311.4Srillig	stmt();
321.4Srillig}
331.4Srillig//indent end
341.4Srillig
351.4Srillig//indent run
361.4Srilligvoid
371.4Srilligexample(bool cond)
381.4Srillig{
391.4Srillig	if (cond)
401.4Srillig		if (cond)
411.4Srillig			if (cond)
421.4Srillig				stmt();
431.4Srillig			else
441.4Srillig				stmt();
451.4Srillig	/* comment */
461.4Srillig		else
471.4Srillig			stmt();
481.4Srillig}
491.4Srillig//indent end
501.4Srillig
511.4Srillig
521.4Srillig/*
531.4Srillig * The keyword 'else' is followed by an expression, as opposed to 'if', which
541.4Srillig * is followed by a parenthesized expression.
551.4Srillig */
561.3Srillig//indent input
571.4Srilligvoid
581.4Srilligfunction(void)
591.4Srillig{
601.4Srillig	if(var>0)var=0;else(var=3);
611.4Srillig}
621.3Srillig//indent end
631.1Srillig
641.4Srillig//indent run
651.4Srilligvoid
661.4Srilligfunction(void)
671.4Srillig{
681.4Srillig	if (var > 0)
691.4Srillig		var = 0;
701.4Srillig	else
711.4Srillig		(var = 3);
721.4Srillig}
731.4Srillig//indent end
74