Question
Consider the following simple grammar (S is the start symbol): S ::= A | B | C A ::= A int B ::= B int
Consider the following simple grammar (S is the start symbol):
S ::= A | B | C A ::= "A" int B ::= "B" int bool C ::= "C" bool
Here, int matches a string representing a valid integer (e.g., "47") and bool matches only "true" or "false". The following Java method parse() implements a parser for this format:
static boolean parse(String[] arr) { if (arr.length == 0) return false; int ind = 0; boolean valid = true; String val = arr[ind]; if (val.equals("A")) { ind++; if (ind < arr.length && isInt(arr[ind])) { // valid } else { valid = false; } } else if (val.equals("B")) { ind++; if (ind < arr.length && isInt(arr[ind])) { ind++; if (ind < arr.length && isBool(arr[ind])) { // valid } else { valid = false; } } } else { // val.equals("C") ind++; if (isBool(arr[ind])) { // valid } else { valid = false; } } return valid && ind == arr.length - 1; }
parse() should return true for inputs matching the grammar and false otherwise. E.g., it should return true for input ["B", "47", "true"] and false for input ["A", "false"]. Assume isBool() and isInt() correctly check for bool and int strings.
There are (at least) two bugs in the parse() function, where it returns true for a String[] input not in the grammar. These bugs can be discovered via test generation after grammar mutation. Mutations can include changing some (non-)terminal of a grammar rule (e.g., changing the second rule to A ::= "A" "B") or removing some (non-)terminal in the rule (e.g., changing the second rule to A ::= int). Once the grammar is mutated, String[] inputs can be generated from the mutated rule to test the code.
Give two mutated grammar rules, such that when each mutated rule is used for test generation, it would expose a bug where parse() returns true for an invalid input. Explain your answers. Each of your rules must expose a distinct bug; you cannot write two mutated rules for the same bug.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started