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

  • Context: C/C++ 
  • Thread starter Thread starter carl123
  • Start date Start date
  • Tags Tags
    C++ Calculator Logic
Click For Summary
SUMMARY

The discussion focuses on creating a C++ logic calculator using a recursive descent parser, adhering to specific requirements such as accepting both 'T' and 'F' in any case, and allowing immediate evaluation of expressions with ';' or '='. The program is designed to handle errors gracefully without exiting, and it should return boolean values from grammar rule functions. The provided code snippet illustrates the structure of the parser, including the implementation of the expression parsing function, but the user encounters compilation issues that need resolution.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with recursive descent parsing techniques
  • Knowledge of boolean logic and expression evaluation
  • Experience with error handling in C++ applications
NEXT STEPS
  • Debug the provided C++ code to identify and fix compilation errors
  • Implement additional grammar rules for logical operators in the recursive descent parser
  • Explore C++ exception handling to improve error reporting in the calculator
  • Learn about tokenization techniques in parsing to enhance the parser's functionality
USEFUL FOR

C++ developers, computer science students, and anyone interested in implementing parsers or logic calculators using recursive descent techniques.

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)
		{
			count << 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.
 

Similar threads

Replies
73
Views
6K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 14 ·
Replies
14
Views
34K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K