Exemplo n.º 1
0
int main(int argc, char **argv) {
	int i;
	srand(time(NULL));
	for(first=first_stack; first<first_stack+PAPERS; first++)
		*first=malloc(STRINGSIZE);
	second=second_stack;
	
	for(i=0; i<PAPERS; i++) {
		char *paper=pop(&first);
		generate_postfix(&paper);
		push(&second, paper);
	}
	
	for(i=0; i<PAPERS; i++) {
		char *paper=pop(&second);
		int a;
		printf("(%s) = ", paper);
		a=parse_postfix(paper);
		printf("%i\n", a);
		sprintf(paper, "%i\n", a);
		push(&first, paper);
	}
	
	return 0;
}
Exemplo n.º 2
0
int main(void) {
    char inf[] = "2*682-3*2+4/2", new_arr[100] = {0};

    infix_to_postfix(inf, new_arr, 100);
    printf("%s\n", new_arr);
    printf("%d\n", parse_postfix(new_arr));

    return 0;
}
Exemplo n.º 3
0
float	evaluate_exp(const char *exp) {

	char str[INPUT_SIZE];
	float result;

	infix_to_postfix(str, exp);
	result =  parse_postfix(str);

	return result;
}
Exemplo n.º 4
0
string
r_language_rep::get_color (tree t, int start, int end) {
  static bool setup_done= false;
  if (!setup_done) {
    r_color_setup_constants (colored);
    r_color_setup_keywords (colored);
    r_color_setup_otherlexeme (colored);
    setup_done= true;
  }

  static string none= "";
  if (start >= end) return none;
  string s= t->label;
  string r1=s(0,start) ;

  int pos=0;int opos;
  bool backquote= false;
  bool after_backquote;
  bool postfix= false;
  bool possible_function= true;
  bool possible_type= false;
  bool possible_class= false;
  bool possible_future_type= false;
  bool possible_future_function= true;
  bool possible_future_class= false;
  string type;
  bool is_markup;

#if 0
  // There isn't much point to the following, because its only effet is pos and type, and both are reset below.
  do {
    do {
      opos=pos;

      parse_string (s, pos);
      if (opos<pos) break;

      parse_comment_single_line (s, pos);
      if (opos < pos) {
	type= "comment";
	break;
      }

      pos++;
    } while(false);
  } while( pos<N(s) );
#endif 

  pos=0;
  do {
    type= none;
    do {
      after_backquote= backquote;
      possible_function= possible_future_function;
      possible_type= possible_future_type;
      possible_class= possible_future_class;
      opos= pos;

      parse_blanks (s, pos);
      if (opos<pos) break;

      parse_string (s, pos);
      if (opos<pos) {
	type= "string";
	backquote= false;
	postfix= false;
	possible_future_function= false;
	possible_future_type= false;
	possible_future_class= false;
	possible_type= false;
	break;
      }

      parse_number (s, pos);
      if (opos<pos) {
	type= "number";
	backquote= false;
	postfix= false;
	possible_future_function= false;
	possible_future_class= false;
	break;
      }

      parse_comment_single_line (s, pos);
      if (opos<pos) {
	type= "comment";
	backquote= false;
	postfix= false;
	possible_future_type= false;
	possible_type= false;
	break;
      }

      parse_modifier (colored, s, pos);
      if (opos<pos) {
	type="keyword";
	backquote= false;
	postfix= false;
	possible_future_type= false;
	possible_type= false;
	possible_function= false;
	break;
      }

      parse_postfix (colored, s, pos);
      if (opos<pos) {
	type="keyword";
	backquote= false;
	postfix= true;
	possible_future_type= false;
	possible_future_class= false;
	possible_type= false;
	possible_function= false;
	possible_future_class= false;
	break;
      }

      parse_class (colored, s, pos);
      if (opos<pos) {
	type= "keyword";
	backquote=false;
	postfix=false;
	possible_future_type= false;
	possible_type= false;
	possible_future_class=true;
	possible_future_function= false;
	break;
      }

      parse_keyword (colored, s, pos);
      if (opos<pos) {
	type= "keyword";
	backquote= false;
	postfix= false;
	possible_future_type= false;
	possible_type= false;
	possible_function= false;
	possible_future_function= false;
	possible_future_class= false;
	break;
      }

      parse_other_op_assign (colored, s, pos);  //not left parenthesis
      if (opos<pos) {
	type= "other_lexeme";// was
	type = "op assign" ;
	backquote= false;
	postfix= false;
	possible_function= false;
	possible_future_function= true;
	possible_future_type= false;
	possible_future_class= false;
	possible_type= false;
	break;
      }

      parse_other_op_index (colored, s, pos);  //not left parenthesis
      if (opos<pos) {
	type= "other_lexeme";// was
	type = "op index" ;
	backquote= false;
	postfix= false;
	possible_function= false;
	possible_future_function= true;
	possible_future_type= false;
	possible_future_class= false;
	possible_type= false;
	break;
      }

      parse_other_lexeme (colored, s, pos);  //not left parenthesis
      if (opos<pos) {
	type= "other_lexeme";// was
	type = "operator" ;
	backquote= false;
	postfix= false;
	possible_function= false;
	possible_future_function= true;
	possible_future_type= false;
	possible_future_class= false;
	possible_type= false;
	break;
      }

      parse_constant (colored, s, pos);
      if (opos<pos) {
	type= "constant";
	backquote= false;
	postfix= false;
	possible_future_function= false;
	possible_future_class= false;
	break;
      }


      parse_backquote (s, pos);
      if (opos<pos) {
	backquote= true;
	postfix= false;
	possible_future_function= false;
	possible_future_class= false;
	break;
      }

      parse_identifier_or_markup (colored, s, pos, postfix, is_markup);
      if (opos<pos) {
	if (is_markup) {type= "identifier_markup";} else type= "identifier";
	backquote= false;
	postfix= false;
	possible_future_function=false;
	possible_future_class= false;
	break;
      }

      parse_parenthesized (s, pos);
      // stops after well parenthesized ) or before  // or /{ or " or /"
      if (opos<pos && pos<=start) {
	type="left_parenthesis";
	backquote= false;
	postfix= false;
	possible_function= false;
	possible_future_function= true;
	possible_future_class= false;
	break;
      }
      if (opos<pos && possible_type==true)
	return "dark green";
      if (opos<pos && after_backquote)  
	return none;

      backquote= false;
      postfix= false;
      pos= opos;
      pos++;
    } while (false); // This while() is so that we can easily escape with break.
  } while (pos<=start);

  if (possible_type) return "dark green";
  if (type=="string") return "#a06040";
  if (type=="operator") return "red";
  if (type=="op assign") return "dark green";
  if (type=="op index") return "dark blue";
  if (type=="comment") return "brown";
  if (type=="keyword" && !after_backquote) return "#8020c0";
  if (type=="other_lexeme") return none;
  if (type=="constant") return "#2060c0";
  if (type=="number") return "#2060c0";
  if (type=="left_parenthesis") return none;
  if (type=="identifier" && !possible_function && !possible_class )  return none;
  if (type=="identifier_markup" && !possible_function && !possible_class ) return COLOR_MARKUP;

  if ( (type=="identifier" || type=="identifier_markup") && possible_function) {
    possible_function= false;
    do {
      do {
	opos=pos;
	parse_blanks (s, pos);
	if (opos<pos) break;

	parse_identifier (colored, s, pos,false);
	if (opos<pos) { possible_function= true; break; }

	parse_number (s, pos);
	if (opos<pos) { possible_function= true; break; }

	parse_constant (colored, s, pos);
	if (opos<pos) { possible_function= true; break; }

	parse_comment_single_line (s, pos);
	if (opos<pos) break;

	parse_parenthesized (s, pos);
	if (opos<pos) { possible_function= true; break; }

      } while (false);
    } while (opos != pos);

    if (!possible_function) {
      if (type=="identifier") {return none;} 
      else return COLOR_MARKUP; // type=="identifier_markup"
    } else do {
	do {
	  opos=pos;
	  parse_blanks (s, pos);
	  if (opos<pos) break;
	  parse_identifier (colored, s, pos,false);
	  if (opos<pos) break;
	  parse_number(s,pos);
	  if (opos<pos) break;
	  parse_constant (colored, s, pos);
	  if (opos<pos) break;
	  parse_comment_single_line(s,pos);
	  if (opos<pos) break;
	  parse_parenthesized (s, pos);
	  if (opos<pos) break;

	  if (type=="identifier") {return none;} 
	  else return COLOR_MARKUP;

	} while (false);
      }
      while (pos<N(s));
  } // type==identifier || type==identifier_markup && possible function

  if ( (type=="identifier" || type=="identifier_markup") && possible_class) {
    do {
      do {
	opos=pos;
	parse_blanks (s, pos);
	if (opos<pos) break;

	parse_identifier (colored, s, pos,false);
	if (opos<pos) break;

	parse_number(s,pos);
	if (opos<pos) break;

	parse_constant (colored, s, pos);
	if (opos<pos) break;

	parse_comment_single_line(s,pos);
	if (opos<pos) break;

	parse_parenthesized (s, pos);
	if (opos<pos) break;

	if (type=="identifier") {return none;} 
	else return COLOR_MARKUP;
      } while (false);
    }
    while (pos<N(s));
  }
  if (type=="identifier_markup") {return COLOR_MARKUP;}
  else return none;
}