Lab Manual
How to Compile and Run LEX / YACC programs?
How to Compile and Run LEX / YACC
programs?
Installing the Software and setting up the
environment variable (path)
Compiling and Running LEX Programs:
Compiling and Running YACC Programs:
Flex – Lexical Analyzer Generator
Regular Expression
LEX programs
Compile & Run LEX and YACC Programs on
Windows
Download the following Software:
1. Download Flex 2.5.4a:
http://gnuwin32.sourceforge.net/packages/fl
ex.htm
2. Download Bison 2.4.1:
http://gnuwin32.sourceforge.net/packages/bi
son.htm
3. Download DevC++:
https://www.bloodshed.net/
Installing the Software and setting up the
environment variable (path):
Install Flex at “C:\GnuWin32”
Install Bison at “C:\GnuWin32”
Install DevC++ at “C:\Dev-Cpp”
Open Environment Variables and paste:
Add “C:\GnuWin32\bin; C:\Dev-Cpp\TDM-GCC-64\bin” to
path.
Compile & Run LEX and YACC Programs on
Windows
Compiling and Running LEX Programs:
flex hello.l
gcc lex.yy.c
a.exe
Compiling and Running YACC Programs:
flex hello.l
bison -dy hello.y
gcc lex.yy.c y.tab.c
a.exe
How to Compile & Run LEX and YACC
programs on UBUNTU?
Run the following commands on terminal:
sudo apt-get update
Install software
1. sudo apt-get intall flex
2. sudo apt-get intall byacc
3. sudo apt-get intall bison
4. sudo apt-get intall bison++
5. sudo apt-get intall byacc-j
How to Compile & Run LEX and YACC
programs on UBUNTU?
Compiling and Running LEX Programs:
flex hello.l
gcc lex.yy.c
./a.out
Compiling and Running YACC Programs:
flex hello.l
bison -d hello.y
gcc lex.yy.c y.tab.c
./a.out
Flex – Lexical Analyzer Generator
A language for specifying lexical analyzers
lang.l Flex compiler lex.yy.c
C compiler
lex.yy.c a.out
-lfl
source code a.out tokens
A lex program has the following form:
%{
1. Declarations
%}
%%
2. Translation rules
%%
3. Auxiliary functions
Implementation of Lexical Analyzer using Lex
A lex program has the following form:
1. Declarations
– It includes declaration of variables used in the code segment and
regular definitions .
– Variable declarations are specified within %{ and %}.
2. Translation rules
– Translation rules each have the form: {Pattern} Action
– Each pattern is a regular expression, which may use the regular
definitions of the declaration section.
– Each section is divided with %%
3. Auxiliary functions
– It holds whatever additional functions are used in the actions.
Regular Expression
The characters that form regular expressions are:
– . : Matches any single character except the newline
character (“\n”).
– * : Matches zero or more copies of the preceding
expression.
– [] : A character class which matches any character
within the brackets.
– ^ : Matches the beginning of a line as the first character
of a regular expression. Also used for negation within
square brackets.
Regular Expression
The characters that form regular expressions are:
– $ : Matches the end of a line as the last character of a regular
expression.
– {} : Indicates how many times the previous pattern is allowed
to match when containing one or two numbers.
– For example: A{1,3} matches one to three occurrences of the
letter A.
– | : Matches either the preceding regular expression or the
following regular expression.
– For example: cow|pig|sheep - matches any of the three words.
Regular Expression
The characters that form regular expressions are:
– + : Matches one or more occurrence of the preceding regular
expression. For example: [0-9]+ matches “1”, “111”, or
“123456” but not an empty string. (If the plus sign were an
asterisk, it would also match the empty string.)
– ?: Matches zero or one occurrence of the preceding regular
expression. For example: -?[0-9]+ matches a signed number
including an optional leading minus.
Regular Expression – example
Micro scanner
Micro scanner
LEX Program -01
LEX Program -02
LEX Program -03
LEX Program -04
LEX Program -05
LEX Program -06
Lex Program to recognize and display keywords,
numbers, and words in a given statement
%{
#include<stdio.h>
%}
%%
if |
else |
printf {printf(“%s, is a keywod”, yytext);}
[0-9]+{printf(“%s, is a number”, yytext);}
[a-za-z]+ {printf(“%s, is a word”, yytext);}
. | \n {ECHO; }
%%
int main(){
printf(“enter the string: \n “);
yylex();
}
int yywrap(){
return 1;
}
Lex example with multiple parts of
speech.
%{
%}
%%
[\t ]+ /* ignore whitespace */ ;
is | am |are |were | was |be |being |been |do |does |did |
will |would |should |can |could |has |have |had
|go { printf("%s: is a verb\n", yytext); }
very | simply |gently |quietly |calmly
| angrily { printf("%s: is an adverb\n", yytext); }
from |behind |above |below
|between { printf("%s: is a preposition\n", yytext); }
Lex example with multiple parts of
speech…
if | then |and |but |or { printf("%s: is a conjunction\n",
yytext); }
their |my |your |his |her |its { printf("%s: is a adjective\n",
yytext); }
I |you |he |she |we |they { printf("%s: is a pronoun\n",
yytext); }
[a-zA-Z]+ {printf("%s: don't recognize, might be a noun\n",
yytext);}
.|\n { ECHO; }
%%
main()
{
yylex();
}