Giter Club home page Giter Club logo

yeaseen / c-compiler Goto Github PK

View Code? Open in Web Editor NEW
20.0 2.0 2.0 6.34 MB

:hamburger: A subset of C Compiler[Lexical Analyzer, Syntax Analyzer, Semantic Analyzer & Intermediate Code Generator] , DAG & TAC implemented in C++ using Flex and Yacc-Bison as an assignment of sessional course CSE310 in undergradute studies in CSE, BUET

C++ 5.53% C 74.00% Lex 3.39% Yacc 16.88% Shell 0.15% Assembly 0.06%
compiler-design yacc bison c-compiler symbol-table lexical-analyzer semantic-analyzer parser-generator dag tac

c-compiler's Introduction

Compiler Construction Project

This project is an endeavor into the construction of a basic compiler for C programs. It encompasses the creation of a symbol table, lexical analysis, syntax and semantic analysis, and code optimization to generate assembly code. The project is structured into several key components, each responsible for different stages of the compilation process.

Project Structure

  • 1_Symbol Table: A foundational data structure that stores information about various entities such as identifiers, objects, and function names encountered throughout the source code. It is implemented as a list of hash tables to manage symbols in different scopes efficiently.

  • 2_Lexical Analyzer (Lexer): Implemented using Flex, this component tokenizes a C source file, outputting the tokens to token.txt and logs to log.txt. A shell script, script.sh, is provided to run the lexer.

  • 3_Syntax and Semantic Analyzer (Parser): Developed with Bison, this parser uses a yacc file to parse C grammar. It is capable of identifying syntax and semantic errors, logging them to error.txt, and printing tokens and identifiers to log.txt. A shell script script.sh script facilitates running this component.

  • 4_Code Optimization and Assembly Code Generation: Modifications to the parser.y file enable the parser to optimize the source code and generate equivalent assembly code. It updates the symbol table with variables and types and reports any errors in error.txt. Successful parsing results in assembly code output for each line of the input C file.

  • Directed Acyclic Graph (DAG) and Three Address Code (TAC) for Simple Expressions: This component of the project focuses on optimizing expressions and intermediate code generation. It constructs Directed Acyclic Graphs (DAG) to eliminate common subexpressions and efficiently represent the relationships between variables and operations. Furthermore, it generates Three Address Code (TAC) as an intermediate representation that simplifies code optimization and translation to machine code.

Getting Started

Prerequisites

  • Flex
  • Bison
  • GCC

Running each part of the Compiler

  1. Testing up the Symbol Table: Go to 1_SymbolTableCopy the input.txt file and pass this as std input to an executable file of SymbolTable.cpp To tokenize a C file and generate logs, run:
    gcc -o SymbolTable SymbolTable.cpp
    ./SymbolTable <content_from_input.txt>
  2. Lexer: Go to 2_lexer To tokenize a C file and generate logs, run:
    ./script.sh
  3. Parser: Go to 3_parser/FUll To parse a C file and generate logs, run:
    ./script.sh
  4. Code Generator: Go to 4_assembly_generator To parse a C file and generate assembly code, run:
    ./script.sh
  5. DAG_&_TAC: Go to DAG & TAC To parse an input expression file and generate equivalent DAG & TAC, run:
    ./script.sh

c-compiler's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.