Beispiel #1
0
/* var-declaration -> type-specifier ID ["["NUM"]"]";" */
static TreeNode * var_declaration (void)
{ TreeNode * t = NULL;
  TreeNode * newNode = NULL;
 
  while (token == COMMENT) unexpectedTokenHandling();
  /* type-specifier */
  switch (token) {
    case VOID:
      t = newStmtNode(VarDecK);
      if (t != NULL) t->type = Void;
      match (VOID);
      break;
    case INT:
      t = newStmtNode(VarDecK);
      if (t != NULL) t->type = Integer;
      match (INT);
      break;
    default: unexpectedTokenHandling(); break;
  }

  while (token == COMMENT) unexpectedTokenHandling();
  /* ID */
  switch (token) {
    case ID:
      if (t != NULL)
        t->attr.name = copyString(tokenString);
      match (ID);
      break;
    default: unexpectedTokenHandling(); break;
  }

  while (token == COMMENT) unexpectedTokenHandling();
  switch (token) {
    /* var-declaration -> type-specifier ID";" */
    case SEMI:
      match (SEMI);
      break;
    /* var-declaration -> type-specifier ID "["NUM"]"";" */
    case SLPAREN:
      t->kind.stmt = ArrayDecK;
      match (SLPAREN);
      newNode = newExpNode (ConstK);
      if ((t != NULL) && (newNode != NULL))
      { newNode->attr.val = atoi (tokenString);
        t->child[0] = newNode;
      }
      match (NUM);
      match (SRPAREN);
      match (SEMI);
      break;
    default:
      unexpectedTokenHandling();
      break;
  }
  return t;
}
Beispiel #2
0
/* EBNF: param -> type-specifier ID ["[""]"] */
static TreeNode * param (void)
{ TreeNode * t = NULL;
  t = newStmtNode (ParamK);

  while (token == COMMENT) unexpectedTokenHandling();
  /* type-specifier */
  if (token == INT)
  { t->type = Integer;
    match (INT);
  }
  else if (token == VOID)
  { t->type = Void;
    match (VOID);
  }
  else 
    unexpectedTokenHandling();

  while (token == COMMENT) unexpectedTokenHandling();
  /* ID */
  if ((t != NULL) && (token == ID))
  { t->attr.name = copyString (tokenString);
    match (ID);
  }

  while (token == COMMENT) unexpectedTokenHandling();
  /* ["[""]"]*/
  if (token == SLPAREN)
  { match (SLPAREN); 
    match (SRPAREN); 
  }
  return t;
}
Beispiel #3
0
static void insertIOFunc(void)
{   TreeNode *func;
    TreeNode *typeSpec;
    TreeNode *param;
    TreeNode *compStmt;

    func = newDeclNode(FuncK);

    typeSpec = newTypeNode(FuncK);
    typeSpec->attr.type = INT;
    func->type = Integer;

    compStmt = newStmtNode(CompK);
    compStmt->child[0] = NULL;      // no local var
    compStmt->child[1] = NULL;      // no stmt

    func->lineno = 0;
    func->attr.name = "input";
    func->child[0] = typeSpec;
    func->child[1] = NULL;          // no param
    func->child[2] = compStmt;

    st_insert("input", -1, addLocation(), func);

    func = newDeclNode(FuncK);

    typeSpec = newTypeNode(FuncK);
    typeSpec->attr.type = VOID;
    func->type = Void;

    param = newParamNode(NonArrParamK);
    param->attr.name = "arg";
    param->child[0] = newTypeNode(FuncK);
    param->child[0]->attr.type = INT;

    compStmt = newStmtNode(CompK);
    compStmt->child[0] = NULL;      // no local var
    compStmt->child[1] = NULL;      // no stmt

    func->lineno = 0;
    func->attr.name = "output";
    func->child[0] = typeSpec;
    func->child[1] = param;
    func->child[2] = compStmt;

    st_insert("output", -1, addLocation(), func);
}
Beispiel #4
0
TreeNode * repeat_stmt(void)
{ TreeNode * t = newStmtNode(RepeatK);
  match(REPEAT);
  if (t!=NULL) t->child[0] = stmt_sequence();
  match(UNTIL);
  if (t!=NULL) t->child[1] = expres();
  return t;
}
Beispiel #5
0
TreeNode *write_stmt(void)
{
    TreeNode *t = newStmtNode(WriteK);
    match(WRITE);
    if (t != NULL)
        t->child[0] = exp();
    return t;
}
Beispiel #6
0
TreeNode * read_stmt(void)
{ TreeNode * t = newStmtNode(ReadK);
  match(READ);
  if ((t!=NULL) && (token==ID))
    t->attr.name = copyString(tokenString);
  match(ID);
  return t;
}
Beispiel #7
0
TreeNode * assign_stmt(void)
{ TreeNode * t = newStmtNode(AssignK);
  if ((t!=NULL) && (token==ID))
    t->attr.name = copyString(tokenString);
  match(ID);
  match(ASSIGN);
  if (t!=NULL) t->child[0] = expres();
  return t;
}
Beispiel #8
0
TreeNode * while_stmt(void) //while_stmt -> WHILE '(' expression ')' '{'statement_list'}'
{
	TreeNode * t = newStmtNode(WhileK);
	match(WHILE); match(LPAREN);
	if(t!=NULL) t->child[0] = exp();
	match(RPAREN); match(LBRACE);
	if(t!=NULL) t->child[1] = statement_list();
	//match(RBRACE);
	return t;
}
Beispiel #9
0
TreeNode * if_stmt(void)
{ TreeNode * t = newStmtNode(IfK);
  match(IF);
  if (t!=NULL) t->child[0] = expres();
  match(THEN);
  if (t!=NULL) t->child[1] = stmt_sequence();
  if (token==ELSE) {
    match(ELSE);
    if (t!=NULL) t->child[2] = stmt_sequence();
  }
  match(ENDIF);
  return t;
}
Beispiel #10
0
/* EBNF: params -> param-list | void */
static TreeNode * params (void)
{ TreeNode * t = NULL;

  while (token == COMMENT) unexpectedTokenHandling();
  if (token == VOID)
  { t = newStmtNode (ParamK);
    t->type = Void;
    match (VOID);
  }
  else
    t = param_list();
  return t;
}
Beispiel #11
0
TreeNode * if_stmt(void)//if_stmt -> IF '(' expression ')' '{'statement_list'}' [ELSE '{'statement_list'}']
{ TreeNode * t = newStmtNode(IfK);
  match(IF);match(LPAREN);
  if (t!=NULL) t->child[0] = exp();
  match(RPAREN);match(LBRACE);
  if (t!=NULL) t->child[1] = statement_list();
  //match(RBRACE);
  if (token==ELSE) {
    match(ELSE);match(LBRACE);
    if (t!=NULL) t->child[2] = statement_list();
    //match(RBRACE);
  }
  return t;
}
Beispiel #12
0
TreeNode * for_stmt(void) //for_stmt -> FOR '('expression';'expression';'expression')''{'statement_list'}'
{
	TreeNode * t = newStmtNode(ForK);
	match(FOR); match(LPAREN);
	if(t!=NULL) t->child[0] = assign_list();
	match(SEMI);
	if(t!=NULL) t->child[1] = exp();
	match(SEMI);
	if(t!=NULL) t->child[2] = assign_list();
	match(RPAREN); match(LBRACE);
	if(t!=NULL) t->child[3]=statement_list();
	//match(RBRACE);
	return t;
}
Beispiel #13
0
TreeNode * var_stmt(void) //var_declaration -> type_specifer ID ';'
{
	TreeNode * t = newStmtNode(Vark);
	switch(token)
	{
		case INT :
			t->type = Integer;
			match(INT); break;
		case CHAR :
			t->type = Char;
			match(CHAR); break;
	}
	if((t!=NULL)&&token==ID) t->attr.name=copyString(tokenString);
	match(ID); match(SEMI);
	return t;
}
Beispiel #14
0
/* return-stmt -> return [expression]";" */
static TreeNode * return_stmt (void)
{ TreeNode * t = NULL;

  while (token == COMMENT) unexpectedTokenHandling();
  if (token == RETURN)
  { match (RETURN);
    t = newStmtNode (ReturnK);
  }

  while (token == COMMENT) unexpectedTokenHandling();
  /* [expression] */
  if (token != SEMI)
    t->child[0] = expression();

  match (SEMI);
  return t;
}
Beispiel #15
0
/* EBNF: compound-stmt -> "{"local-declarations statement-list"}" */
static TreeNode * compound_stmt (void)
{ TreeNode * t = NULL;

  while (token == COMMENT) unexpectedTokenHandling();
  if (token == CLPAREN) /* "{" */
  { t = newStmtNode (ComK);
    match (CLPAREN);
    if (t != NULL)
    { t->child[0] = local_declarations();
      t->child[1] = statement_list();
    }
    match (CRPAREN);
  }
  else
    unexpectedTokenHandling();

  return t;
}
Beispiel #16
0
TreeNode * for_stmt(void)
{
    TreeNode * t = newStmtNode(ForK);
    TreeNode * temp ;
    TreeNode * ultimoIrmao ;
    match(FOR);
    switch(token){
      case LPAREN:
        match(LPAREN);
        if (t!= NULL) t->child[0] = assign_stmt();
        match(COMMA);
        if (t!= NULL) t->child[1] = expres();
        match(COMMA);
        if (temp!= NULL) temp = assign_stmt();
        match(RPAREN);
        if (t!= NULL) t->child[2] = stmt_sequence();

        if (t->child[2] != NULL){ //Se o terceiro filho tiver algo,
          if(t->child[2]->sibling != NULL){ //Se o nó tem irmão, vamos fazer um loop para chegar até o ultimo irmão.
            ultimoIrmao = t->child[2]->sibling;
            while ( ultimoIrmao->sibling != NULL){ //Loop para chegar em ultimo irmão
              ultimoIrmao = ultimoIrmao->sibling;
           }
           ultimoIrmao->sibling = temp;
          }
          else {
            t->child[2]->sibling = temp;
          }
        }
        else{ //Se o que tem dentro do FOR for nada, então vamos só fazer as iterações
          t->child[2] = temp;
        }
        
        match(ENDFOR);
      break;
    default:
      syntaxError("unexpected token -> ");
      printToken(token,tokenString);
      token = getToken();
      break;
    }
    return t;

}
Beispiel #17
0
/* iteration-stmt -> while (expression) statement */
static TreeNode * iteration_stmt (void)
{ TreeNode * t = NULL;
  TreeNode * cond_exp = NULL;
  TreeNode * iter_stmt = NULL;

  while (token == COMMENT) unexpectedTokenHandling();
  if (token == WHILE)
  { t = newStmtNode (WhileK);
    match (WHILE);
    match (LPAREN);
    cond_exp = expression();
    match (RPAREN);
    iter_stmt = statement();
  }
  if (t != NULL)
  { t->child[0] = cond_exp;
    t->child[1] = iter_stmt;
  }
  return t;
}
Beispiel #18
0
/* selection-stmt -> 
 * if (expression) statement [else statement]*/
