You are on page 1of 7

Assignment No : A4

Roll no - 4109

1 Title:
YACC Program.

2 Problem Definition
Parser for sample language using YACC.

3 Learning Objectives:
1. To understand the concept of a parser.

2. To use YACC as a parser generator.

4 S/W and H/W requirements:


1. Open source 64 bit OS.

2. Gedit text editor.


3. bison.

5 Theory
Syntax Analysis:
This is also known as Parsing. t takes the token produced by lexical analysis
as input and generates a parse tree (or syntax tree). In this phase, token ar-
rangements are checked against the source code grammar, i.e. the parser checks
if the expression made by the tokens is syntactically correct.

Syntax analyzers follow production rules defined by means of context-free gram-


mar. The way the production rules are implemented (derivation) divides parsing
into two types : top-down parsing and bottom-up parsing.

1
YACC:
Yacc provides a general tool for imposing structure on the input to a computer
program. The Yacc user prepares a specification of the input process; this in-
cludes rules describing the input structure, code to be invoked when these rules
are recognized, and a low-level routine to do the basic input. Yacc then gener-
ates a function to control the input process. This function, called a parser, calls
the user-supplied low-level input routine (the lexical analyzer) to pick up the
basic items (called tokens) from the input stream. These tokens are organized
according to the input structure rules, called grammar rules; when one of these
rules has been recognized, then user code supplied for this rule, an action, is
invoked; actions have the ability to return values and make use of the values of
other actions.

Yacc is written in a portable dialect of C[1] and the actions, and output sub-
routine, are in C as well. Moreover, many of the syntactic conventions of Yacc
follow C.

6 Related Mathematics
Let S be the solution perspective of the given problem.
The set S is defined as:
S = { s, e, X, Y, F, DD, N DD|s }
Where,
s= Start point
e= End point
F= Set of main functions
DD= set of deterministic data
NDD= set of non deterministic data

X= Input Set.

2
X = {arithematicexpressiontokens, grammearrules}.

Y = {valid, invalid}

s = available expression.
e = expression verified.

F = {fread , flex , fmatch , fret }

fread :function to read the expression.

flex :function to generate tokens for given arithematic expression.

fmatch :function to match the tokens with grammar rules.

fret :function to return result for expression.

DD = {tokens, grammarrules}
NDD =

7 State Diagram

q0 = read input code state


q1 = token generation state
q2 = grammar rule matching state
q3 = valid expression state
q4 = invalid expression state
qf = final state

8 Program Code:
LEX:

3
%{
#include "y.tab.h"
#include <stdio.h>
extern int yylval;
%}

%%

[0-9]+ {yylval=atoi(yytext); return DIGIT; }


int|float|char|double {return DTYPE;}
"#include <"[.a-zA-Z ]*">\n" {return HEADER;}
"main()"[\n]* {return MAIN;}
"{"[\n]* {return STARTB;}
"}"[\n]* {return ENDB;}
";"[\n]* {return ENDS;}
"," {return SEP;}
[-+/*] {return OP;}
"=" {return EQ;}
"(" {return OPB;}
")" {return CLB;}
"if" {return IFC;}
"printf("[^\)]*")" {printf("Function call\n");return FUNC;}
"scanf("[^\)]*")" {printf("Function call\n");return FUNC;}
[a-zA-Z][_a-zA-Z0-9]* {return VAR;}
[ \t]+ ;

%%

int yywrap()
{return 1;}
YACC:

%{
#include <stdio.h>
extern FILE* yyin;
%}
%token DIGIT VAR HEADER MAIN STARTB ENDB
%token ENDS DTYPE SEP FUNC OP EQ
%token IFC OPB CLB

%%

St: S MAIN STARTB stmts ENDB {printf("Program with main\n");};


S: HEADER S | HEADER {printf("HEADERs\n");};
stmts: stmt| stmts stmt| ;

4
stmt: expr|DTYPE varlist ENDS | FUNC ENDS | IFC OPB CLB STARTB
stmts ENDB {printf("\nStatement\n");};
varlist: VAR| varlist SEP VAR ;
expr: VAR EQ dv opn ENDS{printf("Expression\n");};
opn: OP dv | OP dv opn|;
dv: DIGIT|VAR;

%%

void yyerror(char* s)
{
printf("Incorrect Syntax");
}

int main()
{
char ifile[100];
printf("\nEnter input file name: ");
scanf("%s",ifile);
yyin=fopen(ifile,"r");
yyparse();
}

INPUT FILE:

#include <stdio.h>
#include <iostream.h>
#include <math.h>

main()
{
int a,b,c;
printf("Hello");
int d,e;
printf("HI");
scanf("Enter");
a=b+20;
if()
{
c=d;
char x,y;
}
}

5
9 Output:
[be@dbsl15 ~]$ cd 4246/A4
[be@dbsl15 A4]$ yacc -d a4.y
yacc: 8 shift/reduce conflicts, 1 reduce/reduce conflict.
[be@dbsl15 A4]$ lex a4.l
[be@dbsl15 A4]$ gcc lex.yy.c y.tab.c
[be@dbsl15 A4]$ ./a.out

Enter input file name: input.c


HEADERs
Function call
Function call
Function call
Expression

Expression

Statement
Program with main
[be@dbsl15 A4]$

6
10 Conclusion
Thus we successfully implemented YACC program for sample language to check
the validity of arithmatic expression.

You might also like