Question

Pascal programming language Implement a symbol balance checker function for the Pascal programming language. Pascal allows...

Pascal programming language

Implement a symbol balance checker function for the Pascal programming language. Pascal allows for the following pairs: {}, (), [], begin end . All programs will begin with the word "begin" and end with the word "end". Your function should receive an ifstream object which is already open and will return true, all of the symbols match, or false, they do not. You do not have to worry about comments in the program but you do have to avoid other parts of the program's code such as assignment statements (x=y) and other expressions.

For example, Input ' ({}[])() ' is balanced

while ' (({]' is not

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

//using linked list

#include<stdio.h>
#include<stdlib.h>
struct link{
    char data;
    struct link *next;
};
int c=0;
struct link *head=NULL;
void push(char ele)
{
struct link *curr,*temp;
curr=(struct link *)malloc(sizeof(struct link));
curr->data=ele;
curr->next=NULL;
c++;
if(head==NULL)
head=curr;
else
{
   temp=head;
   while(temp->next!=NULL)
   {
        temp=temp->next;
   }
       temp->next=curr;
}
}
void pop()
{
    struct link *temp,*curr;
    if(head==NULL)
       printf("no elements to delete\n");
   else
   {
    temp=head;
    while(temp->next!=NULL)
    {
        curr=temp;
        temp=temp->next;
    }
    curr->next=NULL;
    c--;
}

}
char peep()
{
   struct link *temp;
   temp=head;
   if(temp==NULL)
      return 'p';
   else
   {
  
   while(temp->next!=NULL)
      temp=temp->next;
      return temp->data;
   }
}
main()
{
    char str[100];
    printf("enter expression\n");
    scanf("%s",str);
    int i=0;
   // printf("%s\n",str);
    while(str[i]!='\0')
    {
       if(str[i]=='('||str[i]=='{'||str[i]=='[')
           push(str[i]);
        else if(str[i]==')')
        {
           if(peep()=='(')
              pop();
           else
           {
          
               printf("wrong statement\n");
               exit(0);
           }
        }
        else if(str[i]==']')
        {
           if(peep()=='[')
              pop();
           else
           {
              
               printf("wrong statement\n");
               exit(0);
              }
        }
         else if(str[i]=='}')
        {
           if(peep()=='{')
              pop();
           else
           {
              
               printf("wrong statement\n");
               exit(0);
           }
        }
       i++;  
                             
    }
         // printf("%c\n",str[i]);
          //printf("%d\n",c);
          if(c==0&&str[i]!='}'&&str[i]!=')'&&str[i]!=']')
            printf("correct\n");
          else
             printf("incorrect\n");
                    

  
}

// here I used linked list but we can slove this by stack

// Java program for checking
// balanced Parenthesis

public class BalancedParan
{
   static class stack
   {
       int top=-1;
       char items[] = new char[100];

       void push(char x)
       {
           if (top == 99)
           {
               System.out.println("Stack full");
           }
           else
           {
               items[++top] = x;
           }
       }

       char pop()
       {
           if (top == -1)
           {
               System.out.println("Underflow error");
               return '\0';
           }
           else
           {
               char element = items[top];
               top--;
               return element;
           }
       }

       boolean isEmpty()
       {
           return (top == -1) ? true : false;
       }
   }
  
   /* Returns true if character1 and character2
   are matching left and right Parenthesis */
   static boolean isMatchingPair(char character1, char character2)
   {
   if (character1 == '(' && character2 == ')')
       return true;
   else if (character1 == '{' && character2 == '}')
       return true;
   else if (character1 == '[' && character2 == ']')
       return true;
   else
       return false;
   }
  
   /* Return true if expression has balanced
   Parenthesis */
   static boolean areParenthesisBalanced(char exp[])
   {
   /* Declare an empty character stack */
   stack st=new stack();
  
   /* Traverse the given expression to
       check matching parenthesis */
   for(int i=0;i<exp.length;i++)
   {
          
       /*If the exp[i] is a starting
           parenthesis then push it*/
       if (exp[i] == '{' || exp[i] == '(' || exp[i] == '[')
           st.push(exp[i]);
  
       /* If exp[i] is an ending parenthesis
           then pop from stack and check if the
           popped parenthesis is a matching pair*/
       if (exp[i] == '}' || exp[i] == ')' || exp[i] == ']')
       {
              
           /* If we see an ending parenthesis without
               a pair then return false*/
           if (st.isEmpty())
           {
               return false;
           }
  
           /* Pop the top element from stack, if
               it is not a pair parenthesis of character
               then there is a mismatch. This happens for
               expressions like {(}) */
           else if ( !isMatchingPair(st.pop(), exp[i]) )
           {
               return false;
           }
       }
          
   }
      
   /* If there is something left in expression
       then there is a starting parenthesis without
       a closing parenthesis */
      
   if (st.isEmpty())
       return true; /*balanced*/
   else
       { /*not balanced*/
           return false;
       }
   }
  
   /* UTILITY FUNCTIONS */
   /*driver program to test above functions*/
   public static void main(String[] args)
   {
       char exp[] = {'{','(',')','}','[',']'};
       if (areParenthesisBalanced(exp))
           System.out.println("Balanced ");
       else
           System.out.println("Not Balanced ");
   }

}

Add a comment
Know the answer?
Add Answer to:
Pascal programming language Implement a symbol balance checker function for the Pascal programming language. Pascal allows...
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
  • IMPLEMENT IN C++ Implement a symbol balance checker function for the Pascal programming language. Pascal allows...

    IMPLEMENT IN C++ Implement a symbol balance checker function for the Pascal programming language. Pascal allows for the following pairs: {}, (), [], begin end . All programs will begin with the word "begin" and end with the word "end". Your function should receive an ifstream object which is already open and will return true, all of the symbols match, or false, they do not. You do not have to worry about comments in the program but you do have...

  • GENERAL INSTRUCTIONS              All requirements specified on page 64-67 of the course packet “2.5 Programming Assignment...

    GENERAL INSTRUCTIONS              All requirements specified on page 64-67 of the course packet “2.5 Programming Assignment Submission Requirements” and “2.6 Flow Chart Symbols” should be followed.              Plan the programs on paper before you attempt to write any programs (it will take less time to complete the assignment overall).              Electronic version of your programs (the .m files you create) must be uploaded to Canvas. Attach multiple files to one submission. All files must be received by the beginning of...

  • Please solve the following problem with programming using proper data structures. (Programming Language: Python) A similar...

    Please solve the following problem with programming using proper data structures. (Programming Language: Python) A similar application to the parentheses matching problem comes from hypertext markup language (HTML). In HTML, tags exist in both opening and closing forms and must be balanced to properly describe a web document. This very simple HTML document: Example> Hello, world is intended only to show the matching and nesting structure for tags in the language. Write a program that can check an HTML document...

  • PROGRAM DESCRIPTION: In this assignment, you will be creating a memory matching game in C++. In t...

    c++ PROGRAM DESCRIPTION: In this assignment, you will be creating a memory matching game in C++. In this game, the user will need to match up the pairs symbols A,B,C,D,E on a 4x4 array. For example, the array could be initialized like the following: In this case, X represents an empty slot. The goal is for the user to match the A to the A, the B to the B, etc, until all pairs are matched up to win the...

  • Question 1 The following function header represents which programming language? double calc_weekly_pay( double hours_worked, double pay_rate)...

    Question 1 The following function header represents which programming language? double calc_weekly_pay( double hours_worked, double pay_rate) Question 1 options: Python A C-based language FORTRAN COBOL Question 2 One of my favorite quotes from Jeanette Wind is "Computational thinking is using abstraction and decomposition when attacking a large complex task or designing a large complex system." This supports using _____________ when developing programs. Question 2 options: Repetition structures Assignment statements Selection structures Functions Question 3 The following assignment statement exhibits which...

  • Using C++ language, write a code to implement the "evaluate function" described below using the given...

    Using C++ language, write a code to implement the "evaluate function" described below using the given poly.cpp file and poly.h header file. (ignore the other 3 functions if asked) poly.cpp file: poly.h header file: You are asked to implement four functions for a data structure used to store polynomials. The data structure used for polynomials is a linked list that consists of terms where each term consists of a coefficient, an exponent and a pointer to the next term. A...

  • Programming language C. A small U-Pick farm sells five different U-Pick citrus fruit products whose retail...

    Programming language C. A small U-Pick farm sells five different U-Pick citrus fruit products whose retail prices are: 1. Sugarbells - $1.99/lb, 2. Honeybells - $2.39/lb, 3. Red Grapefruit $1.69/lb, 4. Navel Oranges - $1.49/lb, 5. Pomelo - $1.89/lb. Write a program that allows the user to select one or more products, input the weights of each product, and calculate the total amount due. The farm only accepts cash. Your program will take input of the cash received and calculate...

  • The last 3 cases are tests .cpp files to test the code. The language of this...

    The last 3 cases are tests .cpp files to test the code. The language of this code must be C++ because that is the only I am familiar with. Soreland, a software company, is planning on releasing its first C++ compiler with the hopes of putting MiniSoft's Visual C++ out of business (that reminds me of another story). Consequently, Soreland has contracted with the Fruugle Corporation to design and build part of their compiler. Of course, since Fruugle gets all...

  • C programming is the main one and pick another code of choice!!! Background This assignment is...

    C programming is the main one and pick another code of choice!!! Background This assignment is based on one of the Big Ideas in this course, namely that reading C source code is a real aid in learning how to write C code. To that end, in this project, you write a program that scans C source from the 'Net and calculates some simple statistics. We're learning lots of concepts, and by running the program you write here on several...

  • In this assignment you’ll implement a data structure called a trie, which is used to answer...

    In this assignment you’ll implement a data structure called a trie, which is used to answer queries regarding the characteristics of a text file (e.g., frequency of a given word). This write-up introduces the concept of a trie, specifies the API you’re expected to implement, and outlines submission instructions as well as the grading rubric. Please carefully read the entire write-up before you begin coding your submission. Tries A trie is an example of a tree data structure that compactly...

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
ADVERTISEMENT