Enhance your understanding of context-free grammars, their properties, and ambiguity in formal languages with this targeted quiz. Designed to introduce key terms, concepts, and practical examples, this quiz helps learners grasp the role of context-free grammars and the meaning of ambiguity in syntax analysis.
Which best defines a context-free grammar (CFG) in formal language theory?
Explanation: A context-free grammar is defined such that each production replaces a single non-terminal with a string of terminals and/or non-terminals. The other options are incorrect because context-free grammars can have more than one symbol on the right-hand side, do not require mapping only terminals to non-terminals, and can generate infinite as well as finite languages.
If a grammar allows at least one string to have two distinct parse trees, what is this property called?
Explanation: Ambiguity in a grammar occurs when a string can be generated in more than one way, resulting in different parse trees. Recursion relates to productions referring to themselves. Associativity deals with how operators group, and determinism involves choices without conflicts.
Which of the following grammars is guaranteed to be non-ambiguous?
Explanation: If a grammar generates only one string, there cannot be more than one parse tree for it, so it must be non-ambiguous. Having left-recursion, multiple start symbols, or epsilon productions does not automatically guarantee non-ambiguity, as these can still lead to multiple parse trees for the same string.
Given the grammar S → S + S | S * S | a, why might this grammar be ambiguous for the string 'a + a * a'?
Explanation: For the string 'a + a * a', the grammar allows multiple parse trees due to lack of operator precedence, which is a classic case of ambiguity. The other options are incorrect: the grammar is context-free, it can generate the string, and it has non-terminals as well as terminals.
What is a common method for removing ambiguity from arithmetic grammars?
Explanation: Ambiguity is frequently resolved by structuring the grammar to enforce operator precedence and associativity (e.g., separate rules for addition and multiplication). Allowing every possible string, changing all terminals, or introducing more start symbols do not address ambiguity and can even make the grammar more confusing.
Can the presence of epsilon (empty string) productions in a grammar directly cause ambiguity?
Explanation: Epsilon productions do not inherently make a grammar ambiguous, but in some cases, when combined with other rules, they can create multiple parse trees for the same string. It is incorrect to say they always or never introduce ambiguity, and context-free grammars do allow epsilon productions.
Which type of language is always generated by some context-free grammar?
Explanation: Every context-free language is by definition generated by a context-free grammar. Regular and recursive languages are classes that may or may not be context-free, and unrestricted languages encompass types that go beyond what context-free grammars can produce.
What is a consequence of using an ambiguous context-free grammar in syntax analysis?
Explanation: The situation where a string can be derived in multiple ways means the structure is unclear, potentially leading to ambiguity in how the string should be interpreted. The other options are incorrect because ambiguous grammars do not guarantee unique derivations, do not necessarily improve speed, nor always correspond to infinite languages.
In a context-free grammar, a production like A → aBa means which of the following?
Explanation: The production A → aBa means that whenever A is encountered during derivation, it can be replaced by 'aBa'. The other options are either misleading or too restrictive. 'a' is not a non-terminal, B is not required everywhere, and A can generate more than just 'a'.
Which method is commonly used to check if a context-free grammar is ambiguous?
Explanation: To show ambiguity, one typically needs to exhibit a string having more than one parse tree. Listing terminals, counting productions, or looking for left recursion does not directly assess ambiguity, although these aspects may relate to other properties of the grammar.