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

Comments

  1. Thanks :) that is really helpful

    ReplyDelete
  2. 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

    E: Package 'flex' has no installation candidate

    ReplyDelete
  3. Reading package lists... Done
    Building 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).

    ReplyDelete
  4. root@ubuntu:~/Desktop# lex intar.l
    root@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

    ReplyDelete
  5. It is not $ gcc lex.yy.c y.tab.h -ll
    it 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.

    ReplyDelete
    Replies
    1. Thanks for the perfect solution .. :)

      Delete
    2. What is the use of -d in that command?

      Delete
  6. thanx for the help........

    ReplyDelete
  7. Thanks a lot for sharing these valuable informations.Super blogging.Keep it up.
    web development oman

    ReplyDelete
  8. How do i install flex and bison on ubuntu 14.04? suggest something that will help for offline installtion once packages are dwonloaded.

    ReplyDelete
  9. /tmp/ccsOtGCV.o: In function `yylex':
    lex.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

    ReplyDelete
  10. ~/$ y.tab.h -ll
    y.tab.h: command not found
    can anyone help me out????

    ReplyDelete
  11. kamlesh@kamlesh-Inspiron-3537:~/Documents/Lex$ lex calc1.l
    kamlesh@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?

    ReplyDelete
  12. kamlesh@kamlesh-Inspiron-3537:~/Documents/Lex$ lex calc1.l
    kamlesh@kamlesh-Inspiron-3537:~/Documents/Lex$ yacc calc1.y
    calc1.y:22 parser name defined to default :"parse"
    conflicts: 4 shift/reduce

    ReplyDelete
  13. Ty so much I got it installed

    ReplyDelete
  14. ak@ubuntu:~$ sudo apt-get install flex
    [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

    ReplyDelete
  15. can we compile and run only yacc file

    ReplyDelete
  16. minalm-2808@ubuntu:~$ lex cal.l

    minalm-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:~$

    ReplyDelete
  17. cal.y:46 parser name defined to default :"parse"
    this erroe shows

    ReplyDelete
  18. If 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

Post a Comment

Popular posts from this blog

For June 2021 the best Phone is the iPhone XR

Why the Apple M1 chip is too hyped - perspectives of an average user.