This is a simple logic compiler that interprets a language with boolean expressions and control flow structures.
- Keywords:
AND,OR,NOT,TRUE,FALSE,if,else,then,for,while,var - Symbols:
(,),{,},=,; - Identifiers: Strings that start with a letter and are followed by any number of letters or digits.
The language has the following grammar:
program: A list of statements.statement: An if-else statement, a while loop, a for loop, a variable declaration, or an assignment.statements: A list of statements.expr: A boolean value, a not expression, a binary operation, or an identifier.
ast.candast.h: Define the abstract syntax tree (AST) for the logical expressions.interpreter.candinterpreter.h: Define the interpreter that evaluates the AST.lexer.l: Defines the lexical analyzer that tokenizes the input.parser.y: Defines the syntax analyzer that parses the tokens into an AST.main.c: Contains the main function that ties everything together.Makefile: Defines the build rules for the project.test.c: Contains a test program that runs the logic compiler on several test files and checks if the output matches the expected result.
-
declaration_tests/test_var_declaration.txt: Tests variable declaration. -
declaration_tests/test_var_expression.txt: Tests using variables in expressions. -
declaration_tests/test_var_multiple.txt: Tests declaring multiple variables. -
declaration_tests/test_var_reassignment.txt: Tests reassigning a value to a variable. -
declaration_tests/test_var_usage.txt: Tests using a variable after it has been declared. -
if-else_tests/logical_op_if-else.txt: Tests using logical operators in if-else conditions. -
if-else_tests/nested_if-else.txt: Tests nested if-else statements. -
if-else_tests/simple_if-else.txt: Tests simple if-else statements. -
loop_tests/for_loop.txt: Tests for loops. -
loop_tests/while_loop.txt: Tests while loops.
Each of these test files contains source code in the logic language presented. The test program runs the logic compiler on each file and checks if the output matches the expected result. If the output does not match, the test fails and the program prints an error message.
The logic compiler can:
- Parse and interpret boolean expressions with
AND,OR, andNOToperators. - Handle control flow with
if-elsestatements,whileloops, andforloops. - Declare variables with the
varkeyword and assign values to them. - Reassign values to variables.
The project uses a Makefile for building the executable. Here's how you can build and run the project:
- To build the project, navigate to the project directory in your terminal and run the following command:
makeThis command will compile the source files and generate an executable named logic_compiler.
- To run the logic compiler on a file, use the following command:
./logic_compiler <file>- To clean the project (remove the compiled files), use the following command:
make cleanThis command will remove the object files and the logic_compiler executable.
- To run the tests, first compile the test program with the following command:
gcc test.c -o testThen, run the test program with the following command:
./testThis will print the output of each test file and whether the test passed or failed.
To use the logic compiler, write a logical expression in a text file using the tokens and grammar defined above. Then, run the logic compiler on the file using the command shown in the "Building and Running" section. The logic compiler will print the result of the expression.
Here's an example:
- Write your logical expression in a text file. For example, you might write the following in a file named
example.txt:
var x = TRUE;
var y = FALSE;
var z = x OR y;
- Run the logic compiler on the file:
./logic_compiler example.txtOutput: The logic compiler will print the result of the expression:
Result: 1
- Write your source code in a text file. For example:
var x = TRUE;
var y = FALSE;
var z = FALSE;
if (x) then {
if (y) then {
z = TRUE;
} else {
z = FALSE;
};
} else {
z = TRUE;
};
- Run the logic compiler with the source code file as an argument:
./logic_compiler your_source_code.txt
- The logic compiler will interpret the source code and print the result of the last expression evaluated:
Here's a step-by-step explanation:
-
Three variables
x,y, andzare declared and initialized.xisTRUE,yandzareFALSE. -
The first
ifstatement checks ifxisTRUE. SincexisTRUE, the code inside thethenblock is executed. -
Inside the
thenblock, there's anotherifstatement that checks ifyisTRUE. SinceyisFALSE, the code inside theelseblock is executed. -
In the
elseblock,zis assigned the valueFALSE. -
The program ends after executing the
if-elsestatement. In this language, the result of the program is the value of the last expression evaluated. In this case, the last expression isz = FALSE;inside theelseblock. SinceFALSEis equivalent to0, the result of the program is0.
So, the final values of the variables x, y, and z are TRUE, FALSE, and FALSE respectively, and the result of the program is:
Result: 0
- Write your source code in a text file. For example:
var y = FALSE;
for (var x = TRUE; x; x = FALSE) {
y = TRUE;
};
- Run the logic compiler with the source code file as an argument:
./logic_compiler your_source_code.txt
- The logic compiler will interpret the source code and print the result of the last expression evaluated:
Here's a step-by-step explanation:
-
A variable
yis declared and initialized toFALSE. -
A
forloop is started with a new variablexdeclared and initialized toTRUE. The loop condition isx, and the loop update isx = FALSE. -
Inside the
forloop,yis assigned the valueTRUE. -
On the next iteration,
xis set toFALSEas per the loop update. Since the loop conditionxis nowFALSE, the loop ends. -
The program ends after executing the
forloop. In this language, the result of the program is the value of the last expression evaluated. In this case, the last expression isx = FALSE;in the loop update. SinceFALSEis equivalent to0, the result of the program is0.
So, the final values of the variables x and y are FALSE and TRUE respectively, and the result of the program is:
Result: 0
- Write your source code in a text file. For example:
var counter = TRUE;
var sum = FALSE;
while (counter) {
sum = TRUE;
counter = FALSE;
};
- Run the logic compiler with the source code file as an argument:
./logic_compiler your_source_code.txt
- The logic compiler will interpret the source code and print the result of the last expression evaluated:
Here's a step-by-step explanation:
-
Two variables
counterandsumare declared and initialized.counterisTRUEandsumisFALSE. -
A
whileloop is started with the conditioncounter. -
Inside the
whileloop,sumis assigned the valueTRUEandcounteris set toFALSE. -
On the next iteration, since the loop condition
counteris nowFALSE, the loop ends. -
The program ends after executing the
whileloop. In this language, the result of the program is the value of the last expression evaluated. In this case, the last expression iscounter = FALSE;inside thewhileloop. SinceFALSEis equivalent to0, the result of the program is0.
So, the final values of the variables counter and sum are FALSE and TRUE respectively, and the result of the program is:
Result: 0