aboutsummaryrefslogtreecommitdiff
Context-free grammar for a subset of a natural language

Context-free grammar for a subset of a natural language
Slovak language – future tense

1. Task

Construct a context‑free grammar for a subset of natural language sentences, defined by a certain time. Based on the created grammar, implement the generation of random sentences belonging to the grammar and an algorithm for recognizing entered sentences.

2. Problem statement

The future tense in the Slovak language is called budúci čas or futúrum. The word order in the Slovak language is more or less free; changing it does not alter the meaning of the sentence, but changing the word order can be used to emphasize certain words. The usual word order is: subject + predicate + other members of the sentence + final punctuation mark (end mark).

In the Slovak language, it is possible to build complex sentences (or compound sentences) — these are sentences consisting of several simple sentences and expressing one complete thought. They can be both simple and complex.:

  • A simple complex sentence:
  • Contains two predicative units (two grammatical bases).
    • Example: Jano sadol do auta a odišiel.
    • Translation: Jano got into the car and drove away.
  • Compound complex sentence:
    • Contains more than two predicative units (three or more grammatical bases).
    • Example: Mama vedela, že príde, lebo mu verila.
    • Translation: Mom knew that he would come because she believed in him.

There are many different types of compound sentences, but they always use conjunctions or connective expressions to join the parts of the sentence.

To examine the applicability of context-free grammars in working with morphologically correct sentences, we will implement the agreement of person and number between the subject and the verb of the sentence fragment.

To display the result of the syntax analyzer’s operation, we'll automatically generate a parse tree.

3. Documentation

The exported report can be found at this link.

4. Showcase

I wrote a custom BNF parser for this, here is an example of what it can parse.

Listing 1 shows the generated sentences. You can see that they consist of a different number of fragments, have different endings, and in some fragments the subject is omitted.

Ty preľúbiš Kraskov, ona bude vedieť počítači, podliečíte dážď, cibuľa vyhľadá kuratá, ale oni ovaria morami, ty očítaš knihe, ono bude vedieť umenieie.
Ja načítam Kraskovi, ty budeš mať ruka, keď budeš vedieť stretnutia, ty prelietaš huslu, ale ono bude vedieť princeznej, keď vy učítate mláďatá, ale ty budeš plávať bruchom?
My budeme rozumieť dlaňe, bude robiť oleja.
Púšť bude páliť chlápät, keď ono podrozumie úskalia, keď my budeme mať orchideu, vy budete maľovať lístím, ale oni budú vedieť chlápäťu, keď ukupuje vysvedčeniach, ja utelefonujem pozadím.
Ty budeš telefonovať Nikaragua, keď ono bude vedieť Kraskovi, my budeme rozumieť noci, ale budem maľovať dážďmi?
Vy zaľúbite stromoch, budeš páliť bratoch...
Ty budeš tancovať uja.
Ty ukúpiš huslovi, lístia budú fotografovať Kraskovi, vy opálite umenieia, polia budú učiť mláďa, keď obľúbim knihe, keď my budeme tancovať chvíľe, vy privaríte trolejbusy, ony budú liečiť kosťou, vy budete liečiť vodopádi, bude učiť chvíľou!
Vy budete kupovať pieseňi, ale dážďe budú prerábovať nástupištia, my budeme robiť huslom?
Budú robiť rodiny, keď ja pohľadám bratoch.
Budem umývať sluhami, keď ty obliečíš kráľovnej.
My vypracujeme reportáž, keď ony budú ľúbiť úskaliam, vy budete rozumieť šľachtičnej, zarobia auto?
Nástupište opozná brata, keď ja ukúpim dlaňi, ale ja budem ľúbiť vodopád, ona prevedie kuliami, ona preliečí pážaťa...
Vody obhľadajú kôša, ale vy budete rozumieť zmesiach, ale vyčítam báseňiach, vy budete rozumieť košeľíc...
Vy obrozumiete huslov...
My oučíme stromi, keď obtancuje pecou, ty budeš liečiť hrdinovi?
Ty budeš mailovať stredám?

In listing 2, an example is provided that does not comply with the grammar rules because the sentence starts with a lowercase letter instead of a capital one.

Enter string to parse: my oučíme stromi, keď obtancuje pecou, ty budeš liečiť hrdinovi?
didn't match!

Lisiting 3 shows an example that does not conform to the grammar due to a mismatch in number between the subject and the verb. "My oučím" can be matched in number in the following ways: "My oučíme", "Ja oučím".

Enter string to parse: My oučím stromi, keď obtancuje pecou, ty budeš liečiť hrdinovi?
didn't match!

Listing 4 shows an example of a sentence that complies with the grammar. And figure 1 presents the automatically generated parse tree for it.

Enter string to parse: My oučíme stromi, keď obtancuje pecou, ty budeš liečiť hrdinovi?
matched
./tmp/tree.dot:1:0: info: generated parsing tree

tree.png

Figure 1: Parsing tree of the complient sentence

5. 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 09:23