Lagrangian theory of constrained systems: Cosmological application