Master the fundamental concepts of syntax analysis through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksFunction declarations introduce a name, parameter list, and return type into the program's structure. Clang's ParseFunctionDefinition and GCC's declarator parsing handle prototypes vs definitions, variadic parameters, and pointer return types. Getting signatures right here feeds directly into type checking.
A declaration looks like type name(type param, ...) { body } or a prototype ending in ;. The parser must collect parameter names and types, build an AST node, and distinguish forward declarations from definitions with bodies.
cLoading…
void foo(void) vs void foo()Production compilers embed this step inside a longer pipeline. GCC flows through cpp, cc1, assembly, and ld; Clang uses the driver, Sema, LLVM IR passes, and a target backend. LLVM bitcode, JVM bytecode, and WASM are other familiar IRs at the same layer. The exercise isolates one pass so you can test it alone before chaining it to the next stage.
You will parse function declarations and definitions including parameter lists and return types. This exercise requires building AST nodes that carry full function signatures for the semantic analysis pass to validate call sites.
Grow the statement parser from Statement Parsing into a parser for whole translation units: function definitions, prototypes, parameter lists, local declarations and function calls.
C-subset source on stdin. On top of the previous task's tokens, int, void and char are keywords and , is a token.
cLoading…
A function ending in ; is a prototype. (void) and () both mean "no parameters".
For each function, in order, a header line:
cLoading…
(() when there are no parameters.) A definition's body statements follow, indented two spaces, printed exactly as in Statement Parsing (the body's own braces print no BLOCK line), plus:
DECL TYPE NAME or DECL TYPE NAME VALUE;(call NAME ARG1 ARG2 ...), e.g. (call f) with no arguments.Finally functions=F prototypes=P calls=C, where C counts every call expression, nested ones included.
As in the previous task: stop at the first error and print only error at LINE:COL: MESSAGE. New messages: expected type (where a function or parameter must start), expected identifier, and expected '{' or ';' (after a function's parameter list).
Input:
cLoading…
Output:
cLoading…
parse_function, parse_params, parse_declaration and call parsing inside parse_primary (an identifier followed by ( is a call).NODE_FUNC_DECL-style record with the return type, name, parameter list and an optional body.Hidden tests cover calls with zero and many arguments, calls inside conditions and loops, uninitialised declarations, many parameters, and a stray trailing comma in a parameter list.