Inheritance dependency interactions

I have a Parser with three classes of objects: the analyzer itself, Tokens and States. The parser generates tokens from the lexer. The whole window is black, so the tokens do not know anything about the state of the parser or the parser, and the state does not know anything about the tokens. A fairly simple version of the agreement:

class Parser {
   public function parse() {
      $this->state = new StEmpty;
      while ($token = $this->lexer->get()) {
         $this->state = $this->token->expect($this);
      }
   }
   public function stateStart() {
      return $this->state->stateStart();
   }
}
class StartToken {
   public function expect(Parser $parser) {
      return $parser->stateStart();
   }
}
class StEmpty {
   public function stateStart() {
      return new StStart;
   }
}

, , , , , - (, , ). State , , . Parser State. Parser , State , ( State Parser , -). Parser State, , : , State .

, , State Parser, , ? , .


, , "" :

class Parser {
   public function parse() {
      $this->state = 'StEmpty';

      while ($token = $this->lexer->get()) {
         switch ($token) {
            case 'StartToken':
               switch ($this->state) {
                  case 'StEmpty':
                     $this->state = 'StStart';
                     break;
               }
               break;
         }
      }
   }
}

, , , . PHP .

+3

All Articles