1 2/* 3 * Verilog Grammar for dparser. 4 * 5 * Created from various BNF descriptions found on the web. 6 * Thomas Skibo. 7 * May 23-29, 2003 8 * 9 */ 10 11 12{ #include "vparse.h" } 13 14/* Source Text */ 15source_text : description* ; 16description : module | UDP ; 17 18module : 19 'module' name_of_module list_of_ports? ';' module_item* 'endmodule' | 20 'macromodule' name_of_module list_of_ports? ';' module_item* 21 'endmodule' ; 22 23name_of_module : IDENTIFIER ; 24list_of_ports : '(' port ( ',' port )* ')' ; 25port : 26 port_expression? | 27 '.' name_of_port '(' port_expression? ')' ; 28port_expression : 29 port_reference | 30 '{' port_reference ( ',' port_reference )* '}' ; 31port_reference : 32 name_of_variable | 33 name_of_variable '[' constant_expression ']' | 34 name_of_variable '[' constant_expression ':' constant_expression ']' ; 35name_of_port : IDENTIFIER ; 36 37module_item : 38 parameter_declaration | 39 input_declaration | 40 output_declaration | 41 inout_declaration | 42 net_declaration | 43 reg_declaration | 44 time_declaration | 45 integer_declaration | 46 real_declaration | 47 event_declaration | 48 gate_declaration | 49 UDP_instantiation | 50 module_instantiation | 51 parameter_override | 52 continuous_assign | 53 specify_block | 54 initial_statement | 55 always_statement | 56 task | 57 function ; 58 59UDP : 'primitive' name_of_UDP 60 '(' name_of_variable ( ',' name_of_variable )* ')' ';' 61 UDP_declaration+ UDP_initial_statement? table_definition 62 'endprimitive' ; 63 64UDP_declaration : 65 output_declaration | 66 reg_declaration | 67 input_declaration ; 68 69UDP_initial_statement : 'initial' output_terminal_name '=' init_val ';' ; 70init_val : '1\'b0' | '1\'b1' | '1\'bx' | '1' | '0' ; 71output_terminal_name : IDENTIFIER ; 72 73table_definition : 'table' table_entries 'endtable' ; 74table_entries : 75 combinational_entry+ | 76 sequential_entry+ ; 77combinational_entry : level_input_list ':' OUTPUT_SYMBOL ';' ; 78sequential_entry : input_list ':' state ':' next_state ';' ; 79input_list : level_input_list | edge_input_list ; 80level_input_list : LEVEL_SYMBOL+ ; 81edge_input_list : LEVEL_SYMBOL* edge LEVEL_SYMBOL* ; 82edge : '(' LEVEL_SYMBOL LEVEL_SYMBOL ')' | EDGE_SYMBOL ; 83state : LEVEL_SYMBOL ; 84next_state : OUTPUT_SYMBOL | '-' ; 85OUTPUT_SYMBOL : "[01xX]" ; 86LEVEL_SYMBOL : "[01xX\?bB]" ; 87EDGE_SYMBOL : "[rRfFpPnN\*]" ; 88 89task : 'task' name_of_decld_task ';' 90 tf_declaration* statement_or_null 'endtask' ; 91name_of_decld_task : IDENTIFIER ; 92 93function : 'function' range_or_type? name_of_function ';' 94 tf_declaration+ statement 'endfunction' ; 95range_or_type : range | 'integer' | 'real' ; 96tf_declaration : 97 parameter_declaration | 98 input_declaration | 99 output_declaration | 100 inout_declaration | 101 reg_declaration | 102 time_declaration | 103 integer_declaration | 104 real_declaration | 105 event_declaration ; 106 107/* Declarartions */ 108parameter_declaration : 109 'parameter' range_or_type? list_of_param_assignments ';' ; 110list_of_param_assignments : param_assignment ( ',' param_assignment )* ; 111param_assignment : identifier '=' constant_expression ; 112input_declaration : 'input' range? list_of_variables ';' ; 113output_declaration : 'output' range? list_of_variables ';' ; 114inout_declaration : 'inout' range? list_of_variables ';' ; 115net_declaration : 116 NETTYPE expandrange? delay? list_of_variables ';' | 117 'trireg' charge_strength? expandrange? delay? list_of_variables ';' ; 118NETTYPE : 119 'wire' | 120 'tri' | 121 'tri1' | 122 'supply0' | 123 'wand' | 124 'triand' | 125 'tri0' | 126 'supply1' | 127 'wor' | 128 'trior' | 129 'trireg' ; 130expandrange : 131 range | 132 'scalared' range | 133 'vectored' range ; 134reg_declaration : 'reg' range? list_of_register_variables ';' ; 135time_declaration : 'time' list_of_register_variables ';' ; 136integer_declaration : 'integer' list_of_register_variables ';' ; 137real_declaration : 'real' list_of_register_variables ';' ; 138event_declaration : 'event' name_of_event ( ',' name_of_event )* ';' ; 139continuous_assign : 140 'assign' drive_strength? delay? list_of_assignments ';' | 141 NETTYPE drive_strength? expandrange? delay? list_of_assignments ';' ; 142parameter_override : 'defparam' list_of_param_assignments ';' ; 143list_of_variables : name_of_variable ( ',' name_of_variable )* ; 144name_of_variable : IDENTIFIER ; 145list_of_register_variables : register_decl ( ',' register_decl )* ; 146register_decl : /* XXX Register initializers? */ 147 name_of_register ( '=' constant_expression )? | 148 name_of_memory '[' constant_expression ':' constant_expression ']' ; 149name_of_register : IDENTIFIER ; 150name_of_memory : IDENTIFIER ; 151name_of_event : IDENTIFIER ; 152charge_strength : 153 '(' 'small' ')' | 154 '(' 'medium' ')' | 155 '(' 'large' ')' ; 156drive_strength : 157 '(' STRENGTH0 ',' STRENGTH1 ')' | 158 '(' STRENGTH1 ',' STRENGTH0 ')' ; 159STRENGTH0: 160 'supply0' | 161 'strong0' | 162 'pull0' | 163 'weak0' | 164 'highz0' ; 165STRENGTH1: 166 'supply1' | 167 'strong1' | 168 'pull1' | 169 'weak1' | 170 'highz1' ; 171range : '[' constant_expression ':' constant_expression ']' ; 172list_of_assignments : assignment ( ',' assignment )* ; 173 174/* Primitive Instances */ 175 176gate_declaration : GATETYPE drive_strength? delay? 177 gate_instance ( ',' gate_instance )* ';' ; 178GATETYPE : 179 'and' | 180 'nand' | 181 'or' | 182 'nor' | 183 'xor' | 184 'xnor' | 185 'buf' | 186 'bufif0' | 187 'bufif1' | 188 'not' | 189 'notif0' | 190 'notif1' | 191 'pulldown' | 192 'pullup' | 193 'nmos' | 194 'rnmos' | 195 'pmos' | 196 'rpmos' | 197 'cmos' | 198 'rcmos' | 199 'tran' | 200 'rtran' | 201 'tranif0' | 202 'rtranif0' | 203 'tranif1' | 204 'rtranif1' ; 205 206gate_instance : name_of_gate_instance? range? /* XXX */ 207 '(' terminal ( ',' terminal )* ')' ; 208name_of_gate_instance : IDENTIFIER ; 209UDP_instantiation : name_of_UDP drive_strength? delay? 210 UDP_instance ( ',' UDP_instance )* ; 211name_of_UDP : IDENTIFIER ; 212UDP_instance : name_of_UDP_instance? '(' terminal ( ',' terminal )* ')' ; 213name_of_UDP_instance : identifier ; 214terminal : expression /* | IDENTIFIER */ ; 215 216/* Module Instantiations */ 217module_instantiation : name_of_module parameter_value_assignment? 218 module_instance ( ',' module_instance )* ';' ; 219parameter_value_assignment : 220 '#' '(' expression ( ',' expression )+ ')' | 221 '#' expression ; /* XXX */ 222/* XXX? module arrays? */ 223module_instance : name_of_instance range? 224 '(' list_of_module_connections? ')' ; 225name_of_instance : IDENTIFIER ; 226list_of_module_connections : 227 module_port_connection ( ',' module_port_connection )* | 228 named_port_connection ( ',' named_port_connection )* ; 229module_port_connection : expression | /* NULL */ ; 230named_port_connection : '.' IDENTIFIER '(' expression? ')' ; 231 232/* Behavioral Statements */ 233initial_statement : 'initial' statement ; 234always_statement : 'always' statement ; 235statement_or_null : statement | ';' ; 236 237statement : 238 blocking_assignment ';' | 239 non_blocking_assignment ';' | 240 'if' '(' expression ')' statement_or_null $right 1 | 241 'if' '(' expression ')' statement_or_null 242 'else' statement_or_null $right 2 | 243 'case' '(' expression ')' case_item+ 'endcase' | 244 'casex' '(' expression ')' case_item+ 'endcase' | 245 'casez' '(' expression ')' case_item+ 'endcase' | 246 'forever' statement | 247 'repeat' '(' expression ')' statement | 248 'while' '(' expression ')' statement | 249 'for' '(' assignment ';' expression ';' assignment ')' statement | 250 delay_control statement_or_null | 251 event_control statement_or_null | 252 'wait' '(' expression ')' statement_or_null | 253 '->' name_of_event ';' | 254 seq_block | 255 par_block | 256 task_enable | 257 system_task_enable | 258 'disable' name_of_task_or_block ';' | 259 'force' assignment ';' | 260 'release' lvalue ';' | 261 'assign' assignment ';' | 262 'deassign' lvalue ';' ; 263 264name_of_task_or_block : identifier ; 265name_of_task : identifier ; 266 267assignment : lvalue '=' expression ; 268blocking_assignment : 269 lvalue '=' expression | 270 lvalue '=' delay_control expression | 271 lvalue '=' event_control expression ; 272non_blocking_assignment : 273 lvalue '<=' expression | 274 lvalue '<=' delay_control expression | 275 lvalue '<=' event_control expression ; 276 277case_item : 278 expression ( ',' expression )* ':' statement_or_null | 279 'default' ':'? statement_or_null ; 280seq_block : 281 'begin' statement* 'end' | 282 'begin' ':' name_of_block block_declaration* statement* 'end' ; 283par_block : 284 'fork' statement* 'join' | 285 'fork' ':' name_of_block block_declaration* statement* 'join' ; 286name_of_block : IDENTIFIER ; 287block_declaration : 288 parameter_declaration | 289 reg_declaration | 290 integer_declaration | 291 real_declaration | 292 time_declaration | 293 event_declaration ; 294task_enable : 295 name_of_task ';' | 296 name_of_task '(' expression ( ',' expression )* ')' ';' ; 297system_task_enable : 298 system_name_of_task ';' | 299 system_name_of_task '(' expression? ( ',' expression? )* ')' ';' ; 300system_name_of_task : "\$[a-zA-Z_][a-zA-Z_0-9\$]*"; 301 302/* Specify Section. */ 303specify_block : 'specify' specify_item* 'endspecify' ; 304specify_item : 305 specparam_declaration | 306 path_declaration | 307 level_sensitive_path_declaration | 308 edge_sensitive_path_declaration | 309 system_timing_check | 310 sdpd ; 311 312specparam_declaration : 'specparam' list_of_param_assignments ';' ; 313path_declaration : path_description '=' path_delay_value ';' ; 314path_description : 315 '(' specify_input_terminal_descriptor '=>' 316 specify_output_terminal_descriptor ')' | 317 '(' list_of_path_inputs '*>' list_of_path_outputs ')' ; 318list_of_path_inputs : specify_input_terminal_descriptor 319 ( ',' specify_input_terminal_descriptor )* ; 320list_of_path_outputs : specify_output_terminal_descriptor 321 ( ',' specify_output_terminal_descriptor )* ; 322specify_input_terminal_descriptor : 323 input_identifier | 324 input_identifier '[' constant_expression ']' | 325 input_identifier '[' constant_expression ':' constant_expression ']' ; 326specify_output_terminal_descriptor : 327 output_identifier | 328 output_identifier '[' constant_expression ']' | 329 output_identifier '[' constant_expression ':' constant_expression ']' ; 330input_identifier : IDENTIFIER ; 331output_identifier : IDENTIFIER ; 332path_delay_value : 333 path_delay_expression | 334 '(' path_delay_expression ',' path_delay_expression ')' | 335 '(' path_delay_expression ',' path_delay_expression ',' 336 path_delay_expression ')' | 337 '(' path_delay_expression ',' path_delay_expression ',' 338 path_delay_expression ',' path_delay_expression ',' 339 path_delay_expression ',' path_delay_expression ')' ; 340path_delay_expression : constant_mintypmax_expression ; 341constant_mintypmax_expression : mintypmax_expression ; 342system_timing_check : 343 '$setup' '(' timing_check_event ',' timing_check_event ',' 344 timing_check_limit ( ',' notify_register )? ')' ';' | 345 '$hold' '(' timing_check_event ',' timing_check_event ',' 346 timing_check_limit ( ',' notify_register )? ')' ';' | 347 '$period' '(' controlled_timing_check_event ',' timing_check_limit 348 (',' notify_register )? ')' ';' | 349 '$width' '(' controlled_timing_check_event ',' timing_check_limit ',' 350 (',' constant_expression ',' notify_register)? ')' ';' | 351 '$skew' '(' timing_check_event ',' timing_check_event ',' 352 timing_check_limit (',' notify_register)? ')' ';' | 353 '$recovery' '(' controlled_timing_check_event ',' 354 timing_check_event ',' timing_check_limit 355 ( ',' notify_register )? ')' ';' | 356 '$setuphold' '(' timing_check_event ',' timing_check_event ',' 357 timing_check_limit ',' timing_check_limit ',' 358 ( ',' notify_register )? ')' ';' ; 359timing_check_event : 360 timing_check_event_control? specify_terminal_descriptor 361 ( '&&&' timing_check_condition )? ; 362specify_terminal_descriptor : 363 specify_input_terminal_descriptor | 364 specify_output_terminal_descriptor ; 365controlled_timing_check_event : 366 timing_check_event_control specify_terminal_descriptor 367 ( '&&&' timing_check_condition )? ; 368timing_check_event_control : 369 'posedge' | 370 'negedge' | 371 edge_control_specifier ; 372edge_control_specifier : 373 'edge' '[' edge_descriptor ( ',' edge_descriptor )* ']' ; 374edge_descriptor : '01' | '10' | '0x' | 'x1' | '1x' | 'x0' ; 375timing_check_condition : 376 SCALAR_EXPRESSION | 377 '~' SCALAR_EXPRESSION | 378 SCALAR_EXPRESSION '==' scalar_constant | 379 SCALAR_EXPRESSION '===' scalar_constant | 380 SCALAR_EXPRESSION '!=' scalar_constant | 381 SCALAR_EXPRESSION '!==' scalar_constant ; 382SCALAR_EXPRESSION : expression /* one bit */ ; 383timing_check_limit : expression ; 384scalar_constant : "1'[bB][01]" ; 385notify_register : identifier ; 386level_sensitive_path_declaration : 387 'if' '(' conditional_port_expression ')' 388 '(' specify_terminal_descriptor polarity_operator? '=>' 389 specify_terminal_descriptor ')' '=' path_delay_value ';' | 390 'if' '(' conditional_port_expression ')' 391 '(' list_of_path_inputs polarity_operator? '*>' 392 list_of_path_outputs ')' '=' path_delay_value ';' ; 393conditional_port_expression : 394 port_reference | 395 UNARY_OPERATOR port_reference | 396 port_reference BINARY_OPERATOR port_reference ; 397polarity_operator : '+' | '-' ; 398edge_sensitive_path_declaration : 399 ( 'if' '(' expression ')' )? 400 '(' specify_terminal_descriptor '=>' 401 '(' specify_terminal_descriptor polarity_operator? ':' 402 data_source_expression ')' ')' '=' path_delay_value ';' | 403 ( 'if' '(' expression ')' )? 404 '(' specify_terminal_descriptor '*>' 405 '(' list_of_path_outputs polarity_operator? ':' 406 data_source_expression ')' ')' '=' path_delay_value ';' ; 407data_source_expression : expression /* XXX ? */ ; 408edge_identifier : 'posedge' | 'negedge' ; 409sdpd : 410 'if' '(' sdpd_conditional_expression ')' 411 path_description '=' path_delay_value ';' ; 412sdpd_conditional_expression : 413 expression BINARY_OPERATOR expression | 414 UNARY_OPERATOR expression ; 415 416 417 418/* Expressions */ 419lvalue : 420 identifier | 421 identifier '[' expression ']' | 422 identifier '[' constant_expression ':' constant_expression ']' | 423 concatenation ; 424constant_expression : expression ; 425mintypmax_expression : expression | expression ':' expression ':' expression ; 426 427expression : 428 primary | 429 UNARY_OPERATOR primary | 430 expression BINARY_OPERATOR expression | 431 expression '?' expression ':' expression $right 10 | 432 STRING ; 433 434UNARY_OPERATOR : 435 '+' $unary_op_right 40 | 436 '-' $unary_op_right 40 | 437 '!' $unary_op_right 40 | 438 '~' $unary_op_right 40 | 439 '&' $unary_op_right 40 | 440 '~&' $unary_op_right 40 | 441 '|' $unary_op_right 40 | 442 '~|' $unary_op_right 40 | 443 '^' $unary_op_right 40 | 444 '~^' $unary_op_right 40 | 445 '^~' $unary_op_right 40 ; 446 447BINARY_OPERATOR : 448 '*' $binary_op_left 20 | 449 '/' $binary_op_left 20 | 450 '%' $binary_op_left 20 | 451 '+' $binary_op_left 19 | 452 '-' $binary_op_left 19 | 453 '>>' $binary_op_left 18 | 454 '<<' $binary_op_left 18 | 455 '<' $binary_op_left 17 | 456 '<=' $binary_op_left 17 | 457 '>' $binary_op_left 17 | 458 '>=' $binary_op_left 17 | 459 '==' $binary_op_left 16 | 460 '!=' $binary_op_left 16 | 461 '===' $binary_op_left 16 | 462 '!==' $binary_op_left 16 | 463 '&' $binary_op_left 15 | 464 '^' $binary_op_left 14 | 465 '^~' $binary_op_left 14 | 466 '~^' $binary_op_left 14 | 467 '|' $binary_op_left 13 | 468 '&&' $binary_op_left 12 | 469 '||' $binary_op_left 11 ; 470 471STRING : "\"[^\"]*\"" ; /* can't handle quoted quote */ 472 473primary : 474 number | 475 identifier | 476 identifier '[' expression ']' | 477 identifier '[' constant_expression ':' constant_expression ']' | 478 concatenation | 479 multiple_concatenation | 480 function_call | 481 '(' mintypmax_expression ')' ; 482 483number : DECIMAL_NUMBER | 484 SIZE_BASE NUMBER | 485 REAL_NUMBER ; 486 487SIZE_BASE : "[0-9_]*'[BbOoDdHh]" ; 488NUMBER : "[\+\-]?[0-9a-fA-FxXzZ_\?]+" ; 489DECIMAL_NUMBER : "[\+\-]?[0-9_]+" ; 490REAL_NUMBER : "[\+\-]?[0-9_]+\.[0-9_]+" | 491 "[\+\-]?[0-9_]+(\.[0-9_]+)?[Ee][\+\-][0-9_]+" ; 492 493concatenation : '{' expression ( ',' expression )* '}' ; 494multiple_concatenation : 495 '{' expression '{' expression ( ',' expression )* '}' '}' ; 496function_call : 497 name_of_function '(' (expression ( ',' expression )*)? ')' | 498 name_of_system_function '(' (expression ( ',' expression )*)? ')' | 499 name_of_system_function ; 500name_of_function : identifier ; 501name_of_system_function : "\$[a-zA-Z_][a-zA-Z0-9_\$]*" ; 502 503/* General */ 504identifier : IDENTIFIER ( '.' IDENTIFIER ) * ; 505IDENTIFIER : 506 "\\[^ ]+ " | 507 "[a-zA-Z_][a-zA-Z_0-9\$]*" $term -3 [ 508 if ( v_iskeyword($n0.start_loc.s, $n0.end) ) 509 ${reject}; 510 ] ; 511delay : '#' number | 512 '#' IDENTIFIER | 513 '#' '(' mintypmax_expression ( ',' mintypmax_expression 514 ( ',' mintypmax_expression ) ? ) ? ')' ; 515 516delay_control : 517 '#' number | 518 '#' IDENTIFIER | 519 '#' '(' mintypmax_expression ')' ; 520 521event_control : 522 '@' IDENTIFIER | 523 '@' '(' event_expression ')' ; 524 525event_expression : 526 expression | 527 'posedge' SCALAR_EVENT_EXPRESSION | 528 'negedge' SCALAR_EVENT_EXPRESSION | 529 event_expression 'or' event_expression $left -1 ; 530 531SCALAR_EVENT_EXPRESSION : 532 expression ; /* must resolve to a one bit value */ 533 534