Scope generation and expression extraction