Ejemplo n.º 1
0
Archivo: parser.c Proyecto: Saruta/a2c
struct instruction *parse_instruction(void)
{
  struct expr *expr;
  struct instruction *res;
  switch (lookahead[0]->type)
  {
    case WHILE: return parse_while();
    case DO: return parse_do();
    case IDENTIFIER: case LPAREN:
             expr = parse_expression();
             switch(lookahead[0]->type)
             {
               case ASSIGN: return parse_assignment_instr(expr);
               case EOL:
                            if (expr->exprtype != funcalltype)
                            {
                              if (expr->exprtype == binopexprtype
                                  && expr->val.binopexpr.op == EQ)
                              {
                                error(expr->pos, "unexpected =, did you mean <- ?");
                                exit(1);
                              }
                              else
                                syntaxerror("expected instruction, not expression");
                            }
                            eat(EOL);
                            res = funcallinstr(expr->val.funcall.fun_ident,
                                expr->val.funcall.args, expr->pos);
                            free(expr);
                            return res;
               default:
                            next();
                            syntaxerror("unexpected %s", tok->val);
                            return parse_instruction();
             }
    case RETURN:
             eat(RETURN);
             if (lookahead[0]->type == EOL)
             {
               eat(EOL);
               return return_stmt(NULL);
             }
             else
             {
               expr = parse_expression(); eat(EOL);
               return return_stmt(expr);
             }
    case FOR: return parse_for();
    case IF: return parse_if();
    case SWITCH: return parse_switch();
    case ENDOFFILE: return NULL;
    default:
                    next();
                    syntaxerror("expected instruction, not %s", tok->val);
                    return parse_instruction();
  }
}
Ejemplo n.º 2
0
VeDeviceElement *veDeviceParseElem(char *spec) {
  VeDeviceElement *e;
  VeDeviceEContent *content;
  char *s, *dup, *name, *type, *ptr;
  
  dup = veDupString(spec); /* save spec - may not be alterable... */
  ptr = dup;
  name = veNextLElem(&ptr);
  if (name && strcmp(name,"elem") == 0)
    name = veNextLElem(&ptr); /* skip leading "elem" if there */
  if (!name) {
    veError(MODULE,"veDeviceParseElem: missing name in spec: %s", spec);
    veFree(dup);
    return NULL;
  }
  if (!(type = veNextLElem(&ptr))) {
    veError(MODULE,"veDeviceParseElem: missing type in spec: %s", spec);
    veFree(dup);
    return NULL; /* type */
  }

  /* parse content based upon name */
  content = NULL;
  s = ptr ? ptr : "";
  if (strcmp(type,"trigger") == 0)
    content = parse_trigger(s);
  else if (strcmp(type,"switch") == 0)
    content = parse_switch(s);
  else if (strcmp(type,"valuator") == 0)
    content = parse_valuator(s);
  else if (strcmp(type,"vector") == 0)
    content = parse_vector(s);
  else if (strcmp(type,"keyboard") == 0)
    content = parse_keyboard(s);
  else
    veError(MODULE,"veDeviceParseElem: unknown elem type %s in spec: %s",
	    type, spec);

  if (!content) {
    veFree(dup);
    return NULL; /* parsing of content failed - don't continue */
  }

  e = veAllocObj(VeDeviceElement);
  e->name = veDupString(name);
  e->content = content;
  return e;
}
Ejemplo n.º 3
0
/** Parse statement.
 *
 * @param parse		Parser object.
 * @return		New syntax tree node.
 */
