Refactored grammar and semantic check