C to Python Code Translator
This project implements a C to Python code translator using Lex and Yacc.
Lex (tokens.l) is used for lexical analysis to tokenize C language elements, while Yacc (parser.y) is
responsible for parsing and constructing the syntax tree based on C grammar rules. The resulting
constructs form an Abstract Syntax Tree (AST), which allows the translation of C code structures to
Python-compatible structures.
The project aims to support fundamental C constructs, including variable declarations, functions,
control structures, and expressions, among others. This setup allows the conversion of basic C
programs into Python syntax.
Code Files:
tokens.l (Lex file)
parser.y (Yacc file)
Supported Constructs:
Preprocessor Directives:
Detects #include lines for header files in C.
Whitespace:
Ignores spaces, tabs, and carriage returns.
Keywords:
Identifies control flow keywords like if, else, for, while, and return.
Data Types:
Supports int, double, char, and void data types.
Identifiers:
Handles variable names and function names.
Literals:
Supports integer, floating-point, character, and string literals.
Operators:
Includes assignment, comparison, logical, bitwise, and arithmetic operators.
Punctuation and Delimiters:
Recognizes parentheses, brackets, braces, dots, commas, and semicolons.
Newlines:
Counts newlines for error tracking.
Unknown Tokens:
Catches any unmatched characters as errors.
Program Structure:
Defines the top-level program structure.
Statements:
Supports various statements, including function declarations and expressions.
Block:
Handles blocks of code enclosed in { }.
Variable Declarations:
Supports declaring variables with optional initializations.
Function Declarations:
Handles function declarations with parameters and bodies.
Function Arguments:
Manages function declaration arguments.
Identifiers:
Processes variable and function names as identifiers.
Numeric Literals:
Handles integer, double, char, and string literals.
Data Types:
Supports C data types: int, double, char, void.
Pointers:
Recognizes pointer variables for operations like scanf.
Expressions:
Handles expressions like assignments, arithmetic, and function calls.
Logical Expressions:
Supports logical operations and comparisons.
Function Call Arguments:
Handles function call arguments.
Comparison Operators:
Groups comparison operators (==, !=, <, <=, >, >=).
Conditionals:
Supports if statements with optional else blocks.
Loops:
Handles for and while loops with their conditions and bodies.
Return Statements:
Supports return statements with or without values.
Array Declarations:
Supports declaring arrays with specified sizes.