Skip to content
This repository was archived by the owner on Mar 20, 2024. It is now read-only.

Commit e47bbbc

Browse files
committed
Merge branch 'master' of https://github.com/ExpandingDev/JSGFKit_plus_plus into facelift
2 parents c41e516 + dd8a414 commit e47bbbc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+455
-26
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ A hosted version of the Doxygen generated docs can be found here: http://persona
2222
Doxygen documentation comments are in the code, to generate HTML documentation files from it, run `doxygen Doxyfile` and the html docs will be outputted into the `docs` subdirectory.
2323

2424
## Example programs
25-
It is highly suggested to test to make sure compilation and installation completed successfully. A few example programs come with this library and can be found in the `examples` subdirectory.
25+
A few example programs come with this library and can be found in the `examples` subdirectory.
2626
To build an example program, `cd` into its directory and simply run `make`.
2727
If compilation complains about missing headers or undefined references, you will probably need to adjust the `JSGF_KIT_LD` and `JSGF_KIT_CFLAGS` variables in the `Makefile` to point to the correct library path and include directories (just run `pkg-config --cflags jsgfkit` and `pkg-config --libs jsgfkit` and copy the output in).
2828

Diff for: grammar_test.log

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
PARSED GRAMMAR:
2+
grammar default;
3+
4+
5+
Test Done

Diff for: include/jsgfkitxx/AlternativeSet.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class AlternativeSet : public Expansion
2222
Expansion * clone();
2323
ExpansionType getType() const override;
2424
bool hasChild() const;
25+
bool isOptional() const;
2526
std::shared_ptr<Expansion> getChild(const unsigned int index = 0) const;
2627
std::vector<std::shared_ptr<Expansion>> getChildren() const;
2728
unsigned int childCount() const;

Diff for: include/jsgfkitxx/Expansion.hpp

+14
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class Expansion
2626
/// Returns true if the Expansion has a child Expansion. Tokens will always return false.
2727
virtual bool hasChild() const { return false; };
2828

29+
/// Returns true if the Expansion and/or its children are completely optional
30+
virtual bool isOptional() const { return false; };
31+
2932
/// Returns the number of child expansions this Expansion has. Tokens will always return false.
3033
virtual unsigned int childCount() const { return 0; };
3134

@@ -39,4 +42,15 @@ class Expansion
3942
virtual void replaceChild(std::shared_ptr<Expansion> newChild, const unsigned long index = 0) { };
4043
};
4144

45+
#define EXPANSION_IS_TOKEN(e) ((e)->getType() == TOKEN)
46+
#define EXPANSION_IS_SEQUENCE(e) ((e)->getType() == SEQUENCE)
47+
#define EXPANSION_IS_ALTERNATE_SET(e) ((e)->getType() == ALTERNATE_SET)
48+
#define EXPANSION_IS_PLUS_OPERATOR(e) ((e)->getType() == PLUS_OPERATOR)
49+
#define EXPANSION_IS_KLEENE_STAR(e) ((e)->getType() == KLEENE_STAR)
50+
#define EXPANSION_IS_RULE_REFERENCE(e) ((e)->getType() == RULE_REFERENCE)
51+
#define EXPANSION_IS_TAG(e) ((e)->getType() == TAG)
52+
#define EXPANSION_IS_REQUIRED_GROUPING(e) ((e)->getType() == REQUIRED_GROUPING)
53+
#define EXPANSION_IS_OPTIONAL_GROUPING(e) ((e)->getType() == OPTIONAL_GROUPING)
54+
#define EXPANSION_IS_UNPARSED_SECTION(e) ((e)->getType() == UNPARSED_SECTION)
55+
4256
#endif // EXPANSION_H

Diff for: include/jsgfkitxx/KleeneStar.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class KleeneStar : public Expansion
2020
void replaceChild(std::shared_ptr<Expansion> newChild, const unsigned long index = 0);
2121
ExpansionType getType() const;
2222
bool hasChild() const;
23+
bool isOptional() const;
2324
std::shared_ptr<Expansion> getChild(const unsigned int index = 0) const;
2425
void setChild(std::shared_ptr<Expansion> e);
2526
unsigned int childCount() const;

Diff for: include/jsgfkitxx/OptionalGrouping.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class OptionalGrouping : public Expansion
2020
void replaceChild(std::shared_ptr<Expansion> newChild, const unsigned long index = 0);
2121
ExpansionType getType() const;
2222
bool hasChild() const;
23+
bool isOptional() const;
2324
std::shared_ptr<Expansion> getChild(const unsigned int index = 0) const;
2425
void setChild(std::shared_ptr<Expansion> e);
2526
unsigned int childCount() const;

