def p_new_reg(self, p):
        """new_reg : QREG ID '[' NATURAL_NUMBER ']' ';'
        | QUBIT '[' NATURAL_NUMBER ']' ID ';'
        | QUBIT ID ';'
        | CREG ID '[' NATURAL_NUMBER ']' ';'
        | BIT '[' NATURAL_NUMBER ']' ID ';'
        | BIT ID ';'
        """
        if p[1] == "qreg" or p[1] == "creg":
            # QREG ID '[' NATURAL_NUMBER ']' ';'
            name, length = p[2], p[4]
        else:
            if len(p) < 5:
                # QUBIT ID ';' | BIT ID ';'
                name = p[2]
                length = 1
            else:
                # QUBIT '[' NATURAL_NUMBER ']' ID ';'
                name, length = p[5], p[3]
        if name in self.qregs.keys() or name in self.cregs.keys():
            raise QasmException(f"{name} is already defined at line {p.lineno(2)}")
        if length == 0:
            raise QasmException(f"Illegal, zero-length register '{name}' at line {p.lineno(4)}")
        if p[1] == "qreg" or p[1] == "qubit":
            self.qregs[name] = length
        else:
            self.cregs[name] = length
        p[0] = (name, length)
new_reg : QREG ID '[' NATURAL_NUMBER ']' ';'
        | QUBIT '[' NATURAL_NUMBER ']' ID ';'
        | QUBIT ID ';'
        | CREG ID '[' NATURAL_NUMBER ']' ';'
        | BIT '[' NATURAL_NUMBER ']' ID ';'
        | BIT ID ';'
        
cirq-core/cirq/contrib/qasm_import/_parser.py