diff --git a/alib2algo/src/grammar/parsing/DeterministicLL1Grammar.cpp b/alib2algo/src/grammar/parsing/DeterministicLL1Grammar.cpp index 83381e302cd764a4f7dadcb0d56b904d37357691..33043ccec5ee7dfd853823b2bc7b7fe21f129214 100644 --- a/alib2algo/src/grammar/parsing/DeterministicLL1Grammar.cpp +++ b/alib2algo/src/grammar/parsing/DeterministicLL1Grammar.cpp @@ -14,6 +14,8 @@ #include <exception/AlibException.h> #include <algorithm> +#include "../properties/RecursiveNonterminal.h" + namespace grammar { namespace parsing { @@ -28,6 +30,12 @@ void transformToLL1 ( grammar::CFG & grammar, const alphabet::Symbol & terminal, } grammar::CFG DeterministicLL1Grammar::convert ( const grammar::CFG & param ) { + + if ( std::any_of ( param.getNonterminalAlphabet ( ).begin ( ), param.getNonterminalAlphabet ( ).end ( ), [&] ( const alphabet::Symbol & nonterminal ) { + return grammar::properties::RecursiveNonterminal::isNonterminalRecursive ( param, nonterminal ); + } ) ) + throw exception::AlibException ( "Grammar contains left recursion" ); + grammar::CFG grammar = param; while ( true ) { @@ -44,6 +52,7 @@ grammar::CFG DeterministicLL1Grammar::convert ( const grammar::CFG & param ) { } transformToLL1 ( grammar, elem.first.first.get < alphabet::Symbol > ( ), elem.first.second, elem.second ); + deterministic = false; break; } diff --git a/examples2/grammar/contextFree1-2.txt b/examples2/grammar/contextFree1-2.txt new file mode 100644 index 0000000000000000000000000000000000000000..765d49052d189644cef8cc6668cd71c28684d4d3 --- /dev/null +++ b/examples2/grammar/contextFree1-2.txt @@ -0,0 +1,10 @@ +CFG ( +{'S', 'A', 'B', 'C', 'X'}, +{'a', 'b', 'c', 'd', 'e'}, +{'S' -> 'C' 'd' 'e' 'S' 'e' | 'd' 'd' 'B' | 'c' 'A', +'A' -> 'a' 'd' 'X' | 'b' 'b' 'X', +'X' -> 'c' 'a' 'd' 'X' |, +'B' -> 'b' 'a' 'C' 'd' |, +'C' -> 'A' 'c' 'a' | 'a' +}, +'S') diff --git a/examples2/grammar/contextFree1.txt b/examples2/grammar/contextFree1.txt new file mode 100644 index 0000000000000000000000000000000000000000..c064eb650a0d99d5d01e71ba0e4c56271ec94509 --- /dev/null +++ b/examples2/grammar/contextFree1.txt @@ -0,0 +1,9 @@ +CFG ( +{'S', 'A', 'B', 'C'}, +{'a', 'b', 'c', 'd', 'e'}, +{'S' -> 'C' 'd' 'e' 'S' 'e' | 'd' 'd' 'B' | 'c' 'A', +'A' -> 'C' 'd' | 'b' 'b', +'B' -> 'b' 'a' 'C' 'd' |, +'C' -> 'A' 'c' 'a' | 'a' +}, +'S')