Course: IT794 Compiler Construction Lab Manuaul Tools
Course: IT794 Compiler Construction Lab Manuaul Tools
Lab Manuaul
Tools :
1. Lex / flex
2. Biason – Yacc
3. gcc
4. ANTLR (lex + yacc)
Windows
Option 1
o Flex
o Biason
o Dev-c++ => gcc
o http://www.surajgaikwad.com/2013/10/compile-lex-and-yacc-progs-on-windows.html
o Add path of flex, yacc , and gcc to environment variable PATH
Option 2:
o Winbison => flex + bison
o https://sourceforge.net/projects/winflexbison/
o Dev-c++ from link given in option 1
o Add path of win_bison.exe, and dev-c++/bin for gcc to environment variable PATH
Linux/Ubuntu
Source Language // mini version of existing programming language OR your own programming
language
e.g. mini C (program file extension .c)
Target Language e.g. assembly language (file extension .asm) compatible with TASM
Tokens Data Type (minimum 2) e.g. int, char
Keywords
Operators Binary Artihmetic operators, Assignment
operator, + <any other operator of your choice>
Constants
Control construct
Loop construct
Comments // specify your desired comment pattern for your
programming language
Special symbols
Lex Tool
Input : Lex file (.l extension) specify patterns and actions for all possible tokens of source programing
language
Structure of .l file
%{
%}
%%
section 2: translation rules
%%
section 3 : user-defined auxiliary procedures
Where, The translation rules specify is in following form , each pattern is specified using regular
ecpression
p1 { action1 }
p2 { action2 }
…
pn { actionn }
%{
#include <stdio.h>
%}
%%
int yywrap(void) ///override function for windows, if reference error for this fuction
{
return 0;
}
int yyerror(char *errormsg) ///override function for windows, if reference error for this fuction
{
fprintf(stderr, "%s\n", errormsg);
exit(1);
}
main()
{
yylex();
return 0;
}
Example 2: Use of defining regular definitions (like digit, letter )and use in pattern.
%{
#include <stdio.h>
%}
digit [0-9]
letter [A-Za-z]
id {letter}({letter}|{digit})*
%%
{digit}+ { printf(“number: %s\n”, yytext); }
{id} { printf(“ident: %s\n”, yytext); }
. { printf(“other: %s\n”, yytext); }
%%
main()
{ yylex();
}