25. oktober 2002 - 21:10
Der er
3 kommentarer og
1 løsning
Rekusvis nedstigning C++
Hej Eksperter!
Jeg skal lave en kode til parsning (rekursiv nedstigning), men jeg fatter % is C++..
Nogen der kan hjælpe på vej eller nogen der haft en tillsvarende opgave!??
25. oktober 2002 - 21:22
#1
Jeg lavede følgende på mit forårssemester:
//---------------------------------------------------------------------------
#ifndef ParseNodeH
#define ParseNodeH
#include<iostream>
#include<cstring.h>
#include "TreeNode.h"
//---------------------------------------------------------------------------
using namespace std;
class ParseNode{
private:
public:
ParseNode(string c):name(c){left=0;right=0;center=0;}
string name;
ParseNode * right;
ParseNode * center;
ParseNode * left;
int ruleNo;
virtual TreeNode * toSyntax()=0;
friend ostream & operator <<(ostream & out, ParseNode & p);
};
class ParseNodeS: public ParseNode{
private:
public:
ParseNodeS(string c):ParseNode(c){}
virtual TreeNode * toSyntax(){
if(right->ruleNo==2){
return left->toSyntax();
}
else
{
TreeNode * x = new TreeAddNode(0,left->toSyntax(),right->toSyntax());
return x;
}
}
};
class ParseNodeSPrime: public ParseNode{
private:
public:
ParseNodeSPrime(string c):ParseNode(c){}
virtual TreeNode * toSyntax(){
if(ruleNo==2) return 0;
if(ruleNo==1){
if(right->ruleNo==2){
return center->toSyntax();}
else{
TreeNode * x=new TreeAddNode(0,center->toSyntax(),right->toSyntax());
return x;
}
}
}
};
class ParseNodeA: public ParseNode{
private:
public:
ParseNodeA(string c):ParseNode(c){}
virtual TreeNode * toSyntax(){
if(right->ruleNo==2){
return left->toSyntax();
}
else
{
TreeNode * x = new TreeMultNode(0,left->toSyntax(),right->toSyntax());
return x;
}
}
};
class ParseNodeAPrime: public ParseNode{
private:
public:
ParseNodeAPrime(string c):ParseNode(c){}
virtual TreeNode * toSyntax(){
if(ruleNo==2){return 0;}
if(ruleNo==1){
if(right->ruleNo==2){
return center->toSyntax();
}
else{
TreeNode * x=new TreeMultNode(0,center->toSyntax(),right->toSyntax());
return x;
}
}
}
};
class ParseNodeB: public ParseNode{
private:
public:
ParseNodeB(string c):ParseNode(c){}
virtual TreeNode * toSyntax(){
if(right->ruleNo==2){
return left->toSyntax();
}
else{
TreeNode * x= new TreeExpNode(0,left->toSyntax(),0);
return x;
}
}
};
class ParseNodeBPrime: public ParseNode{
private:
public:
ParseNodeBPrime(string c):ParseNode(c){}
virtual TreeNode * toSyntax(){
return 0;
}
};
class ParseNodeC: public ParseNode{
private:
public:
ParseNodeC(string c):ParseNode(c){}
virtual TreeNode * toSyntax(){
if(ruleNo==1){return center->toSyntax();}
else if(ruleNo==2){return new TreeLeafNode(0,'a');}
else if (ruleNo==3){return new TreeLeafNode(0,'b');}
else {return new TreeEmptyLeafNode(0);}
}
};
class ParseNodeEmpty: public ParseNode{
private:
public:
ParseNodeEmpty(string c):ParseNode(c){}
virtual TreeNode * toSyntax(){return 0;}
};
class ParseNodeTerminal: public ParseNode{
private:
public:
ParseNodeTerminal(string c):ParseNode(c){}
ParseNodeTerminal(char c):ParseNode("_"){name[0]=c;}
virtual TreeNode * toSyntax(){
if (name[0]=='a' || name[0]=='b')
return new TreeLeafNode(0,name[0]);
else
return new TreeEmptyLeafNode(0);
}
};
#endif
------------------------
//---------------------------------------------------------------------------
#ifndef RecursiveDescentParserH
#define RecursiveDescentParserH
#include<string>
#include<iostream>
#include "ParseNode.h"
//---------------------------------------------------------------------------
class RecursiveDescentParser{
public:
string parseString;
char lookahead;
int index;
bool match(char c,ParseNode * & x){
if(lookahead==c){
cout<<"her matches "<<c<<endl;
x=new ParseNodeTerminal(c);
cout<<"her er navn på terminal node "<<x->name[0]<<endl;
index++;
lookahead=parseString[index];
return true;
}
else{
return false;
}
}
void error(){
cout<<"Error in prsing symbol: "<<lookahead<<endl;}
void s(ParseNode * x){
cout<<"SNODE s navn "<<x->name<<endl;
x->ruleNo=1;
x->left=new ParseNodeA("A");
x->right=new ParseNodeSPrime("S'");
// cout<<"S->AS"<<endl;
a(x->left);
sPrime(x->right);
}
void sPrime(ParseNode * x){
cout<<"S'NODE s navn "<<x->name<<endl;
if(match('+',x->left)){
x->center=new ParseNodeA("A");
x->right =new ParseNodeSPrime("S'");
x->ruleNo=1;
// cout<<"S'->+AS'"<<endl;
a(x->center);
sPrime(x->right);
}
else{
x->left= new ParseNodeEmpty("E");
x->ruleNo=2;
//cout<<"S'->epsilon"<<endl;
}
}
void a(ParseNode * x){
x->ruleNo=1;
x->left=new ParseNodeB("B");
x->right=new ParseNodeAPrime("A'");
// cout<<"A->BA'"<<endl;
b(x->left);
aPrime(x->right);
}
void aPrime(ParseNode * x){
if(match('*',x->left)){
x->center= new ParseNodeB("B");
x->right= new ParseNodeAPrime("A'");
x->ruleNo=1;
//cout<<"A'->*BA'"<<endl;
b(x->center);
aPrime(x->right);
}
else{
x->left= new ParseNodeEmpty("E");
x->ruleNo=2;
//cout<<"A'->epsilon"<<endl;
}
}
void b(ParseNode * x){
x->ruleNo=1;
x->left= new ParseNodeC("C");
x->right= new ParseNodeBPrime("B'");
//cout<<"B->CB'"<<endl;
c(x->left);
bPrime(x->right);
}
void bPrime(ParseNode * x){
x->ruleNo=1;
if(match('^', x->left)){
x->right=new ParseNodeBPrime("B'");
// cout<<"B'->^B'"<<endl;
x->ruleNo=1;
bPrime(x->right);
}
else{
x->left= new ParseNodeEmpty("E");
x->ruleNo=2;
//cout<<"B'->epsilon"<<endl;
}
}
void c(ParseNode* x){
if(match( '(',x->left ) ){
x->center= new ParseNodeS("S");
//cout<<"C->(S)"<<endl;
x->ruleNo=1;
s(x->center);
match(')', x->right);
}
else if(match('a',x->left)){
x->left = new ParseNodeTerminal("a");
//cout<<"C->a"<<endl;
x->ruleNo=2;
}
else if(match('b',x->left)){
x->left = new ParseNodeTerminal("b");
//cout<<"C->b"<<endl;
x->ruleNo=3;
}
else if(match('e',x->left)){
x->left = new ParseNodeTerminal("e");
//cout<<"C->b"<<endl;
x->ruleNo=4;
}
else{
system("pause");
error();
}
}
RecursiveDescentParser(){}
ParseNode * parse(string ps){
parseString=ps;
index=0;
lookahead=parseString[index];
cout<<"lookahead0: "<<lookahead<<endl;
ParseNode * result=new ParseNodeS("S");
s(result);
return result;
}
};
#endif