• Submitted By: vivekjz
  • Date Submitted: 03/17/2010 11:51 AM
  • Category: Technology
  • Words: 373
  • Page: 2
  • Views: 300

To study various commands used in LEX and YACC.

LEX is a tool used for generating programs that performs pattern matching on text.
Input : Input files defining rules
Output : A ā€˜Cā€™ source file, lex.yy.c which is defined in a routine yylex().

Input File Format:
- Definitions
Declaration of variables used in program.
%% rules
Contains rules to identify tokens.
%% user code
User program including main().

Values available to the user:
char **yytext - input text to be identified.
int yylong - length of the string in yytext.
FILE **yyin - pointer to input file.
void restart(FILE *file1) - restart scanning of file1.
FILE **yyout - pointer to output file.
YY_CURRENTBUFFER - specifies current buffer position.
YY_START - specifies start condition.
yylex() - routine for rule matching.
output() - put a character to output file.
input() - get a character from input file.
yywrap() - routine invoked when EOF flag is set.
yyerror() - routine invoked when error condition is encountered.
Flowchart specifying flow of input:

User program LEX compiler lex.yy.c Compiler


Steps to execute a LEX program:
1. Create a lex program as filename.l (i.e. with extension .l) ex: vi lex.l
2. To compile the file, type lex filename.l after compilation lex.yy.c file is produced.
3. Compile the ā€˜Cā€™ source file lex.yy.c (i.e. cc lex.yy.c).
4. Type ./a.out to get the output.

YACC is used to specify the grammar for a language.

Format of YACC program:
%% rules
%% user tools

% start - specifies starting symbol.
% token - list of tokens identified by lex program.
% type - specifies type of non-terminals.
% user - data types of yyval().

Steps to execute...