<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def chk_statement(ctx, stmt, grammar, canonical=False):
 """Validate `stmt` according to `grammar`. Marks each statement in the hierearchy with stmt.is_grammatically_valid, which is a boolean. Return True if stmt is valid, False otherwise. """

    n = len(ctx.errors)
    if canonical == True:
        canspec = grammar
    else:
        canspec = []
    _chk_stmts(ctx, stmt.pos, [stmt], None, (grammar, canspec), canonical)
    return n == len(ctx.errors)