# 🛠 Compiler

compilertranslatorinterpretorgrammerparserlexer

How to write your own programming language?

  • Examples
    • GNU Compiler Collection - GCC
    • LLVM
    • Clang
    • javascript Compilers - V8 Engine
    • CPython - python compiler/interpreter
  • Guido van Rossum - Benevolent dictator for life BDFL for Python until 2018
  • Programming is mainly writing correct code for compilers. If you can program in a language means you know how to write instructions for a specific compiler.
  • Programming languages differ widely because their compilers are written so.

# 🌴 Types of Compilers

Cross-Compiler

compiles code for a target platform(CPU architecture)

  1. compiler for different CPU Architecture
  2. on x86 compiling code for ARM Architecture
Assembler

Translate code to assembly languages

Transpiler - S2S compiler

compiles code from one version to another. Eg: Babel in JS world

Eg: From modern ES2015 to ES5 to support older browsers

Decompiler

compiles from a Low Level Language to High Level Language

Bytecode Compiler

JAV, Python VM

JIT - Just in time Compilers

Bootstrapped Compilers
Self Compiled - compiled by itself

# 🖌 Compiler Design

  • Translates code from one form to another

# 🎨 Compiler Frontend

Generally a 5 Stages Process Line. Just how a Buddha rice bown is assembles in a restaurant.

# 🏎 Canonicalization

  1. converting to a format more suitable to work with
  2. remove extra spaces etc.

Very similar to Database System Canonicalization of data to 1NF, 2NF or 3NF

# Preprocessing

  1. Macro Substitution
  2. Conditional Compilation

# 🍇 Lexical Analysis / Tokenization

very important Stage and most complex when implementing any programming language

  1. source code is converted to token, so that a hierarical tree could be generated
  2. Can happen in 2 Stages
    1. Scanning
      1. convert code blocks to leximes
        1. keywords
        2. literals
        3. identifiers
        4. references
        5. comments
        6. seperators
        7. operators
    2. Evaluating
      1. Attackes leximes with values so that they could be used in Parsing stage
      2. Token Name: Token Value Format

Scanning

Scanning is performed using

  • RegEx
  • CFG Context-free Grammer
  • RegEx Languages and
  • FSM Finite State Automata Schemes
Grammer

Language of languages, Chomsky Hierarchy

CFG - Context Free Grammer

Every production rules of the form

where,

is non-terminal and is terminal character

4-tuple

where,

  1. is called a nonterminal character or a variable
  2. is a finite set of terminals,
  3. is a finite relation from to
  4. start variable
CSG - Context Sensitive Grammar

more general than CFG

A formal grammar 📐

Notation Meaning
is a set of nonterminal symbols
is a set of terminal symbols
is a set of production rules, and
is the start symbol

is context-sensitive if all rules in are of the form

CSG formal definition

where,

  1. 👉
  2. 👉 and
  3. 👉

# 🇩🇪 Grammar

Formal Grammer

Finite set of production Rules

Examples, write grammer to match all a, aa, aaaa, aaaaaaa

# 🌲 Parsing / Syntax Analysis

crucial stage in Process

Outputs Parse Tree

# 💻 Symentic Analysis

Outputs Symbol Table

  1. Builds Type checking
  2. Object Binding

# 🖥 Compiler Middle End

  1. Optimizations
  2. Dead code removal
  3. Constant propagation
  4. Rechability Analysis

# 🐲 Compiler Backend

Could be reuses as in cross Compilers

  1. CPU Architecture level Transformation
  2. CPU Scheduling Flags
  3. CPU architecture based Optimization to utilize CPU features like
    1. Hypreadthreaded feature
    2. Multicore
    3. GPUs etc
  4. CPU Heuristics and Algorithms

# 🔎 Overview

# How compilers are used?

Compiler interface usually have a CLI interface. Works just like another CLI tool so as to speak. Take examples of python, node, rust, java, bash, zsh compilers.

# 🐮 Parser Generators

Parser check the grammer and syntax of a natural/computer language. Syntatic Analysis

  • Can generate parser from calculator to complex language
  • filename.l
  • 4 sections
  • commenting is not allowed

Bison Input Grammer File structure

%{
Prologue
%}

Bison declarations

%%
Grammar rules
%%
Epilogue

# 🌀 Resources

I am looking for work opportunities. If you like my work and feels it was helpful kindly support me by


Buy Me A Coffee