static TreeNode * selection_stmt (void)
{ TreeNode * t = NULL;

  while (token == COMMENT) unexpectedTokenHandling();
  if (token == IF)
  { t = newStmtNode (IfK);
    match (IF);
    match (LPAREN);
    if (t != NULL)
      t->child[0] = expression();
    match (RPAREN);
    if (t != NULL)
      t->child[1] = statement();
    /* [else statement] */
    if (token == ELSE)
    { match (ELSE);
      if (t != NULL)
        t->child[2] = statement();
    }
  }
  return t;
}
Beispiel #19
0
static stmt_node *statement(void){
    stmt_node *stmtn = newStmtNode();
    
    if(match(TOKEN_IDENTIFIER,   NO_CONSUME) ||
       match(TOKEN_SCOL,         NO_CONSUME)){
	if((stmtn->assn = assignment()) != NULL)
	    return stmtn;
    }
    else if(match(TOKEN_TYPEKEY, NO_CONSUME)){
	if((stmtn->decn = declaration()) != NULL)
	    return stmtn;
    }
    else if(match(TOKEN_IFKEY,   NO_CONSUME)){
	if((stmtn->ifn = if_()) != NULL)
	    return stmtn;
    }
    else if(match(TOKEN_WHILEKEY,NO_CONSUME)){
	if((stmtn->whilen = while_()) != NULL)
	    return stmtn;
    }
    else if(match(TOKEN_FORKEY,  NO_CONSUME)){
	if((stmtn->forn = for_()) != NULL)
	    return stmtn;
    }
    else if((stmtn->lCur = match(TOKEN_LCUR, CONSUME)) != NULL){
	if((stmtn->sln = statement_list()) != error)
	    if((stmtn->rCur = match(TOKEN_RCUR, CONSUME)) != NULL)
		return stmtn;
    }
    else if((stmtn->ins = match(TOKEN_INSTRUCTION, CONSUME)) != NULL)
	return stmtn;


    freeStmt(stmtn);
    return NULL;
}
Beispiel #20
0
/* expression -> var "=" expression | simple-expression 
 * var -> ID ["["expression"]"]*/
