Grammar and Language
COS3701 - Theoretical Computer Science III · Formal Languages
Grammar and Language
A grammar is a set of rules that defines the structure of a language. In formal languages, a grammar specifies how strings can be generated from a given alphabet. Understanding grammars is essential for studying languages in theoretical computer science.
Definitions
The key components of a grammar include:
- Alphabet: A finite set of symbols. For example, the alphabet {a, b} consists of the symbols 'a' and 'b'.
- String: A finite sequence of symbols from an alphabet. For example, 'abba' is a string over the alphabet {a, b}.
- Grammar (G): A formal system that consists of a set of production rules used to generate strings in a language. A grammar is usually defined as G = (V, Σ, R, S), where:
- V: A set of variables (non-terminal symbols).
- Σ: A set of terminal symbols (the alphabet).
- R: A set of production rules.
- S: The start symbol, a special variable from which generation begins.
Types of Grammars
Grammars can be classified based on their production rules. The most common types include:
- Type 0 (Unrestricted Grammar): No restrictions on the production rules. These grammars can generate any recursively enumerable language.
- Type 1 (Context-sensitive Grammar): Production rules are of the form αAβ → αγβ, where A is a non-terminal, and α, β, and γ are strings of terminals and/or non-terminals. These grammars generate context-sensitive languages.
- Type 2 (Context-free Grammar): Production rules are of the form A → γ, where A is a non-terminal and γ is a string of terminals and/or non-terminals. These grammars generate context-free languages.
- Type 3 (Regular Grammar): Production rules are of the form A → aB or A → a, where A and B are non-terminals and a is a terminal. These grammars generate regular languages.
Remember: The Chomsky hierarchy classifies grammars into these four types based on their generative power and the complexity of the languages they can describe.
Context-Free Grammars
A context-free grammar (CFG) is a specific type of grammar where each production rule replaces a single non-terminal symbol with a string of terminals and non-terminals. CFGs are widely used in programming languages and artificial intelligence.
Example of a Context-Free Grammar
Consider a simple CFG that generates balanced parentheses:
G = (V, Σ, R, S)Where:
V = {S}Σ = {(, )}R = {S → SS, S → (, S, ), S → ε}S = S
This grammar can generate strings like '()', '(())', and '(()())'.
Generating Strings
To generate a string from a grammar, you start from the start symbol and apply production rules to replace non-terminals with their corresponding strings. Let's generate the string '(())' using the context-free grammar defined above:
- Start with the start symbol:
S. - Apply the rule:
S → (, S, ). - Replace
S:S → SS. - Replace the first
S:S → (, S, ). - Replace the second
S:S → ε.
(S)(SS)((S))(())Watch out: Ensure that you apply the production rules correctly. Each step must follow the rules defined in the grammar.
Languages Generated by Grammars
The set of all strings that can be generated by a grammar is called the language of the grammar. For example, the language generated by the context-free grammar for balanced parentheses includes all strings with matching opening and closing parentheses.
Example of a Language
The language of the grammar defined above can be expressed as:
L(G) = { w | w contains matching pairs of parentheses }This language includes strings such as '', '()', '(())', and '(()())'.
Parsing and Derivations
Parsing is the process of determining if a given string belongs to a language generated by a grammar. It involves constructing a parse tree, which represents the structure of the string according to the grammar.
Example of a Parse Tree
Consider the string '(())'. The parse tree for this string based on our CFG is:
S
/ \
( S
/ \
( S
/ \
ε )
)
This tree shows how the string '(())' is derived from the start symbol using the production rules.
Ambiguity in Grammars
A grammar is ambiguous if there are multiple parse trees for the same string. Ambiguity can complicate parsing and understanding of the language. It is essential to identify and resolve ambiguity when designing grammars.
Example of Ambiguity
Consider the following grammar:
S → AB
A → a | aA
B → b | bBThe string 'ab' can be generated in two different ways:
S → AB → aB → ab
S → AB → A → aA → abBoth derivations lead to the same string, showing that the grammar is ambiguous.
Watch out: Ambiguous grammars can lead to confusion. It is often necessary to create a new grammar that is unambiguous for the same language.
Conclusion
Grammars play a crucial role in defining formal languages. Understanding the types of grammars, how to generate strings, and the concept of ambiguity is essential for studying theoretical computer science. Context-free grammars are particularly important for programming languages and their parsing.
Summary
- A grammar consists of an alphabet, strings, production rules, and a start symbol.
- Grammars can be classified into four types based on their generative power.
- Context-free grammars generate context-free languages and are widely used in programming.
- The language of a grammar is the set of all strings it can generate.
- Parsing involves constructing parse trees to determine if a string belongs to a language.
- Ambiguity in grammars can lead to multiple parse trees for the same string.
Check your understanding
- What are the components of a grammar?
- Define a context-free grammar and provide an example.
- How can you identify ambiguity in a grammar?
- What is the significance of parse trees in parsing a string?