#include "btparser.h"
#include "exception.h"
-BTParser::BTParser(ILexer* lxer) : lexer(lxer), current(0)
+BTParser::BTParser() : current(0)
{
}
BTParser::~BTParser()
{
- if(lexer != NULL)
- {
- delete lexer;
- // Input stream was deleted with the lexer so null it out
- IParser::setInput((istream*)NULL);
- }
-}
-
-void BTParser::setInput(char* in)
-{
- IParser::setInput(in);
- lexer->setInput(in);
-}
-
-void BTParser::setInput(string& in)
-{
- IParser::setInput(in);
- lexer->setInput(in);
-}
-
-void BTParser::setInput(istream* in)
-{
- IParser::setInput(in);
- lexer->setInput(in);
}
void BTParser::consume(void)
#include <exception>
#include <vector>
#include "iparser.h"
-#include "ilexer.h"
-#include "ast.h"
class BTParser : public IParser
{
- private:
- ILexer* lexer;
+ protected:
unsigned int current;
std::vector<unsigned int> markers;
std::vector<Token> lookahead;
public:
- BTParser(ILexer* lxer);
+ BTParser();
~BTParser();
- void setInput(char* in);
- void setInput(string& in);
- void setInput(istream* in);
-
void consume(void);
void sync(unsigned int i);
void fill(unsigned int n);
void release(void);
void seek(unsigned int index);
bool isSpeculating(void);
-
- virtual AST* parse(void) = 0;
};
#endif
/******************************************************************************
* Public Functions
*****************************************************************************/
-IParser::IParser() : input(NULL)
+IParser::IParser() : result(NULL), lexer(NULL)
+{
+}
+
+IParser::IParser(ILexer* lxr) : result(NULL), lexer(lxr)
{
}
IParser::~IParser()
{
- if(input != NULL)
+ if(lexer != NULL)
+ {
+ delete lexer;
+ }
+
+ if(result != NULL)
{
- delete input;
+ delete result;
}
}
-void IParser::setInput(char* in)
+void IParser::input(ILexer* lxr)
{
- input = new istringstream( string( in ) );
+ lexer = lxr;
}
-void IParser::setInput(string& in)
+const AST* IParser::ast() const
{
- input = new istringstream( in );
+ return result;
}
-void IParser::setInput(istream* in)
+void IParser::process(IVisitor& visitor)
{
- input = in;
+ visitor.visit(result);
}
-
-
#include <string>
#include <sstream>
#include "ast.h"
+#include "ilexer.h"
#include "ivisitor.h"
using namespace std;
class IParser {
- private:
- istream* input;
+ protected:
+ AST* result;
+ ILexer* lexer;
public:
IParser();
+ IParser(ILexer* in);
virtual ~IParser();
- virtual AST* parse() = 0;
-
- void setInput(char* in);
- void setInput(string& in);
- void setInput(istream* in);
+ virtual void parse() = 0;
+ virtual void input(ILexer* in);
+ virtual const AST* ast() const;
+ virtual void process(IVisitor& visitor);
};
#endif
LLKParser::~LLKParser()
{
- if(lexer != NULL)
- {
- delete lexer;
- // Input stream was deleted with th elexer so null it out
- IParser::setInput((istream*)NULL);
- }
if (lookahead != NULL)
{
delete[] lookahead;
}
}
-void LLKParser::setInput(char* in)
-{
- IParser::setInput(in);
- lexer->setInput(in);
- for (int i = 0; i < k; i++)
- {
- consume();
- }
-}
-
-void LLKParser::setInput(string& in)
-{
- IParser::setInput(in);
- lexer->setInput(in);
- for (int i = 0; i < k; i++)
- {
- consume();
- }
-}
-
-void LLKParser::setInput(istream* in)
-{
- IParser::setInput(in);
- lexer->setInput(in);
- for (int i = 0; i < k; i++)
- {
- consume();
- }
-}
-
void LLKParser::consume(void)
{
if ( lookahead != NULL )
public:
LLKParser(int k_val, ILexer* lxer);
~LLKParser();
-
- void setInput(char* in);
- void setInput(string& in);
- void setInput(istream* in);
-
void consume(void);
void match(TokenType_T type);
Token& lookaheadToken(int i);
TokenType_T lookaheadType(int i);
- virtual AST* parse(void) = 0;
};
#endif
+#include <iostream>
#include "astprinter.h"
-using namespace std;
-
-string ASTPrinter::str()
-{
- return stream.str();
-}
void ASTPrinter::beforeVisit(AST* cur, int depth)
{
void ASTPrinter::afterVisit(AST* cur, int depth)
{
- stream << endl;
+ std::cout << endl;
}
void ASTPrinter::beforeChildren(AST* cur, int depth)
{
- stream << "(" << cur->type() << " " << cur->text();
+ std::cout << "(" << cur->type() << " " << cur->text();
}
void ASTPrinter::afterChildren(AST* cur, int depth)
{
- stream << ")";
+ std::cout << ")";
}
void ASTPrinter::beforeChild(AST* cur, int depth)
{
- stream << endl;
+ std::cout << endl;
for(int i = 0; i< depth; i++)
{
- stream << " ";
+ std::cout << " ";
}
}
#include <sstream>
class ASTPrinter : public IVisitor {
- protected:
- ostringstream stream;
- public:
- ASTPrinter(AST* root) : IVisitor(root) {};
- string str();
private:
void beforeVisit(AST* cur, int depth);
void afterVisit(AST* cur, int depth);
using namespace std;
+IVisitor::IVisitor()
+{
+}
+
+IVisitor::~IVisitor()
+{
+}
+
void IVisitor::visit(AST* cur, int depth)
{
list<AST*>* children;
list<AST*>::iterator it;
- // If we are just starting then use the global tree
- if(cur == NULL) cur = ast;
-
- // Execute or pre-walk actions
- if(depth == 0) beforeVisit( cur, depth );
-
- // Setup our locals
- children = cur->children();
- it = children->begin();
-
- // Visit the tree
- beforeChildren(cur,depth);
- depth++;
- for(; it != children->end(); it++)
+ if (cur != NULL)
{
- beforeChild( *it, depth );
- visit( *it, depth );
- afterChild( *it, depth );
+ // Execute or pre-walk actions
+ if(depth == 0) beforeVisit( cur, depth );
+
+ // Setup our locals
+ children = cur->children();
+ it = children->begin();
+
+ // Visit the tree
+ beforeChildren(cur,depth);
+ depth++;
+ for(; it != children->end(); it++)
+ {
+ beforeChild( *it, depth );
+ visit( *it, depth );
+ afterChild( *it, depth );
+ }
+ afterChildren(cur,depth);
+
+ // Execute our post-walk actions
+ if(depth == 1) afterVisit( cur, depth );
}
- afterChildren(cur,depth);
-
- // Execute our post-walk actions
- if(depth == 1) afterVisit( cur, depth );
}
#include <iostream>
class IVisitor {
- protected:
- AST* ast;
public:
- IVisitor(AST* tree) : ast(tree) {};
- ~IVisitor() { delete ast; }
- void visit(AST* cur = NULL, int depth = 0);
+ IVisitor();
+ ~IVisitor();
+ void visit(AST* cur, int depth = 0);
private:
virtual void beforeVisit(AST* cur, int depth) = 0;
virtual void afterVisit(AST* cur, int depth) = 0;
virtual void afterChild(AST* cur, int depth) = 0;
};
-class IVisitorFactory {
- public:
- virtual IVisitor* createIVisitor(AST* root) = 0;
-};
-
#endif