Formal Languages Block

From MoodleDocs


  1. Idea, string analysis method, general architecture and architecture implementation - Oleg Sychev
  2. Implementation of built-in language - Dmitry Mamontov


The goal of formal languages block is to provide an API for managing formal languages - a well-known mathematical formalism, that defines a set of strings, constrained by rules.

Сurrent status

Feature Description Status
Scanning Breaks string into tokens, that can be useful in further analysis Implemented
Parsing Constructs abstract syntax tree from a string, allowing deep analysis of string or even performing evaluation Not implemented
Managing formal languages Currently C, C++, printf format string scanners are implemented. Also implemented simple lexer for english language. User-defined lexers and parser are going to be implemented in next releases. Implemented partially

Current language implementation status

Language Scanning Parsing
Simple english Implemented Not implemented
C Implemented Not implemented
C++ Implemented Implemented partially
printf formatting string Implemented Not implemented


To work Formal languages block needs some additional components. They all need to be installed in order for question to work.

You need to install question type POASquestion (qtype_poasquestion), which is abstract (i.e. not showing as real question), but contains useful code for scanning and working with Unicode strings.

User interface

block formal langs.PNG

After adding formal languages block to course, a teacher could manage language visibility by clicking on eye icons. The dimmed languages with disabled visibility will not be shown in CorrectWriting or other plugins, that use API of formal language block.

Note, that language visibility can be inherited from site settings. So, when site setting is applied to current course, user will see label "(Site)" before name of language, while "(Course)" label shows that setting applied on course level. If the language visibility for the site and course level are equal, language in this course assumed to be set to site-level visibility, and will be changed when the course-level visibility changes. If site visibility differs from course, it is assumed independent and will not change with the site-level changes.

Administrator also, can edit global visibility of formal languages, using global settings, located in administrator menu (see pictures for details).

block formal langs global settings link.PNG

A central part of admin page will look just like below.

block formal langs global settings page.PNG

When changing site-level language visibility, admin is shown the list of courses, affected by this change.

API for developers

A main block class - block_formal_langs provides a two simple functions, that might be useful for someone, that want to use our API to perform string scanning (see CorrectWriting question type examples to see how it works).

  1. block_formal_langs::available_langs - returns array of languages, that could be used in current context. Receives current context ID.
  2. block_formal_langs::lang_object - returns language object by language ID.

After getting language object, you can use create_from_string or create_from_db to scan string without referring to descriptions in database or refer to them. This will return a special object, that can be used for working with lexemes.

If $a is object, returned by create_from_string or create_from_db, you can use $a->string to get scanned string, $a->stream->tokens to return array of scanned lexemes, $a->stream->errors to get array of errors.

Using scanned lexemes, you could use type() method to obtain special type of lexeme, or value() to obtain a semantic value for lexeme.


1. You can use following code to print names and ids of all available languages in system context.

$langs = block_formal_langs::available_langs( context_system::instance()->id );

foreach($langs as $id => $name)


echo 'id :' . $id . ' name: ' . $name . PHP_EOL;


2. You can use following code to print all tokens' text, scanned a string with C language lexer.

$lang = block_formal_langs::lang_object(2); // Id for C programming language in most databases will be 2

$string = $lang->create_from_string('int a;');

$tokens = $string->stream->tokens;

foreach($tokens as $token)


echo $token->value() . PHP_EOL;