预处理器做了一半大概 把;单行注释 和#! !#多行注释 在ClearRemark中实现
然后在PreProduce中处理宏,由于scheme的宏支持多态,和递归,我暂时只做了单宏
宏储存在map中,内容是一个结构 中一个链表作为 参数的链表
好了代码如下 ,有部分注释:
WordReader.h:
#include <iostream>
#include <sstream>
#include <cstring>
#include <map>
#include <list>
/*
WordReader By InsZVA
*/
typedef struct
{
std::list<std::string> params;
std::string defination;
}syntax;
class WordReader
{
private:
std::string str;
std::string err;
std::map<std::string, syntax> SyntaxMap;
bool ClearRemark();
bool PreProcedure()
{
std::string r;
bool result= ClearRemark();
if (result)std::cout << "------------------------------------STEP1--------------------------------" << std::endl << str<<std::endl;
else {
std::cout << err; return false;
}
result = InitSyntax();
if (result)std::cout << "------------------------------------STEP2--------------------------------" << std::endl << str<<std::endl;
else {
std::cout << err; return false;
}
return true;
}
bool InitSyntax();
public:
WordReader(char * s)
{
str = s;
}
WordReader(std::string& s)
{
str = s;
}
void Procedure()
{
PreProcedure();
}
~WordReader()
{}
};
class IdenticalTable
{
private:
static std::map<int, void*> im;
};
WordReader.cpp:
#include "wordreader.h"
#include "ErrorCode.h"
#define EOFERROR(s1,s2) if(is.eof()){std::ostringstream eos; eos << s1 << s2 << std::endl; err = eos.str(); return false;}
#define ASSUMEMATCH(s,e) EOFERROR(P003,name);is >> in; if (in != s){ std::ostringstream eos; eos << e << name << std::endl; err = eos.str(); return false;}
bool WordReader::ClearRemark()
{
if (str.empty())return false;
std::istringstream is(str);
std::ostringstream os;
char c1, c2;
int line = 1;
is.get(c2);
do{
c1 = c2;
if (is.eof()){
break;
}
if (c1 == 10)line++;
is.get(c2);
if (c1 == ';')
{
is.ignore(1024, 10);
}
else if (c1 == '#'&&c2 == '!')
{
is.get(c2);
while (c1 != '!' || c2 != '#')
{
c1 = c2;
if (is.eof())
{
std::ostringstream eos;
eos << P001 << line << std::endl;
err = eos.str();
return false;
}
is.get(c2);
}
is.get(c2);
}
else
{
switch (c1)
{
case '(':
case ')':
os << " " << c1 <<" ";
break;
default:
os << c1;
}
}
} while (1);
str = os.str();
return true;
}//2015/4/7
bool WordReader::InitSyntax()
{
std::string in;
std::string pre;
std::string name;
syntax syntax_;
SyntaxMap.clear();
if (str.empty())return false;
std::istringstream is(str);
std::ostringstream os;
while (!is.eof())
{
is >> in;
if (in == "define-syntax")
{
is >> name;
EOFERROR(P003, name);
ASSUMEMATCH("(",P002);
ASSUMEMATCH("syntax-rules", P002);
ASSUMEMATCH("(", P002);
ASSUMEMATCH(")", P002);
ASSUMEMATCH("(", P002);
ASSUMEMATCH("(", P002);
ASSUMEMATCH(name, P002); //Modern Match Start
EOFERROR(P003, name);
is >> in;
while (in != ")")
{
syntax_.params.push_back(in);
EOFERROR(P003, name);
is >> in;
} //Params Match OK
std::ostringstream cs; //Context(Modern) Match Start
int i_match=0;
while (i_match!=-1)
{
EOFERROR(P004, name);
is >> in;
cs << in;
if (in == "(")i_match++;
if (in == ")")i_match--;
}
syntax_.defination = cs.str();
SyntaxMap[name]=syntax_; //Context Match OK
ASSUMEMATCH(")", P005); //Rules Match OK
ASSUMEMATCH(")", P006);
EOFERROR(P006, name);
continue;
}
os << " " << in << " ";
}
str = os.str();
return true;
}//2015/4/8
ErrorCode.h:
#define P001 "PreProduce Error(P001):Can not find !# pairs the #! in line:"
#define P002 "PrePeoduce Error(P002):An error was made in the rule of syntax:"
#define P003 "PrePeoduce Error(P003):Unexpected EOF in producing the rule of syntax:"
#define P004 "PrePeoduce Error(P004):Unexpected EOF in producing the contex of syntax:"
#define P005 "PrePeoduce Error(P005):Cannot Find ) to end the rule of syntax:"
#define P006 "PrePeoduce Error(P006):Cannot Find ) to end the syntax:"
test.cpp:
#include "wordreader.h"
#include <iostream>
using namespace std;
int main()
{
string s;
s = " ( define-syntax start ( syntax-rules ( ) ((start exp1)#!asdfas!# exp1 ) ) ) ";
cout << "------------------Sample Input----------------" << endl << s << endl;
WordReader wr(s);
wr.Procedure();
cin >> s;
return 0;
}
文章及代码版权归InsZVA所有,转载请注明住处