This project showcases a project designed to create a lexical analyzer and interpreter for a custom language called Big Add Language using C. Below is an overview of the repository, its files, and their purposes.
Overview
The Big Add Language includes classic declarations, basic arithmetic operations, assignments, loops, and conditionals. This project involves a lexical analyzer that reads code from an input file, parses it, and sends tokens to an interpreter. The interpreter then processes these tokens, performs calculations, and handles errors. The challenge at hand is one of code analysis and execution. We must examine an input file whose name is provided by the user. We have a custom language called “Big Add Language” that features classic declarations and basic addition, subtraction, assignment, loop, and code block conditions in this special situation. Lexical Analyzer takes a code file analysis file and feeds it to the interpreter with declarations, errors, comments, data types, and assignments. When the analyzer detects an error, it will send it to the interpreter, who will display it to the programmer before terminating the code.
Every line in the input file will be analyzed by the interpreter, and the file will be executed using the tokens provided. To begin, the interpreter will look for the first token. If the analyzer finds a token that is legitimate, the interpreter will add it to the stack and utilise it for the calculations that are expected. If the interpreter cannot detect a token on a line, it will ignore whitespace and comments when searching for another line. As a result, if the analyzer finds a comment, it will take it out of the code because comments do not run in the compiler. If the analyzer comes across a loop or a calculation, it will identify it as a loop or a calculation. This will be done by the analyzer for each line before passing them on to the interpreter. If it detects an error, it will terminate the code and display the results to the programmer.
Repository Structure
- Source Code:
analyzer.c
: The core of the lexical analyzer, responsible for reading the code, identifying tokens, and removing comments and whitespace.interpreter.c
: Processes the tokens from the analyzer, executes instructions, and handles errors.main.c
: Entry point of the application, integrating the analyzer and interpreter to execute the input code.
- Test Files: Contains several test files to demonstrate the capabilities of the analyzer and interpreter.
Key Concepts
Lexical Analysis:
- Reads the source code and converts it into tokens.
- Identifies keywords, operators, identifiers, and literals.
- Removes comments and unnecessary whitespace.
Interpreter:
- Processes tokens generated by the lexical analyzer.
- Executes arithmetic operations and control structures.
- Manages variables and memory.
Error Handling:
- Detects syntax and runtime errors.
- Provides meaningful error messages to help debug the code.
How to Use
Clone the repository:
git clone https://github.com/tugcantopaloglu/big-add-analyzer-interpreter.git
cd big-add-analyzer-interpreter
Compile the code:
gcc -o interpreter main.c analyzer.c interpreter.c
Run the interpreter with an input file:
./interpreter input_file.txt
Example Test Cases
The repository includes test files that showcase various features of the Big Add Language, such as:
- Basic arithmetic operations.
- Variable declarations and assignments.
- Loops and conditionals.
Each test case is documented with expected output, helping users understand the behavior and validate their setup.
Conclusion
This repository provides an educational tool for understanding how lexical analyzers and interpreters work. By studying and experimenting with the provided source code, users can gain insights into language parsing and interpretation, making it a valuable resource for students and enthusiasts in the field of compiler construction and programming language design.