Click here to Skip to main content
15,944,942 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys i am trying to make a string generator that generates a random string based upon a given grammar for my uni
i am using rand to choose between the choices given from the gramamar
but i end up with wrong outputs and i dont know why
my grammar is this
<Ε>::=(<Υ>)
<Υ>::=<Α><Β>
<Α>::=ν|<Ε>
<Β>::=-<Υ>|+<Υ>|ε




C++
#include <iostream>
using namespace std;
#include <stdlib.h> 
#include <cstring>
#include <stdio.h>
string st="-->";

string symbo(string s )
{
    label0:
        //int counter=0;
        s=s+"(";
        
        label1:
            int f = rand() % 2; 
            if(f==1)
            {  
                cout<<"the string right now is "<<s<<" and i am in the A-->v \n";
                s=s+"v";
            }
            if(f==0)
            {
                cout<<"the string right now is "<<s<<" and i am in the A--><E> \n";
                //counter++;
                goto label0;
            }
        cout<<"currently the string is "<< s << " and i am out of A \n";
        int g = rand() % 3;
        label2:
            
            cout<<"i am in the B \n";
            if(g==2)
            {
                cout<<"i am in the B--> -<Y> and the string is "<<s<<"\n";
            
                s=s+"-";
                goto label1;
            }
            if(g==1)
            {
                cout<<"i am in the B--> +<Y> and the string is "<<s<<"\n";
                s=s+"+";
                goto label1;
            }
            if(g==0)
            {
                
                cout<<"i am in the B--> e and the string is "<<s<<"\n";
                
            }
        cout<<"i am out of the B and the string is "<<s<<"\n";
        s=s+")";    
    return s;
}

int main()
{
    cout<<"producing a randomly generated string based on the given language and its rules \n";
    
    cout<<"the string is "<< symbo(st)<<"\n";

}




it outpouts constantly this (in the end) even after running the programm like 10 times
the string is -->(v-((v+((((v-v)

Thanks alot for any answers

What I have tried:

i have tried changing the rand numbers i give in each if statements condition other than that i am pretty much exhausted.
Posted
Updated 20-Jan-21 20:46pm
v2
Comments
Christian Graus 20-Jan-21 20:27pm    
Use switch statements. Use srand to seed your generator to get different results

1 solution

This task is typically accomplished using recursive function calls.
Moreover, you are getting always the same result because the random generator starts always with the same sequence: You do need to call srand in order to provide a different starting point (see srand(3): pseudo-random number generator - Linux man page[^]).

Try the program listed below.
(Note, such a code gives more chances to the empty production, in rule B in order to give the program itself chances, pardon the pun, to terminate).
C
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

/*
<Ε>::=(<Υ>)
<Υ>::=<Α><Β>
<Α>::=ν|<Ε>
<Β>::=-<Υ>|+<Υ>|ε
*/


void produce_E();
void produce_Y();
void produce_A();
void produce_B();


int main()
{
  time_t t;
  srand((unsigned) time(&t));

  produce_E();

  return 0;
}


void produce_E()
{
  putchar('(');
  produce_Y();
  putchar(')');
}

void produce_Y()
{
  produce_A();
  produce_B();
}

void produce_A()
{
  switch( rand() % 2) // 2 random choosen alternatives
  {
  case 0:
    putchar('v');
  break;
  case 1:
    produce_E();
  break;
  }
}

void produce_B()
{
  switch ( rand() % 5) // 3 random choosen (unbalanced) alternatives
  {
  case 0:
    putchar('-');
    produce_Y();
  break;
  case 1:
    putchar('+');
    produce_Y();
  break;
  default: // 2,3,4
    // empty
  break;
  }
}
 
Share this answer
 
v2
Comments
Maciej Los 21-Jan-21 6:33am    
5ed!
CPallini 21-Jan-21 6:36am    
Thank you, Maciej!
RastamanVibrations 21-Jan-21 16:12pm    
hello , and thank you very much for your answer my friend you really helped me out and it works perfectly
CPallini 21-Jan-21 16:26pm    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900