2. Page 3 (a) Carol is writing a compiler for a new language. She writes the source code for the compiler in Java and co
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
2. Page 3 (a) Carol is writing a compiler for a new language. She writes the source code for the compiler in Java and co
2. Page 3 (a) Carol is writing a compiler for a new language. She writes the source code for the compiler in Java and compiles it with javac. Unfortunately, when she tests the compiler, she finds that her hand- written parser has many bugs. Rather than attempting to correct her hand-written parser, she decides to use a parser generator instead. Outline the steps she might take to achieve this, making clear how using a parser generator changes the build process for the compiler. (4 marks) (b) Consider the following excerpt from a grammar for a Java-like programming language in Backus-Naur form, as accepted by ANTLR and other parser generators: Page 4 (ii) Using the grammar, draw a parse tree for the following statement: statement : if (expression statement 'else' statement # Stmtif identifier expression ';' # StmtAssign : if (true) x = false; else y = false; expression : 'true' # ExpConstTrue | 'false' # ExpConstFalse : (6 marks) (c) Consider the following claim: identifier: X l'y' : "A human will always be able to optimise code at least as well as a compiler." The grammar also contains a rule to ignore whitespace (not shown). Give one argument in support of this claim and one argument against this claim. (4 marks) (1) Give a brief definition of each of the following components of a grammar: terminal nonterminal alternation For each component, give an example of where it occurs in the grammar above. (6 marks) (Question continues on next page)
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!