Skip to content

maririn312/mini_c_compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Project Title: Mini C Compiler

Project Description:

The Mini C Compiler is a project that aims to create a simple C compiler that can generate executable code from C source files. The compiler will support a subset of the C language syntax and will generate code for a specific target platform (e.g. x86, ARM). The project will involve creating a lexer, parser, code generator, and linker for the target platform. Project Features:

  1. Lexer: The lexer will be responsible for tokenizing the input C source code into a stream of tokens that the parser can use to build an abstract syntax tree (AST).
  2. Parser: The parser will take the stream of tokens generated by the lexer and build an abstract syntax tree (AST) representing the C source code. Code Generator: The code generator will traverse the AST and generate assembly code for the target platform based on the C source code.
  3. Linker: The linker will take the generated assembly code and link it with any required libraries to produce an executable binary. Basic Language Support: The compiler will support a subset of the C language syntax, including basic data types, control structures, and function declarations. Command Line Interface: The compiler will provide a command-line interface for compiling C source files, specifying the target platform, and generating executable binaries.

Technical Implementation:

  1. Lexer: The lexer will be implemented using Flex, a tool for generating lexical analyzers.
  2. Parser: The parser will be implemented using Bison, a tool for generating parsers.
  3. Code Generator: The code generator will be implemented in C and will use the target platform's assembly language syntax to generate executable code.
  4. Linker: The linker will be implemented using a platform-specific tool, such as GCC or Clang. Command Line Interface: The command line interface will be implemented using the getopt library to parse command line arguments.

Conclusion:

The Mini C Compiler is a challenging project that allows C developers to explore their skills in programming a compiler. By building this project, developers can gain experience in lexical analysis, parsing, code generation, and linker technology while creating a simple yet functional C compiler. Unfortunately, it's beyond the scope of a single response to provide a complete example of a Mini C Compiler project, as it requires implementing several components, including the lexer, parser, code generator, and linker, as well as integrating them into a functional C compiler. However, here are some general steps you can follow to implement a Mini C Compiler project: Design the language subset: Determine the subset of C language syntax that your compiler will support. This could include basic data types, control structures, and function declarations, but may not include advanced features like preprocessor macros or complex data structures. Implement the lexer: Use a tool like Flex to generate a lexer that tokenizes the input C source code into a stream of tokens. Implement the parser: Use a tool like Bison to generate a parser that builds an abstract syntax tree (AST) representing the input C source code. Implement the code generator: Traverse the AST and generate assembly code for the target platform based on the C source code. Implement the linker: Use a platform-specific tool, such as GCC or Clang, to link the generated assembly code with any required libraries to produce an executable binary. Build a command-line interface: Create a command-line interface that allows the user to compile C source files, specifying the target platform and generating executable binaries. Test and debug: Test the compiler with various input programs and debug any errors or unexpected behaviors. Keep in mind that this is a simplified outline of the steps required to implement a Mini C Compiler project, and each step can involve a significant amount of coding and testing.

Write a C Compiler!

Usage

test all

./test_compiler.sh /path/to/your/compiler

test specific stages

To test stage 1 and stage 3,

./test_compiler.sh /path/to/your/compiler 1 3

To test from stage 1 to stage 6,

./test_compiler.sh /path/to/your/compiler `seq 1 6`

In order to use this script, your compiler needs to follow this spec:

  1. It can be invoked from the command line, taking only a C source file as an argument, e.g.: ./YOUR_COMPILER /path/to/program.c

  2. When passed program.c, it generates executable program in the same directory.

  3. It doesn’t generate assembly or an executable if parsing fails (this is what the test script checks for invalid test programs).

The script doesn’t check whether your compiler outputs sensible error messages, but you can use the invalid test programs to test that manually.

About

# mini c compiler

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published