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
Posted: Mon May 09, 2022 6: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 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: statement : if ('expression '' statement 'else' statement # Stmtlf identifier '='expression ';' # StmtAssign expression : 'true' #ExpConstTrue | 'false' # ExpConstFalse identifier : 'x' I'Y The grammar also contains a rule to ignore whitespace (not shown). (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)
(ii) Using the grammar, draw a parse tree for the following statement: if (true) x = false; else y = false; (6 marks) (c) Consider the following claim: "A human will always be able to optimise code at least as well as a compiler." Give one argument in support of this claim and one argument against this claim. (4 marks)
(ii) Using the grammar, draw a parse tree for the following statement: if (true) x = false; else y = false; (6 marks) (c) Consider the following claim: "A human will always be able to optimise code at least as well as a compiler." Give one argument in support of this claim and one argument against this claim. (4 marks)