From: Dylan Evans Date: 2009-02-26T18:42:22+09:00 Subject: Re: Nexus Programming Language --0016e65206205ba5c30463cf2e5f Content-Type: multipart/alternative; boundary=0016e65206205ba5bc0463cf2e5d --0016e65206205ba5bc0463cf2e5d Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Admittedly you would need to create an object or struct and manage memory in other languages, but here's my take on polish notation in ruby. class PN #Cause why not? That's why attr_reader :result def initialize @result = 0 end def op(token) #create the node saving the previous node if any @node = { :parent => @node, :op => token, :lval => nil} end def val(token) if(@node[:lval].nil?) then #If this is the first token then it is the lval @node[:lval] = token else #Otherwise the operation can be resolved case(@node[:op]) when '+' then res = @node[:lval] + token when '-' then res = @node[:lval] - token when '/' then res = @node[:lval] / token when '*' then res = @node[:lval] * token end if(@node[:parent].nil?) then #This is the last(aka first) operation so save the result @result = res @node = nil else #Send the result to the parent @node = @node[:parent] val(res) end end end end # 32 * (10 + 5) + 3 pn = PN.new token_list = ['+', '*', 32, '+', 10, 5, 3] token_list.each { |el| if(el.is_a?(String)) then pn.op(el) else pn.val(el) end } puts pn.result On Thu, Feb 26, 2009 at 12:55 PM, Dylan Evans wrote: > On Thu, Feb 26, 2009 at 8:53 AM, Chad Perrin wrote: > > > On Wed, Feb 25, 2009 at 03:57:48PM +0900, Dylan Evans wrote: > > > On Wed, Feb 25, 2009 at 8:30 AM, Chad Perrin > > wrote: > > > > > > > > More than that -- and still relevant today -- parsers for Polish > > notation > > > > are generally more difficult to *write*. I know -- I've tried > > designing > > > > both Polish notation parsers and RPN parsers. In fact, Polish > notation > > > > parsers are more difficult to write for essentially the same reason > > that > > > > RPN parsers are slightly more efficient from the machine's point of > > view. > > > > > > That's seems funny because bison uses RPN as an example. The syntax > isn't > > > complex and once you have tokens you just need to shift the values onto > a > > > stack and when an operator comes along you just apply it to the last > two > > > values then push the result. Or you can create a tree where each value > > > becomes a node and the following operation the parent. > > > > I'm not sure how that "seems funny", considering you just confirmed some > > of what I said -- that RPN parsers are easier to write than Polish > > notation parsers. > > > My mistake, i thought you were saying the opposite of that. None the less > it > sounds like a challenge. I guess the easiest way would be to read tokens > into an array and then parse them as RPN, but that's not very elegant > besides being a little harder. So i'll come back to using a tree, > presumably > were using ruby since this is a ruby list so i would choose to implement my > branch nodes as Hashs of the form; > > { :op => '+', : lval => 7, :rval => 8 } > > There are two ways to do it from here, but in both you read the tokens > creating a node if an operator is read or by populating it's values, the > difference is whether you choose to evaluate a node when it's complete or > the whole expression when it is finished. > Is that more complex? I'm not really sure, i think i'll write it up when i > finish work. > > > > -- > The UNIX system has a command, nice ... in order to be nice to the other > users. Nobody ever uses it." - Andrew S. Tanenbaum > -- The UNIX system has a command, nice ... in order to be nice to the other users. Nobody ever uses it." - Andrew S. Tanenbaum --0016e65206205ba5bc0463cf2e5d Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Admittedly you would need to create an object or struct and manage memory i= n other languages, but here's my take on polish notation in ruby.


