📄 ifs_yacc.y
字号:
%token TOK_IDENTIFIER%token TOK_STATIC_VAR_NAME%token TOK_STATIC_VAR_TABLE%token TOK_INT_LITERAL%token TOK_LANGLE%token TOK_LBRACKET%token TOK_LIMITS%token TOK_NAME_TABLE%token TOK_NULL_ALLOWED%token TOK_PARAMETER_NAME%token TOK_PARAMETER_TABLE%token TOK_RANGLE%token TOK_RBRACKET%token TOK_REAL_LITERAL%token TOK_SPICE_MODEL_NAME%token TOK_STRING_LITERAL%union { Ctype_List_t *ctype_list; Dir_t dir; Boolean_t bool; Range_t range; Data_Type_t dtype; My_Port_Type_t ctype; My_Value_t value; char *str; Bound_t bound; int ival; double rval; Complex_t cval;}%type <ctype_list> ctype_list delimited_ctype_list%type <dir> direction%type <ctype> ctype%type <dtype> dtype%type <range> range int_range%type <value> value number integer_value value_or_dash%type <str> identifier string%type <bool> bool%type <bound> int_or_dash number_or_dash%type <ival> integer%type <rval> real%type <cval> complex%start ifs_file%{/* * resuse the Yacc union for our buffer: */YYSTYPE item_buffer [ITEM_BUFFER_SIZE];/* * Shorthand for refering to the current element of the item buffer: */#define BUF ITEM_BUF(item-1)%}%%ifs_file : {TBL->num_conn = 0; TBL->num_param = 0; TBL->num_inst_var = 0; saw_function_name = FALSE; saw_model_name = FALSE; alloced_size [TBL_PORT] = DEFAULT_SIZE_CONN; alloced_size [TBL_PARAMETER] = DEFAULT_SIZE_PARAM; alloced_size [TBL_STATIC_VAR] = DEFAULT_SIZE_INST_VAR; TBL->conn = (Conn_Info_t*) calloc (DEFAULT_SIZE_CONN, sizeof (Conn_Info_t)); TBL->param = (Param_Info_t*) calloc (DEFAULT_SIZE_PARAM, sizeof (Param_Info_t)); TBL->inst_var = (Inst_Var_Info_t*) calloc (DEFAULT_SIZE_INST_VAR, sizeof (Inst_Var_Info_t)); if (! (TBL->conn && TBL->param && TBL->inst_var) ) { fatal ("Could not allocate enough memory"); } } list_of_tables ;list_of_tables : table | list_of_tables table ;table : TOK_NAME_TABLE {context.table = TBL_NAME;} name_table | TOK_PORT_TABLE {context.table = TBL_PORT; did_default_type = FALSE; did_allowed_types = FALSE; INIT (TBL->num_conn);} port_table {TBL->num_conn = num_items;} | TOK_PARAMETER_TABLE {context.table = TBL_PARAMETER; INIT (TBL->num_param);} parameter_table {TBL->num_param = num_items;} | TOK_STATIC_VAR_TABLE {context.table = TBL_STATIC_VAR; INIT (TBL->num_inst_var);} static_var_table {TBL->num_inst_var = num_items;} ;name_table : /* empty */ | name_table name_table_item ;name_table_item : TOK_C_FUNCTION_NAME identifier {TBL->name.c_fcn_name =strdup (ifs_yytext); saw_function_name = TRUE; if (parser_just_names && saw_model_name) return 0;} | TOK_SPICE_MODEL_NAME identifier {TBL->name.model_name = strdup (ifs_yytext); saw_model_name = TRUE; if (parser_just_names && saw_function_name) return 0;} | TOK_DESCRIPTION string {TBL->name.description = strdup (ifs_yytext);} ;port_table : /* empty */ | port_table port_table_item ;port_table_item : TOK_PORT_NAME list_of_ids {int i; END; FOR_ITEM (i) { TBL->conn[i].name = ITEM_BUF(i).str; }} | TOK_DESCRIPTION list_of_strings {int i; END; FOR_ITEM (i) { TBL->conn[i].description = ITEM_BUF(i).str; }} | TOK_DIRECTION list_of_directions {int i; END; FOR_ITEM (i) { TBL->conn[i].direction = ITEM_BUF(i).dir; }} | TOK_DEFAULT_TYPE list_of_ctypes {int i; END; did_default_type = TRUE; FOR_ITEM (i) { TBL->conn[i].default_port_type = ITEM_BUF(i).ctype.kind; TBL->conn[i].default_type = ITEM_BUF(i).ctype.id; if (did_allowed_types) { check_default_type (TBL->conn[i]); } }} | TOK_ALLOWED_TYPES list_of_ctype_lists {int i; END; did_allowed_types = TRUE; FOR_ITEM (i) { assign_ctype_list (&TBL->conn[i], ITEM_BUF(i).ctype_list); if (did_default_type) { check_default_type (TBL->conn[i]); } }} | TOK_ARRAY list_of_bool {int i; END; FOR_ITEM (i) { TBL->conn[i].is_array = ITEM_BUF(i).bool; }} | TOK_ARRAY_BOUNDS list_of_array_bounds {int i; END; FOR_ITEM (i) { ASSIGN_BOUNDS (conn, i); assert (!TBL->conn[i].has_conn_ref); }} | TOK_NULL_ALLOWED list_of_bool {int i; END; FOR_ITEM (i) { TBL->conn[i].null_allowed = ITEM_BUF(i).bool; }} ;parameter_table : /* empty */ | parameter_table parameter_table_item ;parameter_table_item : TOK_PARAMETER_NAME list_of_ids {int i; END; FOR_ITEM (i) { TBL->param[i].name = ITEM_BUF(i).str; }} | TOK_DESCRIPTION list_of_strings {int i; END; FOR_ITEM (i) { TBL->param[i].description = ITEM_BUF(i).str; }} | TOK_DATA_TYPE list_of_dtypes {int i; END; FOR_ITEM (i) { check_dtype_not_pointer (ITEM_BUF(i).dtype); TBL->param[i].type = ITEM_BUF(i).dtype; }} | TOK_DEFAULT_VALUE list_of_values {int i; END; FOR_ITEM (i) { TBL->param[i].has_default = ITEM_BUF(i).value.has_value; if (TBL->param[i].has_default) { assign_value (TBL->param[i].type, &TBL->param[i].default_value, ITEM_BUF(i).value); } }} | TOK_LIMITS list_of_ranges {int i; END; FOR_ITEM (i) { assign_limits (TBL->param[i].type, &TBL->param[i], ITEM_BUF(i).range); }} | TOK_ARRAY list_of_bool {int i; END; FOR_ITEM (i) { TBL->param[i].is_array = ITEM_BUF(i).bool; }} | TOK_ARRAY_BOUNDS list_of_array_bounds {int i; END; FOR_ITEM (i) { ASSIGN_BOUNDS (param, i); }} | TOK_NULL_ALLOWED list_of_bool {int i; END; FOR_ITEM (i) { TBL->param[i].null_allowed = ITEM_BUF(i).bool; }} ;static_var_table : /* empty */ | static_var_table static_var_table_item ;static_var_table_item : TOK_STATIC_VAR_NAME list_of_ids {int i; END; FOR_ITEM (i) { TBL->inst_var[i].name = ITEM_BUF(i).str; }} | TOK_DESCRIPTION list_of_strings {int i; END; FOR_ITEM (i) { TBL->inst_var[i].description = ITEM_BUF(i).str; }} | TOK_DATA_TYPE list_of_dtypes {int i; END; FOR_ITEM (i) { TBL->inst_var[i].type = ITEM_BUF(i).dtype; }} | TOK_ARRAY list_of_bool {int i; END; FOR_ITEM (i) { TBL->inst_var[i].is_array = ITEM_BUF(i).bool; }} ;list_of_ids : /* empty */ | list_of_ids identifier {ITEM; BUF.str = $2;} ;list_of_array_bounds : /* empty */ | list_of_array_bounds int_range {ITEM; BUF.range = $2;} | list_of_array_bounds identifier {ITEM; BUF.range.is_named = TRUE; BUF.range.u.name = $2;} ;list_of_strings : /* empty */ | list_of_strings string {ITEM; BUF.str = $2;} ;list_of_directions : /* empty */ | list_of_directions direction {ITEM; BUF.dir = $2;} ;direction : TOK_DIR_IN {$$ = IN;} | TOK_DIR_OUT {$$ = OUT;} | TOK_DIR_INOUT {$$ = INOUT;} ;list_of_bool : /* empty */ | list_of_bool bool {ITEM; BUF.bool = $2;} ;list_of_ctypes : /* empty */ | list_of_ctypes ctype {ITEM; BUF.ctype = $2;} ;ctype : TOK_CTYPE_V {$$.kind = VOLTAGE;} | TOK_CTYPE_VD {$$.kind = DIFF_VOLTAGE;} | TOK_CTYPE_VNAM {$$.kind = VSOURCE_CURRENT;} | TOK_CTYPE_I {$$.kind = CURRENT;} | TOK_CTYPE_ID {$$.kind = DIFF_CURRENT;} | TOK_CTYPE_G {$$.kind = CONDUCTANCE;} | TOK_CTYPE_GD {$$.kind = DIFF_CONDUCTANCE;} | TOK_CTYPE_H {$$.kind = RESISTANCE;} | TOK_CTYPE_HD {$$.kind = DIFF_RESISTANCE;} | TOK_CTYPE_D {$$.kind = DIGITAL;} | identifier {$$.kind = USER_DEFINED; $$.id = $1;} ;list_of_dtypes : /* empty */ | list_of_dtypes dtype {ITEM; BUF.dtype = $2;} ;dtype : TOK_DTYPE_REAL {$$ = REAL;} | TOK_DTYPE_INT {$$ = INTEGER;} | TOK_DTYPE_BOOLEAN {$$ = BOOLEAN;} | TOK_DTYPE_COMPLEX {$$ = COMPLEX;} | TOK_DTYPE_STRING {$$ = STRING;} | TOK_DTYPE_POINTER {$$ = POINTER;} ;list_of_ranges : /* empty */ | list_of_ranges range {ITEM; BUF.range = $2;} ;int_range : TOK_DASH {$$.is_named = FALSE; $$.u.bounds.lower.has_bound = FALSE; $$.u.bounds.upper.has_bound = FALSE;} | TOK_LBRACKET int_or_dash maybe_comma int_or_dash TOK_RBRACKET {$$.is_named = FALSE; $$.u.bounds.lower = $2; $$.u.bounds.upper = $4;} ;maybe_comma : /* empty */ | TOK_COMMA ;int_or_dash : TOK_DASH {$$.has_bound = FALSE;} | integer_value {$$.has_bound = TRUE; $$.bound = $1;} ;range : TOK_DASH {$$.is_named = FALSE; $$.u.bounds.lower.has_bound = FALSE; $$.u.bounds.upper.has_bound = FALSE;} | TOK_LBRACKET number_or_dash maybe_comma number_or_dash TOK_RBRACKET {$$.is_named = FALSE; $$.u.bounds.lower = $2; $$.u.bounds.upper = $4;} ;number_or_dash : TOK_DASH {$$.has_bound = FALSE;} | number {$$.has_bound = TRUE; $$.bound = $1;} ;list_of_values : /* empty */ | list_of_values value_or_dash {ITEM; BUF.value = $2;} ;value_or_dash : TOK_DASH {$$.has_value = FALSE;} | value ;value : string {$$.has_value = TRUE; $$.kind = STRING; $$.u.svalue = $1;} | bool {$$.has_value = TRUE; $$.kind = BOOLEAN; $$.u.bvalue = $1;} | complex {$$.has_value = TRUE; $$.kind = COMPLEX; $$.u.cvalue = $1;} | number ;complex : TOK_LANGLE real maybe_comma real TOK_RANGLE {$$.real = $2; $$.imag = $4;} ;list_of_ctype_lists : /* empty */ | list_of_ctype_lists delimited_ctype_list {ITEM; BUF.ctype_list = $2;} ;delimited_ctype_list : TOK_LBRACKET ctype_list TOK_RBRACKET {$$ = $2;} ;ctype_list : ctype {$$ = (Ctype_List_t*)calloc (1, sizeof (Ctype_List_t)); if (!$$) { fatal ("Error allocating memory"); } $$->ctype = $1; $$->next = (Ctype_List_t*)0;} | ctype_list maybe_comma ctype {$$ = (Ctype_List_t*)calloc (1, sizeof (Ctype_List_t)); if (!$$) { fatal ("Error allocating memory"); } $$->ctype = $3; $$->next = $1; /*$$->next = (Ctype_List_t*)0; assert ($1); $1->next = $$;*/} ;bool : TOK_BOOL_YES {$$ = TRUE;} | TOK_BOOL_NO {$$ = FALSE;} ;string : TOK_STRING_LITERAL {$$ = strdup(ifs_yytext);} ;identifier : TOK_IDENTIFIER {$$ = strdup(ifs_yytext);} ;number : real {$$.has_value = TRUE; $$.kind = REAL; $$.u.rvalue = $1;} | integer_value ;integer_value : integer {$$.has_value = TRUE; $$.kind = INTEGER; $$.u.ivalue = $1;} ;real : TOK_REAL_LITERAL {$$ = yydval;} ;integer : TOK_INT_LITERAL {$$ = yyival;} ;%%
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -