Ejemplo n.º 1
0
Exp * expression(Dictionary dict) {
/* Build (and return the root of) the tree for the expression beginning  */
/* with the current token.  At the end, the token is the first one not   */
/* part of this expression.                                              */
    return restricted_expression(dict, TRUE,TRUE);
}
Ejemplo n.º 2
0
Exp * restricted_expression(Dictionary dict, int and_ok, int or_ok) {
    Exp * nl=NULL, * nr, * n;
    E_list *ell, *elr;
    if (is_equal(dict, L'(')) {
	if (!advance(dict)) {
	    return NULL;
	}
	nl = expression(dict);
	if (nl == NULL) {
	    return NULL;
	}
	if (!is_equal(dict, L')')) {
	    dict_error(dict, L"Expecting a \")\".");
	    return NULL;
	}
	if (!advance(dict)) {
	    return NULL;
	}
    } else if (is_equal(dict, L'{')) {
	if (!advance(dict)) {
	    return NULL;
	}
	nl = expression(dict);
	if (nl == NULL) {
	    return NULL;
	}
	if (!is_equal(dict, L'}')) {
	    dict_error(dict, L"Expecting a \"}\".");
	    return NULL;
	}
	if (!advance(dict)) {
	    return NULL;
	}
	nl = make_optional_node(dict, nl);
    } else if (is_equal(dict, L'[')) {
	if (!advance(dict)) {
	    return NULL;
	}
	nl = expression(dict);
	if (nl == NULL) {
	    return NULL;
	}
	if (!is_equal(dict, L']')) {
	    dict_error(dict, L"Expecting a \"]\".");
	    return NULL;
	}
	if (!advance(dict)) {
	    return NULL;
	}
	nl->cost += 1;
    } else if (!dict->is_special) {
	nl = connector(dict);
	if (nl == NULL) {
	    return NULL;
	}
    } else if (is_equal(dict, L')') || is_equal(dict, L']')) {    
	/* allows "()" or "[]" */
        nl = make_zeroary_node(dict);
    } else {
    	dict_error(dict, L"Connector, \"(\", \"[\", or \"{\" expected.");
	return NULL;
    }
    
    if (is_equal(dict, L'&') || (wcscmp(dict->token, L"and")==0)) {
	if (!and_ok) {
	    warning(dict, L"\"and\" and \"or\" at the same level in an expression");
	}
	if (!advance(dict)) {
	    return NULL;
	}
	nr = restricted_expression(dict, TRUE,FALSE);
	if (nr == NULL) {
	    return NULL;
	}
	n = Exp_create(dict);
	n->u.l = ell = (E_list *) xalloc(sizeof(E_list));
	ell->next = elr = (E_list *) xalloc(sizeof(E_list));
	elr->next = NULL;
	
	ell->e = nl;
	elr->e = nr;
	n->type = AND_type;
	n->cost = 0;
    } else if (is_equal(dict, L'|') || (wcscmp(dict->token, L"or")==0)) {
	if (!or_ok) {
	    warning(dict, L"\"and\" and \"or\" at the same level in an expression");
	}
	if (!advance(dict)) {
	    return NULL;
	}
	nr = restricted_expression(dict, FALSE,TRUE);
	if (nr == NULL) {
	    return NULL;
	}
	n = Exp_create(dict);
	n->u.l = ell = (E_list *) xalloc(sizeof(E_list));
	ell->next = elr = (E_list *) xalloc(sizeof(E_list));
	elr->next = NULL;

	ell->e = nl;
	elr->e = nr;
	n->type = OR_type;
	n->cost = 0;
    } else return nl;
    return n;
}
Ejemplo n.º 3
0
/**
 * Build (and return the root of) the tree for the expression beginning
 * with the current token.  At the end, the token is the first one not
 * part of this expression.
 */
static Exp * expression(Dictionary dict)
{
	return restricted_expression(dict, TRUE, TRUE);
}