This project implements a translator for a minimalist programming language in C/C++. The language supports variable declarations, array definitions, assignments, decision structures (if-else), loops (while), print statements, and read statements. The goal is to generate equivalent C code from programs written in this minimalist language.
To build the translator, follow these steps:
- Clone the repository:
git clone https://github.com/human416c6578/MiniLangTranslator.git
cd MiniLangTranslator- Create a build directory and navigate into it:
mkdir build
cd build- Generate build files with CMake:
cmake ..- Build the translator:
makeAfter building the translator, you can run it with an input file:
./translator input_file.txt >> out.cppSuppose you have the following minimalist program in a file named example.txt:
# Example program
var x
var y[5]
x = 10
y[2] = x + 5
if x > 5
print "x is greater than 5"
else
print "x is not greater than 5"
end
while y[2] > 0
print y[2]
y[2] = y[2] - 1
end
Run the translator:
./Translator example.txtThe translated C code will be printed to the console.
The lexer processes the input script and converts it into a sequence of tokens. Tokens include keywords, identifiers, numbers, symbols, and more.
The AST represents the hierarchical structure of the input program. It is generated by parsing the tokens obtained from the lexer.
The translator converts the AST into equivalent C code. It handles variable declarations, assignments, control structures, and expressions.
The main program orchestrates the entire process by reading the input file, invoking the lexer and parser, generating the AST, and finally, translating the AST into C code.
To extend the supported features of the minimalist language, modify the lexer, AST, and translator accordingly. Ensure that the grammar rules are appropriately handled in the parser.
Feel free to contribute to this project by opening issues or pull requests.
-
Comments start with the '#' symbol and continue until the end of the line.
Example:
# This is a comment
-
Variables are declared using the
varkeyword followed by the variable name.Example:
var x
-
Arrays are declared using the
varkeyword, followed by the array name and the number of elements in square brackets.Example:
var y[5]
-
Assignments are done using the assignment operator
=.Example:
x = 10 y[2] = x + 5
-
Decision structures are created using the
if,else, andendkeywords.Example:
if x > 5 # do something else # do something else end
-
While loops are defined with the
while,endkeywords.Example:
while y[2] > 0 # loop body end
-
Print statements are formed using the
printkeyword followed by the variable or a string literal.Example:
print "Hello World"
-
Read statements use the
readkeyword followed by the variable to store the input.Example:
read x
-
Arithmetic expressions can include variables, array elements, and numerical values. Supported operators are
+,-,*,/,|,&.Example:
y[2] = x + 5
-
Relational operators (
>,<,==) can be used in conditions for decision structures and loops. -
Logical operators (
||,&&) are supported for combining conditions.Example:
if x > 5 && y[2] < 10 # do something end
-
Parentheses can be used to group expressions and control the order of evaluation.
Example:
if (x > 5) || (y[2] < 10) # do something end
-
Strings can be included in print statements using double quotes.
Example:
print "Hello, World!"