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

In summary: 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;
  • #1
carl123
56
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
  • #2
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.
 

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

What is a recursive descent parser?

A recursive descent parser is a type of top-down parsing algorithm used to analyze the syntactic structure of a given input. It works by recursively breaking down the input into smaller parts and matching them to a set of production rules.

How does a recursive descent parser work?

A recursive descent parser starts at the top of the input and uses a set of production rules to try and match the input. If a match is found, the parser will move on to the next part of the input. If no match is found, the parser will backtrack and try another production rule until the entire input is parsed or an error is encountered.

What is the difference between a recursive descent parser and other parsing algorithms?

Recursive descent parsers are top-down, meaning they start at the top of the input and work their way down. Other parsing algorithms, such as bottom-up parsers, start at the bottom of the input and work their way up. Recursive descent parsers also use a set of production rules specific to the language being parsed, whereas other algorithms may use more general rules.

What are the advantages of using a recursive descent parser?

Recursive descent parsers are relatively easy to implement and understand, making them a popular choice for smaller projects. They also have good error reporting capabilities, as they can backtrack and try different production rules when an error is encountered.

What are the potential drawbacks of using a recursive descent parser?

Recursive descent parsers can be inefficient for handling large or complex inputs, as they may need to backtrack multiple times before finding a successful match. They also require careful design and planning to ensure a complete and accurate parsing of the input.

Similar threads

  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
3
Replies
73
Views
4K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
5
Views
901
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
Replies
4
Views
3K
Back
Top