Fixin cfg
This commit is contained in:
parent
01b7d96ebd
commit
7d49642376
2 changed files with 12 additions and 3 deletions
|
|
@ -13,11 +13,13 @@ void run(const String& source) {
|
||||||
|
|
||||||
if (!grammar.parse(state, source)) {
|
if (!grammar.parse(state, source)) {
|
||||||
printf("Parsing is failed\n");
|
printf("Parsing is failed\n");
|
||||||
|
CfGrammar::deinitializeCfGrammarParser(state);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!grammar.compile()) {
|
if (!grammar.compile()) {
|
||||||
printf("Compilation is failed\n");
|
printf("Compilation is failed\n");
|
||||||
|
CfGrammar::deinitializeCfGrammarParser(state);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -41,6 +43,12 @@ int main(int argc, const char* argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
printf("Arguments given: ");
|
||||||
|
for (auto idx = 0; idx < argc; idx++) {
|
||||||
|
printf("[%s]", argv[idx]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
CommandLine cmd = {
|
CommandLine cmd = {
|
||||||
{ "grammar", CommandLine::Arg::STR },
|
{ "grammar", CommandLine::Arg::STR },
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,7 @@ void CfGrammar::generateSentences(List<Sentence>& out) {
|
||||||
auto appendTerm = copy->terms.findIdx(termIdx);
|
auto appendTerm = copy->terms.findIdx(termIdx);
|
||||||
auto deleteTerm = appendTerm;
|
auto deleteTerm = appendTerm;
|
||||||
for (auto arg : production->args) {
|
for (auto arg : production->args) {
|
||||||
|
if (arg->isEpsilon) continue;
|
||||||
auto newTerm = copy->terms.newNode({ arg->id, arg->isTerminal });
|
auto newTerm = copy->terms.newNode({ arg->id, arg->isTerminal });
|
||||||
copy->terms.attach(newTerm, appendTerm);
|
copy->terms.attach(newTerm, appendTerm);
|
||||||
appendTerm = newTerm;
|
appendTerm = newTerm;
|
||||||
|
|
@ -62,11 +63,11 @@ void CfGrammar::generateSentences(List<Sentence>& out) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void CfGrammar::printSentence(Sentence& in) {
|
void CfGrammar::printSentence(Sentence& in) {
|
||||||
printf("Sentence:");
|
printf("Sentence: [");
|
||||||
for (auto term : in.terms) {
|
for (auto term : in.terms) {
|
||||||
printf(" %s", term->id.read());
|
printf(" %s", term->id.read());
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf(" ]\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CfGrammar::compile() {
|
bool CfGrammar::compile() {
|
||||||
|
|
@ -103,7 +104,7 @@ bool CfGrammar::compile() {
|
||||||
|
|
||||||
if (arg->isTerminal) {
|
if (arg->isTerminal) {
|
||||||
mTerminals.put(rule->id, {});
|
mTerminals.put(rule->id, {});
|
||||||
} else {
|
} else if (!arg->isEpsilon) {
|
||||||
auto idx = mNonTerminals.presents(arg->id);
|
auto idx = mNonTerminals.presents(arg->id);
|
||||||
if (!idx) {
|
if (!idx) {
|
||||||
printf("Referenced non-terminal '%s' is not defined\n", arg->id.read());
|
printf("Referenced non-terminal '%s' is not defined\n", arg->id.read());
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue