Skip to content
Snippets Groups Projects
Commit 5c6aa604 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

Fix bottom of the stack symbol

parent 54984991
No related branches found
No related tags found
No related merge requests found
Showing
with 142 additions and 5 deletions
......@@ -2,7 +2,7 @@
* BlankSymbol.h
*
* Created on: Mar 26, 2013
* Author: Jan Trávníček
* Author: Jan Travnicek
*/
 
#ifndef BLANK_SYMBOL_H_
......
/*
* BottomOfTheStackSymbol.cpp
*
* Created on: Jun 19, 2014
* Author: Jan Travnicek
*/
#include "BottomOfTheStackSymbol.h"
namespace alphabet {
BottomOfTheStackSymbol::BottomOfTheStackSymbol() {
}
SymbolBase* BottomOfTheStackSymbol::clone() const {
return new BottomOfTheStackSymbol(*this);
}
SymbolBase* BottomOfTheStackSymbol::plunder() && {
return new BottomOfTheStackSymbol(std::move(*this));
}
bool BottomOfTheStackSymbol::operator <(const SymbolBase& other) const {
return other > *this;
}
bool BottomOfTheStackSymbol::operator ==(const SymbolBase& other) const {
return other == *this;
}
bool BottomOfTheStackSymbol::operator >(const SymbolBase& other) const {
return other < *this;
}
bool BottomOfTheStackSymbol::operator ==(const BottomOfTheStackSymbol&) const {
return true;
}
bool BottomOfTheStackSymbol::operator <(const BottomOfTheStackSymbol&) const {
return false;
}
void BottomOfTheStackSymbol::operator>>(std::ostream& out) const {
out << "(Blank symbol)";
}
BottomOfTheStackSymbol::operator std::string () const {
return "";
}
BottomOfTheStackSymbol BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK = BottomOfTheStackSymbol();
} /* namespace alphabet */
/*
* BottomOfTheStackSymbol.h
*
* Created on: Jun 19, 2014
* Author: Jan Travnicek
*/
#ifndef BOTTOM_OF_THE_STACK_SYMBOL_H_
#define BOTTOM_OF_THE_STACK_SYMBOL_H_
#include "Symbol.h"
namespace alphabet {
/**
* Represents blank symbol in an alphabet.
*/
class BottomOfTheStackSymbol : public std::element<BottomOfTheStackSymbol, SymbolBase> {
public:
/**
* Creates a blank symbol.
* @param symbol name of the symbol
*/
BottomOfTheStackSymbol();
virtual SymbolBase* clone() const;
virtual SymbolBase* plunder() &&;
virtual bool operator <(const SymbolBase& other) const;
virtual bool operator ==(const SymbolBase& other) const;
virtual bool operator >(const SymbolBase& other) const;
virtual bool operator ==(const BottomOfTheStackSymbol& other) const;
virtual bool operator <(const BottomOfTheStackSymbol& other) const;
virtual void operator>>(std::ostream& out) const;
virtual operator std::string () const;
static BottomOfTheStackSymbol BOTTOM_OF_THE_STACK;
};
} /* namespace alphabet */
#endif /* BOTTOM_OF_THE_STACK_SYMBOL_H_ */
......@@ -10,6 +10,7 @@
 
#include "LabeledSymbol.h"
#include "BlankSymbol.h"
#include "BottomOfTheStackSymbol.h"
 
