How to check open and closing parenthesis sets in MIT-Scheme for Meep

279 Views Asked by At

Scheme language is used to build control files for the program, Meep. Almost all my Meep control file errors result from mismatched parenthesis which Meep recognizes after the control file is completely processed, that is, at the end of the control file and not very helpful.

Checking parenthesis sounds easy, except in this application a statement can run half a page long with 50 to 100 sets of nested parenthesis so eyeball checking is very difficult. Further, writing an incorrectly formatted statement (missing a ")" ) is not detected if an extra ")" occurs elsewhere within the statement but of course Meep does not perform as expected in that case.

There exists certain structures with-in Meep control files for which all parenthesis must be closed, (!set ... (make ... (list ... (run ..., for examples. I want to write a preprocessor program in C++ that checks for closed "(...)" sets within those and other statements in my control files. I've a start, but am stuck on the logic of a general approach.

I have progressed from the original code, it now looks as follows. The Mit-Scheme file is now written into a buffer successfully. Should I try to search the buffer multiple times sequentially to locate the key words then calculate their order, or should I search the buffer in parallel, looking for the next key word?

A functional Meep control statement follows:

   // read a file into memory
#include <iostream>     // std::cout
#include <fstream>      // std::ifstream
#include <string>
using namespace std;

#include <vector>
#include <limits>
void pause() {
cout << "Press ENTER to continue...";
cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
     }

int main () {
std::string name;
std::string quit = "quit";
std::ifstream is;
do
{
std::string quit = "quit";
std::string name;
std::cout << "Please, enter your file name: --or quit: ";
std::getline (std::cin,name);
if(name==quit) return 1;
std::cout << "Hello, " << name << '\n';
pause();
is.open(name.c_str(),ios::in | std::ifstream::binary); }
while (!is) ;


// get length of file:
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);

// allocate memory:
char * buffer = new char [length];

// read data as a block:
is.read (buffer,length);

is.close();

// print content:
std::cout.write (buffer,length);

delete[] buffer;

return 0;

I am using Ubuntu 14.10 OS and g++ compiler.

What code logic best suited to scanning each line of the control file, ignoring white space and comments (from ; to eol) detecting "(" and ")" within statements and outputting a line number (or the line) when the condition of a required ")" arises?

Here is one statement.

(set! geometry
  (if no-bend?
      (list
       (make block
         (center 0 wvg-ycen)
         (size infinity w infinity)
         (material (make dielectric (epsilon 12)))))
      (list
       (make block
         (center (* -0.5 pad) wvg-ycen)
         (size (- sx pad) w infinity)
         (material (make dielectric (epsilon 12))))
       (make block
         (center wvg-xcen (* 0.5 pad))
         (size w (- sy pad) infinity)
         (material (make dielectric (epsilon 12)))))))

Oh, and I actually misspoke by writing that white space is ignored. It is not. Empty lines of white space are OK, but the control file and I think Mit-Scheme requires a white space separating things. Key words, variables and closing and opening parenthesis must be separated by a space in many circumstances, but I can't say "all" circumstances. Just for your information, I'm not concerned with this level of detail at this time.

0

There are 0 best solutions below