views:

143

answers:

3

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 heirarchy 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  A: 

You should not write implementations in the header except for templates. Header must contains only signatures of your class and functions, and your members.

Convention will be to put only one class per file. It's better for compilation : http://stackoverflow.com/questions/28160/multiple-classes-in-a-header-file-vs-a-single-header-file-per-class. But I think it is correct to put many classes in the same header if classes are less than 10 lines long.

Patrice Bernassola
For one, since it doesn't answer the question, I think this would have belonged to a comment.
sbi
I don't think it's a good idea to group classes (or not) in a file according to their length. IMO several classes belong into the same header if either `A)` they are a complete set of related and rather small classes or `B)` one of them is the main class and the others are a complete set of rather small helpers and satellites.
sbi
I agree with you. In my mind regrouping small classes included your conditions.
Patrice Bernassola
+2  A: 

Without comments the whole things is meaningless.

There should be some lengthy description about how an object instance is used and in what context it is used.

On the other hand I normally start with a BNF Grammar definition ( at leas in the comments) before the class definition. Then design the classes around the grammar.

Martin York
+2  A: 

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.

anno
great got to learn something new
iamrohitbanga