RSG using grammars

I'm a Junior at a university in California. My 1st assignment for my Data Structures class is to create a random sentence generator using grammars and vectors and parsing and a map. The problem is this is the first time I'm hearing about using these. So if follows that I'd have no clue how to code it. I've figured out what they are, but not the how. Per the directions, I have classes for terminals, non-terminals, definitions, and productions (but they're just shells, they don't do anything).

What I understand is (for example) <verb> is a non-terminal that'll return a random verb listed under it. If there is an extension (ie runs<adverb>) it'll jump to an adverb list in the terminal class? The definition class is an entire grammar
{<verb>
runs<adverb>;
sleeps<adverb>;
jump;
}
but when I code that, I get the red lines that say not valid (using Visual Studios). What I take that to mean is that I really don't understand.

The project assignment also came with a grammars file, but I don't understand why have the file if I'm going to define the grammar in the code?
Last edited on
The definition class is an entire grammar [...] but when I code that, I get the red lines that say not valid (using Visual Studios). What I take that to mean is that I really don't understand.

The project assignment also came with a grammars file, but I don't understand why have the file if I'm going to define the grammar in the code?
You're not supposed to just write the grammar in your source file. Obviously that's not going to work because the C++ compiler doesn't know anything about BNF. I'm missing some context, but I would guess you're supposed to either write a parser capable of parsing strings that follow the grammar you were given, or a parser that can parse grammars as input and generate random strings that follow that grammar.

If you're so confused perhaps you should ask your professor for clarification on the assignment. What you're asking doesn't really have much to do with your usage of the language.
Topic archived. No new replies allowed.