Skip to content

This is a Mini Compiler for the GoLang Language implemented using Lex and Yacc, along with C and C++. πŸ‘©β€πŸ’»

License

Notifications You must be signed in to change notification settings

sanskritip/mini-golang-compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

89 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

mini-golang-compiler

Build and execution

  • To ensure the code works well, you need make, g++, flex/lex and bison installed in your system.

  • Clone this repository into your local system. The project has a single global lex file, while has separate yacc files for each of the phases to ensure code cleanliness and ease of maintenance.

  • To build any of the phases, from a terminal, go into the directory and execute the MakeFile using the make command. It then builds the compiler into an executable in the name of gocompiler in that folder.

  • To run the compiler with an input file testinput.go, from the same directory as the previous step run ./gocompiler testinput.go

Note: To help execute and test the compiler quickly, there are a set of prebuilt scripts in the scripts/ folder which will help build and execute predefined test cases using the compiler. Run sh <script-name>.sh

Task List

Lexical Analysis and Token Generation

  • Conversion of strings to numbers.
  • Handling Whitespace
  • Handling Comments
  • Update yylval, yylloc, other global variables (line and col nums) and return token code for each action.
  • Record the line number and first and last column in yylloc for all tokens.
  • Report lexical errors for improper strings, lengthy identifiers, and invalid characters.
  • For each character that cannot be matched to any token pattern, report it and continue parsing with the next character.
  • If an identifier is longer than the maximum (31 characters), report the error, truncate the identifier to the first 31 characters (discarding the rest), and continue.

Symbol Table generation

  • Data Value for identifiers
  • Line numbers for declaration and reference

CFG and Parsing (Syntax validation)

  • Printing
  • Variable declaration
  • Const Declaration
  • Assign Variables - int , float , string , bool , null
  • Print String
  • Simple arithmetic expressions
  • Increment and decrement variables
  • Boolean operations on variables
  • If condition
  • If else condition
  • For loop
    • syntax validation
    • computation - Function
    • Function declaration
    • Function calls - Imports
    • Single import
    • Multiple imports - Identifiers
      • Compute Identifier values and insert in symbol table - [x] Print line number of syntax errors DONE - [x] Print col number of syntax errors - [ ] String computations - [x] Recognizing errors and suggesting fix - [x] Evaluate complex logical expressions

TAC

  • Semmatic error analysis
  • Print out semantic errors and continue parsing Currentlt stops parsing
  • Quadraple format DONE
  • Update Symbol table DONE

Optimisation

  • Type 1 Optimisation ( Remove dead code/ unreachanble code ) DONE
  • Type 2 Optimisation ( Copy Propagation ) DONE
  • Type 3 Optimisation ( Constant Folding ) DONE
  • Type 4 Optimisation ( Constant Propagation ) DONE

Look into

  • Reduce conflicts
  • Parser error recovery ? Even if its declaration related!

Deliverables

  • CFG
  • All the files related to all the tasks
  • Symbol table with required Information
  • ReadMe file for Syntax Error Handling Strategies
  • Abstract syntax tree printed in an inorder way.
  • ReadMe file for Semantic Error Handling
  • Three address code (in Quadruple format) DONE
  • Optimized Three address code. DONE
  • ReadMe file for the Overall Tasks

About

This is a Mini Compiler for the GoLang Language implemented using Lex and Yacc, along with C and C++. πŸ‘©β€πŸ’»

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published