The problem is on line 11:
{$act/title}
PHP does variable interpolation in heredoc strings. When variable names get complicated (like $foo->bar['baz'][$wibble]) it is necessary to delimint which bits of the string are part of the variable and which are just nearby. The syntax for doing this is to wrap the variable in {}.
So on line 11 you've got {$act, PHP recognises this as indicating that here be a variable that starts with "$act". But instead of continuing with the name of a variable, or the end of the variable, indicated by }, it sees a / and it goes WTF?
Do you want the interpolation (I'm guessing not) and keep the {.../TITLE} bit? If yes, then you'll need to delimit the variable so that PHP can tell which bits are variable and which aren't; i.e., {{$act}/TITLE}. If no, you'll need to escape the $ so that it doesn't get flagged as being part of a variable at all; i.e., {\$act/TITLE}.
Or if you can live with escaping any single quotes that appear in your strings, you could write
$query ='<toc>{
for $act in doc("/db/shakespeare/plays/macbeth.xml")/PLAY/ACT
return
<act>
{$act/TITLE}
{
for $scene in $act/SCENE return
<scene>
{$scene/TITLE}
<actors>
{
for $speaker in distinct-values($scene//SPEAKER)
order by $speaker return
<actor>{$speaker}</actor>
}
</actors>
</scene>
}
</act>
}
</toc>';