[Rubygrammar-grammarians] Ruby Grammar Project

Terence Parr parrt at cs.usfca.edu
Sat Nov 26 14:49:30 EST 2005


On Nov 25, 2005, at 4:44 PM, MenTaLguY wrote:

Howdy....thanks for the examples...

> Hmm, I'm hoping some of the grammarian-newcomers can help out with  
> that,
> but to start off here are some bits with method calls:
>
> First:
>
>  foo bar baz

Which object is foo a method of?  Is that the "main" thing I keep  
reading about?  foo returns an object that bar is a method of?

> (where foo and bar are method names) parses the same as:
>
>  foo( bar( baz ) )

Ah.  So it calls bar(baz) first and then uses result as an arg to  
foo?    That is very different from how'd I read it w/o the parens.   
Holy smokes.

> Note that omitting the parenthesis for more than one function in the
> "stack" is deprecated, however.

Thank gawd.

> Next:
>
>  def foo
>    zort = 3 # assign to variable
>    blah = zort # assign to variable from variable
>    ...
>  end
>
> versus:
>
>  def foo
>    blah = zort # assign to variable from method result
>    zort = 3 # assign to variable
>    ...
>  end
>
> I am not sure that 'blah = zort' is parsed differently in these cases,
> however; that subtree may look the same in the AST and simply get
> interpreted differently.

Unless you have lazy evaluation, shouldn't the second version result  
in blah being nil?

Regardless, the AST should just be syntax so you should see two  
assignment trees.  One of the operands juts happens to be an INT.   
I'd generate

(ASSIGN ID ID)

and

(ASSIGN ID INT)

or some such.

> Next:
>
>  foo 1, 2, 3 # ok

ack.

>  foo(1, 2, 3) # ok
>  bar(1, 2, 3) { ... } # ok
>  bar 1, 2, 3 { ... } # parse error
>  bar 1, 2, baz { ... } # parses as bar(1, 2, baz { ... })

Yes, that all seems ok.  It's just

methodCall : ID arglist ;
arglist : "(" exprList ")" | exprList ;

no sweat.  Uh, 'cept for that wacky newline issue where you can have  
\n in the middle of an expression.  I'll figure that part out later...

Ter



More information about the Rubygrammar-grammarians mailing list