aboutsummaryrefslogtreecommitdiff
Grammar with semantics

Grammar with semantics
Random-art generation

1. Overview

  • Augmented a standard context-free BNF grammar with production rule weights (probabilities) to control expression generation density and visual aesthetics.
  • Optimized and left-factorized the extended grammar to ensure compatibility with deterministic parsing methods.
  • Implemented a custom C-based lexer and recursive descent parser to process rules, terminals, nonterminals, and probabilities.
  • Engineered a stochastic expansion algorithm with depth thresholding to synthesize complex GLSL fragment shaders driven by screen coordinates, mouse input, and time.
  • Configured distinct semantic weight profiles to generate varying aesthetic styles (smooth vs. noise-heavy procedural patterns).
  • Developed a custom GNU Emacs major mode (bnf-mode.el) for editing BNF grammars.

2. Documentation

The exported report can be found at this link.

3. Showcase

Example 1

Input:

 <expr> ::= <value> | 0 "-" <expr> | 10 <binop> | <trig> | 0 "abs(" <expr> ")" | "sqrt(abs(" <expr> "))"
<value> ::= <coord> | 2 <mouse> | <time> | <const>
<coord> ::= 4 "c.x" | 3 "c.y"
<mouse> ::= 4 "m.x" | 3 "m.y"
 <time> ::= "sin(t)" | 0 "cos(t)"
<const> ::= "." <digit> <digit> <digit> <digit> <digit> <digit>
<digit> ::= 2 "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
<binop> ::= 10 "(" <expr> " + " <expr> ")" | 5 "(" <expr> " - " <expr> ")" | 3 <expr> "*" <expr> | 0 "(" <expr> "/" <expr> ")"
 <trig> ::= "sin(" <expr> ")" | "cos(" <expr> ")" | "tan(" <expr> ")"

Output:

(((((cos((tan(((cos(((((((cos(((((c.x + (((((sqrt(abs((((cos(sqrt(abs((sqrt(abs(tan(sin((((((((m.y*(sin((((((((((sqrt(abs(((( cos(((sqrt(abs(cos(c.x))) + ((((sqrt(abs(((m.y - ((((((.923344 - (.949094 + m.y)) + m.x) + sin(t)) - m.y)*m.x - sin(t)) + c.y)) - m.x)*sin(t))) - m.y) + m.y) + m.y)*m.y - sin(t))) - m.x)) + m.x) - sin(t)) + m.x))) - sin(t)) - m.x) + sin(t)) - .112088)*c.x - m.x) + .233916) - c.y) + m.x) + sin(t))) + m.x) + sin(t))*c.y*m.x + c.x) + .190365) - .887840) + sin(t)) + m.x) + m.y))*.291275))) - sin(t))))) + .287723) - m.x) - sin(t)))) - m.y) + sin(t))*c.x + m.y) + m.y) + .830306)*m.y*sin(t)) + c.y) + c.y)*m.y + c.x)) + m.x) - c.x) + m.x) - sin(t)) + c.y) + c.y)) + c.x) + c.y))*c.y*c.y + m.x)*.256283) - .585881) + .177134) + m.x) + c.x), (((((((((sqrt(abs(sin(((((((((cos((sqrt(abs((((((((((((sqrt(abs((((m.y - (sqrt(abs(m.x)) - sin((((((((m.x + ((((m.y - (((((((c.x + (((((((((m.x + .018174) - cos(sqrt(abs(((tan(m.x) - .689349) + m.x))))) - .420239) + m.x) + m.x) + sin(t)) + m.x) - m.x) + m.x)) - m.y)*sin(t) + sin(t)) + c.x) + .142664) - m.y) + m.x)) + c.x) + m.y)*.800054 - c.y)) - .509630) + m.y) - c.y) - sin(t))*m.x + .447933) - .237243)))) + m.x) + m.y)))*.733285 + c.x) - m.x) + m.x)*sin(t) + m.x) + .100814) + m.y) - m.x) - m.x) + sin(t)) + m.x) + .907417))) + m.y))*.388498 + sin(t)) + .016879) + c.y) + sin(t)) + .905070) - c.x) - c.x) + m.x)))) + .531642) - sin(t)) + m.y) + m.y) - .722737) + sin(t)) - c.y) - m.y) + m.x), sqrt(abs((m.x - tan(tan((sqrt(abs((((c.x - (((((((sin(sqrt(abs((sqrt(abs((((((m.y - (sin(((((sqrt(abs(((((sin(t) + (((((((((((((sin(t) - ((((((c.y*(((m.x + .127801) - (sqrt(abs((sin(t) + m.x))) + .900278)) + m.y)*m.y - m.x) + .191150) - m.x) + c.y)*sin(t) + .760660) + m.y)) + m.x) + m.y) - .470661) + .239425) + m.x) + m.y) - .085803) + m.x) - m.y) + m.x) + .304005) - .044509)) + m.x) + m.x) + c.x)*.156629)) - m.y) + m.x) - sin(t)) - m.x))*.501841 - m.x)) + m.y)*m.y + .098450) + m.x) - m.x))) + sin(t))))) - c.y) + m.y) + .005222)*m.y + m.x) + m.x) + .030531) + m.x)) + m.x) + c.x))) + m.x)))*c.x)))*m.x)