Diff for: include/jsgfkitxx/PlusOperator.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class PlusOperator : public Expansion
2020
void replaceChild(std::shared_ptr<Expansion> newChild, const unsigned long index = 0);
2121
ExpansionType getType() const;
2222
bool hasChild() const;
23+
bool isOptional() const;
2324
std::shared_ptr<Expansion> getChild(const unsigned int index = 0) const;
2425
void setChild(std::shared_ptr<Expansion> e);
2526
unsigned int childCount() const;

Diff for: include/jsgfkitxx/RequiredGrouping.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class RequiredGrouping : public Expansion
2020
void replaceChild(std::shared_ptr<Expansion> newChild, const unsigned long index = 0);
2121
ExpansionType getType() const;
2222
bool hasChild() const;
23+
bool isOptional() const;
2324
std::shared_ptr<Expansion> getChild(const unsigned int index = 0) const;
2425
void setChild(std::shared_ptr<Expansion> e);
2526
unsigned int childCount() const;

Diff for: include/jsgfkitxx/RuleReference.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class RuleReference : public Expansion
2727
void setRuleName(std::string val);
2828

2929
bool hasChild() const;
30+
bool isOptional() const;
3031
void replaceChild(std::shared_ptr<Expansion> newChild, const unsigned long index = 0);
3132
unsigned int childCount() const;
3233
Expansion * clone();

Diff for: include/jsgfkitxx/Sequence.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Sequence : public Expansion
2121
void replaceChild(std::shared_ptr<Expansion> newChild, const unsigned long index = 0);
2222
ExpansionType getType() const;
2323
bool hasChild() const;
24+
bool isOptional() const;
2425
std::shared_ptr<Expansion> getChild(const unsigned int index = 0) const;
2526
std::string getText() const;
2627

Diff for: include/jsgfkitxx/Token.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Token : public Expansion
3333

3434
unsigned int childCount() const;
3535
bool hasChild() const;
36+
bool isOptional() const;
3637
shared_ptr<Expansion> getChild(const unsigned int index = 0) const;
3738
};
3839

Diff for: include/matchtracker.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef MATCHINFO_H
2+
#define MATCHINFO_H
3+
#include "expansion.h"
4+
#include <string>
5+
#include <vector>
6+
7+
class MatchTracker
8+
{
9+
private:
10+
11+
protected:
12+
std::shared_ptr<std::vector<std::string>> wordList;
13+
14+
public:
15+
/** Default constructor */
16+
MatchTracker(std::shared_ptr<std::vector<std::string>> words, unsigned int startIndex);
17+
18+
MatchTracker(MatchTracker & t);
19+
20+
///Stores the index of the current word that is trying to be matched
21+
unsigned int position;
22+
23+
///Stores a vector of matched tags
24+
std::vector<std::string> matchedTags;
25+
26+
///Stores a vector of matched rules
27+
std::vector<std::string> matchedRules;
28+
29+
static MatchTracker matchExpansion(std::shared_ptr<std::vector<std::string>> words, unsigned int startIndex, std::shared_ptr<Expansion> startExpansion);
30+
31+
bool reachedEnd;
32+
bool successful;
33+
};
34+
#endif // MATCHINFO_H

Diff for: src/alternativeset.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ bool AlternativeSet::hasChild() const {
4343
return expansions.empty();
4444
}
4545

46+
bool AlternativeSet::isOptional() const {
47+
std::vector<std::shared_ptr<Expansion>>::const_iterator it = expansions.begin();
48+
while(it != expansions.end()) {
49+
if((*it)->isOptional()) { // If at least one of the children is optional, then this set can be matched no matter what.
50+
return true;
51+
}
52+
it++;
53+
}
54+
return false;
55+
}
56+
4657
void AlternativeSet::addChild(std::shared_ptr<Expansion> e) {
4758
expansions.push_back(e);
4859
}

Diff for: src/grammar.cpp

