Vous êtes sur la page 1sur 5

YACC

There are many manual method of construction of LR parser. This involves lots of parsing the i/p string. Hence there is a need for automation of this process in order to achieve efficiency in parsing the input. Certain automatic tool for parser generator are available. YACC stands for Yet Another Compiler Compiler which is basically the utility available from UNIX. YACC is one such automatic tool for generating the parser program. YACC provide a general tool for imposing structure on input to a computer program. Basically YACC is LALR parser generator. The YACC can report conflicts or ambiguities in the form of error messages YACC user prepare a specification of input process Rules describing the i/p structure Code to be invoked when there rules are recognized Low level routine to do the basic i/p YACC (Yet Another Compiler Compiler) is a program designed to compile a LALR(1) grammar and to produce the source code of the syntactic analyzer of the language produced by this grammar. Input is a grammar (rules) and actions to take upon recognizing a rule. Output is a C program and optionally a header file of tokens.

YACC Translator
Steps: 1. First we write YACC specification lets name it as x.y and given to YACC compiler by UNIX command. 2. Then it will generate a parser program y.ab.c and another y.tab.h . The header file y.tab.h will store all tokens and so you need not have to create y.tab.h explicitly. 3. The generated y.tab.c program will then be compiled and generate executable a.out file. 4. Then test your YACC program with the help of some valid string and invalid string.

specification file

YACC compiler

y.tab.c and y.tab.h

y.tab.c

CC- the compiler

a.out

i/p string

Executable program a.out

o/p

YACC:Parser Generator Model

YACC Specification
Declaration Section (ordinary c declaration)

Translation Rule

Supporting c Function

%{ /* declaration section */ %} %% /* translation rule section */ %% /*required C function */

A. Declaration Part: In this section ordinary C declaration can be put. Not only this , we can declare grammar token in this section. The declaration of tokens should be within %{ and %}. B. Translation Part: It consists all the production rule of context free grammar with corresponding action. Rule1----------Action1 Rule2----------Action2 And so on C. C Function Section: This section consists of one main function in which the routine yyparser() will be called. And it also consists of required c functions.

Example
To illustrate how to prepare a YACC source program, let us construct a simple desk calculator that read an arithmetic expression, evaluate it.

%{ #include <ctype.h> %} %token DIGIT %% Line :expr \n ; Expr :expr + term |term ; Term :term * factor | factor ; Factor :( expr)

{printf(%d\n,$1);} {$$=$1+$3;}

{$$=$1+$3;}

{$$=$2;}

| DIGIT ; %% Yylex() { Int c; c=getchar(); If(isdigit(c)) { Yylval=c- 0; Return DIGIT; } Return c; }

YACC specification of a simple desk calculator

Lex
Lex is a scanner generator Input is description of patterns and actions Output is a C program which contains a function yylex() which, when called, matches patterns and performs actions per input

Typically, the generated scanner performs lexical analysis and produces tokens for the (YACC-generated) parser

LEX and YACC: a team

call yylex()

LEX yylex()

[0-9]+

YACC yyparse()

next token is NUM

Input Program 12+26

NUM + NUM

Availability lex, yacc on most UNIX systems. bison: a yacc replacement from GNU. flex: fast lexical analyzer. BSD yacc. Windows/MS-DOS versions exist.

Amitosh Kumar Agnihotri 0902910011 Sem -6th Sec-A

Vous aimerez peut-être aussi