static TreeNode * expression (void)
{ TreeNode * t = newExpNode(IdK);
  TreeNode * p = NULL;
  TreeNode * newNode = NULL;
#define CALL 1
#define ARRAY 2
  int factorType = 0;

  while (token == COMMENT) unexpectedTokenHandling();
  if (token == ID)
  { 
    if (t != NULL)
      t->attr.name = copyString(tokenString);
    match (ID);
    /* var -> ID "["expression"]" */
    if (token == SLPAREN)
    { factorType = ARRAY;
      match (SLPAREN);
      t->child[0] = expression();
      match (SRPAREN);
      /* ID "["expression"]" "=" expression */
      if (token == ASSIGN)
      { match (ASSIGN);
      	newNode = newStmtNode(AssignK);
      	if (t != NULL)
        { newNode->child[0] = t;
          t= newNode;
          t->child[1] = expression();
          if (token != SEMI)
            unexpectedTokenHandling();
        }
      }
    }
    /* simple-expression => call => ID "("args")" */
    else if (token == LPAREN)
    { factorType = CALL;
      match (LPAREN);
      if (t != NULL)
      { t->nodekind = ExpK;
        t->kind.exp = CallK;
        t->child[0] = args();
      }
      match (RPAREN);
    }
    /* var | call is followed by assignment, operation,
     * semi-colon or comma. */
    while (token == COMMENT) unexpectedTokenHandling();
    if (token == ASSIGN)
    { if ((factorType == CALL) || (factorType == ARRAY))
        unexpectedTokenHandling();
      /* ID "=" expression */
      else
      { match (ASSIGN);
      	newNode = newStmtNode(AssignK);
      	if (t != NULL)
        { newNode->child[0] = t;
          t= newNode;
          t->child[1] = expression();
          if (token != SEMI)
            unexpectedTokenHandling();
        }
      }
    }
    /* ID [("("args")"|"["expression"]")] 
     * op additivie-expression */
    else if (isOp (token))
    { newNode = newExpNode (OpK);
      if (newNode != NULL)
      { newNode->child[0] = t;
      	newNode->attr.op = token;
      	t = newNode;
      }
      match (token);
      if (t != NULL)
        t->child[1] = additive_expression();
    }
    else if ((token != SEMI) && (token != COMMA) && (token != RPAREN))
      unexpectedTokenHandling();
  }
  else if ((token == LPAREN) || (token == NUM))
    t = simple_expression();
  else
    unexpectedTokenHandling();

  return t;
}
Beispiel #21
0
/* EBNF: declaration -> var-declaration | func-declaration 
 * both declaration types begin with 'type-specifier ID'
 * type-specifier -> int | void */
