Lex Yacc
Lex Yacc
DESIGN
Submitted by
Sanju
Joseph(10msw0006)
Index page
1) What is a compiler?
6) Lex program 1
7) Lex program 2
8) Yacc Program
Lex reads an input stream specifying the lexical analyzer and outputs source
code implementing the lexer in the C programming language.
1)Definition section
2)Rules section
Syn:-
%% Rules 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.
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.
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.
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.
Code:-
%{
/* C code to be copied verbatim */
#include <stdio.h>
%}
%%
/*** Rules section ***/
%%
/*** C Code section ***/
int main(void)
{
/* Call the lexer, then quit. */
yylex();
return 0;
}
Output:-
%{
#include <stdio.h>
%}
%%
stop printf("Stop command received\n");
start printf("Start command received\n");
%%
Output:-
Yacc Program :-
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
%{
#include <stdio.h>
#include <string.h>
int yywrap()
{
return 1;
}
main()
{
yyparse();
}
%}
%%
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
./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