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

  • Home
  • SEARCH
  • 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 399453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:50:53+00:00 2026-05-12T16:50:53+00:00

i have shared the header file containing class definition of a Context Free grammar

  • 0

i have shared the header file containing class definition of a Context Free grammar for a parser. Could you comment on the design.
this code is for my lab assignment.
may be we could get some good programming tips out of this code. is the class hierarchy good or too complicated.

#ifndef CFG_H
#define CFG_H

#include <iostream>
#include <set>
#include <list>

using namespace std;



class Terminal;


class CfgSymbol
{
protected:
    char ch;
    set<Terminal*> first;
    set<Terminal*> follow;

public:
    CfgSymbol()
    {
        ch = '\0';
    }

    CfgSymbol(char c) : ch(c)
    {
    }

    virtual void computeFirst() = 0;
};


class Terminal: public CfgSymbol
{
private:

public:
    Terminal(): CfgSymbol()
    {
    }

    Terminal(char c) : CfgSymbol(c)
    {
        computeFirst();
    }

    virtual void computeFirst()
    {
        first->insert(this);
    }

};

class NonTerminal: public CfgSymbol
{
private:

public:
    virtual void computeFirst();
    virtual void computeFollow();
};

class SymbolString
{
public:
    CfgProduction* prd;
    list<CfgSymbol*> symstr;

    void computeFirst();
    void computeFollow();
};

class CfgProduction
{
private:
    NonTerminal lhs;
    SymbolString rhs;
public:
    int add_terminal(char t);
    int add_nonterminal(char n);
    int set_lhs(char c);
};

class Cfg
{
public:
    vector<CfgProduction*> prdList;

    void addProduction(const CfgProduction& cfg);
    void computeFirst();
    void computeFollow();
    void computeFirstFollow();

};

#endif
  • 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-05-12T16:50:53+00:00Added an answer on May 12, 2026 at 4:50 pm

    Spirit’s documentation should give you a nice intro for grammar implementation (with templates). Don’t know your level, maybe that’s too simple, but its quite interesting :

    As a grammar becomes complicated, it
    is a good idea to group parts into
    logical modules. For instance, when
    writing a language, it might be wise
    to put expressions and statements into
    separate grammar capsules. The grammar
    takes advantage of the encapsulation
    properties of C++ classes. The
    declarative nature of classes makes it
    a perfect fit for the definition of
    grammars. Since the grammar is nothing
    more than a class declaration, we can
    conveniently publish it in header
    files. The idea is that once written
    and fully tested, a grammar can be
    reused in many contexts. We now have
    the notion of grammar libraries.

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

Sidebar

Related Questions

When I create shared libraries, I have a header file (but with no file
I have 2 programs that share a header file. This header file defines a
I have a class like class K { static int a; static int b;
I have a YACC (Bison) grammar, a Lex (Flex) tokenizer, and a C program
I have a Visual C++ solution with 2 projects AlgorithmA & AlgorithmB and both
This is probably quite simple, but I just don't know how to do this...
I want to design & implement a simple windowing & widget for OpenGL running
I have 4 .c files hello.c , here.c , bye.c and main.c . One
I have the following code which works as expected: #include <iostream> using namespace std;
So this is a homework assignment, and it's kind of a doozy so i'll

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.