Here's another tricky one: parsing double quoted strings, especially when they use the %Q syntax. <br>

<br>

First, strings can contain nested ruby expressions (full programs, actually):<br>

<br>

&quot;1 + 2 = #{1 + 2}&quot;&nbsp; -&gt; 1 + 2 = 3<br>

<br>

Double-quoted strings can also be constructed with % or %Q and an
delimiter character, according to these rules (from the Pickaxe
book).<br>

<br>

&quot;Following the type character is a delimiter, which can be any nonalphabetic or nonmultibyte<br>

character. If the delimiter is one of the characters (, [, {, or &lt;, the literal<br>

consists of the characters up to the matching closing delimiter, taking account of nested<br>

delimiter pairs. For all other delimiters, the literal comprises the characters up to the<br>

next occurrence of the delimiter character.&quot;<br>

<br>

Here are a few of examples of valid strings:<br>

<br>

%Q/1 + 2 = #{ 1 + 2 }/ &nbsp; &nbsp; &nbsp;&nbsp; # -&gt; 1 + 2 = 3<br>

%/1 + 2 = #{ 1 + 2 }/ &nbsp;&nbsp;&nbsp;&nbsp;  <br>

%{1 + 2 = #{ 1 + 2 }}<br>

%(1 + #{ %(2 = #{ 1 + 2 })})&nbsp; <br>


<br>

%((1 + 2) * 3 = 9)&nbsp; &nbsp; &nbsp; &nbsp; # -&gt; (1 + 2) * 3 = 9<br>
<span>
<br>
Martin</span><br><br><div><span class="gmail_quote">On 11/26/05, <b class="gmail_sendername">Terence Parr</b> &lt;<a href="mailto:parrt@cs.usfca.edu" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
parrt@cs.usfca.edu</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>On Nov 25, 2005, at 4:44 PM, MenTaLguY wrote:<br><br>Howdy....thanks for the examples...<br><br>&gt; Hmm, I'm hoping some of the grammarian-newcomers can help out with<br>&gt; that,<br>&gt; but to start off here are some bits with method calls:
<br>&gt;<br>&gt; First:<br>&gt;<br>&gt;&nbsp;&nbsp;foo bar baz<br><br>Which object is foo a method of?&nbsp;&nbsp;Is that the &quot;main&quot; thing I keep<br>reading about?&nbsp;&nbsp;foo returns an object that bar is a method of?<br><br>&gt; (where foo and bar are method names) parses the same as:
<br>&gt;<br>&gt;&nbsp;&nbsp;foo( bar( baz ) )<br><br>Ah.&nbsp;&nbsp;So it calls bar(baz) first and then uses result as an arg to<br>foo?&nbsp;&nbsp;&nbsp;&nbsp;That is very different from how'd I read it w/o the parens.<br>Holy smokes.<br><br>&gt; Note that omitting the parenthesis for more than one function in the
<br>&gt; &quot;stack&quot; is deprecated, however.<br><br>Thank gawd.<br><br>&gt; Next:<br>&gt;<br>&gt;&nbsp;&nbsp;def foo<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;zort = 3 # assign to variable<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;blah = zort # assign to variable from variable<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;...
<br>&gt;&nbsp;&nbsp;end<br>&gt;<br>&gt; versus:<br>&gt;<br>&gt;&nbsp;&nbsp;def foo<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;blah = zort # assign to variable from method result<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;zort = 3 # assign to variable<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;...<br>&gt;&nbsp;&nbsp;end<br>&gt;<br>&gt; I am not sure that 'blah = zort' is parsed differently in these cases,
<br>&gt; however; that subtree may look the same in the AST and simply get<br>&gt; interpreted differently.<br><br>Unless you have lazy evaluation, shouldn't the second version result<br>in blah being nil?<br><br>Regardless, the AST should just be syntax so you should see two
<br>assignment trees.&nbsp;&nbsp;One of the operands juts happens to be an INT.<br>I'd generate<br><br>(ASSIGN ID ID)<br><br>and<br><br>(ASSIGN ID INT)<br><br>or some such.<br><br>&gt; Next:<br>&gt;<br>&gt;&nbsp;&nbsp;foo 1, 2, 3 # ok<br><br>

ack.<br><br>&gt;&nbsp;&nbsp;foo(1, 2, 3) # ok<br>&gt;&nbsp;&nbsp;bar(1, 2, 3) { ... } # ok<br>&gt;&nbsp;&nbsp;bar 1, 2, 3 { ... } # parse error<br>&gt;&nbsp;&nbsp;bar 1, 2, baz { ... } # parses as bar(1, 2, baz { ... })<br><br>Yes, that all seems ok.&nbsp;&nbsp;It's just
<br><br>methodCall : ID arglist ;<br>arglist : &quot;(&quot; exprList &quot;)&quot; | exprList ;<br><br>no sweat.&nbsp;&nbsp;Uh, 'cept for that wacky newline issue where you can have<br>\n in the middle of an expression.&nbsp;&nbsp;I'll figure that part out later...
<br><br>Ter<br><br>_______________________________________________<br>Rubygrammar-grammarians mailing list<br><a href="mailto:Rubygrammar-grammarians@rubyforge.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
Rubygrammar-grammarians@rubyforge.org</a><br><a href="http://rubyforge.org/mailman/listinfo/rubygrammar-grammarians" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
http://rubyforge.org/mailman/listinfo/rubygrammar-grammarians</a><br></blockquote></div><br>