Example #1
0
bool Writer::bracketsNeeded(SymbolType type, SymbolType nestedType) const
{
    switch (arity(type)) {
    case 0:
        return false;

        break;

    case 1:
        return arity(nestedType) == 2;

        break;

    case 2:
        if (arity(nestedType) < 2) {
            return false;
        }

        return precendence(type) <= precendence(nestedType);

        break;

    default:
        throw(1);

        break;
    }
}
Example #2
0
void FunctionParser::parse(std::list<std::string> &parsed, std::string& arg)
{
	parsed.clear();
	std::stack<std::string> s;
	getRidOfSpaces();
	SI i = f.begin();
	bool sign = 0;
	bool first = true;
	if (i != f.end() && *i == '-') {
		sign = 1;
		++i;
	}
	for (; i != f.end();) {
		if (isdigit(*i)) {
			std::string n = getNumber(i);
			if (n == "####") {
				parsed.clear();
				parsed.push_back("The number must contain only one .!");
				return;
			}
			if (sign) {
				sign = 0;
				n.insert(n.begin(), '-');
			}
			parsed.push_back(n);
		}
		else if (isOperator(*i)) {
			std::string op;
			op.insert(op.begin(), *i);
			if (!s.empty()) {
				while (!s.empty() && precendence(s.top(), op) >= 0 && s.top() != "(") {
					parsed.push_back(s.top());
					s.pop();
				}
			}
			s.push(op);
			++i;
		}
		else if (*i == '(') {
			std::string bracket = "(";
			s.push(bracket);
			++i;
		}
		else if (*i == ')') {
			while (!s.empty() && s.top() != "(") {
				parsed.push_back(s.top());
				s.pop();
			}
			if (s.empty()) {
				parsed.clear();
				parsed.push_back("#There's an ) without (!");;
				return;
			}
			s.pop();
			if (!s.empty() && isFunction(s.top())) {
				parsed.push_back(s.top());
				s.pop();
			}
			++i;
		}
		else if (isalpha(*i)) {
			std::string name = getName(i);
			if (!isFunction(name)) {
				parsed.push_back(name);
				if (first) {
					arg = name;
					first = false;
				}
				else {
					if (name != arg) {
						parsed.clear();
						parsed.push_back("#The function can take only one argument!");
					}
				}
			}
			else {
				s.push(name);
			}
		}
		else {
			parsed.clear();
			parsed.push_back("#Unknown token!");
			return;
		}
	}

	while (!s.empty()) {
		parsed.push_back(s.top());
		s.pop();
	}
}
Example #3
0
//main
int main(void) {
    char infix[SIZE], postfix[SIZE], token, following_token, element;
    int i = 0, k = 0, j = 1;
    printf("Provide input: ");
	//store user input into infix array
    scanf("%s", infix);
	//push a terminating character onto the stack
    push('#');

    //check each individual token
    while ((token = infix[i++]) != '\0') {

        //push left parentheses onto stack
        if (token == '(') {
            push(token);
        }
        else {
            //verify token is a digit
            if (isdigit(token)) {
                following_token = infix[i];
                postfix[k++] = token;
				//check if following digit in numeric
                while (isdigit(following_token)) {
					//determine tokens for multiple digit numbers
                    postfix[k++] = following_token;
                    following_token = infix[i + j];
                    j++;
                    i++;
                }
				//put a separator to distinguish numbers
                postfix[k++] = '|';
            } 
			//check if token is equal to a slosing RPAREN
			else if (token == ')') {
                while (s[top] != '(') {
                    postfix[k++] = pop();
                }
                element = pop();
            } else {
                //prder of precendence for each operator
                while (precendence(s[top]) >= precendence(token)) {
                    postfix[k++] = pop();
                }
                push(token);
            }
        }
    }

    // loop through the stack until terminating character is reached
    while (s[top] != '#') {
        postfix[k++] = pop();
    }
	//add terminating character to array
    postfix[k] = '\0';
	printf("Postfix expression %s\n\n", postfix);
	printf("Stack form:\n");
    for (i = 0; postfix[i] != '\0'; i++) {
        token = postfix[i];
        if (!isdigit(token)) {
                switch (token) {
					case '-':
						printf("sub\n");
						break;
					case '+':
						printf("add\n");
						break;
					case '\\':
						printf("div\n");
						break;
					case '*':
						printf("mul\n");
						break;
					default:
						printf("Invalid token, program terminating\n");
						return 2;
				}
        } else {
			//print the digits
            printf("push ");
            while (isdigit((token = postfix[i]))) {
                printf("%c", token);
                i++;
            }
            printf("\n");
        }
    }

    printf("done\n");
    return 0;
}