You should take the first steps yourself, based on TheIdeasMan's suggestions. But I wouldn't class helios' LALR parser as
really simple in a general sense (it's not beginners' code!), though it is pretty much as basic as you can get and still be an LALR parser (all it does is basic arithmetic.)
If you restrict your parser to deal with one simple statement per line then you can make the parsing far simpler (just a sequence of if statements) so you can get something up and running quickly. Then you can replace the parser with a proper one later on.
And if you make your language an interpreted scripting language to start with, rather than a compiled one, you can make things even easier. And get immediate feedback about what's going on, too.
Out of interest, what do you know about parsing so far? If you're new to it, you might want to check out this site:
Introduction to Parsing
http://www.mollypages.org/page/grammar/index.mp
Andy
PS I think you might need to brush up your C++ a bit before continuing...
Where hello.txt is just
the output of the adjusted code below is
What file do you want to compile?hello.txt
"Hello, world!"
<return> to exit |
Adjusted code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
// MAX_PATH is a MSVC specific define
#ifndef MAX_PATH
#define MAX_PATH 260
#endif
char filename[MAX_PATH + 1];
char input_line[MAX_PATH + 1];
int argvcount;
string randomaccess;
int i;
int main(int argc, char *argv[]) {
cout << "What file do you want to compile?";
cin.getline(filename, 100);
ifstream file_in(filename);
argvcount = 0;
i = 0;
// DOES NOT COMPUTE!
//while( i <= argc) {
while(!file_in.eof()) { // NOT THE BEST WAY... (do some readin up!)
i++;
argvcount++;
file_in.getline(input_line, MAX_PATH);
// 1. not the way to compare a C null-terminated string
// 2. why are you comparing it with the command line arg?
// rather than the line you're just read?
//if (argv[argvcount] == "print") {
// use strncmp to check first 6 chars of input line (not a robust approach!)
// http://www.cplusplus.com/reference/cstring/strncmp/
if (strncmp(input_line, "print ", 6) == 0) {
argvcount++;
// why are you using the command line arg here?
//randomaccess = argv[argvcount];
// JUST FOR ILLUSTRATION (need to do proper string parsing)
randomaccess = input_line + 6; // length of print plus one for space
cout << randomaccess << "\n"; // added newline
}
}
cout << "<return> to exit\n"; // let user know what to do
cin.get();
return 0;
}
|