Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I made a simple predictive parser, and I tried it to covert to C.
But keep stuck on logical errors. Can I ask this one for help?

C++
<pre>#include<iostream>
#include<string>
#include<deque>
using namespace std;
int n, n1, n2;
int getPosition(string arr[], string q, int size)
{
	for (int i = 0; i < size; i++)
	{
		if (q == arr[i])
			return i;
	}
	return -1;
}
int main()
{
	string prods[10], first[10], follow[10], nonterms[10], terms[10];
	string pp_table[20][20] = {};
	cout << "Enter the number of productions : ";
	cin >> n;
	cin.ignore();
	cout << "Enter the productions" << endl;
	for (int i = 0; i < n; i++)
	{
		getline(cin, prods[i]);
		cout << "Enter first for " << prods[i].substr(3) << " : ";
		getline(cin, first[i]);
	}
	cout << "Enter the number of Terminals : ";
	cin >> n2;
	cin.ignore();
	cout << "Enter the Terminals" << endl;
	for (int i = 0; i < n2; i++)
	{
		cin >> terms[i];
	}
	terms[n2] = "$";
	n2++;
	cout << "Enter the number of Non-Terminals : ";
	cin >> n1;
	cin.ignore();
	for (int i = 0; i < n1; i++)
	{
		cout << "Enter Non-Terminal : ";
		getline(cin, nonterms[i]);
		cout << "Enter follow of " << nonterms[i] << " : ";
		getline(cin, follow[i]);
	}

	cout << endl;
	cout << "Grammar" << endl;
	for (int i = 0; i < n; i++)
	{
		cout << prods[i] << endl;
	}

	for (int j = 0; j < n; j++)
	{
		int row = getPosition(nonterms, prods[j].substr(0, 1), n1);
		if (prods[j].at(3) != '#')
		{
			for (int i = 0; i < first[j].length(); i++)
			{
				int col = getPosition(terms, first[j].substr(i, 1), n2);
				pp_table[row][col] = prods[j];
			}
		}
		else
		{
			for (int i = 0; i < follow[row].length(); i++)
			{
				int col = getPosition(terms, follow[row].substr(i, 1), n2);
				pp_table[row][col] = prods[j];
			}
		}
	}
	//Display Table
	for (int j = 0; j < n2; j++)
		cout << "\t" << terms[j];
	cout << endl;
	for (int i = 0; i < n1; i++)
	{
		cout << nonterms[i] << "\t";
		//Display Table
		for (int j = 0; j < n2; j++)
		{
			cout << pp_table[i][j] << "\t";
		}
		cout << endl;
	}
	//Parsing String
	char c;
	do {
		string ip;
		deque<string> pp_stack;
		pp_stack.push_front("$");
		pp_stack.push_front(prods[0].substr(0, 1));
		cout << "Enter the string to be parsed : ";
		getline(cin, ip);
		ip.push_back('$');
		cout << "Stack\tInput\tAction" << endl;
		while (true)
		{
			for (int i = 0; i < pp_stack.size(); i++)
				cout << pp_stack[i];
			cout << "\t" << ip << "\t";
			int row1 = getPosition(nonterms, pp_stack.front(), n1);
			int row2 = getPosition(terms, pp_stack.front(), n2);
			int column = getPosition(terms, ip.substr(0, 1), n2);
			if (row1 != -1 && column != -1)
			{
				string p = pp_table[row1][column];
				if (p.empty())
				{
					cout << endl << "String cannot be Parsed." << endl;
					break;
				}
				pp_stack.pop_front();
				if (p[3] != '#')
				{
					for (int x = p.size() - 1; x > 2; x--)
					{
						pp_stack.push_front(p.substr(x, 1));
					}
				}
				cout << p;
			}
			else
			{
				if (ip.substr(0, 1) == pp_stack.front())
				{
					if (pp_stack.front() == "$")
					{
						cout << endl << "String Parsed." << endl;
						break;
					}
					cout << "Match " << ip[0];
					pp_stack.pop_front();
					ip = ip.substr(1);
				}
				else
				{
					cout << endl << "String cannot be Parsed." << endl;
					break;
				}
			}
			cout << endl;
		}
		cout << "Continue?(Y/N) ";
		cin >> c;
		cin.ignore();
	} while (c == 'y' || c == 'Y');
	return 0;
}







Hope to get a positive reply!

What I have tried:

Didn`t upload my trials, as it doesn`t work
Posted
Updated 26-May-20 13:26pm
Comments
jeron1 25-May-20 11:57am    
It would help greatly if you were more specific in your question. What your expected output is versus what is actually happening for a given input. Maybe narrow the scope to a particular chuck of code that you are having issues with.
[no name] 25-May-20 12:25pm    
Replace cout with something like printf and similar for cin and you are done, I think...
The Other John Ingram 25-May-20 13:34pm    
There is no cout or cin in plain c. Use printf and scanf.
Also must remove all c++ libs.
[no name] 25-May-20 16:45pm    
Which is in case of deque not that easy ;)
Rick York 26-May-20 18:49pm    
That is C++ - you have a lot more than just logic errors if you want to use it C.

Since C is a subset of C++ which doesn't include classes at all (since the addition of classes to C was what created the original C++), it doesn't include strings, generics, stacks, or any of the other niceties that make that code work at all.

Duplicating these in C would be a extensive task, and would not produce "good C code" at the end - it would produce a big, clumsy lump of code that would neither work well, nor be easily maintainable. And would be a huge task.

Instead of finding random code in a different language and trying to get it to work in your actual target, I'd strongly suggest that you re-read your homework question and start writing your own code to accomplish it. It'll be quicker, as well as produce better code ...
 
Share this answer
 
Comments
[no name] 25-May-20 12:35pm    
But is the code not almost C apart from cin/cout?
OriginalGriff 25-May-20 13:56pm    
Ah, you saw the other stuff then! :laugh:
[no name] 25-May-20 16:05pm    
Yes Sir :-O :)
Your found code may show you the logic flow, but "converting" this (somehow "stolen") code wont work, because C++ provides some classes and functions which arent "1:1 translatable" to C.

The tips of my fellows are correct, but I wonder which errors did you stuck?

Til than I recommend you a good Learn C tutorial. Interesting is that it also explains some technology which you will need to solve your homework. :-O
 
Share this answer
 
There is absolutely no advantage of doing so
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900