namespace alphabet {
 
......@@ -34,6 +35,10 @@ bool SymbolBase::operator==(const BlankSymbol&) const {
return false;
}
 
bool SymbolBase::operator==(const BottomOfTheStackSymbol&) const {
return false;
}
bool SymbolBase::operator<(const LabeledSymbol& other) const {
return typeid(*this).before(typeid(other));
}
......@@ -42,4 +47,8 @@ bool SymbolBase::operator<(const BlankSymbol& other) const {
return typeid(*this).before(typeid(other));
}
 
bool SymbolBase::operator<(const BottomOfTheStackSymbol& other) const {
return typeid(*this).before(typeid(other));
}
} /* namespace alphabet */
......@@ -15,11 +15,12 @@ namespace alphabet {
 
class LabeledSymbol;
class BlankSymbol;
class BottomOfTheStackSymbol;
 
/**
* Represents symbol in an alphabet.
*/
class SymbolBase : public std::elementBase<LabeledSymbol, BlankSymbol> {
class SymbolBase : public std::elementBase<LabeledSymbol, BlankSymbol, BottomOfTheStackSymbol> {
public:
virtual ~SymbolBase() noexcept;
 
......@@ -33,9 +34,11 @@ public:
 
virtual bool operator==(const LabeledSymbol& other) const;
virtual bool operator==(const BlankSymbol& other) const;
virtual bool operator==(const BottomOfTheStackSymbol& other) const;
 
virtual bool operator<(const LabeledSymbol& other) const;
virtual bool operator<(const BlankSymbol& other) const;
virtual bool operator<(const BottomOfTheStackSymbol& other) const;
 
friend std::ostream& operator<<(std::ostream&, const SymbolBase&);
 
......
......@@ -37,6 +37,9 @@ L1:
} else if(character == 'B') {
m_Current.type = TokenType::BLANK;
return *this;
} else if(character == 'Z') {
m_Current.type = TokenType::BOTTOM;
return *this;
} else {
m_In.seekg(pos);
m_Current.type = TokenType::ERROR;
......
......@@ -10,6 +10,7 @@ class SymbolFromStringLexer {
public:
enum class TokenType {
BLANK,
BOTTOM,
TEOF,
ERROR,
};
......
#include "SymbolFromStringParser.h"
#include "../AlibException.h"
#include "BlankSymbol.h"
#include "BottomOfTheStackSymbol.h"
#include "LabeledSymbol.h"
 
namespace alphabet {
......@@ -14,6 +15,8 @@ Symbol* SymbolFromStringParser::parse() {
switch(token.type) {
case SymbolFromStringLexer::TokenType::BLANK:
return new Symbol(BlankSymbol());
case SymbolFromStringLexer::TokenType::BOTTOM:
return new Symbol(BottomOfTheStackSymbol());
case SymbolFromStringLexer::TokenType::ERROR: {
label::Label* label = m_LabelParser.parse();
if(label != NULL) {
......
......@@ -9,6 +9,7 @@
 
#include "../sax/ParserException.h"
#include "BlankSymbol.h"
#include "BottomOfTheStackSymbol.h"
#include "LabeledSymbol.h"
#include "../label/Label.h"
 
......@@ -26,6 +27,10 @@ Symbol SymbolFromXMLParser::parse(std::list<sax::Token>& input) const {
popToken(input, sax::Token::TokenType::START_ELEMENT, "BlankSymbol");
popToken(input, sax::Token::TokenType::END_ELEMENT, "BlankSymbol");
return Symbol(BlankSymbol());
} else if(isToken(input, sax::Token::TokenType::START_ELEMENT, "BottomOfTheStackSymbol")) {
popToken(input, sax::Token::TokenType::START_ELEMENT, "BottomOfTheStackSymbol");
popToken(input, sax::Token::TokenType::END_ELEMENT, "BottomOfTheStackSymbol");
return Symbol(BlankSymbol());
} else {
throw sax::ParserException(sax::Token("", sax::Token::TokenType::START_ELEMENT), input.front());
}
......
......@@ -8,7 +8,6 @@
#include "SymbolToStringComposer.h"
#include "../label/LabelToStringComposer.h"
#include <algorithm>
#include "../label/Label.h"
#include "LabeledSymbol.h"
 
namespace alphabet {
......@@ -26,6 +25,12 @@ void SymbolToStringComposer::Visit(void* userData, const BlankSymbol&) {
out << "#B";
}
 
void SymbolToStringComposer::Visit(void* userData, const BottomOfTheStackSymbol&) {
std::stringstream &out = *((std::stringstream*) userData);
out << "#Z";
}
void SymbolToStringComposer::Visit(void* userData, const Symbol& symbol) {
symbol.getSymbol().Accept(userData, *this);
 
......
......@@ -22,6 +22,8 @@ class SymbolToStringComposer : public SymbolBase::visitor_type {
void Visit(void*, const Symbol& symbol);
void Visit(void*, const LabeledSymbol& symbol);
void Visit(void*, const BlankSymbol& symbol);
void Visit(void*, const BottomOfTheStackSymbol& symbol);
public:
/**
* Prints text representation of Symbol to the output stream.
......
......@@ -6,7 +6,6 @@
*/
 
#include "SymbolToXMLComposer.h"
#include "LabeledSymbol.h"
 
#include "../ToXMLComposers.h"
 
......@@ -19,6 +18,13 @@ void SymbolToXMLComposer::Visit(void* userData, const BlankSymbol&) const {
out.push_back(sax::Token("BlankSymbol", sax::Token::TokenType::END_ELEMENT));
}
 
void SymbolToXMLComposer::Visit(void* userData, const BottomOfTheStackSymbol&) const {
std::list<sax::Token> &out = *((std::list<sax::Token>*) userData);
out.push_back(sax::Token("BottomOfTheStackSymbol", sax::Token::TokenType::START_ELEMENT));
out.push_back(sax::Token("BottomOfTheStackSymbol", sax::Token::TokenType::END_ELEMENT));
}
void SymbolToXMLComposer::Visit(void* userData, const LabeledSymbol& symbol) const {
std::list<sax::Token> &out = *((std::list<sax::Token>*) userData);
 
......
......@@ -22,6 +22,7 @@ class SymbolToXMLComposer : public SymbolBase::const_visitor_type {
void Visit(void*, const Symbol& symbol) const;
void Visit(void*, const LabeledSymbol& symbol) const;
void Visit(void*, const BlankSymbol& symbol) const;
void Visit(void*, const BottomOfTheStackSymbol& symbol) const;
 
public:
/**
......
......@@ -35,4 +35,4 @@ Label LabelFromXMLParser::parse(std::list<sax::Token>& input) const {
}
}
 
} /* namespace string */
} /* namespace label */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment