// File: /workspace/github_cpp_makefile_star2_O2/matteorr[P]EarleyParser/EarleyParser.cpp
// Function: EarleyParser::initialize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool)
void EarleyParser::initialize( string filePath, bool probablityParser )
{
	this->probabilityParser = probablityParser;

	string line;
	ifstream inputFile;

	inputFile.open( filePath.c_str(), fstream::in );

	if( inputFile.is_open() )
	{
		while( inputFile.good() )
		{
			/*
			 * reads a line (production) from the context free input grammar
			 */
			getline( inputFile, line );

			//skips empty line
			if( line.size() == 0 ) continue;

			cout << "[DEBUG]: " << line << endl;

			/*
			 * extracts tokens of the production
			 * separated by "->"
			 * first token is terminal symbol
			 * second token is production rule
			 * third tolken is production probability
			 */
			vector< string > tokenVector;
			this->splitToVector( line, &tokenVector, "->", false );

			//skips comment line
			if( tokenVector.at(0) == "#" ) continue;

			for( unsigned int i = 0; i < tokenVector.size(); i++ ){
				cout << "[DEBUG]: " << "line token: [" << tokenVector.at(i) << "]" << endl;
			}

			/*
			 * extracts all symbols in the production
			 * separated by " "
			 */
			vector< string > productionTokenVector;
			this->splitToVector( tokenVector.at(1), &productionTokenVector, " ", false );
			for( unsigned int i = 0; i < productionTokenVector.size(); i++ ){
				cout << "[DEBUG]: " << "production token: [" << productionTokenVector.at(i) << "]" << endl;
			}

			/*
			 * looks in the hash table of productions
			 * if the non terminal symbol was allready inserted
			 */
			EarleyParser::productionHashTable::iterator got = this->earleyProductions.find( tokenVector.at(0) );
			if( got != this->earleyProductions.end() )
			{
				/*
				 * element found in hash table (key tokenVector->at(0) contained)
				 * add a new Production at the end of the list
				 */

				cout << "[DEBUG]: found [" << got->first << "] adding Production to existing list" << endl << endl;

				got->second.push_back( Production( atof( tokenVector.at(2).c_str() ), productionTokenVector.size(), productionTokenVector ) );

			}else{
				/*
				 * element not found in hash table (key tokenVector->at(0) not contained)
				 * create a new list and save it in hash map
				 * adds new Production to the list
				 */

				list<Production> productionList;

				pair< string, list<Production> > productions( tokenVector.at(0), productionList );

				std::pair<EarleyParser::productionHashTable::iterator, bool> insertionResult;
				insertionResult = this->earleyProductions.insert( productions );
				if( insertionResult.second == true ){
					/*
					 * element inserted with success
					 */
					insertionResult.first->second.push_back( Production( atof( tokenVector.at(2).c_str() ), productionTokenVector.size(), productionTokenVector ) );

					cout << "[DEBUG]: created new list [" << productions.first << "] and inserted Production with success" << endl << endl;
				}
			}

		}
		inputFile.close();

		/*
		 * check probabilities coherence if specified by the input boolean
		 */
		if( this->probabilityParser == true ){

			for( productionHashTable::iterator i = this->earleyProductions.begin(); i != this->earleyProductions.end(); i++ ){

				float productionProbability = 0;

				cout << "[DEBUG]: checking probability of [" << i->first << "]" << endl;

				for( list<Production>::iterator j = i->second.begin(); j != i->second.end(); j++ ){

					productionProbability += j->getProductionProbability();
				}
				if( productionProbability != 1 ){
					cout << "[DEBUG]: probability of [" << i->first << "] does not sum up to 1! - BAD INPUT GRAMMAR" << endl;
					exit(1);
				}
			}
		}

	}else{
		cout << "Error: not able to open input file [" << filePath << "]" << endl;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x328, %rsp            # imm = 0x328
movq %rsi, %rbx
movq %rdi, -0x30(%rbp)
movb %dl, 0x8(%rdi)
leaq -0xa0(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq -0x348(%rbp), %rdi
callq 0x21f0
movq (%rbx), %rsi
leaq -0x348(%rbp), %rdi
pushq $0x8
popq %rdx
callq 0x2140
leaq -0x2d0(%rbp), %rdi
callq 0x2070
testb %al, %al
je 0x2aff
movq -0x30(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x38(%rbp)
leaq -0x348(%rbp), %rbx
movq 0xa8df(%rip), %r13      # 0xcfd8
leaq 0x5920(%rip), %r14      # 0x8020
movq -0x348(%rbp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, -0x328(%rbp,%rax)
jne 0x2b39
movq %rbx, %rdi
leaq -0xb0(%rbp), %rsi
callq 0x2040
cmpq $0x0, -0xa8(%rbp)
je 0x2700
movq %r13, %rdi
movq %r14, %rsi
callq 0x21c0
movq %rax, %rdi
leaq -0xb0(%rbp), %rsi
callq 0x2190
movq %rax, %rdi
callq 0x2100
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
andq $0x0, -0x60(%rbp)
leaq -0xe8(%rbp), %r15
movq %r15, %rdi
leaq 0x58b9(%rip), %rsi      # 0x802a
leaq -0x140(%rbp), %rdx
callq 0x20c0
leaq -0xb0(%rbp), %rsi
leaq -0x70(%rbp), %r12
movq %r12, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq 0x2d42
movq %r15, %rdi
callq 0x20f0
movq %r12, %rdi
xorl %esi, %esi
callq 0x4c0a
movq %rax, %rdi
leaq 0x587b(%rip), %rsi      # 0x802d
callq 0x4bf9
testb %al, %al
jne 0x2af1
xorl %r15d, %r15d
movl %r15d, %r15d
movq -0x68(%rbp), %rax
subq -0x70(%rbp), %rax
sarq $0x5, %rax
cmpq %r15, %rax
jbe 0x2826
movq %r13, %rdi
movq %r14, %rsi
callq 0x21c0
movq %rax, %rdi
leaq 0x5844(%rip), %rsi      # 0x802f
callq 0x21c0
movq %rax, %r12
leaq -0x70(%rbp), %rdi
movq %r15, %rsi
callq 0x4c0a
movq %r12, %rdi
movq %rax, %rsi
callq 0x2190
movq %rax, %rdi
leaq 0x5829(%rip), %rsi      # 0x803d
callq 0x21c0
movq %rax, %rdi
callq 0x2100
incl %r15d
jmp 0x27c2
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
andq $0x0, -0x40(%rbp)
leaq -0x70(%rbp), %rdi
pushq $0x1
popq %rsi
callq 0x4c0a
movq %rax, %r15
leaq -0xe8(%rbp), %r12
movq %r12, %rdi
leaq 0x5d4f(%rip), %rsi      # 0x85a1
leaq -0x140(%rbp), %rdx
callq 0x20c0
movq %r15, %rsi
leaq -0x50(%rbp), %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0x2d42
movq %r12, %rdi
callq 0x20f0
xorl %r15d, %r15d
movl %r15d, %r15d
movq -0x48(%rbp), %rax
subq -0x50(%rbp), %rax
sarq $0x5, %rax
cmpq %r15, %rax
jbe 0x28df
movq %r13, %rdi
movq %r14, %rsi
callq 0x21c0
movq %rax, %rdi
leaq 0x579b(%rip), %rsi      # 0x803f
callq 0x21c0
movq %rax, %r12
leaq -0x50(%rbp), %rdi
movq %r15, %rsi
callq 0x4c0a
movq %r12, %rdi
movq %rax, %rsi
callq 0x2190
movq %rax, %rdi
leaq 0x5770(%rip), %rsi      # 0x803d
callq 0x21c0
movq %rax, %rdi
callq 0x2100
incl %r15d
jmp 0x287b
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
callq 0x4c0a
movq -0x38(%rbp), %rdi
movq %rax, %rsi
callq 0x533e
movq %rax, %r15
testq %rax, %rax
je 0x29c1
movq %r13, %rdi
leaq 0x5747(%rip), %rsi      # 0x8053
callq 0x21c0
leaq 0x8(%r15), %rsi
movq %rax, %rdi
callq 0x2190
movq %rax, %rdi
leaq 0x573d(%rip), %rsi      # 0x8064
callq 0x21c0
movq %rax, %rdi
callq 0x2100
movq %rax, %rdi
callq 0x2100
leaq -0x70(%rbp), %rdi
pushq $0x2
popq %rsi
callq 0x4c0a
movq (%rax), %rdi
callq 0x2220
movsd %xmm0, -0x78(%rbp)
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %r12
leaq -0x118(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x4c3c
subq %rbx, %r12
shrq $0x5, %r12
cvtsd2ss -0x78(%rbp), %xmm0
leaq -0xe8(%rbp), %rbx
movq %rbx, %rdi
movl %r12d, %esi
leaq -0x118(%rbp), %rdx
callq 0x7658
addq $0x28, %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x4c2c
movq %rbx, %rdi
leaq -0x348(%rbp), %rbx
callq 0x76a4
leaq -0x118(%rbp), %rdi
callq 0x4ba2
jmp 0x2ae8
leaq -0x90(%rbp), %r12
movq %r12, -0x88(%rbp)
movq %r12, -0x90(%rbp)
andq $0x0, -0x80(%rbp)
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
callq 0x4c0a
leaq -0xe8(%rbp), %r15
movq %r15, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x4c90
movq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0x55a8
testb $0x1, %dl
je 0x2ad0
movq %rax, %r15
leaq -0x70(%rbp), %rdi
pushq $0x2
popq %rsi
callq 0x4c0a
movq (%rax), %rdi
callq 0x2220
movsd %xmm0, -0x78(%rbp)
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %r12
leaq -0x100(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x4c3c
subq %rbx, %r12
shrq $0x5, %r12
cvtsd2ss -0x78(%rbp), %xmm0
leaq -0x140(%rbp), %rbx
movq %rbx, %rdi
movl %r12d, %esi
leaq -0x100(%rbp), %rdx
callq 0x7658
addq $0x28, %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x4c2c
movq %rbx, %rdi
leaq -0x348(%rbp), %rbx
callq 0x76a4
leaq -0x100(%rbp), %rdi
callq 0x4ba2
movq %r13, %rdi
leaq 0x55ec(%rip), %rsi      # 0x8089
callq 0x21c0
movq %rax, %rdi
leaq -0xe8(%rbp), %rsi
callq 0x2190
movq %rax, %rdi
leaq 0x55ea(%rip), %rsi      # 0x80a5
callq 0x21c0
movq %rax, %rdi
callq 0x2100
movq %rax, %rdi
callq 0x2100
leaq -0xe8(%rbp), %rdi
callq 0x4cc6
leaq -0x90(%rbp), %rdi
callq 0x521a
leaq -0x50(%rbp), %rdi
callq 0x4ba2
leaq -0x70(%rbp), %rdi
callq 0x4ba2
jmp 0x2700
movq 0xa4d2(%rip), %rdi      # 0xcfd8
leaq 0x5628(%rip), %rsi      # 0x8135
callq 0x21c0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2190
leaq 0x5519(%rip), %rsi      # 0x803d
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x2100
jmp 0x2c2e
leaq -0x348(%rbp), %rdi
callq 0x2060
movq -0x30(%rbp), %rax
cmpb $0x1, 0x8(%rax)
jne 0x2c2e
addq $0x20, -0x30(%rbp)
leaq 0x556d(%rip), %r14      # 0x80cc
leaq 0x54d7(%rip), %r15      # 0x803d
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
testq %rax, %rax
je 0x2c2e
movq 0xa457(%rip), %rdi      # 0xcfd8
movq %r14, %rsi
callq 0x21c0
movq -0x30(%rbp), %rcx
leaq 0x8(%rcx), %r12
movq %rax, %rdi
movq %r12, %rsi
callq 0x2190
movq %rax, %rdi
movq %r15, %rsi
callq 0x21c0
movq %rax, %rdi
callq 0x2100
movq -0x30(%rbp), %rax
leaq 0x28(%rax), %rbx
xorps %xmm0, %xmm0
movq %rbx, %r13
movq (%r13), %r13
cmpq %rbx, %r13
je 0x2be2
movss %xmm0, -0x38(%rbp)
leaq 0x10(%r13), %rdi
callq 0x76fe
movss -0x38(%rbp), %xmm1
addss %xmm0, %xmm1
movaps %xmm1, %xmm0
jmp 0x2bbd
ucomiss 0x541b(%rip), %xmm0     # 0x8004
jne 0x2bf1
jnp 0x2b66
movq 0xa3e0(%rip), %rdi      # 0xcfd8
leaq 0x54f0(%rip), %rsi      # 0x80ef
callq 0x21c0
movq %rax, %rdi
movq %r12, %rsi
callq 0x2190
leaq 0x54f3(%rip), %rsi      # 0x8109
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x2100
pushq $0x1
popq %rdi
callq 0x2200
leaq -0x348(%rbp), %rdi
callq 0x2080
leaq -0xb0(%rbp), %rdi
callq 0x20f0
addq $0x328, %rsp            # imm = 0x328
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x2c6c
movq %rax, %rbx
leaq -0x140(%rbp), %rdi
callq 0x76a4
leaq -0x100(%rbp), %rdi
callq 0x4ba2
jmp 0x2cb2
movq %rax, %rbx
jmp 0x2d2d
jmp 0x2caf
jmp 0x2caf
movq %rax, %rbx
jmp 0x2c9a
movq %rax, %rbx
leaq -0xe8(%rbp), %rdi
callq 0x76a4
leaq -0x118(%rbp), %rdi
callq 0x4ba2
jmp 0x2d0a
jmp 0x2d02
movq %rax, %rbx
jmp 0x2cbe
movq %rax, %rbx
leaq -0xe8(%rbp), %rdi
callq 0x4cc6
leaq -0x90(%rbp), %rdi
callq 0x521a
jmp 0x2d0a
movq %rax, %rbx
leaq -0xe8(%rbp), %rdi
callq 0x20f0
jmp 0x2d0a
jmp 0x2d07
jmp 0x2d07
jmp 0x2d07
jmp 0x2d02
movq %rax, %rbx
leaq -0xe8(%rbp), %rdi
callq 0x20f0
jmp 0x2d18
jmp 0x2d15
jmp 0x2d15
jmp 0x2d02
jmp 0x2d07
jmp 0x2d02
jmp 0x2d02
movq %rax, %rbx
jmp 0x2d21
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x4ba2
jmp 0x2d18
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
callq 0x4ba2
leaq -0x348(%rbp), %rdi
callq 0x2080
leaq -0xb0(%rbp), %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x22c0
nop