<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Ruby is a single pass parser! Eeck!</title>
	<atom:link href="http://brian.pontarelli.com/2006/06/21/ruby-is-a-single-pass-parser-eeck/feed/" rel="self" type="application/rss+xml" />
	<link>http://brian.pontarelli.com/2006/06/21/ruby-is-a-single-pass-parser-eeck/</link>
	<description>Brian Pontarelli</description>
	<pubDate>Wed, 07 Jan 2009 01:34:33 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
		<item>
		<title>By: Brian Pontarelli</title>
		<link>http://brian.pontarelli.com/2006/06/21/ruby-is-a-single-pass-parser-eeck/#comment-756</link>
		<dc:creator>Brian Pontarelli</dc:creator>
		<pubDate>Thu, 14 Sep 2006 20:13:42 +0000</pubDate>
		<guid isPermaLink="false">http://brian.pontarelli.com/2006/06/21/ruby-is-a-single-pass-parser-eeck/#comment-756</guid>
		<description>Hmmm not sure how you got that to work if the classes are in the same file. Like this blows chunks for me:

&lt;pre&gt;
class Test1
  def foo
    puts Test2.new.bar
  end
end

t = Test1.new.foo

class Test2
  def bar
    puts "bar"
  end
end
&lt;/pre&gt;

I get this error
&lt;pre&gt;
test.rb:3:in `foo': uninitialized constant Test1::Test2 (NameError)

        from test.rb:7

&lt;/pre&gt;

The issue is that in order to parse this code the interpreter starts at the top of the file and starts parsing. It encounters the class Test1 and probably (I haven't looked at the code) builds a complete AST/symbol-table for it. Next it sees the plain line of code and parses it building another AST. Since that line of code belongs to the global space it is executed. This in turn executes the Test1 method called foo, which tries to create Test2. Since the parser never got to the Test2 definition, explosions. If ruby used a two pass parser it would build the AST/symbol-table for both classes and the global scope and then execute the global scoped code. Then when it encountered the Test2 reference it would already have the AST/symbols for it and could execute it.

Of course I'm guessing a lot at the implementation, but most programming languages regardless of compiled or interpreted build symbol tables and syntax trees and all that jazz before anything is executed.</description>
		<content:encoded><![CDATA[<p>Hmmm not sure how you got that to work if the classes are in the same file. Like this blows chunks for me:</p>
<pre>
class Test1
  def foo
    puts Test2.new.bar
  end
end

t = Test1.new.foo

class Test2
  def bar
    puts "bar"
  end
end
</pre>
<p>I get this error</p>
<pre>
test.rb:3:in `foo': uninitialized constant Test1::Test2 (NameError)

        from test.rb:7
