Design a parser for a hypothetical language
OpenAISoftware Engineer · OnsiteLast updated
This question evaluates competency in compiler and language design, covering grammar analysis, parsing algorithms (LL/LR/GLR/Earley/PEG), lexer and parser implementation, AST construction, error reporting and recovery, operator precedence and associativity, and analysis of time/space trade-offs.
You are given a set of syntax rules for a hypothetical programming language written in BNF/EBNF. Design data structures and algorithms that: (
- parse the grammar into an internal representation (terminals, nonterminals, productions, precedence/associativity); (
- detect issues such as left recursion, ambiguity, unreachable or non-productive symbols; (
- apply transformations (left-recursion elimination, left factoring) to enable efficient parsing; (
- compute FIRST/FOLLOW sets and determine whether the grammar is LL ( 1); if not LL ( 1), justify choosing LR ( 1), GLR, Earley, or PEG parsing and outline construction; (
- implement a lexer and parser that build an abstract syntax tree (AST) for input programs; (
- provide error reporting and recovery with expected-token hints; (
- support operator precedence and associativity declarations; (
- analyze time and space complexity for grammar processing and parsing, and discuss trade-offs; (
- optionally support incremental or streaming parsing. Provide pseudocode and explain your design choices.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.