patterncppMinor
C++ program that recognizes a context-free language
Viewed 0 times
freeprogramlanguagerecognizesthatcontext
Problem
This program (C++) was for a class project. A program that recognizes a given context-free language by implementing a pushdown-automaton (PDA).
The PDA recognizes the following language:
S → $T$
T → T+T | T-T | T*T | T/T | (T) | CX
X → XX | C | N | _ | e
C → a | b | c | · · · | z
N → 0 | 1 | 2 | · · · | 9
The program passes all test-cases. I have documented the code and host it on GitHub as well. Here is the project link and readme file.
Out of desire to improve my programming skills, I'm seeking advice to improve my coding, optimization, … any advice that might help advance my programming skills.
```
#include
#include
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 Epsi
The PDA recognizes the following language:
S → $T$
T → T+T | T-T | T*T | T/T | (T) | CX
X → XX | C | N | _ | e
C → a | b | c | · · · | z
N → 0 | 1 | 2 | · · · | 9
The program passes all test-cases. I have documented the code and host it on GitHub as well. Here is the project link and readme file.
Out of desire to improve my programming skills, I'm seeking advice to improve my coding, optimization, … any advice that might help advance my programming skills.
```
#include
#include
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 Epsi
Solution
Your code lacks encapsulation
Your Lexer/Parser should really be a class that contains its own state. It means that you can encapsulate initialization and tidy up into well know concrete places. Hide private implementation details and use some standard libraries.
Should use standard libraries more:
Your generic stack could be implemented using one of many standard containers.
Don't do this
See: Why is “using namespace std;” considered bad practice?
Your Lexer/Parser should really be a class that contains its own state. It means that you can encapsulate initialization and tidy up into well know concrete places. Hide private implementation details and use some standard libraries.
Should use standard libraries more:
Your generic stack could be implemented using one of many standard containers.
Don't do this
using namespace std;See: Why is “using namespace std;” considered bad practice?
Code Snippets
using namespace std;Context
StackExchange Code Review Q#84300, answer score: 2
Revisions (0)
No revisions yet.