Loading the code workspace…
Extend your parser to handle statements and statement lists.
Implement statement parsing functions:
parse_statement(): Dispatch to specific statement parsersparse_expression_statement(): Expression followed by semicolonparse_return_statement(): return expr;parse_if_statement(): if (cond) stmt [else stmt]parse_while_statement(): while (cond) stmtparse_block(): { stmt* }Handle expression statements:
;; alone) are validHandle return statements:
return; (void return)return expr; (value return)Handle if statements:
if (condition) statementelse statementelse binds to nearest ifHandle blocks:
return 42;if (x > 0) return x; else return -x;{ int x; x = 5; return x; }Statement dispatch — looking at the leading token and choosing the if/while/return production — is the backbone of every real parser; CPython's new PEG parser and Clang's ParseStmt do precisely this. Getting the dangling-else and block-scoping cases right here is what separates a demo parser from one that handles real programs.