The paper introduces semantic grammar specifications that attach typing and scope constraints to a context-free grammar. During Earley descent, the system checks these constraints to reject only prefixes that have no semantically valid continuation (safe pruning), and guarantees that each remaining prefix has at least one valid completion (dead-end freedom). This goes beyond syntax-only constrained decoding, which cannot catch semantic errors like type mismatches.
The implementation was validated differentially against production compilers (ocamlc, cc) across prefixes of 65 valid programs, showing zero false prunes. In a test of 30 invalid programs, the semantic oracle detected 25 errors mid-stream, whereas a syntax-only oracle detected none. A generation study across twelve models found nonnegative semantic-minus-syntactic improvements for every model-language pair, with maxima of +15.2 points on STLC correctness and +14.3 points on ML validity.