Email: bknpk@hotmail.com Phone: +972-54-7649119


V

 

This page presents a program to check parenthesis validity in a c, cpp, perl etc... It is implemented as part of a c++ (cpp) series of exercises , that I did.

  1. As an exercise to study and practice c++, I decided to write a program, which checks the validity of parenthesis, curly braces etc. The rule says, that each open parenthesis must have a close one ((->).{->}).

  2. The file gets a configuration file as the first argument. This is shown in the following, triple single parenthesis, example:
    (;)
    {;}
    [;]

  3. The program builds a data base of parenthesis lookup. It uses a class as a basic data base item:

    1. class c_block_member {
    2. public:
    3.   c_block_member(string st_file_input, string::size_type pos);
    4.   string st_b_start;
    5.   string st_b_end;
    6. };
    7. c_block_member::c_block_member(string st_file_input, string::size_type pos) {
    8.   //(;)
    9.   st_b_start=st_file_input.substr(0, pos++);
    10.   st_b_end =st_file_input.substr(pos, st_file_input.size());
    11. }//c_block_member::c_block_member

    Each time a line is read from the configuration file, it is checked, parenthesis are extracted and stored in a list of c_block_member classes.

    1.   infile.open (argv[1], ios::in);
    2.   if(infile.is_open()) {
    3.     c_block_i=0;
    4.     while(getline(infile,st)) {
    5.       pos = st.find(';');
    6.       if(pos == string::npos) {
    7.         cout << "bad input " << st << endl;
    8.         return(0);
    9.       } else {
    10.         p = new c_block_member(st, pos);
    11.         l_block.push_back(*p);
    12.         c_block_i++;
    13.       }
    14.       if(c_block_i >= MAX_BLOCKS) {
    15.         cout << "too many block descriptors " << endl;
    16.         break;
    17.       }
    18.       delete p;
    19.     }//while
    20.   } else {
    21.     cout << "can not open " << argv[1] << " for reading parentheses data" << endl;
    22.   }


  4. To check the validity of the file another list of classes is used. It has a start and stop class members and some debug info (like line number and line content). I use two functions to add an element and its debug info, when an open parentheses is encountered. Once an end one is matched, it must be the one which closes the last element in the list.

    1.   if(s_start.compare(l_c_parentheses.back().st_p_start) == 0) {//equal
    2.     l_c_parentheses.pop_back();
    3.   } else {
    4.     cout << "error for parentheses " << st << endl;

    The file is read line by line. Each line is processed character by character using string iterator:

    1.       while(getline(infile,st)) {
    2.       for(s_it = st.begin(); s_it != st.end(); ++s_it) {//extract one char from line
    3.         for(it=l_block.begin(); it != l_block.end(); it++) {
    4.           sc=*s_it;
    5.           if(sc.compare(it->st_b_end) == 0) check_p_end(sc);


  5. The code uses assert statement to stop on error. For instance an end parentheses must have a start one:

    1. //Get the start parentheses
    2. string s_start;
    3. for(b_it=l_block.begin(); b_it != l_block.end(); b_it++) {
    4.   if(b_it->st_b_end == st) {
    5.     s_start = b_it->st_b_start;
    6.     break;
    7.   }
    8. }
    9. assert(s_start.size() != 0);


  6. Some tips for debugging the program using gdb:

    b parentheses.cpp:104
    r parentheses.txt parentheses.cpp
    printf "%s", st._M_dataplus._M_p

    It is also useful to create a gdb file with common used commands. In this simple case, run is always used with c and c++ parentheses file input. A break point plus print commands to be exectuted on break are also added.

    define r1
      run parentheses.txt $arg0
    end
    b parentheses.cpp:151
    b parentheses.cpp:158
    command 1
    p p
    end
    #can be replaced by
    define d1
      display p
    end

    #gdb --command=parentheses_gdb.txt a.out
    Now you only need to type the cpp input file:
    r1 program.cpp

  7. The code of the c++ parentheses.cpp is available on this site.

  ...



Home

some memory VHDL models


A simple c-code program to read a memory array from verilog RTL, using VPI.



Download Area







Search This Site


Feedback This Site




new pages on this site