Skip to content

Eqbal G Mansoori Compilers :); a simple version of a compiler including mathematical expressions with specified rules and instructions - Principles of Compilers Design final project - Winter 2025 /// by theMHD

Notifications You must be signed in to change notification settings

theMHD-120/EGC-Flex-Yacc-Compilers-Project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 

Repository files navigation

EGC-Flex-Yacc-Compilers-Project

Pirnciples of Compilers Design - Windter 2025

Project summary

||| In the name of ALLAH |||
-----------------------------
Eqbal G Mansoori Compilers; a simple version of a compiler including mathematical expressions with specified rules and instructions.
In this compiler, we have this +, -, *, /, (, ) and = operators and white spaces; oppsite of real expressions, in this compiler, PLUS and MINUS operators have higher priority than MULT and DIV operators; also PLUS and MINUS operators, have Right Associativity (left in reality :); consider these specifications and read documentation files in Guidance folder ...

Main files

  1. Scanner.l: Lexical analysis phase (to extract tokens and deliver them to Syntax analysis phase) ...
  2. Parser.y: Syntax analysis phase (to check syntax of input expression, generate intermediate representation (three-address code) and calculate the final result of input expression;
  3. common.h: A common header file (between .l and .y files) that contains a Factor struct to determine type of numbers during calculations (are they Numbers or Temporary variables?) ...

Parser.y

  1. To determine Tokens:

      %token <str> ID
      %token <num> NUMBER
      %token PLUS MINUS MULT DIV LPAREN RPAREN ASSIGN SEMICOLON
    
  2. To determine the mentioned speciafications of PLUS and MINUS, i used an ambiguous grammar and these two lines (in Parser.y file):

      %left MULT DIV
      %right PLUS MINUS
    
  3. To determine start symbol and types of grammar Non-terminals:

      %start stmt
      %type <str> stmt
      %type <val> expr
    

Guidance (Help) files

  1. Lex & Flex.pdf: What is Lex and Flex? ...
  2. Yacc & Bison.pdf: What is Yacc and Bison? ...
  3. Project Definition.pdf: The problem statement and project definition.
  4. Project Report & Description.pdf: A complete description and report of the project and used codes.
  5. What is $$.png: What is $$? synthesized attribute of each parent node (left-hand side non-terminal) in each production.

How to execute?

I have executed these files in Windows Subsystem for Linux (WSL); WSL is a feature of Windows that allows you to run a Linux environment on your Windows machine, without the need for a separate virtual machine or dual booting.

Using these lines (in order), we can execute .l and .y files in WSl (last line is an example for input expression) ...

  bison -d -o parser.c Parser.y
  flex -o lexer.c Scanner.l
  gcc -o compiler parser.c lexer.c -lm
  echo "b = 20*(24/6)+45 -60;" | ./compiler

About

Eqbal G Mansoori Compilers :); a simple version of a compiler including mathematical expressions with specified rules and instructions - Principles of Compilers Design final project - Winter 2025 /// by theMHD

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published