sv-parser icon indicating copy to clipboard operation
sv-parser copied to clipboard

Incorrect parsing of procedural assignment inside procedural block

Open 0ncorhynchus opened this issue 2 months ago • 0 comments

Description

It appears that sv_parser incorrectly classifies certain procedural assignments as implicit declarations when they appear inside a sequential block (begin ... end).

module top;
  bit clock = 0;
  initial begin
    clock = ~clock; // Here
  end
endmodule

Expected Behavior

The statement clock = ~clock; should be parsed as a procedural assignment because it appears inside a procedural context (initial block).

Actual Behavior

sv_parser interprets the statement as a data_declaration with an implicit type.

> cargo run --example parse_sv -t sample.sv
SourceText
 Description
  ModuleDeclaration
   ModuleDeclarationAnsi
...
            SeqBlock
             Keyword
              Token: 'begin' @ line:3
             BlockItemDeclaration
              BlockItemDeclarationData
               DataDeclaration
                DataDeclarationVariable
                 DataTypeOrImplicit
                  ImplicitDataType
                 ListOfVariableDeclAssignments
                  VariableDeclAssignment
                   VariableDeclAssignmentVariable
                    VariableIdentifier
                     Identifier
                      SimpleIdentifier
                       Token: 'clock' @ line:4
                    Symbol
                     Token: '=' @ line:4
                    Expression
                     ExpressionUnary
                      UnaryOperator
                       Symbol
                        Token: '~' @ line:4
                      Primary
                       PrimaryHierarchical
                        ClassQualifierOrPackageScope
                         ClassQualifier
                        HierarchicalIdentifier
                         Identifier
                          SimpleIdentifier
                           Token: 'clock' @ line:4
                        Select
                         BitSelect
                 Symbol
                  Token: ';' @ line:4
             Keyword
              Token: 'end' @ line:5
    Keyword
     Token: 'endmodule' @ line:6

parse succeeded: "sample.sv"

Why this is a problem

The statement can be parsed as a data_declaration with an implicit data type by referring only to pure BNF in the IEEE 1800-2017 standard. However, the standard forbids an implicit data type in data_declaration. The footnote in Annex A states:

  1. In a data_declaration that is not within a procedural context, it shall be illegal to use the automatic keyword. In a data_declaration, it shall be illegal to omit the explicit data_type before a list_of_variable_decl_assignments unless the var keyword is used.

Therefore, clock = ~clock; does not match a valid data_declaration rule, and should be parsed as a procedural assignment instead.

0ncorhynchus avatar Nov 24 '25 10:11 0ncorhynchus