This project implements a 3-address code translator for microC, a simplified subset of the C programming language. The translator takes microC source code as input and generates an intermediate representation in the form of 3-address quads, along with a symbol table. This intermediate representation serves as a machine-independent translation that can later be targeted to specific architectures like x86 or x86-64.
The project consists of several key files:
-
a9_220101019.l: The lexical analyzer specification (Flex file) that defines tokens for the microC language, including keywords, identifiers, constants, operators, and punctuators.
-
a9_220101019.y: The parser specification (Bison file) that defines the grammar rules for microC and includes semantic actions to generate 3-address code. This file also contains data structures for the symbol table and quad array.
-
a9_220101019.h: A header file containing shared type definitions and forward declarations used by both the lexer and parser.
-
Makefile: Automates the compilation process, generating the lexer, parser, and final executable.
-
220101019_test1.mc: A sample microC program used to test the translator.
The translator supports the following microC language features:
- Arithmetic, shift, relational, bit, logical, and assignment expressions
- Simple assignment operator (=)
- Simple variable, pointer, array, and function declarations
- Type specifiers: void, char, int, and float
- Variable initialization
- Compound statements (blocks)
- Expression statements
- Selection statements (if-else)
- Iteration statements (while, do-while, for)
- Jump statements (return)
- Function definitions
- GCC compiler
- Flex (Fast Lexical Analyzer Generator)
- Bison (Parser Generator)
- Make utility
To compile the project, run:
make clean
dos2unix a9_220101019.l a9_220101019.y( Ensure all files have Unix-style line endings (LF, not CRLF):)
make
To run the translator on a test file:
./a9_220101019 220101019_test1.mc
This will process the input file and generate the 3-address code in a file named 220101019_quads1.out
.
To view the generated 3-address code:
cat 220101019_quads1.out
The output file contains two main sections:
-
Symbol Table: Lists all variables, functions, and temporary variables with their types, sizes, and memory offsets.
Symbol Table: global ---------------------------------------- Name Type Base Type Size Offset Kind ---------------------------------------- global_float float void 8 0 global i int void 4 8 global a array int 40 12 global ...
-
Quad Array: Shows the 3-address code instructions generated from the input program.
Quad Array: ---------------------------------------- Index Op Arg1 Arg2 Result ---------------------------------------- L100 = 3.140000 global_float L101 = 10 i L102 = 5 v L103 + i 1 t1 ...
The lexical analyzer (a9_220101019.l) recognizes tokens in the microC language and passes them to the parser. It handles:
- Keywords (int, float, if, while, etc.)
- Identifiers
- Constants (integer, floating-point, character)
- String literals
- Operators and punctuators
- Comments (single-line and multi-line)
The parser (a9_220101019.y) defines the grammar rules for microC and includes semantic actions to generate 3-address code. It builds:
- A symbol table to track variables, functions, and their properties
- A quad array to store the generated 3-address code
During parsing, semantic actions:
- Create and manage symbol tables for different scopes
- Generate temporary variables for intermediate results
- Emit 3-address quads for expressions, statements, and control flow
- Handle type checking and conversions