+33-23
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ void Grammar::parseUnaryOperators(const vector<Expansion *> & expansions, vector
284284

285285
while(expansionIterator != expansions.end())
286286
{
287-
if ((*expansionIterator)->getType() == UNPARSED_SECTION)
287+
if (EXPANSION_IS_UNPARSED_SECTION(*expansionIterator))
288288
{
289289
if (expansionFound)
290290
{
@@ -356,7 +356,7 @@ void Grammar::parseUnaryOperators(const vector<Expansion *> & expansions, vector
356356
{
357357
if (foundStart)
358358
{
359-
if ((*expansionIterator)->getType() == UNPARSED_SECTION) // Could contain the ending }
359+
if (EXPANSION_IS_UNPARSED_SECTION(*expansionIterator)) // Could contain the ending }
360360
{
361361
UnparsedSection * up = (UnparsedSection *) *expansionIterator;
362362
if (Grammar::stringStartsWith(up->getSection(), "}")) // Found the end of the tag!
@@ -403,7 +403,7 @@ void Grammar::parseUnaryOperators(const vector<Expansion *> & expansions, vector
403403
else
404404
{
405405
// Looking for a starting bracket {
406-
if ((*expansionIterator)->getType() == UNPARSED_SECTION) // May contain the { we're looking for
406+
if (EXPANSION_IS_UNPARSED_SECTION(*expansionIterator)) // May contain the { we're looking for
407407
{
408408
UnparsedSection * up = (UnparsedSection *) *expansionIterator;
409409
if (Grammar::stringEndsWith(up->getSection(), "{")) // Found the start of the tag!
@@ -438,7 +438,7 @@ void Grammar::parseUnaryOperators(const vector<Expansion *> & expansions, vector
438438
}
439439
else // Looking for a expansion that is taggable
440440
{
441-
if (!((*expansionIterator)->getType() == PLUS_OPERATOR || (*expansionIterator)->getType() == KLEENE_STAR || (*expansionIterator)->getType() == UNPARSED_SECTION))
441+
if (!(EXPANSION_IS_PLUS_OPERATOR(*expansionIterator) || EXPANSION_IS_KLEENE_STAR(*expansionIterator) || EXPANSION_IS_UNPARSED_SECTION(*expansionIterator)))
442442
{
443443
foundLegalExpansion = true; // Found a taggable expansion, select it and start searching for tags
444444
selectedExpansion = *expansionIterator;
@@ -472,7 +472,7 @@ void Grammar::parseOptionalGroupings(const vector<Expansion *> & expansions, vec
472472
vector<Expansion *>::iterator expansionIterator = exp.begin();
473473
while(expansionIterator != exp.end())
474474
{
475-
if ((*expansionIterator)->getType() == UNPARSED_SECTION)
475+
if (EXPANSION_IS_UNPARSED_SECTION(*expansionIterator))
476476
{
477477

478478
UnparsedSection * up = (UnparsedSection *) *expansionIterator;
@@ -584,7 +584,7 @@ void Grammar::parseRequiredGroupings(const vector<Expansion *> & expansions, vec
584584
vector<Expansion *>::iterator expansionIterator = exp.begin();
585585
while(expansionIterator != exp.end())
586586
{
587-
if ((*expansionIterator)->getType() == UNPARSED_SECTION)
587+
if (EXPANSION_IS_UNPARSED_SECTION(*expansionIterator))
588588
{
589589
UnparsedSection * up = (UnparsedSection *) *expansionIterator;
590590
string childString;
@@ -713,7 +713,7 @@ void Grammar::parseRuleReferences(const vector<Expansion *> & expansions, vector
713713
{
714714
if (startSearch)
715715
{
716-
if ((*expansionIterator)->getType() == UNPARSED_SECTION)
716+
if (EXPANSION_IS_UNPARSED_SECTION(*expansionIterator))
717717
{
718718
UnparsedSection * up = (UnparsedSection *) *expansionIterator;
719719
if (Grammar::stringEndsWith(up->getSection(), "<"))
@@ -737,7 +737,7 @@ void Grammar::parseRuleReferences(const vector<Expansion *> & expansions, vector
737737
}
738738
else if (endSearch)
739739
{
740-
if ((*expansionIterator)->getType() == UNPARSED_SECTION)
740+
if (EXPANSION_IS_UNPARSED_SECTION(*expansionIterator))
741741
{
742742
UnparsedSection * up = (UnparsedSection *) *expansionIterator;
743743
if (Grammar::stringStartsWith(up->getSection(), ">"))
@@ -764,7 +764,7 @@ void Grammar::parseRuleReferences(const vector<Expansion *> & expansions, vector
764764
}
765765
else if (tokenSearch)
766766
{
767-
if ((*expansionIterator)->getType() == TOKEN)
767+
if (EXPANSION_IS_TOKEN(*expansionIterator))
768768
{
769769
endSearch = true;
770770
tokenSearch = false;
@@ -929,7 +929,7 @@ vector<Expansion *> Grammar::parseTokensFromString(std::string part)
929929
}
930930

931931
bool Grammar::isEmptyUnparsedSection(Expansion * e) {
932-
if(e->getType() == UNPARSED_SECTION) {
932+
if(EXPANSION_IS_UNPARSED_SECTION(e)) {
933933
UnparsedSection * u = (UnparsedSection *) e;
934934
std::string s = u->getSection();
935935
if(s.size() == 0) {
@@ -973,7 +973,7 @@ Expansion * Grammar::parseAlternativeSets(vector<Expansion *> & exp) {
973973

974974
while (expansionIterator != exp.end())
975975
{
976-
if ((*expansionIterator)->getType() == UNPARSED_SECTION)
976+
if (EXPANSION_IS_UNPARSED_SECTION(*expansionIterator))
977977
{
978978
UnparsedSection * up = (UnparsedSection *) *expansionIterator;
979979
if (Grammar::stringContains(up->getSection(), "|"))
@@ -1111,7 +1111,7 @@ vector<shared_ptr<MatchInfo>> Grammar::getMatchingExpansions(shared_ptr<Expansio
11111111
{
11121112
vector<shared_ptr<MatchInfo>> matchVector;
11131113

1114-
if ((e.get())->getType() == TOKEN)
1114+
if (EXPANSION_IS_TOKEN(e))
11151115
{
11161116
Token * t = (Token *) e.get();
11171117
if (t->getText() == (words[wordPosition]))
@@ -1124,7 +1124,7 @@ vector<shared_ptr<MatchInfo>> Grammar::getMatchingExpansions(shared_ptr<Expansio
11241124
// No match
11251125
}
11261126
}
1127-
else if ((e.get())->getType() == RULE_REFERENCE)
1127+
else if (EXPANSION_IS_RULE_REFERENCE(e))
11281128
{
11291129
RuleReference * ref = (RuleReference *) e.get();
11301130

@@ -1139,7 +1139,7 @@ vector<shared_ptr<MatchInfo>> Grammar::getMatchingExpansions(shared_ptr<Expansio
11391139
}
11401140
}
11411141
}
1142-
else if ((e.get())->getType() == OPTIONAL_GROUPING)
1142+
else if (EXPANSION_IS_OPTIONAL_GROUPING(e))
11431143
{
11441144
OptionalGrouping * og = (OptionalGrouping *) e.get();
11451145
vector<shared_ptr<MatchInfo>> m1 = getMatchingExpansions(og->getChild(), words, wordCount, wordPosition);
@@ -1155,7 +1155,7 @@ vector<shared_ptr<MatchInfo>> Grammar::getMatchingExpansions(shared_ptr<Expansio
11551155
matchVector.insert(matchVector.end(), moreMatches.begin(), moreMatches.end());
11561156
}
11571157
}
1158-
else if ((e.get())->getType() == REQUIRED_GROUPING)
1158+
else if (EXPANSION_IS_REQUIRED_GROUPING(e))
11591159
{
11601160
RequiredGrouping * rg = (RequiredGrouping *) e.get();
11611161
vector<shared_ptr<MatchInfo>> m1 = getMatchingExpansions(rg->getChild(), words, wordCount, wordPosition);
@@ -1166,7 +1166,7 @@ vector<shared_ptr<MatchInfo>> Grammar::getMatchingExpansions(shared_ptr<Expansio
11661166
matchVector.insert(matchVector.end(), m1.begin(), m1.end());
11671167
}
11681168
}
1169-
else if (e.get()->getType() == TAG)
1169+
else if (EXPANSION_IS_TAG(e))
11701170
{
11711171
Tag * t = (Tag *) e.get();
11721172
vector<shared_ptr<MatchInfo>> m1 = getMatchingExpansions(t->getChild(), words, wordCount, wordPosition);
@@ -1177,14 +1177,14 @@ vector<shared_ptr<MatchInfo>> Grammar::getMatchingExpansions(shared_ptr<Expansio
11771177
matchVector.insert(matchVector.end(), m1.begin(), m1.end());
11781178
}
11791179
}
1180-
else if ((e.get())->getType() == ALTERNATE_SET)
1180+
else if (EXPANSION_IS_ALTERNATE_SET(e))
11811181
{
11821182
AlternativeSet * as = (AlternativeSet *) e.get();
11831183
for (shared_ptr<Expansion> x : as->getChildren())
11841184
{
11851185
vector<shared_ptr<MatchInfo>> m1 = getMatchingExpansions(x, words, wordCount, wordPosition);
11861186

1187-
if (m1.size() == 0 && (x->getType() == KLEENE_STAR || x->getType() == OPTIONAL_GROUPING)) // Stupid OptionalGrouping
1187+
if (m1.size() == 0 && (EXPANSION_IS_KLEENE_STAR(x) || EXPANSION_IS_OPTIONAL_GROUPING(x))) // Stupid OptionalGrouping
11881188
{
11891189
continue;
11901190
}
@@ -1197,7 +1197,7 @@ vector<shared_ptr<MatchInfo>> Grammar::getMatchingExpansions(shared_ptr<Expansio
11971197
}
11981198
}
11991199
}
1200-
else if ((e.get())->getType() == SEQUENCE)
1200+
else if (EXPANSION_IS_SEQUENCE(e))
12011201
{
12021202
Sequence * seq = (Sequence *) e.get();
12031203
vector<shared_ptr<MatchInfo>> localMatchVector;
@@ -1207,7 +1207,7 @@ vector<shared_ptr<MatchInfo>> Grammar::getMatchingExpansions(shared_ptr<Expansio
12071207
for (shared_ptr<Expansion> x : expansions)
12081208
{
12091209
vector<shared_ptr<MatchInfo>> m1 = getMatchingExpansions(x, words, wordCount, wordPosition);
1210-
if (m1.size() == 0 && (x->getType() == KLEENE_STAR || x->getType() == OPTIONAL_GROUPING)) // Stupid OptionalGrouping
1210+
if (m1.size() == 0 && (EXPANSION_IS_KLEENE_STAR(x) || EXPANSION_IS_OPTIONAL_GROUPING(x))) // Stupid OptionalGrouping
12111211
{
12121212
matchedCount++; // Still counts a match
12131213
continue;
@@ -1237,7 +1237,15 @@ vector<shared_ptr<MatchInfo>> Grammar::getMatchingExpansions(shared_ptr<Expansio
12371237
}
12381238
}
12391239

1240-
if (matchedCount != seq->childCount()) // Not all of the required matches were met!
1240+
unsigned int requiredMatched = 0;
1241+
for(shared_ptr<Expansion> x : expansions) {
1242+
if(!EXPANSION_IS_OPTIONAL_GROUPING(x) && !EXPANSION_IS_KLEENE_STAR(x)) {
1243+
requiredMatched++;
1244+
}
1245+
}
1246+
1247+
//std::cout << "Needed: " << seq->childCount() << " Got: " << matchedCount << std::endl;
1248+
if (matchedCount < requiredMatched) // Not all of the required matches were met!
12411249
{
12421250
localMatchVector.clear();
12431251
}
@@ -1248,7 +1256,7 @@ vector<shared_ptr<MatchInfo>> Grammar::getMatchingExpansions(shared_ptr<Expansio
12481256
matchVector.insert(matchVector.end(), localMatchVector.begin(), localMatchVector.end());
12491257
}
12501258
}
1251-
else if ((e.get()->getType()) == KLEENE_STAR)
1259+
else if (EXPANSION_IS_KLEENE_STAR(e))
12521260
{
12531261
KleeneStar * ks = (KleeneStar *) e.get();
12541262
bool done = false;
@@ -1281,7 +1289,7 @@ vector<shared_ptr<MatchInfo>> Grammar::getMatchingExpansions(shared_ptr<Expansio
12811289
}
12821290
}
12831291
}
1284-
else if ((e.get())->getType() == PLUS_OPERATOR)
1292+
else if (EXPANSION_IS_PLUS_OPERATOR(e))
12851293
{
12861294
PlusOperator * po = (PlusOperator *) e.get();
12871295
bool done = false;
@@ -1331,6 +1339,8 @@ vector<shared_ptr<MatchInfo>> Grammar::matchesRule(const shared_ptr<Rule> rule,
13311339
vector<shared_ptr<MatchInfo>> m1 = getMatchingExpansions(rule->getRuleExpansion(), wordArray, words.size(), 0);
13321340
unsigned int matchCount = 0;
13331341
for (shared_ptr<MatchInfo> mi2 : m1) {
1342+
//std::cout << "MI: " << mi2->getMatchingSection() << ", " << matchCount << ", " << printExpansionType(mi2->getExpansion().get()) << std::endl;
1343+
13341344
if (mi2->getMatchingSection() != "") {
13351345
matchCount++;
13361346
}

Diff for: src/kleenestar.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ bool KleeneStar::hasChild() const {
3636
return childExpansion != nullptr;
3737
}
3838

39+
bool KleeneStar::isOptional() const {
40+
return true;
41+
}
42+
3943
void KleeneStar::replaceChild(std::shared_ptr<Expansion> newChild, const unsigned long index) {
4044
childExpansion = newChild;
4145
}

0 commit comments

Comments
 (0)