This program is a simple interpreter for a subset of the C programming language, specifically designed to handle basic operations such as integer and string variable declarations, assignments, arithmetic operations, string concatenations, and input/output operations. The program processes the input line by line and maintains a symbol table to keep track of declared variables.
- Integer and String Declarations: Supports the declaration of integer and string variables.
- Assignment Operations: Handles assignments for both integer and string variables.
- Arithmetic Operations: Supports basic arithmetic operations (
+
,-
,*
,/
) on integer variables. - String Concatenation: Allows concatenation of string variables.
- Input and Output: Supports
scanf
for user input andprint
for output.
The interpreter recognizes the following commands:
- Syntax:
int <variable_name> = <expression>
orint <variable_name>
- Example:
int a = 5 int b = a + 3 int c
- Syntax:
string <variable_name> = "<value>"
orstring <variable_name>
- Example:
string s = "hello" string t = s + " world" string u
- Syntax:
<variable_name> = <expression>
- Example:
int x = a + b * 2
- Syntax:
<variable_name> = <string_expression>
- Example:
string greeting = "hello" + " world"
- Syntax:
print <variable_name>
- Example:
print a print greeting
- Syntax:
scanf <variable_name>
- Example:
scanf a scanf s
- Syntax:
exit
- Description: Terminates the interpreter.
The interpreter provides basic error handling, including:
- Undefined Variables: Reports an error if a variable is used before it is declared.
- Uninitialized Variables: Reports an error if a variable is used before it is initialized.
- Type Mismatch: Reports an error if operations are attempted between incompatible types (e.g., adding a string and an integer).
- Division by Zero: Reports an error if an attempt is made to divide by zero.
-
Compile the program using a C compiler:
gcc sample.c -o interpreter
-
Run the interpreter:
./interpreter
-
Start typing commands as per the syntax described above. The interpreter will continue to process commands until you type
exit
.
>>> int a = 10
>>> int b = 20
>>> int c = a + b
>>> print c
30
>>> string s = "Hello"
>>> string t = s + " World"
>>> print t
Hello World
>>> scanf c
Enter value for c: 50
>>> print c
50
>>> exit
- The interpreter currently supports only basic integer arithmetic and string concatenation.
- No support for floating-point numbers or complex data types.
- No support for control structures (e.g., loops, conditionals).