class PN
=A0=A0=A0 #Cause why not? That's why
=A0=A0= =A0 attr_reader :result
=A0=A0=A0 def initialize
=A0=A0=A0=A0=A0=A0=A0 @result =3D 0
=A0=A0= =A0 end
=A0=A0=A0
=A0=A0=A0 def op(token)
=A0=A0=A0=A0=A0=A0=A0 #= create the node saving the previous node if any
=A0=A0=A0=A0=A0=A0=A0 @n= ode =3D { :parent =3D> @node, :op =3D> token, :lval =3D> nil}
=A0=A0=A0 end
=A0=A0=A0
=A0=A0=A0 def val(token)
=A0=A0=A0=A0=A0= =A0=A0 if(@node[:lval].nil?) then
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 #If = this is the first token then it is the lval
=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0 @node[:lval] =3D token
=A0=A0=A0=A0=A0=A0=A0 else
=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0 #Otherwise the operation can be resolved
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 case(@node[:op])
=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0 when '+' then
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0 res =3D @node[:lval] + token
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= when '-' then
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 re= s =3D @node[:lval] - token
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 when '/= ' then
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 res =3D @node[:lval] / token<= br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 when '*' then
=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 res =3D @node[:lval] * token
=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0 end
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 if(@node[= :parent].nil?) then
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 #This = is the last(aka first) operation so save the result
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 @result =3D res
=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 @node =3D nil
=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0 else
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 #Send th= e result to the parent
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 @no= de =3D @node[:parent]
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 val(= res)
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 end
=A0=A0=A0=A0=A0=A0=A0 end
=A0=A0=A0 end
end


#=A0 32 * (10 = + 5) + 3
pn =3D PN.new
token_list =3D ['+', '*', 32, = '+', 10, 5, 3]

token_list.each { |el|
=A0=A0=A0 if(el.is_= a?(String)) then
=A0=A0=A0=A0=A0=A0=A0 pn.op(el)
=A0=A0=A0 else
=A0=A0=A0=A0=A0=A0=A0 pn.val(el)
=A0=A0=A0 end
}
puts pn.result



On Thu, Feb 2= 6, 2009 at 12:55 PM, Dylan Evans <dylan.star@gmail.com> wrote:
<= div class=3D"Wj3C7c">On Thu, Feb 26, 2009 at 8:53 AM, Chad Perrin <perrin@apotheon.com> wrote:

> On Wed, Feb 25, 2009 at 03:57:48PM +0900, Dylan Evans wrote:
> > On Wed, Feb 25, 2009 at 8:30 AM, Chad Perrin <perrin@apotheon.com>
> wrote:
> > >
> > > More than that -- and still relevant today -- parsers for Po= lish
> notation
> > > are generally more difficult to *write*. =A0I know -- I'= ve tried
> designing
> > > both Polish notation parsers and RPN parsers. =A0In fact, Po= lish notation
> > > parsers are more difficult to write for essentially the same= reason
> that
> > > RPN parsers are slightly more efficient from the machine'= ;s point of
> view.
> >
> > That's seems funny because bison uses RPN as an example. The = syntax isn't
> > complex and once you have tokens you just need to shift the value= s onto a
> > stack and when an operator comes along you just apply it to the l= ast two
> > values then push the result. Or you can create a tree where each = value
> > becomes a node and the following operation the parent.
>
> I'm not sure how that "seems funny", considering you jus= t confirmed some
> of what I said -- that RPN parsers are easier to write than Polish
> notation parsers.


My mistake, i thought you were saying the opposite of that. None the less i= t
sounds like a challenge. I guess the easiest way would be to read tokens into an array and then parse them as RPN, but that's not very elegant besides being a little harder. So i'll come back to using a tree, presu= mably
were using ruby since this is a ruby list so i would choose to implement my=
branch nodes as Hashs of the form;

{ :op =3D> '+', : lval =3D> 7, :rval =3D> 8 }

There are two ways to do it from here, but in both you read the tokens
creating a node if an operator is read or by populating it's values, th= e
difference is whether you choose to evaluate a node when it's complete = or
the whole expression when it is finished.
Is that more complex? I'm not really sure, i think i'll write it up= when i
finish work.



--
The UNIX system has a command, nice ... in order to be nice to the other users. Nobody ever uses it." - Andrew S. Tanenbaum



