Parse Parentheses

Write a program to implement Algorithm 3-9, “Parse Parentheses,” matching
braces rather than parentheses. In your implementation, push the line
number into the stack rather than the opening brace. When an error
occurs, print the line number for the unmatched opening brace or
unmatched closing brace. Test your program by running the source code
through itself (there should be no errors)

Here's the algorithm

Algorithm parseParens
This algorithm reads a source program and parses it to make
sure all opening-closing parentheses are paired.
1 loop (more data)
1 read (character)
2 if (opening parenthesis)
1 pushStack (stack, character)
3 else
1 if (closing parenthesis)
1 if (emptyStack (stack))
1 print (Error: Closing parenthesis not matched)
2 else
1 popStack(stack)
3 end if
2 end if
4 end if
2 end loop
3 if (not emptyStack (stack))
1 print (Error: Opening parenthesis not matched)
end parseParens

Here's my code

#include<stdio.h>
#include<stdlib.h>

struct node{
int data;
struct node *next;
}*head;


void destroy(){
struct node *temp = head;
while(temp != NULL){
head = temp->next;
free(temp);
temp = head;
}
free(temp);
head = NULL;
}


void push(int num){
struct node *newnode;
newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = num;

if(head == NULL){
head = newnode;
newnode->next = NULL;
}
else{
newnode->next = head;
head = newnode;
}
}


int pop(){
struct node *temp;
int num;
temp = head;
if(temp == NULL){
return -1;
}
else{
head = temp->next;
num = temp->data;
free(temp);
}
return num;
}


void display(){
struct node *temp;
temp = head;
if(head == NULL)
printf("Stack is Empty\n");
else{
while(temp != NULL){
printf("%d\n", temp->data);
temp = temp->next;
}
}
}


void parenthesis(char *str){
int i, check;

destroy();

for(i=0; i<strlen(str); i++){
if(str[i] == '('){
push(5);
}
else if(str[i] == ')'){
check = pop();
if(check == -1){
printf("Closing parentheses not end\n");
return;
}
}
}
check = pop();
if(check != -1){
printf("Opening parentheses not matched\n");
}
else{
printf("Parentheses matched\n");
}
destroy();
}


int main()
{
int choice,number;

while(1){
printf("1. Push Stack\n");
printf("2. Pop Stack\n");
printf("3. Display Stack\n");
printf("4. Parsing Unmatched Parenthesis\n");
printf("\nEnter your choice : ");
scanf("%d", &choice);

if(choice == 1)
{
printf ("Enter a number to Push : ");
scanf ("%d", &number);
push(number);
display();
}
else if(choice == 2){
pop();
display();
}
else if(choice == 3){
display();
}
else if(choice == 4){
char str[100];
printf("Enter the Equation: ");
scanf("%s", &str);
parenthesis(str);
}

else
printf("Invalid choice\n");
}
return 0;
}


Last edited on
You can do something similar to make a code prettier, tracking indentions etc.

I prefer to do it with counters... increment on ( and decrement on ) for example, instead of using a full on stack for it.

Looks pretty good though, apart from being a touch overly complicated.


Last edited on
Topic archived. No new replies allowed.