Context-free grammar: Difference between revisions

From The Battle for Wesnoth Wiki
Dugi (talk | contribs)
Created page with "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..."
 
→Examples: Format to take advantage of syntax highlighting
Line 7: Line 7:
== Examples ==
== Examples ==


<nowiki>main=Hello world|Hi world|Ahoi world</nowiki>
<syntaxhighlight lang=wml>
name_generator= _ <<
    main=Hello world|Hi world|Ahoi world
>>
</syntaxhighlight>


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


 
<syntaxhighlight lang=wml>
 
name_generator= _ <<
<nowiki>main=Hello {noun}|Hi {noun}</nowiki>
    main=Hello {noun}|Hi {noun}
 
    noun=world|planet|Universe|Earth
<nowiki>noun=world|planet|Universe|Earth</nowiki>
>>
</syntaxhighlight>


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

Revision as of 21:34, 12 April 2016

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 [1].

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.

This page was last edited on 12 April 2016, at 21:34.
Cookies help us deliver our services. By using our services, you agree to our use of cookies.