Example frame:

10.png

Example 2

Input:

 <expr> ::= <value> | 0 "-" <expr> | 10 <binop> | 3 <trig> | 1 "abs(" <expr> ")" | 5 "sqrt(abs(" <expr> "))"
<value> ::= 3 <coord> | <mouse> | <time> | <const>
<coord> ::= 4 "c.x" | 3 "c.y"
<mouse> ::= 4 "m.x" | 3 "m.y"
 <time> ::= "sin(t)" | "cos(t)"
<const> ::= "." <digit> <digit> <digit> <digit> <digit> <digit>
<digit> ::= 10 "0" | 9 "1" | 8 "2" | 7 "3" | 6 "4" | 5 "5" | 4 "6" | 3 "7" | 2 "8" | 1 "9"
<binop> ::= 1 "(" <expr> " + " <expr> ")" | 30 "(" <expr> " - " <expr> ")" | 5 <expr> "*" <expr> | 7 "(" <expr> "/" <expr> ")"
 <trig> ::= "sin(" <expr> ")" | "cos(" <expr> ")" | 0 "tan(" <expr> ")"

Output:

((sqrt(abs((sqrt(abs(sqrt(abs(((c.x/sqrt(abs(sqrt(abs(((((abs(((((((((abs(sqrt(abs(sin(((( cos(((cos(sqrt(abs(sqrt(abs(((sin((sqrt(abs(((sqrt(abs(sqrt(abs((((sqrt(abs((sqrt( abs((((sqrt(abs(sqrt(abs(((((((sqrt(abs(c.x))*(sqrt(abs((sqrt(abs(sin(abs((((c.x - (abs(abs((((sqrt(abs((sqrt(abs(sqrt(abs(((sqrt(abs((sin(sin(sin(((sin((cos(((cos(abs((( sqrt(abs(cos(c.x)))*cos((c.y - c.x)) - m.y) - c.x))) - c.x)/c.x)) - c.y)) - c.y) - sin(t)))))/.024006))) - c.x) - .183139))))) - m.x))) - c.y)/sin(t)) - sin(t)))) - sin(t))) - c.x) - m.y))))) - m.x))) - c.x) - c.x) - sin(t)) - c.x) - cos(t)) - c.y)/c.x)*m.x)))) - .223574) - c.x) - cos(t)))) - c.y)))/m.x) - c.x) - m.x))))) - c.x) - cos(t)))) - m.x)) - c.x) - c.x)))))) - c.x) - c.y)) - sin(t)) - .406444) - c.y))))) - m.y)/m.x) - c.x) - c.y)/cos(t))/.262350) - sin(t)) - c.y)) - c.x) - c.x)/c.x)/m.x)))))) - cos(t)))))) - c.x))) - c.x), cos(t), sin(((((((((cos(sqrt(abs((sqrt(abs(c.y)) - c.x)))) - (sqrt(abs((sqrt(abs(m.x)) - ((cos(sqrt(abs((m.y - c.y)))) - sqrt(abs(abs((sqrt(abs((((abs((sqrt(abs(sqrt(abs(cos((m.y - ((.254220 - (sqrt(abs(sqrt(abs(((m.y - sin(sqrt(abs((cos(sqrt(abs(sqrt(abs(sin(sqrt(abs(abs(sin(((((sqrt(abs(m.x)) - c.y)*.213340*c.x*cos(t) - .877721)/m.y) - .632123))))))))))) - c.x))))) - c.x))))) - cos(t))) + c.y))))))) - sin(t))) - c.y)/c.x) - c.x))) - c.y)))))*c.x - c.y))))/.123224))/m.x) - c.y)*.273026/c.x)/c.y) - cos(t)) - m.x)/m.y)))

Example frame:

13.png

4. Unlicense

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to https://unlicense.org/

Date: 2025H1

Author: Martin Michalec

Created: 2026-11-08 Sun 11:23