0% found this document useful (0 votes)
341 views9 pages

Lex Yacc

This document describes an assignment on compiler design using YACC and LEX tools. It includes definitions of compilers, LEX and YACC tools, the structure of LEX files, how LEX and YACC work together, examples of LEX and YACC programs to implement a thermostat controller, and system configuration details for installing LEX and YACC on Ubuntu.

Uploaded by

Sanju Joseph
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
341 views9 pages

Lex Yacc

This document describes an assignment on compiler design using YACC and LEX tools. It includes definitions of compilers, LEX and YACC tools, the structure of LEX files, how LEX and YACC work together, examples of LEX and YACC programs to implement a thermostat controller, and system configuration details for installing LEX and YACC on Ubuntu.

Uploaded by

Sanju Joseph
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

ASSIGNMENT ON COMPILER

DESIGN

YACC AND LEX TOOL

Submitted by
Sanju
Joseph(10msw0006)
Index page
1) What is a compiler?

2) What is a Lex Tool?

3) Structure of a lex file

4) What is YACC Tool?

5) Using the lex Program with the yacc


Tool

6) Lex program 1

7) Lex program 2

8) Yacc Program

9) System Configuration and installation


What is a compiler?
A compiler is a computer program (or set of programs) that
transforms source code written in a programming language (the
source language) into another computer language (the target
language, often having a binary form known as object code). The
most common reason for wanting to transform source code is to
create an executable program.

What is a Lex Tool?


Lex is a program that generates lexical analyzers ("scanners" or "lexers").
Lex is commonly used with the yacc parser generator. Lex, originally written
by Mike Lesk and Eric Schmidt,is the standard lexical analyzer generator on
many Unix systems, and a tool exhibiting its behavior is specified as part of
the POSIX standard.[citation needed]

Lex reads an input stream specifying the lexical analyzer and outputs source
code implementing the lexer in the C programming language.

Though traditionally proprietary software, versions of Lex based on the


original AT&T code are available as open source, as part of systems such as
OpenSolaris and Plan 9 from Bell Labs. Another popular open source version of
Lex is flex, the "fast lexical analyzer".

Structure of a lex file

The structure of a lex file is intentionally similar to that of a yacc file;


files are divided up into three sections, separated by lines that contain
only two percent signs, as follows:

1)Definition section

The definition section is the place to define macros and to import


header files written in C. It is also possible to write any C code here,
which will be copied verbatim into the generated source file.

2)Rules section

Syn:-
%% Rules section%%

The rules section is the most important section; it associates patterns


with C statements. Patterns are simply regular expressions. When the lexer
sees some text in the input matching a given pattern, it executes the
associated C code. This is the basis of how lex operates.
3)C code section

The C code section contains C statements and functions that are copied
verbatim to the generated source file. These statements presumably contain
code called by the rules in the rules section. In large programs it is more
convenient to place this code in a separate file and link it in at compile
time.

What is YACC Tool?

YACC can parse input streams consisting of tokens with certain values. This
clearly describes the relation YACC has with Lex, YACC has no idea what
'input streams' are, it needs preprocessed tokens. While you can write your
own Tokenizer, we will leave that entirely up to Lex.

A note on grammars and parsers. When YACC saw the light of day, the tool was
used to parse input files for compilers: programs. Programs written in a
programming language for computers are typically *not* ambiguous - they have
just one meaning. As such, YACC does not cope with ambiguity and will
complain about shift/reduce or reduce/reduce conflicts.

Using the lex Program with the yacc Tool

When used alone, the lex program generator produces a lexical analyzer that
recognizes simple, one-word input or receives statistical input. You can also
use the lex program with a parser generator, such as the yacc command. The
yacc command generates a program, called a parser, that analyzes the
construction of more than one-word input. This parser program operates well
with the lexical analyzers that the lex command generates. The parsers
recognize many types of grammar with no regard to context. These parsers need
a preprocessor to recognize input tokens such as the preprocessor that the
lex command produces.

