Take the 2-minute tour ×

I have read the related questions, but none of them appears to address the question directly. I am working on writing a PHP script interpreter. I have the AST generating proper nodes for everything except classes. Handling classes is a bit different than handling functions, so I am looking for how to handle classes that are standalone, and that extend other classes.

I have looked at ANTLR, but I can't afford the overhead as this is for an embedded platform. What I am looking for is the theory conceptually behind classes in ASTs so they can be executed by the executor portion of the interpreter. Good links with specific answers to this issue are definitely appreciated.

share|improve this question
add comment

5 Answers

up vote 2 down vote accepted

ANTLR is more or less irrelevant to your problem.

A class in PHP is basically a map from strings to attributes. Each attribute can be public, private, protected. Each attribute also holds a value, which could be a static variable or a method. Methods are functions that (in PHP) take an implicit $this parameter. So you can think of a class as basically a fancy array object in PHP.

When you create an object, you give it a pointer to your PHP class object. When you call a method on that object, you look up the method through the class object which you get through that pointer.

I hope this helps.

share|improve this answer
Right, but that leads to re-interpreting the tokens for that object each time the class is instantiated right? I guess that is the difference between Google's V8 and other JavaScript implementations, and that I need to figure it out. Thanks for the info. –  Heat Miser May 29 '09 at 0:46
add comment

Perhaps you need some structures for interpretation that are a bit more removed from the source grammar? I don't know much about PHP, but it sound like you're really asking how structure the program model in a way that's convenient for interpretation. I'd regard an AST generated via ANTLR as a bit too close to the source for what you want.

share|improve this answer
add comment

I have found that the best method in a top down compiler, the best method is to treat the classes generally just like functions, in that they are just nodes without needing to evaluate the arguments before descending into the tree.

The differences are in the executor, which has to create an execution scope in which to encapsulate the methods and variables that comprise the class node.

share|improve this answer
add comment

I'd suggest using JavaCC (or the fork FreeCC) to parse and build your AST. JavaCC generates a parser which has no runtime dependencies. It's difficult to write a smaller/faster parser than the code which JavaCC generates.

share|improve this answer
Cool, I'll take a look at it, one thing that I have to watch is that it is compatible with Dalvik bytecode, I've found that most java .class files are, but some are not –  Heat Miser May 29 '09 at 16:52
JavaCC outputs just a couple of Java class files. These would get compiled with your regular compiler. FreeCC has better support for generics, so I would use that since your coding Java 1.5. –  brianegge May 31 '09 at 22:47
Thanks for the help, I'll see if I can use it –  Heat Miser Jun 2 '09 at 21:58
add comment

Take a look at the phc abstract grammar, it does exactly that. (Incidentally, it sounds like using phc's front-end might be better than reinventing the wheel).

Class_def ::= Class_mod CLASS_NAME extends:CLASS_NAME? implements:INTERFACE_NAME* Member* ;
Class_mod ::= "abstract"? "final"? ;

Interface_def ::= INTERFACE_NAME extends:INTERFACE_NAME* Member* ;

Member ::= Method | Attribute ;

Method ::= Signature Statement*? ;
Signature ::= Method_mod is_ref:"&"? METHOD_NAME Formal_parameter* ;
Method_mod ::= "public"? "protected"? "private"? "static"? "abstract"? "final"? ;
Formal_parameter ::= Type is_ref:"&"? var:Name_with_default ;
Type ::= CLASS_NAME? ;
Name_with_default ::= VARIABLE_NAME Expr? ;

Attribute ::= Attr_mod vars:Name_with_default* ;
Attr_mod ::= "public"? "protected"? "private"? "static"? "const"?  ;
share|improve this answer
Thanks Paul, I'm still looking at using PHC for C / C++ based clients, but how can I use it on Android ( Java / Dalvik )? –  Heat Miser Jul 30 '09 at 18:25
I'll assume native apps aren't possible? Then maybe as web service using the XML interface? –  Paul Biggar Jul 30 '09 at 18:44
add comment

Your Answer


