10
Jun 07

PHP2Py

Path: mv.asterisco.pt!mvalente
From: mvale…@ruido-visual.pt (Mario Valente)
Newsgroups: mv
Subject: PHP2Py
Date: Sun, 10 Jun 07 01:04:21 GMT

If I had the time and patience of days gone by, I would
get my photocopied Aho compiler book [1] from some closet or
box laying about (I dont know which), get my skills up on
XSLT (which I hate from back experience [2]) and use the
PHC [3] PHP compiler option of outputting AST trees [4]
in XML format to create PHP2PY, a PHP-to-Python translator,
using XSLT to transform the PHC-created AST XML tree
into Python source code.

[1]
http://en.wikipedia.org/wiki/Principles_of_Compiler_Design
http://en.wikipedia.org/wiki/Compilers:_Principles%2C_Techniques%2C_and_Tools

[2]
http://www.gildot.org/articles/04/05/21/1654202.shtml#10

[3]
http://www.phpcompiler.org/doc/runningphc.html#xml

[4]
http://en.wikipedia.org/wiki/Abstract_syntax_tree

As it is, downloading, installing and running PHC to
look at the generated XML for a simple “Hello World” and
putting together a proof-of-concept translator will have
to do to satisfy my hacking thirst…

— MV

PS – If you want to try it out, go get PHC, use it to
generate an XML AST of <? echo “Hello world!”; ?> into
a file called hello.xml. Processing it with the following
Python script should get you this:

#PHP2PY generated
print ‘Hello world!’

8<—————————————————<8
import xml.parsers.expat

InMethod=0
InString=0
MethodCall=0
StringValue=0

def start_element(name, attrs):
global InMethod, InString, MethodCall, StringValue

#print 'Start element:', name, attrs
if name == 'Token_method_name':
InMethod=1
if name == 'Token_string':
InString=1

if name == 'value' and InMethod:
MethodCall=1
if name == 'value' and InString:
StringValue=1

def end_element(name):
global InMethod, InString, MethodCall, StringValue

#print 'End element:', name
if name == 'Token_method_name':
InMethod=0
if name == 'Token_string':
InString=0

def char_data(data):
global InMethod, InString, MethodCall, StringValue

if MethodCall:
if data == "%run%": print "#PHP2PY generated"
if data == "echo": print "print",
MethodCall=0
if StringValue:
print "'"+data+"'"
StringValue=0

p = xml.parsers.expat.ParserCreate()

p.StartElementHandler = start_element
p.EndElementHandler = end_element
p.CharacterDataHandler = char_data

hellofile = open("hello.xml")
p.ParseFile(hellofile)
hellofile.close()


10
Jun 07

Heavy Mental IV

Path: mv.asterisco.pt!mvalente
From: mvale…@ruido-visual.pt (Mario Valente)
Newsgroups: mv
Subject: Heavy Mental IV
Date: Sat, 07 Jun 07 23:49:21 GMT

And another music example with favorite bands of mine.
And once again opposites do seem to attract.

Heavy: Sigue Sigue Sputnik

http://www.youtube.com/watch?v=pk30a0qsVIk
http://www.youtube.com/watch?v=28hFIxgtv0o
http://www.youtube.com/watch?v=pS71DJUTDU8

Mental: Kraftwerk

http://www.youtube.com/watch?v=9YCZVeIJ8xk
http://www.youtube.com/watch?v=qDde1es7aoU
http://www.youtube.com/watch?v=HieIpE-Q19c

— MV


10
Jun 07

Heavy Mental V

Path: mv.asterisco.pt!mvalente
From: mvale…@ruido-visual.pt (Mario Valente)
Newsgroups: mv
Subject: Heavy Mental V
Date: Sat, 07 Jun 07 23:49:21 GMT

And to have at least an example which is not music
oriented, here’s a books example from a heavy mental mind.

Heavy:

Stephen King, “The Stand” and “Christine”
Neal Stephenson, “Snow Crash” and “Cryptonomicon”
William Gibson, “Neuromancer”
Richard Morgan, “Altered Carbon”
Stephen Donaldson, “The Chronicles of Thomas Covenant, Unbeliever”
Frank Herbert, “Dune”

Mental:

Peter Drucker, “Management”
Christensen, “The Innovators Dilemma”
Kurzweil, “The Age of Spiritual Machines”
Hayek, “The Road to Serfdom” and “Fatal Conceit, the Errors of Socialism”
Pirsig, “Zen and the Art of Motorcycle Maintenance”
Ayn Rand, “The Virtue of Selfishness” and “Capitalism, the Unknown Ideal”

— MV


10
Jun 07

Heavy Mental III

Path: mv.asterisco.pt!mvalente
From: mvale…@ruido-visual.pt (Mario Valente)
Newsgroups: mv
Subject: Heavy Mental III
Date: Sat, 07 Jun 07 23:29:21 GMT

Another music example of heavy mental mind.
And here opposites seem to attract.

Heavy: Queensryche

http://www.youtube.com/watch?v=TIX6B2tcezA
http://www.youtube.com/watch?v=MpPDFIswQ-c
http://www.youtube.com/watch?v=OjNRE8Hg4hs
http://www.youtube.com/watch?v=UjjS_9qq3-g
http://www.youtube.com/watch?v=rWLqzLZvOZY

Mental: Marillion

http://www.youtube.com/watch?v=Q7sIzWKHGwQ
http://www.youtube.com/watch?v=xXwlorEn1lY
http://www.youtube.com/watch?v=ULngjhKnZ0o
http://www.youtube.com/watch?v=kIPrJmzG7Ss
http://www.youtube.com/watch?v=j50t1x-_vVk

— MV


09
Jun 07

Heavy Mental II

Path: mv.asterisco.pt!mvalente
From: mvale…@ruido-visual.pt (Mario Valente)
Newsgroups: mv
Subject: Heavy Mental II
Date: Sat, 07 Jun 07 22:19:21 GMT

Another music example. Other two of my favorite bands
of all time…

Heavy: Kiss

http://www.youtube.com/watch?v=3oWriOMGj_E
http://www.youtube.com/watch?v=ZSghlgQ7rsM
http://www.youtube.com/watch?v=1lcwROZBI9Q
http://www.youtube.com/watch?v=TvjVy2ETJz4
http://www.youtube.com/watch?v=PzmqgLmCzNQ

Mental: Saga

http://www.youtube.com/watch?v=4RQ2MrlIKD0
http://www.youtube.com/watch?v=OaNlc_8fvlw
http://www.youtube.com/watch?v=HUJhCxFbFEo
http://www.youtube.com/watch?v=redbX2364Rc
http://www.youtube.com/watch?v=oricZ9XbHlM

— MV