This code implements a simple compiler-like parser for a programming language with support for variables, assignments, expressions, loops, conditionals, and printing. It utilizes the Bison parser generator and supports basic operations like addition, subtraction, multiplication, and division. It also allows for the declaration of variables of types int, float, char, and string.
A[26]: Integer array for storing integer values.B[26]: Float array for storing float values.C[26]: Char array for storing character values.D[26][26]: 2D array for storing string values.var[26]: Array to track the type of each variable.l[26]: Array for location storage for variables.loc[26]: Array for storing variable locations.d[3],op[3]: Arrays for storing operations and conditions.
ID: Identifier (variable name).CH: Single character.CHH: String of characters.NUM_INT: Integer numbers.NUM_FLOAT: Float numbers.INT: Integer type keyword.FLOAT: Float type keyword.CHAR: Char type keyword.STRING: String type keyword.PRINT: Print keyword.FOR: For loop keyword.IF: If conditional keyword.THEN: Then keyword in conditionals.ELSE: Else keyword in conditionals.
s: Program statement (starting point of the grammar).stm: Single statement.exp,term,factor: Expressions, terms, and factors for arithmetic operations.assm: Assignment statement.incd: Increment/Decrement operations.rel: Relational expression.log: Logical expressions.italz: Variable initialization.count: Counter for loops.cond: Condition expression for loops.for: For loop statement.if: If-Else conditional statement.decl: Declaration of variables.
- A program consists of a sequence of statements. Statements can be expressions, assignments, declarations, prints, if-statements, or loops.
- A
forloop follows the formatfor(italz; cond; count){s}. It supports both conditions based on location and variable arrays. The loop iterates based on specified conditions and modifies the variables accordingly.
- Conditions are checked using relational operators (
<,>,<=,>=,==,!=) to control the execution of a loop.
- Increment and decrement operations for variables are supported using the
++and--operators. These operations modify variables' values and affect loop execution.
- Conditional checks are implemented with
if (log) then {...}andif (log) then {...} else {...}. Logical expressions are evaluated using&,|, and!operators.
- Variables can be initialized using
int,float,char, andstringtypes. For example,int a = 5;.
- Variables are declared with types (
int,float,char,string) and can optionally be initialized. Errors are raised if variables are redeclared with a different type.
- Variables, strings, characters, or integers can be printed using the
printkeyword. Examples includePRINT(ID)for printing the value of a variable.
- Arithmetic operations such as addition (
+), subtraction (-), multiplication (*), and division (/) are supported within expressions.
- Variables can be assigned new values using the assignment operator (
=). For example,a = 5;orb = 3.5;.
The following errors are handled:
- Syntax errors during parsing.
- Uninitialized variable usage.
- Redeclaring variables with a different type.
- Dividing by zero in arithmetic operations.
❌Error : variable not declared before