Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/358219384

Applications of context free grammars-CFG In Sonic System

Conference Paper · January 2022

CITATIONS READS
0 204

1 author:

Amlan Nag
Thompson Rivers University
4 PUBLICATIONS   1 CITATION   

SEE PROFILE

All content following this page was uploaded by Amlan Nag on 30 March 2022.

The user has requested enhancement of the downloaded file.


Applications of context free grammars-CFG In
Sonic System
Amlan Nag

Bachelor of Computing Science,Thompson Rivers University, Kamloops, BC , Canada


Data Analyst and Back-end Developer, Fiscal Realities Economists , Kamloops, BC , Canada

amlannag6@gmail.com

Abstract—This project will outline some techniques for feasible and then outputs the resulting sequence of terminals.
context-free grammar (CFGs). Specifically, our group will be If a* precedes a symbol, it indicates that it can be extended
looking at an application (Sonic System) that can be thought into zero or more occurrences of that symbol. The addition of
out as music technology. The objective is to build a system
that can simplify the creation of complex musical generation a + means that the character can be extended into one or more
by allowing users to create a generative CFG and then use circumstances. Grammars can also be compounded, meaning
it to quickly define and generate different types of music. We that one grammar applied to an initial sequence is utilized as
can look at Grammar inference which is the procedure of input to another.
understanding grammar from examples. Grammar inference
has been successfully applied to many diverse projects such as A. Some DSLs are also known as domain-specific languages
speech recognition and robotics. The topic’s focus will be on the that are created for just one particular goal. Athena CL is one
use of randomized productions. By using this system, complex
definitions can be created as a series of productions. The intended
such example.
operation is to create a system for defining context-free grammars B. AthenaCL is considered a declarative style of program-
(CFGs) and using randomized production inference to solve ming. This style is a lot easier than other styles to define what
problems in software engineering and programming language
one wants rather than writing particular commands to gener-
development. In addition, studying and creating CFG systems
specifically expands a sequence of terminals into non-terminals ate that definition. But, as the information is not represented in
using randomized productions to expand a sequence of terminals more complex components in imperative languages, it makes
into non-terminals. This is in contrast to the more usual action of them easier to create and render.
parsing a string of previously created terminals into higher-level
non-terminals, which is essentially the same thing. After then, a C. DSLs probably lean towards more of a decorative style
series of terminals can be processed to produce an output, such as rather than broader programming languages
a sound or a song. This approach can easily generate complicated D. CFGs are commonly used to define a programming lan-
definitions as a series of productions, each of which maps a
symbol to one or more probabilistically-weighted expansions,
guage’s high-level structure. The CFG engine we will be
resulting in complex randomized but structured behaviour. creating will be a tool to create simple DSLs.
Index Terms—component, formatting, style, styling, insert III. I MPLEMENTATION
I. I NTRODUCTION Now let’s talk about the implementation of context-free
grammar in Sonic System and the Musical scale of EECS20N:
This project will outline some techniques for context-free
Signals and Systems. An operation of CFG resulting sequence
grammar (CFGs). Specifically, our group will be looking at
of terminals.
an application (Sonic System). A system defining (CFGs) and
Here is a python based file of Sonic.ipynb where this
expanding a sequence of terminals into non-terminals using
program file accepts a string of symbols separated by spaces.
randomized production. A system that can produce some sort
A grammar file expands each character recursively according
of output, such as a sound or a song. The objective is to build
to the specified grammar until no further expansions are
a system that can simplify the creation of complex musical
feasible and then outputs the resulting sequence of terminals.
generation by allowing users to create a generative CFG and
In general, grammar in my system is a set of products that
then use it to quickly define and generate different types of
map a symbol to one or more expansions. A series of one or
music.
more characters is defined as an expansion. Each development
II. L EARNING OBJECTIVES is labelled with a weight that indicates how likely it is to be
A grammar file expands each character recursively accord- picked from the available options. Grammars in my system are
ing to the specified grammar until no further expansions are represented as YAML files, a data serialization format known
for its readability. The grammar is defined as a hash, with
Identify applicable funding agency here. If none, delete this. symbols as keys and a hash of expansions as values. Each
expansion hash is keyed by a single space-delimited string of
characters with an integer weight as a value.
Then – from a developed project where CFG implemented
in the sonic system - a waveform function which is modelled
as successive additions and multiplications.
If a* precedes a symbol, it indicates that it can be extended
into zero or more occurrences of that symbol. The addition
of a + means that the character can be extended into one
or more circumstances. A range enclosed in brackets (such
as [220-880] above) is developed into an integer drawn at
random from that range. Grammars can include recursive
productions, but each presentation must have at least one
non-recursive expansion since the script expands the sequence
until it only contains terminals. Code ** Sonic.py prints the
sequence formed by consecutive expansions, as shown above.
The first line contains the beginning sequence, while the last
line generates the final series of non-terminals.
Of course, this sequence is meaningless unless we can
convert it to the waveform it represents, which in this case
is a square wave convolved with a sine-wave and then added
to another sine-wave (the numbers are the frequencies). I built Fig. 1. Implementation of CFG
another script, wav gen.py, that accepts this type of sequence
and generates a wav file.
Grammars can also be compounded, meaning that one
grammar applied to an initial sequence is utilized as input
to another. When several grammar files are sent to gen.py as
parameters, the grammar is composed by calling the first one
mentioned first with the beginning sequence, sending its result
to the second, etc
This can be seen through an example using this YAML file,
song.yaml. This is a file that shows a simple definition of a
song.
A. Programming Implementation Of Sonic System
a) Description: This code shows the corresponding sin
waves for each Note and their frequencies. When we compose
these two grammars together, we get the following, each note
in a song produces a corresponding sin wave which you can
see here.
This sequence was in turn expanded using notes.yaml,
giving us the wave-forms of the notes. If we want to hear this
song, we cannot use gen.py, as it combines waveforms into
a single waveform through addition or multiplication instead Fig. 2. Converting Notes to Waveforms
of sequencing We can use another script to perform this task.
Wavsequencer.py. wavsequencer.py takes an initial sequence
and an arbitrary number of grammar files, like gen.py and R EFERENCES
wavgen.py, expands the initial sequence by composing the
[1] Introduction to compilers and Language Design. (n.d.). Retrieved Jan-
grammars and then uses wavgen.py to render each symbol uary 21, 2022, from http://www3.nd.edu/ dthain/compilerbook/
in the new sequence [2] Dynamic compilation of weighted context-free grammars. (n.d.). Re-
trieved January 21, 2022, from https://aclanthology.org/P98-2147.pdf
ACKNOWLEDGMENT [3] Vı́rseda, Rafael. (2021). Learning Compiler Design: From the Imple-
mentation to Theory. 609-610. 10.1145/3456565.3460041.
I would also like to show my gratitude to the Chakrapani [4] Heaps, heapsort, priority queues - math.uaa.alaska.edu.
Kurra, Devendrasinh Adajania, Rajat Mahajan from Thompson (n.d.). Retrieved January 21, 2022, from
Rivers University for sharing their pearls of wisdom with this http://math.uaa.alaska.edu/ afkjm/cs351/handouts/heaps.pdf
[5] Ariza, C. (n.d.). Project sample: Generative context-free grammars. MIT
research, and I thank Dr. Yan Yan for reviewing insights of OpenCourseWare, Massachusetts Institute of Technology. Retrieved
the project in the development stage. January 21, 2022, from https://ocw.mit.edu/courses/music-and-theater-
arts/21m-380-music-and-technology-algorithmic-and-generative-music-
spring-2010/assignments-and-projects/generative-context/

View publication stats

You might also like