Question

Create the rules of a C.F.G. which generates the language of legal definitions for primitive variable...

Create the rules of a C.F.G. which generates the language of legal definitions for primitive variable types in C++ (You will likely want multiple grammar variables).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear,

Feel free to reach out if you have doubts. Please do rate if the answer was helpful. Thanks ----------------------------------------------------------------------------------------------------- Copyable Code: -----------------------------------------

Program :

#include <cstdlib>
#include <iostream>

using namespace std;
// ------------Function Prototypes----------------------------------------------
void DFA_push(char); // Calls ^ Push() after performing certain validations
void DFA_pop(char); // Calls pop() after performing certain validations.  
void process_string(string);
string transition(int, char); // return a value from the PDA Table
int return_col(char); //will return which column in the PDA table to go to.
void status(); // Prints whether string is, Accepted or Rejected or Crashed
void push(char); // Pushes a CHAR into the stack
char pop(); // Simply pops the top most CHAR and returns the value
void print_stack();
void print(int, char, char, char);
//------------------------------------------------------------------------------
struct stack{
char ch;
struct stack * top;
};
struct stack * head=NULL;

//Subsets of the Alphabet
char E_op[4]={'+','-','/','*'};
char E_ch[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; //a-z
char E_n[11]={'0','1','2','3','4','5','6','7','8','9','_'}; //0-9
const int accept_state=5;
int current_state=0;
bool state_crash=0;
/*---------------------TRANSITION TABLE-----------------------------------------
This is the TRANSITION Function of the Push-Down-Automota(PDA)
-NOTE: The symbol '~' means Epsilon
-Rows are [States], Columns are subset of [symbols]

-A value in the table is an Instruction in the form [Move][Pop][Push]
-ex, Suppose you are in state q0 and read symbol '$',
the instruction returned will be "1~$".
-That instruction means, Goto State(1), Pop(~Epsilon), Push($)
*/
string PDA[7][7]={
//(Column) $ E_op E_n E_ch ( ) NOT(E)
// (ROW)
{"1~$", "6~~","6~~", "6~~", "6~~", "6~~", "6~~"}, //q0
{"6~~", "6~~","6~~", "2~~", "1~(", "6~~", "6~~"}, //q1
{"5$~", "3~~","2~~", "2~~", "6~~", "4(~", "6~~"}, //q2
{"6~~", "6~~","6~~", "2~~", "1~(", "6~~", "6~~"}, //q3
{"5$~", "1~~","6~~", "6~~", "6~~", "4(~", "6~~"}, //q4
{"6~~", "6~~","6~~", "6~~", "6~~", "6~~", "6~~"}, //q5
{"6~~", "6~~","6~~", "6~~", "6~~", "6~~", "6~~"} //q6

};
//---------------------------------------Main()---------------------------------

int main(int argc, char *argv[])
{
string word;
string choice;
bool bye=0;

while(bye!=1){
cout<<"Do you want to enter a string ? Y (yes) or N (no): ";
cin >> choice;
if (choice=="n" or choice=="N"){
bye=1;
} else if (choice=="y" or choice=="Y"){
cout<<"Please enter string : "; cin >> word; cout<<endl<<endl;
current_state=0;
state_crash=0; //resetting values
process_string(word); // Process the String that was entered.
status();
cout<<endl<<endl;
}else{
cout<<"Incorrect Input-Try Again: ";
}   
}  
}

//----------------------------------End of Main()-------------------------------

void process_string(string word){
/* This function will read the input string CHAR by CHAR,
- for each character,
- refer to the PDA and retrieve the instruction
- Process the instruction (MOVE-POP-PUSH)
- Print each transition
*/
int i=0;
string instruction; //MOVE-POP-PUSH
cout<<"Start State: q0"<<endl;
for (i=0; i<word.length(); i++){
if (state_crash==1){
break;
}
instruction=transition(current_state, word[i]);
current_state=atoi(&instruction[0]);
DFA_pop(instruction[1]);
DFA_push(instruction[2]);
print(current_state,word[i],instruction[1],instruction[2]);
}

}

string transition(int current_state, char c){
/*
Based on the [current_state] and the [Symbol Read], What to do next ?
Returns an instruction (move-pop-push) fetched from the PDA Table.
*/
return PDA[current_state][return_col(c)];  
// Looks up PDA Table based on [current_state][Column the char belongs to]
}

int return_col(char c){
/*
NOTE:
The Columns in PDA Table are not symbols, they are [Subset of symbols]
So, before we lookup the table, we need to know which subset the CHAR belongs to
This function will return the INDEX of the column a Symbol(Char) belongs to.
Ex: '$' belongs to index 0, '5' belongs to index 2...etc
*/
int i=0;

if (c=='(' ) return 4;
if (c==')' ) return 5;
if (c=='$' ) return 0;

for (i=0; i<4; i++){
if (c==E_op[i]) return 1;
}

for (i=0; i<26; i++){
if (c==E_ch[i]) return 3;
}

for (i=0; i<11; i++){
if (c==E_n[i])return 2;
}

return 6;
}

void DFA_pop(char c){
/* NOTE-> -A PDA can pop an Epsilon.---> DO nothing
-A PDA pop must check if the popped symbol matches  
*/
char ch;
if (c=='~') return; // If (Episilon), do nothing
ch=pop();
if (ch!=c) state_crash=1;//If the popped symbol doesn't match, PDA Crashes
}

void DFA_push(char c){
// Push a symbol only if symbol is not (Epsilon).
if (c != '~') push(c);
}

//----------------------(Generic Stack functions)-------------------------------

void push(char c){
struct stack * node;
struct stack * current=head;
node=new struct stack;
node->ch=c;
node->top=NULL;

if (head==NULL){
head=node;
}else{
while(current->top){
current=current->top;
}
current->top=node;
}

}

char pop(){
struct stack * current=head;
struct stack * current_prev=head;
char ch;

if (head==NULL){
state_crash=1;
return '%';
}else if (head->top==NULL){
ch=head->ch;
head=NULL;
return ch;
}else{
while(current->top){
current=current->top;
if (current->top){
current_prev=current_prev->top;
}
}
ch=current->ch;
current_prev->top=NULL;
return ch;
}
}

//--------------------------(PRINT Functions)-----------------------------------
void status(){
if (state_crash==1){
cout<<"CRASHED"<<endl;
}else if (current_state==accept_state){
cout<<"ACCEPTED"<<endl;
}else{
cout<<"REJECTED"<<endl;
}
}


void print_stack(){
struct stack * current=head;
if (head==NULL){
cout<<"No items in stack"<<endl;
}else{
while(current){
cout<<current->ch;
current=current->top;
}
}
}

void print(int current_state, char read, char popped, char pushed){
//Symbol Read: Symbol Popped: Symbol Pushed: Current State:
cout<<"Read : "<<read;
if (popped=='~'){
cout<<" Popped:(epsilon)";
}else{
cout<<" Popped: "<<popped;
}

if (pushed=='~'){
cout<<" Pushed:(epsilon)";
}else{
cout<<" Pushed: "<<pushed;
}
cout<<" Current State:q"<<current_state<<endl;
}

output :

Do you want to enter a string ? Y (yes) or N (no): y   
Please enter string : 123 8765   
  
  
Start State: q0
Read : 1 Popped:(epsilon) Pushed:(epsilon) Current State:q6
Read : 2 Popped:(epsilon) Pushed:(epsilon) Current State:q6
Read : 3 Popped:(epsilon) Pushed:(epsilon) Current State:q6
REJECTED   
  
  
Do you want to enter a string ? Y (yes) or N (no): Incorrect Input-Try Again: Do you want to enter a string ? Y (yes) or N (no)
: y
Please enter string : 1234567890   
  
  
Start State: q0
Read : 1 Popped:(epsilon) Pushed:(epsilon) Current State:q6
Read : 2 Popped:(epsilon) Pushed:(epsilon) Current State:q6
Read : 3 Popped:(epsilon) Pushed:(epsilon) Current State:q6
Read : 4 Popped:(epsilon) Pushed:(epsilon) Current State:q6
Read : 5 Popped:(epsilon) Pushed:(epsilon) Current State:q6
Read : 6 Popped:(epsilon) Pushed:(epsilon) Current State:q6
Read : 7 Popped:(epsilon) Pushed:(epsilon) Current State:q6
Read : 8 Popped:(epsilon) Pushed:(epsilon) Current State:q6
Read : 9 Popped:(epsilon) Pushed:(epsilon) Current State:q6
Read : 0 Popped:(epsilon) Pushed:(epsilon) Current State:q6
REJECTED

Do you want to enter a string ? Y (yes) or N (no): N   
  
  
...Program finished with exit code 0   
Press ENTER to exit console.

screen shot :

main.cpp 1 #include«stdlib> #include iostream» 4 using namespace std 5 IIFunction Prototypes- 6 void DFA push (char); // Call

Thank You.

Add a comment
Know the answer?
Add Answer to:
Create the rules of a C.F.G. which generates the language of legal definitions for primitive variable...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a context-free grammar that generates the same language as regular expression which is ab*|c+ (Describe...

    Write a context-free grammar that generates the same language as regular expression which is ab*|c+ (Describe the four components of context-free grammar which are start symbol(S), non-terminals(NT), terminals(T), and set of production rules(P))

  • For the language anbn+mcm, where m, n 0.. a) Create a context-free grammar that generates this...

    For the language anbn+mcm, where m, n 0.. a) Create a context-free grammar that generates this language b) Create a pushdown automata that accepts this language.

  • 1. In a language, each sentence is a string starting with a capital letter followed by...

    1. In a language, each sentence is a string starting with a capital letter followed by two or more small letters and ending with three or more digits. Example sentences are Abc001, Zyxw9876 etc. a) Write a BNF grammar for the language. b) Show the derivation and the parse tree of the string Test3501. 2. Augment the following BNF to write an attribute grammar. Assume the only possible variable types are integer and float. The language rules are as follows:...

  • Consider the following DFA: А B Which of the following rules does NOT belong to the...

    Consider the following DFA: А B Which of the following rules does NOT belong to the right-regular grammar that generates the same language recognized by the above DFA? a. None of the other options B-10 b. B1B Ca Cờoc BOC ce

  • In this research, you are asked to write about php programming language, write a dummy paper...

    In this research, you are asked to write about php programming language, write a dummy paper to show all the required information about this language that might help readers with less knowledge to know how to use it. Use the following points to talk about your programming language: - The historical background when, how this language began and, what is the domain that it was used for. - The category of this language, according to Language categories in Chapter1. -...

  • Augment the following BNF to write an attribute grammar. Assume the only possible variable types are...

    Augment the following BNF to write an attribute grammar. Assume the only possible variable types are integer and float. The language rules are as follows: 1) The types of variables in an expression do not have to be the same. The type of the expression result is that of the first (or leftmost) variable in the expression. 2) If the type of variable to be assigned is integer, the type of the expression must be integer. 3) If the type...

  • Give a CFG that generates the language L(a*b*c*) \ { anbncn | n is a non-negative integer }. This...

    Give a CFG that generates the language L(a*b*c*) \ { anbncn | n is a non-negative integer }. This question is quite challenging; you will first need to devise a good strategy for how the CFG should work and then create the CFG to implement the strategy. You might want to do the other questions first. No messy writing please.

  • 1. Match each language to the typing model that best describes it. __ Ruby __ Prolog...

    1. Match each language to the typing model that best describes it. __ Ruby __ Prolog __ Scala __ lo A. static type checking with strong type safety B. dynamic type checking with strong (duck) type safety C. a prototype-based object model, uses dynamic typing. D. types are not specified, but when it compiles it will give errors if variables are not compatible 2. Match each language with it's corresponding strength __ Ruby __ Prolog __ Scala __ lo A....

  • JAVA :The following are descriptions of classes that you will create. Think of these as service...

    JAVA :The following are descriptions of classes that you will create. Think of these as service providers. They provide a service to who ever want to use them. You will also write a TestClass with a main() method that fully exercises the classes that you create. To exercise a class, make some instances of the class, and call the methods of the class using these objects. Paste in the output from the test program that demonstrates your classes’ functionality. Testing...

  • please use language R Question #1: (25 points) For this question, you will need to create...

    please use language R Question #1: (25 points) For this question, you will need to create several variables for use in later questions in this exam. Unless otherwise specified, you should provide summary statistics and interpret the mean, median, and range for each newly created variable. You should also comment on the normality (i.e., of the distribution of values) of each new variable. A) (6 points) Create a variable called subuse97 that is the sum of four other variables -...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT