hi i need help writing a program that will read in input file containing c++ code and then checks for grouping matching symbols like { } ,[ ] ,and ( ). if there is a mismatch symbol in the input file the the program will read and recognize the error.

i have a basic idea on where to start. i know how to implememtn the input file but am haing trouble using "stacks" to make this work. the program will ignore characters and only focus on mismatched symbols. for example:
if the input file is something like this:

int func4(int m, int n)
if (n==0)
return 0;
return m+func4(m,n-1}:

the error would by the mismtch symbols ( }

any help would be greatly appreciated!
thank you

7 Years
Discussion Span
Last Post by nucleon

>int func4(int m, int n)
Assuming you're working with a string, not an integer, you need to adjust your parameter list accordingly.

Your function should loop through the string until it either encounters a symbol or the end of the string is reached. If a new opening symbol is reached, it enters another level of recursion; if it encounters the closing symbol for itself, it should return successfully; if it's a closing symbol but not the one that opened the function, it should return an error since it's a mismatch. Finally, if the function encounters the end of the string, no closing symbol was found and it should return an error.


Consider this C++ (correct) module:

#include <iostream>
void f()
   ... balanced text ...
   std::cout << "} missed\n";
   ... balanced text ...
   /// @todo ( counter!

How about balances of {} and () without some kind of (simplified) C++ scanner (lexical analyzer)?..


Minimally you must handle single quotes, double quotes, single-line comments, and multi-line comments, since, as ArkM has pointed out, these constructs may contain parens (etc.) which you will want to ignore.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.