--
The UNIX sy= stem has a command, nice ... in order to be nice to the other users. Nobody= ever uses it." - Andrew S. Tanenbaum
--0016e65206205ba5bc0463cf2e5d-- --0016e65206205ba5c30463cf2e5f Content-Type: application/octet-stream; name="pn.rb" Content-Disposition: attachment; filename="pn.rb" Content-Transfer-Encoding: base64 X-Attachment-Id: f_frn8s8f90 CgpjbGFzcyBQTiAKICAgICNDYXVzZSB3aHkgbm90PyBUaGF0J3Mgd2h5CiAgICBhdHRyX3JlYWRl ciA6cmVzdWx0CiAgICBkZWYgaW5pdGlhbGl6ZQogICAgICAgIEByZXN1bHQgPSAwCiAgICBlbmQK ICAgIAogICAgZGVmIG9wKHRva2VuKQogICAgICAgICNjcmVhdGUgdGhlIG5vZGUgc2F2aW5nIHRo ZSBwcmV2aW91cyBub2RlIGlmIGFueQogICAgICAgIEBub2RlID0geyA6cGFyZW50ID0+IEBub2Rl LCA6b3AgPT4gdG9rZW4sIDpsdmFsID0+IG5pbH0KICAgIGVuZAogICAgCiAgICBkZWYgdmFsKHRv a2VuKQogICAgICAgIGlmKEBub2RlWzpsdmFsXS5uaWw/KSB0aGVuCiAgICAgICAgICAgICNJZiB0 aGlzIGlzIHRoZSBmaXJzdCB0b2tlbiB0aGVuIGl0IGlzIHRoZSBsdmFsCiAgICAgICAgICAgIEBu b2RlWzpsdmFsXSA9IHRva2VuCiAgICAgICAgZWxzZQogICAgICAgICAgICAjT3RoZXJ3aXNlIHRo ZSBvcGVyYXRpb24gY2FuIGJlIHJlc29sdmVkCiAgICAgICAgICAgIGNhc2UoQG5vZGVbOm9wXSkK ICAgICAgICAgICAgd2hlbiAnKycgdGhlbgogICAgICAgICAgICAgICAgcmVzID0gQG5vZGVbOmx2 YWxdICsgdG9rZW4KICAgICAgICAgICAgd2hlbiAnLScgdGhlbiAKICAgICAgICAgICAgICAgIHJl cyA9IEBub2RlWzpsdmFsXSAtIHRva2VuCiAgICAgICAgICAgIHdoZW4gJy8nIHRoZW4gCiAgICAg ICAgICAgICAgICByZXMgPSBAbm9kZVs6bHZhbF0gLyB0b2tlbgogICAgICAgICAgICB3aGVuICcq JyB0aGVuCiAgICAgICAgICAgICAgICByZXMgPSBAbm9kZVs6bHZhbF0gKiB0b2tlbgogICAgICAg ICAgICBlbmQKICAgICAgICAgICAgaWYoQG5vZGVbOnBhcmVudF0ubmlsPykgdGhlbgogICAgICAg ICAgICAgICAgI1RoaXMgaXMgdGhlIGxhc3QoYWthIGZpcnN0KSBvcGVyYXRpb24gc28gc2F2ZSB0 aGUgcmVzdWx0CiAgICAgICAgICAgICAgICBAcmVzdWx0ID0gcmVzCiAgICAgICAgICAgICAgICBA bm9kZSA9IG5pbAogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAjU2VuZCB0aGUgcmVz dWx0IHRvIHRoZSBwYXJlbnQKICAgICAgICAgICAgICAgIEBub2RlID0gQG5vZGVbOnBhcmVudF0K ICAgICAgICAgICAgICAgIHZhbChyZXMpCiAgICAgICAgICAgIGVuZAogICAgICAgIGVuZAogICAg ZW5kCmVuZAoKCiMgIDMyICogKDEwICsgNSkgKyAzCnBuID0gUE4ubmV3CnRva2VuX2xpc3QgPSBb JysnLCAnKicsIDMyLCAnKycsIDEwLCA1LCAzXQoKdG9rZW5fbGlzdC5lYWNoIHsgfGVsfAogICAg aWYoZWwuaXNfYT8oU3RyaW5nKSkgdGhlbgogICAgICAgIHBuLm9wKGVsKQogICAgZWxzZQogICAg ICAgIHBuLnZhbChlbCkKICAgIGVuZAp9CgpwdXRzIHBuLnJlc3VsdAo= --0016e65206205ba5c30463cf2e5f--