Wednesday, 22 August 2018

An Introduction to Lex and Yacc Tools

Instructions and Commands for Lex


Note: Lines with prefix $ are commands, and with # are instructions and comments

# gcc is generally already installed on Ubuntu
# GCC: GNU Compiler Collection
# gcc: GNU C Compiler, g++: GNU C++ Compiler
# For More info: https://gcc.gnu.org/onlinedocs/gcc/

# File with extension ".l" is a lex file and with ".c" is a c file

# create a dedicated directory (optional), e.g. directory with name "compiler"
$ sudo mkdir compiler
$ cd compiler

# create a lex file with name "pattern.l"
$ sudo gedit pattern.l

# create lex.yy.c file with following command
$ flex pattern-count.l

# install flex if it is not installed
$ sudo apt-get install flex
$ flex pattern-count.l
$ ls

# Compile the lex.yy.c file with gcc compiler
$ gcc lex.yy.c
$ ls

# run the executable file
$ ./a.out
# At run time, enter sequence of intput characters, and terminate input by ctrl+d

# Comple and link to library (if lex file does not have yywrap and/or main functions)
$ gcc lex.yy.c -ll

# Place output in a file instead of default executable file a.out
$ gcc lex.yy.c -o pattern-count
$ ./pattern-count

# g++ compiler also can be used for compilation of c and cpp file
$ g++ lex.yy.c -ll
$ g++ lex.yy.c -ll -o pattern-count

# Write scanner/lexical analyzer on stdout instead of file lex.yy.c
$ flex -t pattern1.l > pattern1.c
$ gcc pattern1.c -o pattern1-la

# Supply the input as a file at run time
$ flex -t pattern-count2.l > pattern2.c
$ gcc pattern2.c -o pattern2-la
$ ./pattern2-la input.txt

# For lex file without yywrap() and main() functions
$ flex -t pattern-count0.l > pattern0.c
$ gcc pattern0.c -ll -o pattern0-la
$ ./pattern0-la

## Followings are some examples of Lex files to play with:

test.l, test2.l, test3.l, pattern-count.l, pattern-count0.l, pattern-count1.l, pattern-count2.l

lwc.l, lwc2.l, lwc3.l, appendLineNo.l, appendLineNo1.l, appendLineNo2.l

identifier.l, identifier2.l, identifier3.l, identifier4.l

Instructions and Commands for Yacc with Lex


Note: Lines with prefix $ are commands, and with # are instructions and comments

# File with extension ".y" is a yacc file and with ".l" is a lex file

$ yacc

# if Yacc is not installed then install bison
$ sudo apt-get install bison

# create yacc file that contains grammar description e.g. cal1.y
$ sudo gedit cal1.y

# generate parser y.tab.c, from grammar description cal1.y
$ yacc cal1.y

# compile y.tab.c file that outputs default executable file "a.out"
$ gcc y.tab.c

# if there is any error like undefined references then compile along with the ly library that contains the LR parsing program
$ gcc y.tab.c -ly

# execute a.out file and pass some expression, it may give syntax error if expression is not recognized by specified grammar in yacc file
$ ./a.out

### Using scanner and parser i.e. flex and yacc together ###

# generate header file y.tab.h that contains definitions of tokens, along with parser y.tab.c, using –d option
$ yacc -d cal3.y

# generate scanner lex.yy.c (Note: lex file e.g. cal3.l must include y.tab.h)
$ flex cal3.l

# compile the scanner and parser and link with lex and yacc library i.e. ll and ly, it outputs "a.out"
$ gcc lex.yy.c y.tab.c -ll -ly

# default executable file "a.out" can be renamed with "calculator3.exe" as
$ gcc lex.yy.c y.tab.c -ll -ly -o calculator3.exe

# execute this file, and pass some expression as input
$ ./calculator3.exe

## Followings are some examples of Yacc and Lex files to play with:

cal1.y, cal2.y, cal3.y, cal3.l, cal4.y, cal4.l, cal5.y, cal5.l


References:

This tutorial is compiled form following sources.
Lex & Yacc Tutorials by: Tom NiemannVictor EijkhoutA. KarkareDragon Book(Pages- 140, 287)



No comments:

Post a Comment

Thanks for your comments.