Using grammars in forward and backward search