GCC主要数据结构之c_token

本文介绍了一种用于C语言预处理与词法分析的数据结构——C_token。该结构体用于存储各种类型的符号,包括关键字、标识符、预处理指令等,并记录了这些符号的位置信息及可能的附加信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

/* A single C token after string literal concatenation and conversion    of preprocessing tokens to tokens.  */ struct GTY (()) c_token {   /* The kind of token.  */   ENUM_BITFIELD (cpp_ttype) type : 8;   /* If this token is a CPP_NAME, this value indicates whether also      declared as some kind of type.  Otherwise, it is C_ID_NONE.  */   ENUM_BITFIELD (c_id_kind) id_kind : 8;   /* If this token is a keyword, this value indicates which keyword.      Otherwise, this value is RID_MAX.  */   ENUM_BITFIELD (rid) keyword : 8;   /* If this token is a CPP_PRAGMA, this indicates the pragma that      was seen.  Otherwise it is PRAGMA_NONE.  */   ENUM_BITFIELD (pragma_kind) pragma_kind : 8;   /* The location at which this token was found.  */   location_t location;   /* The value associated with this token, if any.  */   tree value;

  source_range get_range () const   {     return get_range_from_loc (line_table, location);   }

  location_t get_finish () const   {     return get_range ().m_finish;   } };

 
 
typedef struct table{ string id; int value; struct table *tail; }*Table_; Table_ Table(string id,int value,Table_ table){ Table tb = malloc(sizeof(struct table)); tb.id = tb.to_string(id); tb.value = value; tb->tail = tail; return tb; } Table_ table = null; int lookup(Table table,string id){ assert(table != null); if(id == table.id) return table.value; return lookup(table,id); } void update(Table_ *tabptr, string id, int value) { *tabptr = Table(id, value, *tabptr); } // Lexer and Parser Tokens typedef enum { TOK_EOF, TOK_INT, TOK_ID, TOK_ASSIGN, TOK_PRINT, TOK_LPAREN, TOK_RPAREN, TOK_SEMICOLON, TOK_PLUS, TOK_MINUS, TOK_TIMES, TOK_DIV, TOK_COMMA } TokenType; TokenType current_token; int int_val; string id_val; // Assume these functions are provided by the lexer void next_token(void); void lexer_error(void) { fprintf(stderr, "Lexer error\n"); exit(1); } 41 void match(TokenType expected) { if (current_token == expected) next_token(); else { fprintf(stderr, "Syntax error\n"); exit(1); } } // Recursive Descent Functions void prog(void); void stm(void); void parse_base_stm(void); void exps(void); int exp(void); int arith_exp(void); int arith_exp_tail(int left); int term(void); int term_tail(int left); int factor(void); 58 int main() { next_token(); prog(); return 0; } void prog() { stm(); } void stm() { parse_base_stm(); while (current_token == TOK_SEMICOLON) { match(TOK_SEMICOLON); parse_base_stm(); } } void parse_base_stm() { if (current_token == TOK_ID) { string id = id_val; match(TOK_ID); match(TOK_ASSIGN); int val = exp(); update(&table, id, val); } else if (current_token == TOK_PRINT) { match(TOK_PRINT); match(TOK_LPAREN); exps(); match(TOK_RPAREN); printf("\n"); } else { fprintf(stderr, "Syntax error in statement\n"); exit(1); } } void exps() { int val = exp(); printf("%d ", val); while (current_token == TOK_COMMA) { match(TOK_COMMA); val = exp(); printf("%d ", val); } } int exp() { if (current_token == TOK_ID || current_token == TOK_PRINT) { stm(); match(TOK_COMMA); return exp(); } return arith_exp(); } int arith_exp() { return arith_exp_tail(term()); } int arith_exp_tail(int left) { if (current_token == TOK_PLUS || current_token == TOK_MINUS) { TokenType op = current_token; match(current_token); int right = term(); left = (op == TOK_PLUS) ? left + right : left - right; return arith_exp_tail(left); } return left; } int term() { return term_tail(factor()); } int term_tail(int left) { if (current_token == TOK_TIMES || current_token == TOK_DIV) { TokenType op = current_token; match(current_token); int right = factor(); left = (op == TOK_TIMES) ? left * right : left / right; return term_tail(left); } return left; } int factor() { if (current_token == TOK_INT) { int val = int_val; match(TOK_INT); return val; } else if (current_token == TOK_ID) { string id = id_val; match(TOK_ID); return lookup(table, id); } else if (current_token == TOK_LPAREN) { match(TOK_LPAREN); int val = exp(); match(TOK_RPAREN); return val; } else { fprintf(stderr, "Syntax error in factor\n"); exit(1); } } 帮我修改带啊吗
最新发布
03-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值