Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7903237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:41:59+00:00 2026-06-03T09:41:59+00:00

I have a Parser with three classes of objects: the parser itself, Token s,

  • 0

I have a Parser with three classes of objects: the parser itself, Tokens, and States. The Parser generates tokens from the lexer. Everything is black boxed, so the tokens know nothing about the parser state or the parser, and the state knows nothing of the tokens. A fairly simple version of the arrangement:

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;
   }
}

The problem I’m running into is that sometimes when a state changes, the parser needs to take some action (such as add a rule to the tree when the ending-rule token is reached). Only the State knows that, so it is up to the state to tell the parser what to do. Problem is getting the Parser to the State. I could inject the Parser in the state constructor, but not every State needs the parser, and that would lead to a lot of duplicate code (unless I had a base class for the States and the Parser was a protected member, but I want to avoid extending anything). I could also inject the Parser in the state methods that need it, but I have a similar problem: that would be a lot of duplication, and not all of the State implementations would need the parser for given methods.

So my question is how can I get the State to know about the Parser when it needs to without unnecessary inheritance or code duplication? If I need another class that’s perfectly acceptable.


In case this is difficult to follow, here is an “unraveled” version:

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;
         }
      }
   }
}

The answer to this question could apply to other languages as well, but I know this would be easier to do in languages that allow overloading. PHP does not.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:42:00+00:00Added an answer on June 3, 2026 at 9:42 am

    PHP 5.4 introduces traits: http://php.net/manual/en/language.oop5.traits.php

    Perhaps you could use traits as a halfway between inheritance and injection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found that writing PHP code within classes can become rather long: $this->parser->parse_syntax($this->get_language_path($this->language),
I have a few NSManagedObject classes. I am pulling down some JSON data from
Suppose I have an ambiguous language expressed in combinator parser. Is there a way
I have a buggy xml that contains empty attributes and I have a parser
I have the following parser grammar (this is a small sample): expr: ident assignop
In my google maps program I have simple KML parser which retrieve only coordinates
I have created a small parser in c using flex and bison. The parser
I have created a class with 3 sub classes in Java. The 3 sub
I have a data structure object which is loaded from an XML file inside
I have two classes Teacher and Student . I am trying to get teacher

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.