bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: syntax error while parsing a case command within `$(...)'


From: Koichi Murase
Subject: Re: syntax error while parsing a case command within `$(...)'
Date: Tue, 16 Feb 2021 18:19:48 +0800

2021年2月16日(火) 16:28 Andreas Schwab <schwab@linux-m68k.org>:
> See 2.4 Reserved Words.
>
>     This recognition shall only occur when none of the characters is
>     quoted and when the word is used as:

I think that section just describes the necessary condition that the
word is recognized as the reserved keyword: "This recognition shall
*only* occur...". It doesn't explain whether the reserved keyword can
be really used to construct the AST following the shell grammar. For
example, this alone doesn't explain why

$ if :; then echo A; fi if :; then echo A; fi

(i.e., the combination "fi if") is a syntax error.

Hmm, it seems we need to combine the Yacc-BNF notation at the end of
Sec. 2.10 to understand this? For example,

$ case x in (x) if :; then echo; fi esac

can be derived as

case_clause
-> Case WORD linebreak in linebreak case_list Esac
-> Case 'x' ε in ε case_list Esac
-> Case 'x' ε in ε case_item_ns Esac
-> Case 'x' ε in ε '(' pattern ')' compound_list Esac
-> Case 'x' ε in ε '(' 'x' ')' linebreak term Esac
-> Case 'x' ε in ε '(' 'x' ')' ε and_or Esac
-> Case 'x' ε in ε '(' 'x' ')' ε pipeline Esac
-> Case 'x' ε in ε '(' 'x' ')' ε pipe_sequence Esac
-> Case 'x' ε in ε '(' 'x' ')' ε command Esac
-> Case 'x' ε in ε '(' 'x' ')' ε compound_command Esac
-> Case 'x' ε in ε '(' 'x' ')' ε if_clause Esac
-> ...

where ε is an empty string. OK, now I understood this behavior is
actually required by the POSIX standard. Can we find any textual
explanation on this rule? Or maybe this behavior is intuitive enough
for those who understand the shell grammar so that they don't see the
necessity of an additional explanation...

Thank you for the comment!

--
Koichi



reply via email to

[Prev in Thread] Current Thread [Next in Thread]