Mar Baselios College Of Engineering And Technology (2024-25)
This README provides instructions on how to compile and run C programs, Lex files, and integrate Lex with Yacc for the KTU S7 Compiler Design Lab.
- Compile the C program:
gcc filename.c -o program_name
- Run the compiled program:
./program_name
- Enter a nix-shell with gcc:
nix-shell -p gcc
- Compile and run as normal:
gcc filename.c -o program_name ./program_name
flex filename.l
gcc lex.yy.c -o lexer -lfl
./lexer
lex filename.l
gcc lex.yy.c -o lexer -ll
./lexer
flex filename.l
gcc lex.yy.c
./a.out
- Create a file named
build.sh
with the following content:#!/bin/bash flex "$1" gcc lex.yy.c -o lexer -lfl ./lexer
- Make the script executable:
chmod +x build.sh
- Run the script:
./build.sh filename.l
- Add flex and bison to your configuration file:
environment.systemPackages = with pkgs; [ flex bison ];
- Rebuild your system:
sudo nixos-rebuild switch
- Use nix-shell to run the files:
nix-shell -p flex flex lex.l gcc lex.yy.c -o lexer -lfl ./lexer
- Write your Lex (.l) and Yacc (.y) files.
- Generate C files:
flex lexer.l bison -d parser.y
- Compile the generated C files:
gcc -c lex.yy.c parser.tab.c
- Link the object files:
gcc lex.yy.o parser.tab.o -o parser -lfl
- Run the parser:
./parser
For a detailed guide on Lex and Yacc integration, refer to the Lex-Yacc Integration Guide.
- For "undefined reference to
yywrap
" error:- Add
%option noyywrap
at the top of your Lex file, or - Define
yywrap()
in your Lex file:int yywrap() { return 1; }
- Add
- For "cannot find -lfl" or "cannot find -ll" errors on Ubuntu or Debian:
sudo apt-get install libfl-dev
Ensure you have Flex (or Lex), Bison (or Yacc), and gcc installed on your system before compiling Lex files, Yacc files, or C programs.
For more information: