FAF 233 Nicolai Petcov 8
FAF 233 Nicolai Petcov 8
Computer Programming
Laboratory work #7
Author: Verified:
George Răbus, Alexandru Furdui
std. gr. FAF-233
Nicolae Petcov
std. gr. FAF-233
Theory Background
A Domain-Specific Language (DSL) is a programming or specification language
dedicated to a particular problem domain, a particular problem representation technique,
and/or a particular solution technique. Unlike general-purpose programming languages,
which are designed to be broadly applicable, DSLs are crafted for a specific application
domain or problem set. The goal of DSLs is to provide a more expressive and concise
means for addressing problems within their designated scope. The lexer first tokenizes
the syntax, then the parser orders the operations with binary tree algorithm and the
interpreter executes the code
grammar/syntax
The DSL has the following operators:
add - addition
sub - substraction
mult - multiplication
div - division
init - initialize integer
fl - initialize float
The program should start with ”START” and end with ”END”
Every line should end with ”;”
print(x) - printing a variable
auto zero after initialization of variable
if (n>x) necessary bracets for all conditions
The Task
Describe your task, and enumerate the task/tasks you have implemented:
1. Implement a lexer
A lexer, short for lexical analyzer, is a crucial component in the process of compiling
or interpreting programming languages. Its primary purpose is to take the source
code of a program (a sequence of characters) and break it down into a stream of
tokens. Tokens are the smallest units of meaning in a programming language and
represent the building blocks of the language’s syntax.
2. Implement a parser
A parser, short for parser generator or syntactic analyzer, is a crucial component
in the process of interpreting or compiling programming languages. Its primary
purpose is to analyze the syntactic structure of the source code and generate a hier-
archical representation, typically in the form of an abstract syntax tree (AST). The
FAF-233 Name Surname; Laboratory Work №7 4
parser ensures that the input adheres to the grammatical rules of the programming
language.
Technical implementation
Here is an example where the parser is ordering the expression in binary tree algoritm
TreeNode * Parser :: parseExpression ()
{
TreeNode * left = parseTerm () ;
if ( currentToken - > type == TOKEN_ADDITION || currentToken
- > type == TOKEN_SUBTRACTION )
{
wasMinusOperation = true ;
Token op = * currentToken ;
currentToken ++;
TreeNode * right = parseTerm () ;
left = createBinaryOpNode ( op . value , left , right ) ;
}
wasMinusOperation = false ;
return left ;
}
Results
Describe your results.
As a result we got a semi-functional DSL system where the user can input any operations
and get the correct answer.
FAF-233 Name Surname; Laboratory Work №7 5
Conclusion
The DSL project demonstrated the creation of a simple programming language with
a lexer, parser, and interpreter. The lexer efficiently tokenizes source code, the parser
constructs a binary tree-based Abstract Syntax Tree, and the interpreter executes the
code. Incorporating basic arithmetic operations and control flow statements, the project
illustrated the fundamental principles of language processing. While this DSL is limited,
it provides a foundation for understanding how compilers and interpreters handle code,
making it an insightful exercise for anyone delving into programming language design
and implementation.
Bibliography
1. stackoverflow.com
2. chat.openai.com
3. geekforgeeks.com
4. youtube.com
5. github.com