Running Lex and Yacc Program in ubuntu
Easy Ways Guide to Run Lex and Yacc Programs on ubuntu.
This post will show you how to run Lex and Yacc programs easily on ubuntu 12.04
I have assumed that you have a working Ubuntu 12.04 LTS version installed on your system and the computer is connected to the network.
Ubuntu does not come installed with a lex and yacc compiler to do so install it first by
1. Opening the terminal
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 pasword.
Congratualtions Lex and Yacc have been installed on your system.
Running a Lex and Yacc program
for compiling a lex 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
3. type - lex file.l
4. then type - cc lex.yy.c -ll
5. then type - ./a.out
Your lex progam will be running now (provided it is correct).
for 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 file.l
4. type yacc file.y
5. type cc lex.yy.c y.tab.h -ll
6. type ./a.out
The lex and yacc will run succesfully now
Thanks :) that is really helpful
ReplyDeleteReading package lists... Done
ReplyDeleteBuilding dependency tree
Reading state information... Done
Package flex is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
E: Package 'flex' has no installation candidate
Reading package lists... Done
ReplyDeleteBuilding dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
bison : Depends: m4
Depends: libbison-dev (= 1:2.5.dfsg-2.1) but it is not going to be installed
Conflicts: bison:i386
bison:i386 : Depends: libc6:i386 (>= 2.6) but it is not going to be installed
Conflicts: bison
flex:i386 : Depends: libc6:i386 (>= 2.11) but it is not going to be installed
Recommends: gcc:i386 but it is not going to be installed or
c-compiler:i386
m4:i386 : Depends: libc6:i386 (>= 2.4) but it is not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
root@ubuntu:~/Desktop# lex intar.l
ReplyDeleteroot@ubuntu:~/Desktop# yacc intar.y
root@ubuntu:~/Desktop# cc lex.yy.c y.tab.h -ll
cc: error: y.tab.h: No such file or directory
please provide a solution for this
use
Deleteyacc -dy intar.y
thANKS
Deleteit should be gcc not cc dumbass
DeleteIt can also be written as cc.
DeleteThank you :)
ReplyDeleteIt is not $ gcc lex.yy.c y.tab.h -ll
ReplyDeleteit is $ gcc lex.yy.c y.tab c -ll
because y.tab.h is just a header file that is included within the lex file to hold the references to the indentifiers in yacc. y.tab.c is the c file created as a result of the yacc program. gcc is a c compiler and needs a c file to compile.
and do $ yacc -d prog.y
you have left out -d there.
Thanks for the perfect solution .. :)
DeleteWhat is the use of -d in that command?
Deletethanx for the help........
ReplyDeletethank u helped me...
ReplyDeleteThanks a lot for sharing these valuable informations.Super blogging.Keep it up.
ReplyDeleteweb development oman
How do i install flex and bison on ubuntu 14.04? suggest something that will help for offline installtion once packages are dwonloaded.
ReplyDelete/tmp/ccsOtGCV.o: In function `yylex':
ReplyDeletelex.yy.c:(.text+0x4f1): undefined reference to `yywrap'
/tmp/ccsOtGCV.o: In function `input':
lex.yy.c:(.text+0x102c): undefined reference to `yywrap'
collect2: error: ld returned 1 exit status
how to resolve his problem
~/$ y.tab.h -ll
ReplyDeletey.tab.h: command not found
can anyone help me out????
kamlesh@kamlesh-Inspiron-3537:~/Documents/Lex$ lex calc1.l
ReplyDeletekamlesh@kamlesh-Inspiron-3537:~/Documents/Lex$ cc lex.yy.c -ll
calc1.l:3:23: fatal error: y.tab.h: No such file or directory
#include "y.tab.h"
^
compilation terminated.
kamlesh@kamlesh-Inspiron-3537:~/Documents/Lex$
solution please?
kamlesh@kamlesh-Inspiron-3537:~/Documents/Lex$ lex calc1.l
ReplyDeletekamlesh@kamlesh-Inspiron-3537:~/Documents/Lex$ yacc calc1.y
calc1.y:22 parser name defined to default :"parse"
conflicts: 4 shift/reduce
🤓 good job ...
ReplyDeleteTy so much I got it installed
ReplyDeleteak@ubuntu:~$ sudo apt-get install flex
ReplyDelete[sudo] password for ak:
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package flex is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
can we compile and run only yacc file
ReplyDeleteminalm-2808@ubuntu:~$ lex cal.l
ReplyDeleteminalm-2808@ubuntu:~$
minalm-2808@ubuntu:~$ yacc -d cal.y
minalm-2808@ubuntu:~$ ./a.out
bash: ./a.out: No such file or directory
minalm-2808@ubuntu:~$
cal.y:46 parser name defined to default :"parse"
ReplyDeletethis erroe shows
lavada
ReplyDeleteit is good
ReplyDeleteIf internet wouldn't have been existed it would have been very hard for programmers like us to learn ew things , thnx man izzat bacha li aapne !
ReplyDelete