static TreeNode * declaration(void)
{ TreeNode * t = NULL;
  TreeNode * newNode = NULL;
  char * name = NULL;

  while (token == COMMENT) unexpectedTokenHandling();  
  /* type-specifier */
  switch (token) {
    case VOID:
      t = newStmtNode (VarDecK);
      if (t != NULL) t->type = Void;
      match (VOID);
      break;
    case INT:
      t = newStmtNode (VarDecK);
      if (t != NULL) t->type = Integer;
      match (INT);
      break;
    default: unexpectedTokenHandling(); break;
  }

  while (token == COMMENT) unexpectedTokenHandling(); 
  /* ID */
  switch (token) {
    case ID:
      if (t != NULL)
        t->attr.name = copyString(tokenString);
      match (ID);
      break;
    default: unexpectedTokenHandling(); break;
  }

  while (token == COMMENT) unexpectedTokenHandling();
  switch (token) {
    /* var-declaration -> type-specifier ID";" */
    case SEMI:
      match (SEMI);
      break;
    /* var-declaration -> type-specifier ID "["NUM"]"";" */
    case SLPAREN:
      if (t != NULL) 
        t->kind.stmt = ArrayDecK;
      match (SLPAREN);
      if (token != NUM)
        unexpectedTokenHandling();
      else
      { newNode = newExpNode (ConstK);
        if ((newNode != NULL) && (t != NULL))
       	{ newNode->attr.val = atoi(tokenString);
          t->child[0] = newNode;
          match (NUM);
        }
      }
      match (SRPAREN);
      match (SEMI);
    /* func-declaration -> 
     * type-specifier ID "("params")" compound-stmt */
    case LPAREN:
      if (t != NULL)
      { t->kind.stmt = FuncDecK;
        match (LPAREN);
        t->child[0] = params();
        match (RPAREN);
        t->child[1] = compound_stmt();
      }
      break;
    default: unexpectedTokenHandling(); break;
  }
  return t;
}