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

EndOfFile symbol

parent 3e232094
No related branches found
No related tags found
No related merge requests found
Showing
with 150 additions and 6 deletions
/*
* EndSymbol.cpp
*
* Created on: Jun 19, 2014
* Author: Jan Travnicek
*/
#include "EndSymbol.h"
namespace alphabet {
EndSymbol::EndSymbol() {
}
SymbolBase* EndSymbol::clone() const {
return new EndSymbol(*this);
}
SymbolBase* EndSymbol::plunder() && {
return new EndSymbol(std::move(*this));
}
bool EndSymbol::operator <(const SymbolBase& other) const {
return other > *this;
}
bool EndSymbol::operator ==(const SymbolBase& other) const {
return other == *this;
}
bool EndSymbol::operator >(const SymbolBase& other) const {
return other < *this;
}
bool EndSymbol::operator ==(const EndSymbol&) const {
return true;
}
bool EndSymbol::operator <(const EndSymbol&) const {
return false;
}
void EndSymbol::operator>>(std::ostream& out) const {
out << "(Blank symbol)";
}
EndSymbol::operator std::string () const {
return "";
}
EndSymbol EndSymbol::END = EndSymbol();
} /* namespace alphabet */
/*
* EndSymbol.h
*
* Created on: Jun 19, 2014
* Author: Jan Travnicek
*/
#ifndef END_SYMBOL_H_
#define END_SYMBOL_H_
#include "Symbol.h"
namespace alphabet {
/**
* Represents blank symbol in an alphabet.
*/
class EndSymbol : public std::element<EndSymbol, SymbolBase> {
public:
/**
* Creates a blank symbol.
* @param symbol name of the symbol
*/
EndSymbol();
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 EndSymbol& other) const;
virtual bool operator <(const EndSymbol& other) const;
virtual void operator>>(std::ostream& out) const;
virtual operator std::string () const;
static EndSymbol END;
};
} /* namespace alphabet */
#endif /* END_SYMBOL_H_ */
......@@ -11,6 +11,7 @@
#include "LabeledSymbol.h"
#include "BlankSymbol.h"
#include "BottomOfTheStackSymbol.h"
#include "EndSymbol.h"
 
namespace alphabet {
 
......@@ -39,6 +40,10 @@ bool SymbolBase::operator==(const BottomOfTheStackSymbol&) const {
return false;
}
 
bool SymbolBase::operator==(const EndSymbol&) const {
return false;
}
bool SymbolBase::operator<(const LabeledSymbol& other) const {
return typeid(*this).before(typeid(other));
}
......@@ -51,4 +56,8 @@ bool SymbolBase::operator<(const BottomOfTheStackSymbol& other) const {
return typeid(*this).before(typeid(other));
}
 
bool SymbolBase::operator<(const EndSymbol& other) const {
return typeid(*this).before(typeid(other));
}
} /* namespace alphabet */
......@@ -16,11 +16,12 @@ namespace alphabet {
class LabeledSymbol;
class BlankSymbol;
class BottomOfTheStackSymbol;
class EndSymbol;
 
/**
* Represents symbol in an alphabet.
*/
class SymbolBase : public std::elementBase<LabeledSymbol, BlankSymbol, BottomOfTheStackSymbol> {
class SymbolBase : public std::elementBase<LabeledSymbol, BlankSymbol, BottomOfTheStackSymbol, EndSymbol> {
public:
virtual ~SymbolBase() noexcept;
 
......@@ -35,10 +36,12 @@ 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 EndSymbol& other) const;
 
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 EndSymbol& other) const;
 
friend std::ostream& operator<<(std::ostream&, const SymbolBase&);
 
......
......@@ -8,12 +8,13 @@
#ifndef LABEL_FEATURES_H_
#define LABEL_FEATURES_H_
 
namespace label {
namespace alphabet {
 
enum class FEATURES {
LABELED_SYMBOL,
BLANK_SYMBOL,
BOTTOM_OF_THE_STACK_SYMBOL
LABELED,
BLANK,
BOTTOM,
END
};
 
} /* namespace label */
......
......@@ -40,6 +40,9 @@ L1:
} else if(character == 'Z') {
m_Current.type = TokenType::BOTTOM;
return *this;
} else if(character == 'E') {
m_Current.type = TokenType::END;
return *this;
} else {
m_In.seekg(pos);
m_Current.type = TokenType::ERROR;
......
......@@ -11,6 +11,7 @@ public:
enum class TokenType {
BLANK,
BOTTOM,
END,
TEOF,
ERROR,
};
......
......@@ -2,6 +2,7 @@
#include "../AlibException.h"
#include "BlankSymbol.h"
#include "BottomOfTheStackSymbol.h"
#include "EndSymbol.h"
#include "LabeledSymbol.h"
 
namespace alphabet {
......@@ -17,6 +18,8 @@ Symbol SymbolFromStringParser::parse() {
return Symbol(BlankSymbol());
case SymbolFromStringLexer::TokenType::BOTTOM:
return Symbol(BottomOfTheStackSymbol());
case SymbolFromStringLexer::TokenType::END:
return Symbol(EndSymbol());
case SymbolFromStringLexer::TokenType::ERROR:
return Symbol(LabeledSymbol(m_LabelParser.parse()));
case SymbolFromStringLexer::TokenType::TEOF:
......
......@@ -10,6 +10,7 @@
#include "../sax/ParserException.h"
#include "BlankSymbol.h"
#include "BottomOfTheStackSymbol.h"
#include "EndSymbol.h"
#include "LabeledSymbol.h"
#include "../label/Label.h"
 
......@@ -24,8 +25,10 @@ Symbol SymbolFromXMLParser::parse(std::list<sax::Token>& input) const {
return parseBlankSymbol(input);
} else if(isToken(input, sax::Token::TokenType::START_ELEMENT, "BottomOfTheStackSymbol")) {
return parseBlankSymbol(input);
} else if(isToken(input, sax::Token::TokenType::START_ELEMENT, "EndSymbol")) {
return parseEndSymbol(input);
} else {
throw sax::ParserException(sax::Token("LabeledSymbol, BlankSymbol, BottomOfTheStackSymbol", sax::Token::TokenType::START_ELEMENT), input.front());
throw sax::ParserException(sax::Token("LabeledSymbol, BlankSymbol, BottomOfTheStackSymbol, EndSymbol", sax::Token::TokenType::START_ELEMENT), input.front());
}
}
 
......@@ -48,6 +51,12 @@ Symbol SymbolFromXMLParser::parseBottomOfTheStackSymbol(std::list<sax::Token>& i
return Symbol(BottomOfTheStackSymbol());
}
 
Symbol SymbolFromXMLParser::parseEndSymbol(std::list<sax::Token>& input) const {
popToken(input, sax::Token::TokenType::START_ELEMENT, "EndSymbol");
popToken(input, sax::Token::TokenType::END_ELEMENT, "EndSymbol");
return Symbol(EndSymbol());
}
Symbol SymbolFromXMLParser::parseValue(std::list<sax::Token>& input) const {
Symbol res = parse(input);
 
......
......@@ -43,6 +43,7 @@ class SymbolFromXMLParser : public sax::FromXMLParser {
Symbol parseLabeledSymbol(std::list<sax::Token>& input) const;
Symbol parseBlankSymbol(std::list<sax::Token>& input) const;
Symbol parseBottomOfTheStackSymbol(std::list<sax::Token>& input) const;
Symbol parseEndSymbol(std::list<sax::Token>& input) const;
/**
* Parses the XML tokens and returns symbol. The input is destroyed in the process.
* @param input XML tokens represented as list of tokens
......
......@@ -31,6 +31,12 @@ void SymbolToStringComposer::Visit(void* userData, const BottomOfTheStackSymbol&
out << "#Z";
}
 
void SymbolToStringComposer::Visit(void* userData, const EndSymbol&) {
std::stringstream &out = *((std::stringstream*) userData);
out << "#E";
}
void SymbolToStringComposer::Visit(void* userData, const Symbol& symbol) {
symbol.getSymbol().Accept(userData, *this);
 
......
......@@ -23,6 +23,7 @@ class SymbolToStringComposer : public SymbolBase::visitor_type {
void Visit(void*, const LabeledSymbol& symbol);
void Visit(void*, const BlankSymbol& symbol);
void Visit(void*, const BottomOfTheStackSymbol& symbol);
void Visit(void*, const EndSymbol& symbol);
 
public:
/**
......
......@@ -25,6 +25,13 @@ void SymbolToXMLComposer::Visit(void* userData, const BottomOfTheStackSymbol&) c
out.push_back(sax::Token("BottomOfTheStackSymbol", sax::Token::TokenType::END_ELEMENT));
}
 
void SymbolToXMLComposer::Visit(void* userData, const EndSymbol&) const {
std::list<sax::Token> &out = *((std::list<sax::Token>*) userData);
out.push_back(sax::Token("EndSymbol", sax::Token::TokenType::START_ELEMENT));
out.push_back(sax::Token("EndSymbol", sax::Token::TokenType::END_ELEMENT));
}
void SymbolToXMLComposer::Visit(void* userData, const LabeledSymbol& symbol) const {
std::list<sax::Token> &out = *((std::list<sax::Token>*) userData);
 
......
......@@ -23,6 +23,7 @@ class SymbolToXMLComposer : public SymbolBase::const_visitor_type {
void Visit(void*, const LabeledSymbol& symbol) const;
void Visit(void*, const BlankSymbol& symbol) const;
void Visit(void*, const BottomOfTheStackSymbol& symbol) const;
void Visit(void*, const EndSymbol& symbol) const;
 
public:
/**
......
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