Skip to content
Snippets Groups Projects
Commit b80d7cb8 authored by Martin Žák's avatar Martin Žák
Browse files

Adds LeftRegularGrammar

parent 8635c06a
No related branches found
No related tags found
No related merge requests found
/*
* LeftRegularGrammar.cpp
*
* Created on: Nov 17, 2013
* Author: martin
*/
#include "LeftRegularGrammar.h"
namespace grammar {
bool LeftRegularGrammar::isValidRule(const Rule& rule) const {
return checkLeftSide(rule.getLeftSide()) && checkRightSide(rule.getRightSide());
}
bool LeftRegularGrammar::checkLeftSide(const list<Symbol>& leftSide) const {
if (leftSide.size() != 1) {
return false;
}
if (nonTerminalSymbols.find(leftSide.front()) == nonTerminalSymbols.end()) {
return false;
}
return true;
}
bool LeftRegularGrammar::checkRightSide(const list<Symbol>& rightSide) const {
if (rightSide.size() == 0) {
return true;
} else if (rightSide.size() == 1) {
const Symbol& symbol = rightSide.front();
//check epsilon
if (symbol.getSymbol() == "") {
return true;
}
//check that symbol exists
return ((terminalSymbols.find(symbol) != terminalSymbols.end())
|| (nonTerminalSymbols.find(symbol) != nonTerminalSymbols.end()));
} else {
//check if first symbol is nonterminal
list<Symbol>::const_iterator symbol = rightSide.begin();
if (nonTerminalSymbols.find(*symbol) == nonTerminalSymbols.end()) {
return false;
}
symbol++;
//check if following symbols are terminal
for (; symbol != rightSide.end(); symbol++) {
if (terminalSymbols.find(*symbol) == terminalSymbols.end()) {
return false;
}
}
return true;
}
}
} /* namespace grammar */
/*
* LeftRegularGrammar.h
*
* Created on: Nov 17, 2013
* Author: martin
*/
#ifndef LEFTREGULARGRAMMAR_H_
#define LEFTREGULARGRAMMAR_H_
#include "Grammar.h"
namespace grammar {
class LeftRegularGrammar : public Grammar {
private:
bool checkLeftSide(const list<Symbol>& leftSide) const;
bool checkRightSide(const list<Symbol>& rightSide) const;
protected:
bool isValidRule(const Rule& rule) const;
};
} /* namespace grammar */
#endif /* LEFTREGULARGRAMMAR_H_ */
<grammar>
<nonTerminalSymbols>
<symbol>S</symbol>
<symbol>A</symbol>
</nonTerminalSymbols>
<terminalSymbols>
<symbol>a</symbol>
<symbol>b</symbol>
</terminalSymbols>
<rules>
<rule>
<leftSide>
<symbol>S</symbol>
</leftSide>
<rightSide>
<symbol>A</symbol>
</rightSide>
</rule>
<rule>
<leftSide>
<symbol>A</symbol>
</leftSide>
<rightSide>
<symbol>a</symbol>
</rightSide>
</rule>
<rule>
<leftSide>
<symbol>A</symbol>
</leftSide>
<rightSide>
<symbol>A</symbol>
<symbol>b</symbol>
<symbol>b</symbol>
<symbol>b</symbol>
</rightSide>
</rule>
</rules>
<startSymbol>S</startSymbol>
</grammar>
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