Parsing the php code

Problem

Given a tokenized PHP file, give me a map from class to list of functions.

Here is same PHP code.

class Foo {
    public $aMemberVar = 'aMemberVar Member Variable';
    public $aFuncName = 'aMemberFunc';
 
 
function aMemberFunc() {
        print 'Inside `aMemberFunc()`';
    }
function bob() {
print "hi, Hi am functopm bob2 ";
}
}

class Olivia
{
function say() { print "hi"; }
}
$foo = new Foo;

class bogus ;


Write the code to return the map of the class name to the list of functions.

Answer

This is a very vague question with very little detailed requirements. To solve this problem properly, I made the following assumptions:


  • Input is list of tokens after tokenization.
  • all the special chars (",', {,}, \,) are separate token

Given the assumptions, we need to examine each token and handle the following cases:
  • if token = class, mark the class flag if neither of single/double quotation is non-zero and take the next non-empty string as a class name and add it to class map (if not exists) 
  • if token is {, increase left_bracket count by 1.
  • if token =  "function" and we are not in single/double quotation, mark function flag to true.
  • if token is  } decrease left_bracket by 1
  • if token is " decrease double quotation by 1 if it is greater than 1
  • if token is ' decrease single quotation by 1 if it is greater than 1
  • if token is a word,
    • add the word to class map if class flag is True
    • add the word to function list of the current class if function flag is True
This question seems to be more of design question than algorithm question.

Here is a solution in python:



Practice statistics

 40:00 to write up the complete code. 

Comments

Popular posts from this blog

Planting flowers with no adjacent flower plots

Find the maximum number of bomb that can be detonated