Difference between revisions of "Context-free grammar"

From The Battle for Wesnoth Wiki
(Examples: Format to take advantage of syntax highlighting)
(External links: reword parenthetical)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Context-free grammar can be used to generate random strings, from short words to entire paragraphs. If you are interested in its theory, read more on [https://en.wikipedia.org/wiki/Context-free_grammar].
+
Context-free grammar can be used to generate random strings, from short words to entire paragraphs. If you are interested in its theory, read more on [https://en.wikipedia.org/wiki/Context-free_grammar Wikipedia's article on the topic].
  
 
== Syntax ==
 
== Syntax ==
Line 28: Line 28:
  
 
Each nonterminal has a reduced probability for possibilities that were chosen the last time.
 
Each nonterminal has a reduced probability for possibilities that were chosen the last time.
 +
 +
A production may include vertical bars or curly braces by using the following predefined nonterminals:
 +
* <code>{!}</code> - expands to a |
 +
* <code>{(}</code> - expands to a {
 +
* <code>{)}</code> - expands to a }
 +
 +
== External links ==
 +
* [https://dugy.github.io/?M2NvbvRmdWzsbGFu5zJzb3VyY+V0eXDlf21haW49e0hlbGxvfSwge3Zpc2l0b3J9IQpIZWxsbz1IZWxsb3xIZXl8V2VsY29tZQp2aXNpdG9yPXZpc2l0b3J8ZnJpZW5kfHBhbABjY2ZnAkA= Dugi's online context-free grammar tester] (warning: if opening, be sure to do so in a new tab)

Latest revision as of 04:31, 8 May 2023

Context-free grammar can be used to generate random strings, from short words to entire paragraphs. If you are interested in its theory, read more on Wikipedia's article on the topic.

Syntax

In WML, it uses its own simple syntax. It is usually a translatable string and should be in <<French quotation marks>>. The code is composed from nonterminals. They are written on separate lines. Each nonterminal is substituted by one of its possibilities separated by the | symbol, the name is separated from possibilities with =. The nonterminal that makes the result is named main. The possibilities can contain other nonterminals, marked by {curly brackets}.


Examples

name_generator= _ <<
    main=Hello world|Hi world|Ahoi world
>>

The result is either Hello world or Hi world or Ahoi world.

name_generator= _ <<
    main=Hello {noun}|Hi {noun}
    noun=world|planet|Universe|Earth
>>

The result is either Hello or Hi, followed by one of the four possible nouns, world, planet, Universe or Earth.

Other rules

A nonterminal can reference itself, which can, under the right circumstances, lead to a loop that expands indefinitely, causing a stack overflow. Avoid this unless you really know what you are doing.

Each nonterminal has a reduced probability for possibilities that were chosen the last time.

A production may include vertical bars or curly braces by using the following predefined nonterminals:

  • {!} - expands to a |
  • {(} - expands to a {
  • {)} - expands to a }

External links

This page was last edited on 8 May 2023, at 04:31.