Intro
Blur's song Girls & Boys featured the lines
girls who are boys who like boys to be girls
who do boys like they're girls, who do girls like they're boys
in the chorus. These lines have a pretty uniform structure: you have a subject, which is either girls
or boys
, a relative clause, either who are
, who like
, or who do
, an object, again either girls
or boys
, and an optional modifier, which is similar to the relative clause, except it can also be like they're
or to be
. In EBNF this might be written as:
subject ::= "boys" | "girls"
relative_clause ::= "who are" | "who like" | "who do"
modifier ::= relative_clause | "like theyre" | "to be"
tail ::= relative_clause subject (modifier sentence)?
sentence ::= subject tail
The challenge
Your task is to, given a string, see if it is valid in this format. you can assume the string will be all lowercase, and will contain only letters and spaces (this means that theyre
will have no apostrophe). It should output a boolean value or 0
/1
. input and output can be done with any of the standard I/O methods. the input string may be empty. all words will be separated by a single space, but they will not all be valid words. for example, the input will never be girls who likeboys
but it might be girls who like bananas
Example I/O
girls who are boys who like boys to be girls who do boys like theyre girls who do girls like theyre boys -> true
boys who like boys -> true
true
girls to be boys -> false
`to be` is only valid in the modifier position, not as the leading relative clause.
girls who like boys to be girls to be boys -> false
The second `to be` is used in the relative clause position here, since after the first `to be` a new sentence begins, and sentences can't use modifiers in the relative clause position.
boys -> false
boys who are -> false
who are boys -> false
boys boys -> false
<empty input> -> false
Scoring
This is code-golf, so the shortest answer in characters wins.
tail ::= relative_clause (subject modifier)? sentence
instead? \$\endgroup\$