The Legendre-Stirling numbers of the second kind were introduced by Everitt et al. in the spectral theory of powers of the Legendre differential expressions. In this paper, we provide a combinatorial code for Legendre-Stirling set partitions. As an application, we obtain combinatorial expansions of the Legendre-Stirling numbers of both kinds. Moreover, we present grammatical descriptions of the Jacobi-Stirling numbers of both kinds.