You are on page 1of 3

Running Lex and Yacc Program in ubuntu 22.

04

Ubuntu does not come installed with a lex and yacc compiler to do
so install it first by
1. opening the terminal by right click on everywhere and go to open
terminal. Then…
2. type sudo apt-get update….next
2. type  sudo apt-get install flex
3. enter your password

after installation of flex finishes


4. type sudo apt-get install bison
5. enter your password.
Successfully  Lex and Yacc have been installed on your system.

running a Lex and Yacc program                


1. write the lex program in a file and save it as file.l (where file is the name of the
file).
2. open the terminal and navigate to the directory where you have saved the file.l
(e.x CD Desktop)
3. type  lex/flex filename.l
4. then type  cc lex.yy.c -o app-name.out
5. then type  ./a.out

our lex progam will be running now (comment should be correct ).


or compiling lex and yacc together
1. write lex program in a file file.l and yacc in a file file.y
2. open the terminal and navigate to the directory where you have saved the
files.
3. type lex filename.l
4. type yacc filename.y and type yacc -d filename.y
5. type cc lex.yy.c y.tab.h -lfl or cc -o filename.out y.tab.c lex.yy.c
6. type ./a.out

The lex and yacc will run succesfully now


Example a program for declaring a variable

lex program

%{

#include<stdio.h>

#include "y.tab.h"

%}

%%

int|float|char return KEY;

"," return COMMA;

";" return COLON;

[a-zA-Z]+[a-zA-Z0-9]* return ID;

"\n" return NL;

%%

void yyerror(const char *str){

printf("Declaration syntax error");

int yywrap(){

return 0;

int main(){

yyparse();

return 0;

}
yacc program

%{

#include<stdio.h>

%}

%token ID KEY COLON NL COMMA

%%

start:KEY identifier COLON NL {printf("your variable declaration is valid!\


n");}

identifier: identifier COMMA ID|ID;

%%

##Do your own exercise to improve your compiler skill and for your
project work.#

You might also like