Pirnciples of Compilers Design - Windter 2025
||| 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 ...
Scanner.l
: Lexical analysis phase (to extract tokens and deliver them to Syntax analysis phase) ...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;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?) ...
-
To determine Tokens:
%token <str> ID %token <num> NUMBER %token PLUS MINUS MULT DIV LPAREN RPAREN ASSIGN SEMICOLON
-
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
-
To determine start symbol and types of grammar Non-terminals:
%start stmt %type <str> stmt %type <val> expr
Lex & Flex.pdf
: What is Lex and Flex? ...Yacc & Bison.pdf
: What is Yacc and Bison? ...Project Definition.pdf
: The problem statement and project definition.Project Report & Description.pdf
: A complete description and report of the project and used codes.What is $$.png
: What is $$? synthesized attribute of each parent node (left-hand side non-terminal) in each production.
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