The lex program recognizes only extended regular expressions and formats them
into character packages called tokens, as specified by the input file. When
using the lex program to make a lexical analyzer for a parser, the lexical
analyzer (created from the lex command) partitions the input stream. The
parser (from the yacc command) assigns structure to the resulting pieces. You
can also use other programs along with the programs generated by either the
lex or yacc commands.

A token is the smallest independent unit of meaning as defined by either the


parser or the lexical analyzer. A token can contain data, a language keyword,
an identifier, or other parts of a language syntax.
The yacc program looks for a lexical analyzer subroutine named yylex, which
is generated by the lex command. Normally, the default main program in the
lex library calls the yylex subroutine. However, if the yacc command is
installed and its main program is used, the yacc program calls the yylex
subroutine. In this case, where the appropriate token value is returned, each
lex program rule should end with the following:

return(token);

The yacc command assigns an integer value to each token defined in the yacc
grammar file through a #define preprocessor statement. The lexical analyzer
must have access to these macros to return the tokens to the parser. Use the
yacc -d option to create a y.tab.h file, and include the y.tab.h file in the
lex specification file by adding the following lines to the definition
section of the lex specification file:

%{
#include "y.tab.h"
%}

Alternately, you can include the lex.yy.c file in the yacc output file by
adding the following line after the second %% (percent sign, percent sign)
delimiter in the yacc grammar file:

#include "lex.yy.c"

The yacc library should be loaded before the lex library to obtain a main
program that invokes the yacc parser. You can generate lex and yacc programs
in either order.

Lex program 1:-


Aim:-It recognizes strings of numbers (integers) in the input, and simply
prints them out.

Code:-

*******************/*** Definition section ***/

%{
/* C code to be copied verbatim */
#include <stdio.h>
%}

/* This tells flex to read only one input file */


%option noyywrap

%%
/*** Rules section ***/

/* [0-9]+ matches a string of one or more digits */


[0-9]+ {
/* yytext is a string containing the matched text. */
printf("Saw an integer: %s\n", yytext);
}

.|\n { /* Ignore all other characters. */ }

%%
/*** C Code section ***/

int main(void)
{
/* Call the lexer, then quit. */
yylex();
return 0;
}

Output:-

Lex program 2:-

%{
#include <stdio.h>
%}

%%
stop printf("Stop command received\n");
start printf("Start command received\n");
%%
Output:-

Yacc Program :-

Aim:- To develop a thermostat controller


1)writing lex to return the tokens

vi thermo.lex(lex program)

%{
#include <stdio.h>
#include "y.tab.h"
%}
%%
[0-9]+ return NUMBER;
heat return TOKHEAT;
on|off return STATE;
target return TOKTARGET;
temperature return TOKTEMPERATURE;
\n /* ignore end of line */;
[ \t]+ /* ignore whitespace */;
%%
2) writing yacc file to write grammer

vi thermo.yacc (yacc program)

%{
#include <stdio.h>
#include <string.h>

void yyerror(const char *str)


{
fprintf(stderr,"error: %s\n",str);
}

int yywrap()
{
return 1;
}

main()
{
yyparse();
}

%}

%token NUMBER TOKHEAT STATE TOKTARGET TOKTEMPERATURE

%%
commands: /* empty */
| commands command
;

command:
heat_switch
|
target_set
;

heat_switch:
TOKHEAT STATE
{
printf("\tHeat turned on or off\n");
}
;

target_set:
TOKTARGET TOKTEMPERATURE NUMBER
{
printf("\tTemperature set\n");
}
;
%%

3) Running the lex file and yacc file by the follwing command

lex thermo.lex
yacc -d thermo.y

As a result lex.yy.c and y.tab.c are generated..our parser is ready we


need to compile it by

cc lex.yy.c y.tab.c -o therm

./therm

Output:-

System Configuration:-
My system config:-
Os :- Ubuntu 10.10
C compiler:- gcc
lexical tool:- lex from the flex.
RAM:-512 MB

Installation:-
- package name for lex is flex install it by the sudo command
- sudo apt-get install flex
- check with lex cmd which returns the path of the lex tool.
- Package name for yacc is bison.
- Sudo apt-get install bison

You might also like