C/C++ Create a C++ logic calculator (T/F) using a recursive descent parser

AI Thread Summary
The discussion revolves around the development of a logic calculator that adheres to specific requirements, including case insensitivity for 'T' and 'F', immediate evaluation indicators using ';' or '=', and error handling that allows continued input after an error. The program is expected to use a recursive descent parser and return boolean values for grammar rule functions. The provided code snippet demonstrates an attempt to implement the calculator, but the user encounters compilation issues. Key components include a `Token` class for managing token types and values, and a `PTree` class for constructing the parse tree. The `Expr` function is designed to parse expressions but contains errors that prevent compilation. The user is advised to focus on the compiler's error messages, as resolving the first indicated error often resolves subsequent issues. The overall goal is to create a functional logic calculator that can handle various input formats and provide accurate outputs while maintaining robustness against errors.
carl123
Messages
55
Reaction score
0
I'm supposed to make a simple logic calculator based on the following requirements:

1. The user can use either capital or lower case 'T' and 'F' in their expression.
2. Use either a ';' or '=' to indicate the expression should be evaluated immediately.
3. The program should not exit when it encounters an error in the expression. Rather, it should output error information and then accept the next expression.
4. Use either a capital or lowercase 'Q' to indicate you want to exit the program.
5. Functions for the grammar rules will return type bool
6. You must use a recursive descent parser

Program output should look like this:

T&F = F
f|t = T
(T&f)^(t|F) = T
!F = T
q


This is what I have so far but, I can't figure out why my code won't compile

HTML:
#include <iostream>
#include <vector>
using namespace std;class Token {
	Tokentype   type;
	string      value;
	int     linenum;

public:
	Token(Tokentype t, string v = "") {
		type = t;
		value = v;
	}
	Tokentype getType() { return type; }
	string getValue() { return value; }
	int getLinenum() { return linenum; }
};

vector<string> int_list;
vector<string> float_list;

class PTree {
	PTreeNodetype   type;
	PTree *left;
	PTree *right;
public:
	PTree(PTreeNodetype t, PTree *l = 0, PTree *r = 0) {
		type = t;
		left = l;
		right = r;
	}
	PTreeNodetype getType() { return type; }
};

// expr ::= term PLUS expr | term MINUS expr | term 

PTree *
Expr() {

	PTree *term = Term();
	Token *t;

	if (!term)
		return 0;

	t = getToken();

	if (t == NULL) {
		delete t;
		return 0;
	}
	if (t->getType() != T_SC)
	{
		if (t->getType() == T_RPAREN) {
			pushbacktoken(t);
			return new PTree(EXPR, term);
		}

		if (t->getType() != T_PLUS && t->getType() != T_MINUS)
		{
			cout << t->getLinenum() << ":" << "Error:    expected + or -" << endl;
			pushbacktoken(t);
			delete t;
			return 0;
		}

		delete t;
		PTree *expr = Expr();
		if (!expr)
			return 0;

		return new PTree(EXPR, term, expr);
	}
 
Technology news on Phys.org
If code won't compile, the compiler usually indicates which line(s) of code are causing the problem. Often, if you fix the first line that is shown in the list of errors, that can clear up subsequent errors.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...

Similar threads

Replies
36
Views
3K
Replies
14
Views
34K
Replies
4
Views
5K
Replies
3
Views
3K
Replies
2
Views
3K
Replies
4
Views
4K
Back
Top