Exemple #1
0
TreeNode * factor(void)
{ TreeNode * t = NULL;
  switch (token) {
    case NUM :
      t = newExpNode(ConstK);
      if ((t!=NULL) && (token==NUM))
        t->attr.val = atoi(tokenString);
      match(NUM);
      break;
    case ID :
      t = newExpNode(IdK);
      if ((t!=NULL) && (token==ID))
        t->attr.name = copyString(tokenString);
      match(ID);
      break;
    case LPAREN :
      match(LPAREN);
      t = expres();
      match(RPAREN);
      break;
    default:
      syntaxError("unexpected token -> ");
      printToken(token,tokenString);
      token = getToken();
      break;
    }
  return t;
}
Exemple #2
0
/* EBNF: factor -> "("expression")" | var | call | NUM */
static TreeNode * factor (void)
{ TreeNode * t;
  TreeNode * p;

  while (token == COMMENT) unexpectedTokenHandling();
  switch (token) { 
    case LPAREN: /* "("expression")" */
      match (LPAREN);
      t = expression();
      match (RPAREN);
      break; 
    case ID: /* var | call */
      t = newExpNode (IdK);
      if ((t != NULL) && (token == ID))
      	t->attr.name = copyString (tokenString);
      match (ID);

      while (token == COMMENT) unexpectedTokenHandling();
      /* call -> ID"("args")" */
      if (token == LPAREN)
      { p = newExpNode (CallK);
      	p->attr.name = t->attr.name;
      	t = p;
      	match (LPAREN);
      	t->child[0] = args();
      	match (RPAREN);
      }
      /* var -> ID"["expression"]" */
      else if (token == SLPAREN) 
      { p = newExpNode (IdK);
      	p->attr.name = t->attr.name;
      	t = p;
      	match (SLPAREN);
      	t->child[0] = expression();
      	match (SRPAREN);
      }
      break;
    case NUM: /* NUM */
      t = newExpNode (ConstK);
      if ((t != NULL) && (token == NUM))
        t->attr.val = atoi(tokenString);
      match (NUM);
      break;
    default:
      unexpectedTokenHandling();
      break;
  }
  return t;
}
Exemple #3
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;
}
Exemple #4
0
static exp_node *expression(void){
    exp_node *expn = newExpNode();
    
    if(match(TOKEN_LBRA,       NO_CONSUME) ||
       match(TOKEN_IDENTIFIER, NO_CONSUME) ||
       match(TOKEN_LITERAL,    NO_CONSUME))
	if((expn->termn = term()) != NULL)
	    if((expn->termtln = term_tail()) != error)
		return expn;

    freeExp(expn);
    return NULL;
}
Exemple #5
0
TreeNode * term(void)
{ TreeNode * t = factor();
  while ((token==TIMES)||(token==OVER))
  { TreeNode * p = newExpNode(OpK);
    if (p!=NULL) {
      p->child[0] = t;
      p->attr.op = token;
      t = p;
      match(token);
      p->child[1] = factor();
    }
  }
  return t;
}
Exemple #6
0
TreeNode * simple_exp(void)
{ TreeNode * t = term();
  while ((token==PLUS)||(token==MINUS))
  { TreeNode * p = newExpNode(OpK);
    if (p!=NULL) {
      p->child[0] = t;
      p->attr.op = token;
      t = p;
      match(token);
      t->child[1] = term();
    }
  }
  return t;
}
Exemple #7
0
TreeNode * term(void) //term -> factor [mulop term]
{ TreeNode * t = factor();
  while ((token==MUL)||(token==DIV))
  { TreeNode * p = newExpNode(OpK);
    if (p!=NULL) {
      p->child[0] = t;
      p->attr.op = token;
      t = p;
      match(token);
      p->child[1] = factor();
    }
  }
  return t;
}
Exemple #8
0
TreeNode * expres(void)
{ TreeNode * t = simple_exp();
  if ((token==LT)||(token==EQ)) {
    TreeNode * p = newExpNode(OpK);
    if (p!=NULL) {
      p->child[0] = t;
      p->attr.op = token;
      t = p;
    }
    match(token);
    if (t!=NULL)
      t->child[1] = simple_exp();
  }
  return t;
}
Exemple #9
0
/* EBNF: term -> factor {mulop factor} */
static TreeNode * term (void)
{ TreeNode * t = factor();
  TreeNode * newNode;

  while (token == COMMENT) unexpectedTokenHandling();
  /* {mulop factor} */
  while (isMulop(token))
  { newNode = newExpNode (OpK);
    if (newNode != NULL)
    { newNode->child[0] = t;
      newNode->attr.op = token;	    
      t = newNode;
    }
    match (token); // match mulop
    if (t != NULL)
      t->child[1] = factor();
  }
  return t;
}
Exemple #10
0
/* EBNF: additive-expression -> term {addop term} */
static TreeNode * additive_expression (void)
{ TreeNode * t = term();
  TreeNode * newNode;

  while (token == COMMENT) unexpectedTokenHandling();
  /* {addop term} */
  while (isAddop(token))
  { newNode = newExpNode (OpK);
    if (newNode != NULL)
    { newNode->child[0] = t;
      newNode->attr.op = token;
      t = newNode;
    }
    match (token); // match addop
    if (t != NULL)
      t->child[1] = term();
  }
  return t;
}
Exemple #11
0
/* EBNF: simple-expression -> 
 * addtivite-expression [relop additivie-expression] */
static TreeNode * simple_expression (void)
{ TreeNode * t = additive_expression();
  TreeNode * newNode;

  while (token == COMMENT) unexpectedTokenHandling();
  /* [relop additive-expression]  */
  if (isRelop(token))
  { newNode = newExpNode (OpK);
    if (newNode != NULL)
    { newNode->child[0] = t;
      newNode->attr.op = token;
      t = newNode;
    }
    match (token); // match relop
    if (t != NULL)
      t->child[1] = additive_expression();
  }
  return t;
}
Exemple #12
0
TreeNode* expression(void)
{
    TreeNode* t = simple_exp();

    if (s_token == LT || s_token == EQ )
    {
        TreeNode* p = newExpNode(OpK);
        if (p != NULL)
        {
            p->child[0] = t;
            p->attr.op = s_token;
            t = p;
        }

        match(s_token); // TODO: This is equal to getToken(). why get a new token at here ?
        if (t != NULL);
        t->child[1] = simple_exp();
    }

    return t;
}
Exemple #13
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;
}
Exemple #14
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;
}