</pre>
<p>The issue is that in order to parse this code the interpreter starts at the top of the file and starts parsing. It encounters the class Test1 and probably (I haven&#8217;t looked at the code) builds a complete AST/symbol-table for it. Next it sees the plain line of code and parses it building another AST. Since that line of code belongs to the global space it is executed. This in turn executes the Test1 method called foo, which tries to create Test2. Since the parser never got to the Test2 definition, explosions. If ruby used a two pass parser it would build the AST/symbol-table for both classes and the global scope and then execute the global scoped code. Then when it encountered the Test2 reference it would already have the AST/symbols for it and could execute it.</p>
<p>Of course I&#8217;m guessing a lot at the implementation, but most programming languages regardless of compiled or interpreted build symbol tables and syntax trees and all that jazz before anything is executed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh</title>
		<link>http://brian.pontarelli.com/2006/06/21/ruby-is-a-single-pass-parser-eeck/#comment-755</link>
		<dc:creator>Josh</dc:creator>
		<pubDate>Thu, 14 Sep 2006 18:47:31 +0000</pubDate>
		<guid isPermaLink="false">http://brian.pontarelli.com/2006/06/21/ruby-is-a-single-pass-parser-eeck/#comment-755</guid>
		<description>The code I posted was just an example of referring to another class before its definition without any problem. 

An interpreter doesn't necessarily have to parse the entire file. Typically, they read a line, then evaluate it. That is why that code blew up. Here's a shorter code sample to illustrate that it isn't ruby's problem; it's not a problem at all, really.


# python
f()
def f():
    print "f() called"
</description>
		<content:encoded><![CDATA[<p>The code I posted was just an example of referring to another class before its definition without any problem. </p>
<p>An interpreter doesn&#8217;t necessarily have to parse the entire file. Typically, they read a line, then evaluate it. That is why that code blew up. Here&#8217;s a shorter code sample to illustrate that it isn&#8217;t ruby&#8217;s problem; it&#8217;s not a problem at all, really.</p>
<p># python<br />
f()<br />
def f():<br />
    print &#8220;f() called&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Pontarelli</title>
		<link>http://brian.pontarelli.com/2006/06/21/ruby-is-a-single-pass-parser-eeck/#comment-754</link>
		<dc:creator>Brian Pontarelli</dc:creator>
		<pubDate>Thu, 14 Sep 2006 14:13:11 +0000</pubDate>
		<guid isPermaLink="false">http://brian.pontarelli.com/2006/06/21/ruby-is-a-single-pass-parser-eeck/#comment-754</guid>
		<description>
Comment fixed somewhat. The code still isn't complete, so I'm not sure what it did exactly. You should definitely use PRE tags rather than CODE tags for code snippets. 


Whether or not a language is interpreted or not, the parser still has to parse an entire file before execution. It needs to build the AST. Ruby doesn't parse the entire file but appears to only parse the file until it see the class in question and then stops. Meaning that the AST doesn't have all the symbols from the file (eeck!). This could be a performance thing, but I doubt it.


I think the code snippet you supplied illustrates two things and both are runtime not parse time (it got truncated so I'm not certain). First that ruby class variables can be declared anywhere without problems because the class is not fixed and you can add new variables and methods at anytime. And second that a class is always initialized by calling the initialize method.
</description>
		<content:encoded><![CDATA[<p>Comment fixed somewhat. The code still isn&#8217;t complete, so I&#8217;m not sure what it did exactly. You should definitely use PRE tags rather than CODE tags for code snippets. </p>
<p>Whether or not a language is interpreted or not, the parser still has to parse an entire file before execution. It needs to build the AST. Ruby doesn&#8217;t parse the entire file but appears to only parse the file until it see the class in question and then stops. Meaning that the AST doesn&#8217;t have all the symbols from the file (eeck!). This could be a performance thing, but I doubt it.</p>
<p>I think the code snippet you supplied illustrates two things and both are runtime not parse time (it got truncated so I&#8217;m not certain). First that ruby class variables can be declared anywhere without problems because the class is not fixed and you can add new variables and methods at anytime. And second that a class is always initialized by calling the initialize method.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh</title>
		<link>http://brian.pontarelli.com/2006/06/21/ruby-is-a-single-pass-parser-eeck/#comment-753</link>
		<dc:creator>Josh</dc:creator>
		<pubDate>Thu, 14 Sep 2006 03:31:51 +0000</pubDate>
		<guid isPermaLink="false">http://brian.pontarelli.com/2006/06/21/ruby-is-a-single-pass-parser-eeck/#comment-753</guid>
		<description>I seem to have broken your comment feature with some ruby code. But you can trust what I wrote. :)</description>
		<content:encoded><![CDATA[<p>I seem to have broken your comment feature with some ruby code. But you can trust what I wrote. :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh</title>
		<link>http://brian.pontarelli.com/2006/06/21/ruby-is-a-single-pass-parser-eeck/#comment-752</link>
		<dc:creator>Josh</dc:creator>
		<pubDate>Thu, 14 Sep 2006 03:30:29 +0000</pubDate>
		<guid isPermaLink="false">http://brian.pontarelli.com/2006/06/21/ruby-is-a-single-pass-parser-eeck/#comment-752</guid>
		<description>Ruby is an interpreted language. Therefore you'll have problems when you evaluate code that causes the evaluation of unknown symbols. The code below runs fine, though.

&lt;pre&gt;
class Account
  def initialize
    @withdrawals = []
  end
  
  def withdraw(amount)
    @withdrawals 
  end
end
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Ruby is an interpreted language. Therefore you&#8217;ll have problems when you evaluate code that causes the evaluation of unknown symbols. The code below runs fine, though.</p>
<pre>
class Account
  def initialize
    @withdrawals = []
  end

  def withdraw(amount)
    @withdrawals
  end
end
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>
