Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Context-free grammar
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Examples == {{more citations needed section|date=July 2018}} === Words concatenated with their reverse === The grammar <math>G = (\{S\}, \{\mathrm{a}, \mathrm{b}\}, P, S)</math>, with productions : {{math|''S'' β a''S''a}}, : {{math|''S'' β b''S''b}}, : {{math|''S'' β Ξ΅}}, is context-free. It is not proper since it includes an {{math|Ξ΅}}-production. A typical derivation in this grammar is : {{math|''S'' β a''S''a β aa''S''aa β aab''S''baa β aabbaa}}. This makes it clear that <math>L(G) = \{ww^R:w\in\{a,b\}^*\}</math>. The language is context-free; however, it can be proved that it is not [[Regular language|regular]]. If the productions : {{math|''S'' β a}}, : {{math|''S'' β b}}, are added, a context-free grammar for the set of all [[palindrome]]s over the alphabet {{math|{{mset|a, b}}}} is obtained.{{sfn|Hopcroft|Ullman|1979|loc=Exercise 4.1a| p=103}} === Well-formed parentheses === The canonical example of a context-free grammar is parenthesis matching, which is representative of the general case. There are two terminal symbols {{math|(}} and {{math|)}} and one nonterminal symbol {{math|''S''}}. The production rules are : {{math|''S'' β ''SS''}}, : {{math|''S'' β (''S'')}}, : {{math|''S'' β ()}} The first rule allows the {{math|''S''}} symbol to multiply; the second rule allows the {{math|''S''}} symbol to become enclosed by matching parentheses; and the third rule terminates the recursion.{{sfn|Hopcroft|Ullman|1979|loc=Exercise 4.1b|p=103}} === Well-formed nested parentheses and square brackets === A second canonical example is two different kinds of matching nested parentheses, described by the productions: : {{math|''S'' β ''SS''}} : {{math|''S'' β ()}} : {{math|''S'' β (''S'')}} : {{math|''S'' β []}} : {{math|''S'' β [''S'']}} with terminal symbols {{math|[}}, {{math|]}}, {{math|(}}, {{math|)}} and nonterminal {{math|''S''}}. The following sequence can be derived in that grammar: : {{math|([ [ [ ()() [ ][ ] ] ]([ ]) ])}} === Matching pairs === In a context-free grammar, we can pair up characters the way we do with [[bracket]]s. The simplest example: : {{math|''S'' β a''Sb''}} : {{math|''S'' β ab}} This grammar generates the language {{math|{{mset|a<sup>''n''</sup>b<sup>''n''</sup> : ''n'' β₯ 1}}}}, which is not [[regular language|regular]] (according to the [[pumping lemma for regular languages]]). The special character {{math|Ξ΅}} stands for the empty string. By changing the above grammar to : {{math|''S'' β a''S''b}} : {{math|''S'' β Ξ΅}} we obtain a grammar generating the language {{math|{{mset|a<sup>''n''</sup>b<sup>''n''</sup> : ''n'' β₯ 0}}}} instead. This differs only in that it contains the empty string while the original grammar did not. === Distinct number of as and bs === A context-free grammar for the language consisting of all strings over {{mset|a, b}} containing an unequal number of {{math|a}}s and {{math|b}}s: : {{math|''S'' β ''T'' {{pipe}} ''U''}} : {{math|''T'' β ''V''a''T'' {{pipe}} ''V''a''V'' {{pipe}} ''T''a''V''}} : {{math|''U'' β ''V''b''U'' {{pipe}} ''V''b''V'' {{pipe}} ''U''b''V''}} : {{math|''V'' β a''V''b''V'' {{pipe}} b''V''a''V'' {{pipe}} Ξ΅}} Here, the nonterminal {{math|''T''}} can generate all strings with more as than {{math|b}}s, the nonterminal {{math|''U''}} generates all strings with more {{math|b}}s than {{math|a}}s and the nonterminal {{math|''V''}} generates all strings with an equal number of {{math|a}}s and {{math|b}}s. Omitting the third alternative in the rules for {{math|''T''}} and {{math|''U''}} does not restrict the grammar's language. === Second block of bs of double size === Another example of a non-regular language is <math> \{ \text{b}^n \text{a}^m \text{b}^{2n} : n \ge 0, m \ge 0 \} </math>. It is context-free as it can be generated by the following context-free grammar: : {{math|''S'' β b''S''bb {{!}} ''A''}} : {{math|''A'' β a''A'' {{!}} Ξ΅}} === First-order logic formulas === The [[First-order logic#Formation rules|formation rules]] for the terms and formulas of formal logic fit the definition of context-free grammar, except that the set of symbols may be infinite and there may be more than one start symbol.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Context-free grammar
(section)
Add topic