stree_stat_t *parse_stat(parse_t *parse)
{
	stree_stat_t *stat;

	stree_vdecl_t *vdecl_s;
	stree_if_t *if_s;
	stree_switch_t *switch_s;
	stree_while_t *while_s;
	stree_for_t *for_s;
	stree_raise_t *raise_s;
	stree_break_t *break_s;
	stree_return_t *return_s;
	stree_wef_t *wef_s;
	stree_exps_t *exp_s;

#ifdef DEBUG_PARSE_TRACE
	printf("Parse statement.\n");
#endif
	switch (lcur_lc(parse)) {
	case lc_var:
		vdecl_s = parse_vdecl(parse);
		stat = stree_stat_new(st_vdecl);
		stat->u.vdecl_s = vdecl_s;
		break;
	case lc_if:
		if_s = parse_if(parse);
		stat = stree_stat_new(st_if);
		stat->u.if_s = if_s;
		break;
	case lc_switch:
		switch_s = parse_switch(parse);
		stat = stree_stat_new(st_switch);
		stat->u.switch_s = switch_s;
		break;
	case lc_while:
		while_s = parse_while(parse);
		stat = stree_stat_new(st_while);
		stat->u.while_s = while_s;
		break;
	case lc_for:
		for_s = parse_for(parse);
		stat = stree_stat_new(st_for);
		stat->u.for_s = for_s;
		break;
	case lc_raise:
		raise_s = parse_raise(parse);
		stat = stree_stat_new(st_raise);
		stat->u.raise_s = raise_s;
		break;
	case lc_break:
		break_s = parse_break(parse);
		stat = stree_stat_new(st_break);
		stat->u.break_s = break_s;
		break;
	case lc_return:
		return_s = parse_return(parse);
		stat = stree_stat_new(st_return);
		stat->u.return_s = return_s;
		break;
	case lc_do:
	case lc_with:
		wef_s = parse_wef(parse);
		stat = stree_stat_new(st_wef);
		stat->u.wef_s = wef_s;
		break;
	default:
		exp_s = parse_exps(parse);
		stat = stree_stat_new(st_exps);
		stat->u.exp_s = exp_s;
		break;
	}

#ifdef DEBUG_PARSE_TRACE
	printf("Parsed statement %p\n", stat);
#endif
	return stat;
}
Ejemplo n.º 4
0
/* Handle command line options. */
static void parse_args(struct eject_control *ctl, int argc, char **argv)
{
	static const struct option long_opts[] =
	{
		{"auto",	required_argument, NULL, 'a'},
		{"cdrom",	no_argument,	   NULL, 'r'},
		{"cdspeed",	required_argument, NULL, 'x'},
		{"changerslot", required_argument, NULL, 'c'},
		{"default",	no_argument,	   NULL, 'd'},
		{"floppy",	no_argument,	   NULL, 'f'},
		{"force",       no_argument,       NULL, 'F'},
		{"help",	no_argument,	   NULL, 'h'},
		{"listspeed",   no_argument,       NULL, 'X'},
		{"manualeject", required_argument, NULL, 'i'},
		{"noop",	no_argument,	   NULL, 'n'},
		{"no-unmount",	no_argument,	   NULL, 'm'},
		{"no-partitions-unmount", no_argument, NULL, 'M' },
		{"proc",	no_argument,	   NULL, 'p'},
		{"scsi",	no_argument,	   NULL, 's'},
		{"tape",	no_argument,	   NULL, 'q'},
		{"trayclose",	no_argument,	   NULL, 't'},
		{"traytoggle",	no_argument,	   NULL, 'T'},
		{"verbose",	no_argument,	   NULL, 'v'},
		{"version",	no_argument,	   NULL, 'V'},
		{0, 0, 0, 0}
	};
	int c;

	while ((c = getopt_long(argc, argv,
				"a:c:i:x:dfFhnqrstTXvVpmM", long_opts, NULL)) != -1) {
		switch (c) {
		case 'a':
			ctl->a_option = 1;
			ctl->a_arg = parse_switch(optarg, _("argument error"),
						"on", "off",  "1", "0",  NULL);
			break;
		case 'c':
			ctl->c_option = 1;
			ctl->c_arg = strtoul_or_err(optarg, _("invalid argument to --changerslot/-c option"));
			break;
		case 'x':
			ctl->x_option = 1;
			ctl->x_arg = strtoul_or_err(optarg, _("invalid argument to --cdspeed/-x option"));
			break;
		case 'd':
			ctl->d_option = 1;
			break;
		case 'f':
			ctl->f_option = 1;
			break;
		case 'F':
			ctl->F_option = 1;
			break;
		case 'h':
			usage(stdout);
			break;
		case 'i':
			ctl->i_option = 1;
			ctl->i_arg = parse_switch(optarg, _("argument error"),
						"on", "off",  "1", "0",  NULL);
			break;
		case 'm':
			ctl->m_option = 1;
			break;
		case 'M':
			ctl->M_option = 1;
			break;
		case 'n':
			ctl->n_option = 1;
			break;
		case 'p':
			ctl->p_option = 1;
			break;
		case 'q':
			ctl->q_option = 1;
			break;
		case 'r':
			ctl->r_option = 1;
			break;
		case 's':
			ctl->s_option = 1;
			break;
		case 't':
			ctl->t_option = 1;
			break;
		case 'T':
			ctl->T_option = 1;
			break;
		case 'X':
			ctl->X_option = 1;
			break;
		case 'v':
			ctl->v_option = 1;
			break;
		case 'V':
			printf(UTIL_LINUX_VERSION);
			exit(EXIT_SUCCESS);
			break;
		default:
		case '?':
			usage(stderr);
			break;
		}
	}

	/* check for a single additional argument */
	if ((argc - optind) > 1)
		errx(EXIT_FAILURE, _("too many arguments"));

	if ((argc - optind) == 1)
		ctl->device = xstrdup(argv[optind]);
}
Ejemplo n.º 5
0
static void parse_option(struct setterm_control *ctl, int ac, char **av)
{
	int c;
	enum {
		OPT_TERM = CHAR_MAX + 1,
		OPT_RESET,
		OPT_RESIZE,
		OPT_INITIALIZE,
		OPT_CURSOR,
		OPT_REPEAT,
		OPT_APPCURSORKEYS,
		OPT_LINEWRAP,
		OPT_DEFAULT,
		OPT_FOREGROUND,
		OPT_BACKGROUND,
		OPT_ULCOLOR,
		OPT_HBCOLOR,
		OPT_INVERSESCREEN,
		OPT_BOLD,
		OPT_HALF_BRIGHT,
		OPT_BLINK,
		OPT_REVERSE,
		OPT_UNDERLINE,
		OPT_STORE,
		OPT_CLEAR,
		OPT_TABS,
		OPT_CLRTABS,
		OPT_REGTABS,
		OPT_BLANK,
		OPT_DUMP,
		OPT_APPEND,
		OPT_FILE,
		OPT_MSG,
		OPT_MSGLEVEL,
		OPT_POWERSAVE,
		OPT_POWERDOWN,
		OPT_BLENGTH,
		OPT_BFREQ,
		OPT_VERSION,
		OPT_HELP
	};
	static const struct option longopts[] = {
		{"term", required_argument, NULL, OPT_TERM},
		{"reset", no_argument, NULL, OPT_RESET},
		{"resize", no_argument, NULL, OPT_RESIZE},
		{"initialize", no_argument, NULL, OPT_INITIALIZE},
		{"cursor", required_argument, NULL, OPT_CURSOR},
		{"repeat", required_argument, NULL, OPT_REPEAT},
		{"appcursorkeys", required_argument, NULL, OPT_APPCURSORKEYS},
		{"linewrap", required_argument, NULL, OPT_LINEWRAP},
		{"default", no_argument, NULL, OPT_DEFAULT},
		{"foreground", required_argument, NULL, OPT_FOREGROUND},
		{"background", required_argument, NULL, OPT_BACKGROUND},
		{"ulcolor", required_argument, NULL, OPT_ULCOLOR},
		{"hbcolor", required_argument, NULL, OPT_HBCOLOR},
		{"inversescreen", required_argument, NULL, OPT_INVERSESCREEN},
		{"bold", required_argument, NULL, OPT_BOLD},
		{"half-bright", required_argument, NULL, OPT_HALF_BRIGHT},
		{"blink", required_argument, NULL, OPT_BLINK},
		{"reverse", required_argument, NULL, OPT_REVERSE},
		{"underline", required_argument, NULL, OPT_UNDERLINE},
		{"store", no_argument, NULL, OPT_STORE},
		{"clear", required_argument, NULL, OPT_CLEAR},
		{"tabs", optional_argument, NULL, OPT_TABS},
		{"clrtabs", optional_argument, NULL, OPT_CLRTABS},
		{"regtabs", optional_argument, NULL, OPT_REGTABS},
		{"blank", optional_argument, NULL, OPT_BLANK},
		{"dump", optional_argument, NULL, OPT_DUMP},
		{"append", required_argument, NULL, OPT_APPEND},
		{"file", required_argument, NULL, OPT_FILE},
		{"msg", required_argument, NULL, OPT_MSG},
		{"msglevel", required_argument, NULL, OPT_MSGLEVEL},
		{"powersave", required_argument, NULL, OPT_POWERSAVE},
		{"powerdown", optional_argument, NULL, OPT_POWERDOWN},
		{"blength", optional_argument, NULL, OPT_BLENGTH},
		{"bfreq", optional_argument, NULL, OPT_BFREQ},
		{"version", no_argument, NULL, OPT_VERSION},
		{"help", no_argument, NULL, OPT_HELP},
		{NULL, 0, NULL, 0}
	};
	static const ul_excl_t excl[] = {
		{ OPT_DEFAULT, OPT_STORE },
		{ OPT_TABS, OPT_CLRTABS, OPT_REGTABS },
		{ OPT_MSG, OPT_MSGLEVEL },
		{ 0 }
	};
	int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;

	while ((c = getopt_long_only(ac, av, "", longopts, NULL)) != -1) {
		err_exclusive_options(c, longopts, excl, excl_st);
		switch (c) {
		case OPT_TERM:
			ctl->opt_term = set_opt_flag(ctl->opt_term);
			ctl->opt_te_terminal_name = optarg;
			break;
		case OPT_RESET:
			ctl->opt_reset = set_opt_flag(ctl->opt_reset);
			break;
		case OPT_RESIZE:
			ctl->opt_resize = set_opt_flag(ctl->opt_resize);
			break;
		case OPT_INITIALIZE:
			ctl->opt_initialize = set_opt_flag(ctl->opt_initialize);
			break;
		case OPT_CURSOR:
			ctl->opt_cursor = set_opt_flag(ctl->opt_cursor);
			ctl->opt_cu_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_REPEAT:
			ctl->opt_repeat = set_opt_flag(ctl->opt_repeat);
			ctl->opt_rep_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_APPCURSORKEYS:
			ctl->opt_appcursorkeys = set_opt_flag(ctl->opt_appcursorkeys);
			ctl->opt_appck_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_LINEWRAP:
			ctl->opt_linewrap = set_opt_flag(ctl->opt_linewrap);
			ctl->opt_li_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_DEFAULT:
			ctl->opt_default = set_opt_flag(ctl->opt_default);
			break;
		case OPT_FOREGROUND:
			ctl->opt_foreground = set_opt_flag(ctl->opt_foreground);
			ctl->opt_fo_color = parse_febg_color(optarg);
			break;
		case OPT_BACKGROUND:
			ctl->opt_background = set_opt_flag(ctl->opt_background);
			ctl->opt_ba_color = parse_febg_color(optarg);
			break;
		case OPT_ULCOLOR:
			ctl->opt_ulcolor = set_opt_flag(ctl->opt_ulcolor);
			ctl->opt_ul_color = parse_ulhb_color(av, &optind);
			break;
		case OPT_HBCOLOR:
			ctl->opt_hbcolor = set_opt_flag(ctl->opt_hbcolor);
			ctl->opt_hb_color = parse_ulhb_color(av, &optind);
			break;
		case OPT_INVERSESCREEN:
			ctl->opt_inversescreen = set_opt_flag(ctl->opt_inversescreen);
			ctl->opt_invsc_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_BOLD:
			ctl->opt_bold = set_opt_flag(ctl->opt_bold);
			ctl->opt_bo_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_HALF_BRIGHT:
			ctl->opt_halfbright = set_opt_flag(ctl->opt_halfbright);
			ctl->opt_hb_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_BLINK:
			ctl->opt_blink = set_opt_flag(ctl->opt_blink);
			ctl->opt_bl_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_REVERSE:
			ctl->opt_reverse = set_opt_flag(ctl->opt_reverse);
			ctl->opt_re_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_UNDERLINE:
			ctl->opt_underline = set_opt_flag(ctl->opt_underline);
			ctl->opt_un_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_STORE:
			ctl->opt_store = set_opt_flag(ctl->opt_store);
			break;
		case OPT_CLEAR:
			ctl->opt_clear = set_opt_flag(ctl->opt_clear);
			ctl->opt_cl_all = parse_switch(optarg, _("argument error"),
						"all", "reset", NULL);
			break;
		case OPT_TABS:
			ctl->opt_tabs = set_opt_flag(ctl->opt_tabs);
			parse_tabs(av, optarg, &optind, ctl->opt_tb_array);
			break;
		case OPT_CLRTABS:
			ctl->opt_clrtabs = set_opt_flag(ctl->opt_clrtabs);
			parse_tabs(av, optarg, &optind, ctl->opt_tb_array);
			break;
		case OPT_REGTABS:
			ctl->opt_regtabs = set_opt_flag(ctl->opt_regtabs);
			ctl->opt_rt_len = parse_regtabs(av, optarg, &optind);
			break;
		case OPT_BLANK:
			ctl->opt_blank = set_opt_flag(ctl->opt_blank);
			ctl->opt_bl_min = parse_blank(av, optarg, &optind);
			break;
		case OPT_DUMP:
			ctl->opt_snap = set_opt_flag(ctl->opt_snap);
			ctl->opt_sn_num = parse_snap(av, optarg, &optind);
			break;
		case OPT_APPEND:
			ctl->opt_append = set_opt_flag(ctl->opt_append);
			ctl->opt_sn_num = parse_snap(av, optarg, &optind);
			break;
		case OPT_FILE:
			ctl->opt_snapfile = set_opt_flag(ctl->opt_snapfile);
			ctl->opt_sn_name = optarg;
			break;
		case OPT_MSG:
			ctl->opt_msg = set_opt_flag(ctl->opt_msg);
			ctl->opt_msg_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_MSGLEVEL:
			ctl->opt_msglevel = set_opt_flag(ctl->opt_msglevel);
			ctl->opt_msglevel_num = parse_msglevel(optarg);
			if (ctl->opt_msglevel_num == 0) {
				ctl->opt_msg = set_opt_flag(ctl->opt_msg);
				ctl->opt_msg_on |= 1;
			}
			break;
		case OPT_POWERSAVE:
			ctl->opt_powersave = set_opt_flag(ctl->opt_powersave);
			ctl->opt_ps_mode = parse_powersave(optarg);
			break;
		case OPT_POWERDOWN:
			ctl->opt_powerdown = set_opt_flag(ctl->opt_powerdown);
			ctl->opt_pd_min = parse_blank(av, optarg, &optind);
			break;
		case OPT_BLENGTH:
			ctl->opt_blength = set_opt_flag(ctl->opt_blength);
			ctl->opt_blength_l = parse_blength(av, optarg, &optind);
			break;
		case OPT_BFREQ:
			ctl->opt_bfreq = set_opt_flag(ctl->opt_bfreq);
			ctl->opt_bfreq_f = parse_bfreq(av, optarg, &optind);
			break;
		case OPT_VERSION:
			printf(UTIL_LINUX_VERSION);
			exit(EXIT_SUCCESS);
		case OPT_HELP:
			usage();
		default:
			errtryhelp(EXIT_FAILURE);
		}
	}
}
Ejemplo n.º 6
0
int main(int argc, char* argv[]) {
	try {
    TCLAP::CmdLine cmd("Command description message", ' ', "0.2");
    
    TCLAP::SwitchArg lex_switch("l", "lex", "Process up to the Lexer phase");
    TCLAP::SwitchArg parse_switch("p", "parse", 
                                  "Process up to the Parser phase");
    TCLAP::SwitchArg sem_switch("s", "sem", 
                                "Process up to the Semantic Analyser phase");
    TCLAP::SwitchArg tup_switch("t", "tup", 
                                "Process up to the Tuple phase");
    TCLAP::SwitchArg compile_switch("c", "compile", 
                                    "Process all phases and compile (default)",
                                    true);
    TCLAP::SwitchArg quiet_switch("q", "quiet", 
                                  "Only display error messages (default)", true);
    TCLAP::SwitchArg verbose_switch("v", "verbose", 
                                    "Display all Trace messages");
    TCLAP::ValueArg< std::string > out_arg("o", "out", 
                                           "Output file (default -out=STDOUT)", 
                                           false, "out", "string");
    TCLAP::ValueArg< std::string > err_arg("e", "err", 
                                           "Error file (default -err=STDOUT)", 
                                           false, "err", "string");
    TCLAP::UnlabeledMultiArg<std::string> input_args("input", "Input files", true, "string");
    
    std::vector<TCLAP::Arg*> args;
    args.push_back(&lex_switch);
    args.push_back(&parse_switch);
    args.push_back(&sem_switch);
    args.push_back(&tup_switch);
    args.push_back(&compile_switch);
    cmd.xorAdd(args);
    cmd.add(quiet_switch);
    cmd.add(verbose_switch);
    cmd.add(out_arg);
    cmd.add(err_arg);
    cmd.add(input_args);

    cmd.parse(argc, argv);

    std::vector< std::string > inputFiles = input_args.getValue();
    if (inputFiles.size() == 0) {
      fprintf(stderr, "No input files. Please specify 1 or more .cs13 files using -I <file>\n");
      return -1;
    }

    Administrator *admin;
    std::string err = err_arg.getValue();
    if (err.compare("err") != 0) {  
      // Have to create the error file to be able to write to it
      std::ofstream filestream(err_arg.getValue());
      if (!filestream) {
        fprintf(stderr, "Error creating %s\n", err_arg.getValue().c_str());
        return -1;
      }
      filestream.clear();
      filestream.close();
      admin = new Administrator(inputFiles, err_arg.getValue());
    } else {
      admin = new Administrator(inputFiles);
    }
    std::string out = out_arg.getValue();
    if (out.compare("out") != 0) {
      admin->set_output_file(out_arg.getValue());
    }

    if (verbose_switch.isSet()) {
      admin->ShowTrace(true);
    }
    
    if (lex_switch.isSet()) {
      admin->LexerPhase();
    } else if (parse_switch.isSet()) {
      admin->ParserPhase();
    } else if (sem_switch.getValue()) {
		  admin->SemanticAnalysisPhase();
    } else if (tup_switch.isSet()) {
      admin->TupleGenerationPhase();
    } else {
      admin->Compile();
    }    
	} catch(TCLAP::ArgException &e) {
    fprintf(stderr, "error: %s for arg %s\n", e.error().c_str(), e.argId().c_str());
	}

  return 0;
}