department of computer science & engineering compiler...
TRANSCRIPT
Department Of Computer Science & Engineering
Compiler Design
B.Tech III Yr.II Sem Lab Manual
ISO 9001:2000
Affiliated to Jawaharlal Nehru Technological University,
AP Dhullapaly, Quthbullapur, Secunderabad-500014.
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 2
INDEX S. No Name of the Program Page No 1
Design a Lexical analyzer. The lexical analyzer should ignore redundant s tabs and new lines. It should also ignore comments. Although the syntax specifications that identifiers can be arbitrarily long, you may restrict the length to some reasonable Value.
8-11
2
Implement the lexical analyzer using JLex ,flex or lex other lexical analyzer generating tools.
12-15
3
Design Predictive Parser for the given language
16-18
4
Design LALR bottom up parser for the above language
19
5
Convert the BNF rules into YACC form and write code to generate abstract syntax tree
20-21
6
Write program to generate machine code from the abstract syntax tree generated by the Parser .The following instruction set may considered as target code.
22-26
7. Program for computation of first
27-29
8. Program to find whether a string is a keyword or not.
30-31
9. Program to find whether a string is constant or not.
32-33
10. Program to find whether a string is identifier or not.
34-37
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 3
LAB OBJECTIVE
The language Processors comprises assemblers, compilers and interpreters. It deals with the recognition the translation, and the execution of formal languages It is closely related to compiler construction.
Compiler is system software that converts high level language into low level language.
We human beings can’t program in machine language (low level lang.) understood by computers so we program in high level language and compiler is the software which bridges the gap between user and computer
Students will gain the knowledge of
Lexical Analysis for decomposing a character stream into lexical units Syntax analysis for recovering context-free structure from an input stream, error
correction Semantic analysis for enforcing non-context-free requirements, attribute grammars. Semantic definition, for describing the meaning of a phrase (we rely on interpretive
definitions) Implementation of programming concepts, control structures Data representation, implementation of data structures Partial evaluation, for removing interpretation overhead Code generation: instruction selection, register allocation Further semantic analysis: document validation , type checking.
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 4
PROGRAMS
To provide an understanding of the language translation peculiarities by designing a complete translator for a mini language.
Consider the following mini language, a simple procedural high-level language, only operating on integer data, with a syntax looking vaguely like a simple C crossed with Pascal. The syntax of the language is defined by the following BNF grammar
<program> ::= <block>
<block> ::= { <variabledefinition> <slist> }
| {<slist>}
<variabledefinition> ::= int <vardeflist> ;
<vardeflist> ::= <vardec> I <vardec> , <vardetlist> <vardec> ::= <identifier> I <identifier> [ <constant> ] <slist> ::= <statement> I <statement> ; <slist>
<statement> ::= <assignment> I <ifstatement> I <whilestatement> I <block> I <printstatement> I <empty>
<assignment> ::= <identifier> = <expression>
<identifier> [ <expression> ] = <expression> <ifstatement> ::= if <bexpression> then <slist> else <slist> endif
if <bexpression> then <slist> endif <whilestatement> ::= while <bexpression> do <slist> enddo <printstatement> ::= print ( <expression> )
<expression> ::= <expression> <addingop> <term> 1 <term> 1 <addingop> <term> <bexpression> ::= <expression> <relop> <expression>
<relop> ::= < I <= I == I >= I > I !=
<addingop> ::= + 1-
<term> ::= <term> <multop> <factor> I <factor> <multop> ::= ' 1/
<factor> ::= <constant> I <identifier> 1 <identifier> [ <expression>]
I ( <expression> )
<constant> ::= <digit> 1 <digit> <constant> <identifier> ::= <identifier> <Ietterordigit> I <letter> <lelterordigit> ::= <letter> I <digit>
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 5
<letter> ::= alblcldlelflglhlililklllmlnlolplqlrlsltlulvlwlxlYlz
<digit> ::= 0|1|2|3|4|5|6|7|8|9|
<empty> has the obvious meaning
Comments (zero or more characters enclosed between the standard C/Java-style comment brackets / can be inserted. The language has rudimentary support for 1-dimensional arrays. The declaration a[31 declares an array of three elements, referenced as a[O], a[1] and a[2]. Note also that you should worry about the scoping of names.
A simple program written in this language is:
{ int a[3],t1,t2;
t1=2;
a[0]=1; a[1]=2; a[t1]=3;
t2=-(a[2]+t1*6)/(a[2]-t1);
if t2>5 then
print(t2);
else {
int t3;
t3=99;
t2=-25;
print{-t1+t2*t3); /* this is a comment on 2 lines */
) endif)
The following is a simple register-based machine, supporting a total of 17 instructions. It has the distinct internal storage areas. The first is the set of 8 registers, used by the individual instruction as detailed below, the second is an area used for the storage of variables and the third is an area for the storage of program. The instructions can be preceded by a label. This consists of an int, in the range 1 to 9999 and the label is followed by a colon to separate it from the rest of II instruction. The numerical label can be used as the argument to a jump instruction, as detailed bel In the description of the individual instructions below, instruction argument types are specified follows:
R
specifies a register in the form RO, R1, R2, R3, R4, R5, R6 or R7 (or rO, r1, etc.).
L
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 6
specifies a numerical label (in the range 1 to 9999).
v
specifies a ''variable location" (a variable number, or a variable location pointed to by a register• below).
A
specifies a constant value, a variable location, a register or a variable location pointed to by a regi (an indirect address). Constant values are specified as an integer value, optionally preceded~, minus sign, preceded by a # symbol. An indirect address is specified by an @ followed by a regi So, for example, an A-type argument could have the form 4 (variable number 4), #4 (the constant 4), r4 (register 4) or @r4 (the contents of register 4 identifies the variable location to be access,
The instruction set is defined as follows:
LOAD A,R
loads the integer value specified by A into register R.
STORE R,V
stores the value in register R to variable V.
OUT R
outputs the value in register R.
NEG R
negates the value in register R.
ADDA,R
adds the value specified by A to register R, leaving the result in register R.
SUB A,R
subtracts the value specified by A from register R, leaving the result in register R.
MULA, R
multiplies the value specified by A by register R, leaving the result in register R.
DIV A, R
divides register R by the value specified by A, leaving the result in register R.
JMP L
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 7
Causes an unconditional jump to the instruction with the label L.
JEQ R, L
jumps to the instruction with the label L if the value in register R is zero.
JNE R,l
jumps to the instruction with the label L if the value in register R is not zero.
JGE R,l
jumps to the instruction with the label L if the value in register R is greater than or equal to zero.
JGT R,L
jumps to the instruction with the label L if the value in register R is greater than zero . . E JLE R,L
jumps to the instruction with the label L if the value in register R is less than or equal to zero.
JLT R,L
jumps to the instruction with the label L if the value in register R is less than zero.
NOP
is an instruction with no effect. It can be tagged by a label.
STOP
stops execution of the machine. All programs should terminate by executing a STOP instruction.
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 8
1) Name of the Experiment:
AIM: Design a Lexical analyzer. The lexical analyzer should ignore redundant s tabs and new lines. It should also ignore comments. Although the syntax specifications that identifiers can be arbitrarily long, you may restrict the length to some reasonable Value.
S/W & H/W Requirements:
S/W: Yet Another Compiler to Compiler (YACC) Compiler, Office –XP,
H/W: Pentium IV Processor, 256 MB RAM, 40 GB Hard Disk
ALGORITHM:
We make use of two functions.
look up() – it takes string as argument and checks its presence in the symbol table. If the string is found then returns the address else it returns NULL .
insert() – it takes string as its argument and the same is inserted into the symbol table and the corresponding address is returned.
Step1: Start
Step2: function scan
lexbuf [50] of char
tokenvalue[10]
begin
store scanned char in c
if c blank or tab,then
do nothing
else if c is \n,then
line_number :=line_number=1
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 9
else if c is a digit
token value:=value of the digit and successive digits
return tokentype
Step3: p:=lookup(lexbuf)
if p:=NULL, then
p:=insert(lexbuf,id)
token value:=lexbuf
return(attribute from symboltable)
else
tokenvalue:=NULL
return(NULL) end
Step4: Stop
Result
INPUT file xx.txt
{
int t1,t2
t1=2
t2=t1*3/2;
if t2>5 then
print(t2);
else
{
int t3;
t3=99;
t2=-25
print(-t1+t2+t3);
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 10
}
End if
OUTPUT
SOB else
int SOB
ID int
SEPE ID
ID ENDST
ENDST ID
ID ASSIGN
ASSIGN NUM
ENDST ID
ID ASSIGN
ASSIGN SUBOP
ID NUM
MULOP ENDST
DIVOP OPENPRA
ENDST SUBOP
If ID
ID ADDOP
GT ID
NUM MULOP
Then ID
print CLOSEPRA
OPENPRA ENDST
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 11
ID EOB
CLOSEPRA endif
ENDST
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 12
2) Name of the Experiment:
AIM: Implement the lexical analyzer using JLex, flex or lex other lexical analyzer generating tools.
S/W & H/W Requirements:
S/W: Yet Another Compiler to Compiler (YACC) Compiler, Office –XP,
H/W: Pentium IV Processor, 256 MB RAM, 40 GB Hard Disk
ALGORITHM:
Step1: Start
Step2: Declare the declarations for the given language tokens like digit, alphabet, white space, delimiters, etc.
digit[0-9]
letter[A-Z a-z]
delim[\t\n]
W${delim}+ID{(letter)(letter/digit)}+
Integer {digit}+
% %
{ws} {printf (“SpecialCharacters”)}
{ID} {printf(“Identifiers”)}
{digit} {printf(“\n Integer)}
if {printf(“keyword”)}
else {printf(keyword)}
“& &” {printf(logical operators)}
“>”{printf(logical operators)}
“<”{printf(logical operators)}
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 13
“<=”{printf(logical operators)}
“>=”{printf(logical operators)}
“=” {printf(“\n \n”)}
“!”{printf(“\n \n”)}
“+” {printf(“arithmetic operator”)}
“-“ {printf (“arithmetic”)
“*” {printf(“arithmetic”)}
“%” {printf(arithmetic”)}
% % {printf(“arithmetic”)}
Step3: Write the auxiliary procedure in main() function
Step4: end
Step5: Stop
Program to Implement Lexical Analyser
#include<string.h>
#include<ctype.h>
void main()
{
FILE *fp;
char ch , c[20];
void compare(char *c);
clrscr();
fp=fopen("lexil.c","w+");
while((ch=getchar())!='$')
putc(ch,fp);
rewind(fp);
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 14
fscanf(fp,"%s",c);
while(!(feof(fp)))
{
compare(c);
fscanf(fp,"%s",c);
}
getch();
}
void compare(char *c)
{
if(strcmp(c,"main")==0)
printf("%s->keyword \n",c);
else if((strcmp(c,"scanf")==0)||(strcmp(c,"printf")==0)
||(strcmp(c,"void")==0)||(strcmp(c,"if")==0)||
(strcmp(c,"else")==0)||(strcmp(c,"getch")==0))
printf("%s->keyword \n",c);
else if((strcmp(c,"int")==0)||(strcmp(c,"char")==0)||
(strcmp(c,"float")==0))
printf("%s->keyword \n",c);
else if((strcmp(c,"while")==0)||(strcmp(c,"do")==0)||
(strcmp(c,"switch")==0)||(strcmp(c,"case")==0))
printf("%s->keyword \n",c);
else if( (c[0]>=65 && c[0]<=90)||(c[0]>=97 && c[0]<=122))
printf("%s->identifier \n", c);
else if(((strcmp(c,"+")==0)||(strcmp(c,"-")==0)||
(strcmp(c,"*")==0)||(strcmp(c,"/")==0)))
printf("%s->arithmetic operator \n",c);
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 15
else if((strcmp(c,",")==0))
printf("%s->comma \n",c);
else if((strcmp(c,"(")==0))
printf("%s->left parenthesis \n",c);
else if(strcmp(c,")")==0)
printf("%s->right parenthesis \n",c);
else if((strcmp(c,"{")==0)||(strcmp(c,"}")==0))
printf("%s->braces \n",c);
else if((strcmp(c,"=")==0))
printf("%s->assignment operator \n",c);
else if((strcmp(c,";")==0))
printf("%s->semicolon \n",c);
else
printf("%s->constant\n",c);
}
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 16
3) Name of the Experiment
AIM: Design Predictive Parser for the given language
S/W & H/W Requirements:
S/W: Yet Another Compiler to Compiler (YACC) Compiler, Office –XP,
H/W: Pentium IV Processor, 256 MB RAM, 40 GB Hard Disk
ALGORITHM
Step1: Start
Step2: declare w[10] as char and Z as an array
Step3: enter the string with $ at the end
Step4: if (A(w[z]) then increment z and check for (B(w[z])) and if satisfies increment z and check for ‘d’ if d is present then increment and check for (D(w[z]))
Step5: if step 4 is satisfied then the string is accepted
Else string is not Step6: give for the grammer A-> bc/ab in the loop A(int k) Step7: Describe the grammer b->c/d in the loop B (int k) Step8: Similarly describe the grammer D->d/abcd Step9: if steps7,8,9 are satisfied accordingly string is accepted Else string is not accepted Step10: Stop //Aim: Implementation of Predictive Parser. (Recursive Descent Parsing) #include<stdio.h> int n,i=0; char s[20]; void E(void); void E1(void); void T(void); void T1(void); void F(void); void error(void); void main() { clrscr(); printf("The given grammar is\n"); printf("E -> TE1\n"); printf("E1 -> +TE1/# \n");
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 17
printf("T -> FT1\n"); printf("T1 -> *FT1/#\n"); printf("F -> (E)/d\n"); printf("Enter the string you want to parse:\n"); scanf("%s",s); E(); if(s[i]=='\0') printf("string is valid\n"); else printf("string is invalid\n"); } void E() { T(); E1(); } void E1() { if(s[i]=='+') { i++; T(); E1(); } else if(s[i]=='#') i++; else error(); } void T() { F(); T1(); } void T1() { if(s[i]=='*') { i++; F(); T1(); } else if(s[i]=='#') i++; else error(); } void F()
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 18
{ if(s[i]=='(') { i++; E(); if(s[i]==')') i++; else error(); } else if(s[i]=='d') i++; else error(); } void error() { printf("string is invalid\n"); exit(0); } Result
INPUT file aa.txt
{
int a, ba[10];
a=a*a;
b=a*b;
if a<b
print (a);
else
print(b);
}
OUTPUT:
Successful parsing
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 19
4) Name of the Experiment:
AIM: Design LALR bottom up parser for the above language
S/W & H/W Requirements:
S/W: Yet Another Compiler to Compiler (YACC) Compiler, Office –XP,
H/W: Pentium IV Processor, 256 MB RAM, 40 GB Hard Disk
ALGORITHM
Step1: Start Step2: Initially the parser has s0 on the stack where s0 is the initial state and w$ is in
buffer Step3: Set ip point to the first symbol of w$ Step4: repeat forever, begin Step5: Let S be the state on top of the stack and a symbol pointed to by ip Step6: If action [S, a] =shift S then begin Push S1 on to the top of the stack Advance ip to next input symbol Step7: Else if action [S, a], reduce A->B then begin Pop 2* |B| symbols of the stack Let S1 be the state now on the top of the stack Step8: Output the production A->B End Step9: else if action [S, a]=accepted, then return Else Error() End Step10: Stop Result
[ ] $ lex parser.l
[ ] $ Yacc -d parser.y
[ ] $ cc lex.yy.c y.tab.c -ll -ld
[ ] $ ./a.out
2+3
5.000
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 20
5) Name of the Experiment:
AIM: Convert the BNF rules into YACC form and write code to generate abstract syntax tree
S/W & H/W Requirements:
S/W: Yet Another Compiler to Compiler (YACC) Compiler, Office –XP,
H/W: Pentium IV Processor, 256 MB RAM, 40 GB Hard Disk
ALGORITHM
Step1: Start
Step2: declare the declarations as a header file
{include<ctype.h>}
Step3: token digit
Step4: define the translations rules like line, expr, term, factor
Line:exp ‘\n’ {printf(“\n %d \n”,$1)}
Expr:expr’+’ term ($$=$1=$3}
Term:term ‘+’ factor($$ =$1*$3}
Factor
Factor:’(‘enter’) ‘{$$ =$2)
% %
Step5: define the supporting C routines
Step6: Stop
Result
[ ] $ lex int.l
[ ] $ Yacc -d int y
[ ] $ cc lex.yy.c y.tab.c -ll -ly
[ ] $ ./a.out test.c
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 21
POS Operator Arg1 Arg2 Result
0 < a b to
1 == to FALSE 5
2 + a b +1
3 = t1 a
4 GOTO 5
5 < a b t2
6 == t2 FALSE 10
7 + a b +3
8 = t3 a
9 GOTO 5
10 <= a b t4
11 == t4 FALSE +=5
12 - a b +5
13 = t5 c
14 GOTO 17
15 + a b t6
16 = t6 c
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 22
6) Name of the Experiment:
AIM: Write program to generate machine code from the abstract syntax tree generated by the Parser .The following instruction set may considered as target code.
S/W & H/W Requirements:
S/W: Yet Another Compiler to Compiler (YACC) Compiler, Office –XP,
H/W: Pentium IV Processor, 256 MB RAM, 40 GB Hard Disk
ALGORITHM
Step1: Start
Step2: for every three –address statement of the form x=y op z
Step3: begin
Step4: Call getreg() to obtain the location L which the computation y op z should be performed
Step5: Obtain the current location of the operand y by consulting its address descriptor , and if the values of Y are currently both in the memory location as well as in the register, then prefer the register. If the value of y is not currently available in 1,then generate an instruction MOV y,l
Step6: Generate the instruction OP Z,l and update the address descriptor of X to indicate that X is now available in l and in register then update t\ its descriptor to indicate that it will contain the run time value of x
Step7: If the current values of y ad/or z are in register and we have no further use for them, and they are live at the end of the block ,then after the register descriptor to indicate that after the execution of the statement x=y op z, those registers will no longer contain y and / or z.
Step8: store all results
Step9: Stop
AIM: IMPLEMENATION OF CODE GENERATION
#include<stdio.h>
#include<conio.h>
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 23
#include<string.h>
#include<ctype.h>
#define MAX 50
void push(char);
char pop(void);
int top=-1;
char stack[MAX];
void main()
{
int i;
char str[20],a,b,c;
clrscr();
printf("\n\t\t *** CODE GENERATION ***\n\n\t for single register microprocessor");
printf("\n\n\n\n ENTER THE POSTFIX EXPRESSION:");
gets(str);
printf("\n");
for(i=0;str[i]!='\0';i++)
{
if(isalpha(str[i]))
push(str[i]);
else
{
b=pop();
a=pop();
switch(str[i])
{
case '+': printf("\n\n\t\t LOAD %c",a);
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 24
printf("\n\n\t\t ADD %c",b);
printf("\n\n\t\t MOV temp(R)");
break;
case '-': printf("\n\n\t\t LOAD %c",a);
printf("\n\n\t\t SUB %c",b);
printf("\n\n\t\t MOV temp(R)");
break;
case '*': printf("\n\n\t\t LOAD %c",a);
printf("\n\n\t\t MUL %c",b);
printf("\n\n\t\t MOV temp(R)");
break;
case '/': printf("\n\n\t\t LOAD %c",a);
printf("\n\n\t\t DIV %c",b);
printf("\n\n\t\t MOV temp(R)");
break;
}
push('R');
}
}
getch();
}
void push(char item)// push unction
{
if(top==MAX)
printf("\n\n STACK OVERFLOW");
else
{
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 25
top=top+1;
stack[top]=item;
}
}
char pop(void)// pop function
{
char item;
if(top==-1)
{
printf("\n STACK UNDERFLOW");
}
else
{
item=stack[top];
top--;
}
return item;
}
Result
Input file text1.txt
=+ 1 2
a 1 2
a 2 3
a 3 4
+ a b c
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 26
Output file target.txt
STORE t1 , 2
STORE a[1],2
STORE a[2],3
STORE a[3],4
LOAD a , R0
LOAD b , R1
ADD R1 , R0
SUB R0 , c
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 27
7) Name of the Experiment:
AIM: Computation of First
S/W & H/W Requirements:
S/W: Yet Another Compiler to Compiler (YACC) Compiler, Office –XP,
H/W: Pentium IV Processor, 256 MB RAM, 40 GB Hard Disk
ALGORITHM:
To compute FIRST(X) for all grammar symbols x, apply the following rules until no more terminals can be added to any FIRST set. Step1: if X is terminal, then FIRST(X) is {X}. Step 2: if X is nonterminal and X-> aα is a production, then add a to FIRST(X). if X->€ to FIRST(X). Step 3: if -> Y1,Y2,…….Yk is a production, then for all i such that all of Y1,….Yi-1 are nonterminals and FIRST(Yj) contains € for j=1,2,…… i-1, add every non-€ symbol in FIRST(Y1) to FIRST(x). if V is in FIRST(Yj) for j=1,2,………k, then add € to FIRST(X). PROGRAM: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char t[5],nt[10],p[5][5],first[5][5],temp; int i,j, not, nont, k=0,f=0; clrscr(); printf("\n Enter the no. of Non-terminals in the grammar:"); scanf ("%d", &nont); printf("\nEnter the Non-terminals in the grammer:\n"); for(i=0;i<nont;i++) { scanf("\n%c",&nt[i]); }
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 28
printf("\nEnter the no. of Terminals in the grammer: ( Enter e for absiline ) "); scanf("%d",¬); printf("\nEnter the Terminals in the grammer:\n"); for(i=0;i<not||t[i]=='$';i++) { scanf("\n%c",&t[i]); } for(i=0;i<nont;i++) { p[i][0]=nt[i]; first[i][0]=nt[i]; } printf("\nEnter the productions :\n"); for(i=0;i<nont;i++) { scanf("%c",&temp); printf("\nEnter the production for %c ( End the production with '$' sign ) :",p[i][0]); for(j=0;p[i][j]!='$';) { j+=1; scanf("%c",&p[i][j]); } } for(i=0;i<nont;i++) { printf("\nThe production for %c -> ",p[i][0]); for(j=1;p[i][j]!='$';j++) { printf("%c",p[i][j]); } } for(i=0;i<nont;i++) { f=0; for(j=1;p[i][j]!='$';j++) { for(k=0;k<not;k++) { if(f==1) break; if(p[i][j]==t[k]) { first[i][j]=t[k]; first[i][j+1]='$'; f=1; break; } else if(p[i][j]==nt[k])
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 29
{ first[i][j]=first[k][j]; if(first[i][j]=='e') continue; first[i][j+1]='$'; f=1; break; } } } } for(i=0;i<nont;i++) { printf("\n\nThe first of %c -> ",first[i][0]); for(j=1;first[i][j]!='$';j++) { printf("%c\t",first[i][j]); } } getch(); } RESULT: Enter the no. of Non-terminals in the grammer:3 Enter the Non-terminals in the grammer: ERT Enter the no. of Terminals in the grammer: ( Enter e for absiline ) 5 Enter the Terminals in the grammer: ase*+ Enter the productions Enter the production for E ( End the production with '$' sign ) :a+s$ Enter the production for R ( End the production with '$' sign ) :e$ Enter the production for T ( End the production with '$' sign ) :Rs$ The production for E -> a+s The production for R -> e The production for T -> Rs The first of E -> a The first of R -> e The first of T-> e s
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 30
8) Name of the Experiment AIM: To find whether a given string is keyword or not S/W & H/W Requirements:
S/W: Yet Another Compiler to Compiler (YACC) Compiler, Office –XP,
H/W: Pentium IV Processor, 256 MB RAM, 40 GB Hard Disk
ALGORITHM: Step1: Start. Step 2: Declare a character storing the keywords, s[5][10]={"if","else","goto","continue","return"} and another character array to store the string to be compared st[], initialize integer variables I, flag=0,m. Step 3: Input the string that is to be compared st[]. Step 4: Repeat step 5 to 6 till counter i becomes equal to number of keywords stored in the array. Step 5: Compare the string entered by the user with the strings in the character array by using m=strcmp(st,s[i]), where strcmp function returns true if both the strings are equal. Step 6: i=i+1. Step 7: If flag=1 then it is keyword. Step 8: Else it is not a keyword. Step 9: Stop.
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 31
PROGRAM: #include<stdio.h> #include<conio.h> #include<string.h> void main() { int i,flag=0,m; char s[5][10]={"if","else","goto","continue","return"},st[10]; clrscr(); printf("\n enter the string :"); gets(st); for(i=0;i<5;i++) { m=strcmp(st,s[i]); if(m==0) flag=1; } if(flag==0) printf("\n it is not a keyword"); else printf("\n it is a keyword"); getch(); } RESULT: enter the string : return it is a keyword enter the string :hello it is not a keyword
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 32
9) Name of the Experiment AIM: To find whether a string is constant or not S/W & H/W Requirements:
S/W: Yet Another Compiler to Compiler (YACC) Compiler, Office –XP,
H/W: Pentium IV Processor, 256 MB RAM, 40 GB Hard Disk
ALGORITHM: Step1: Start Step 2: Declare a character array str[] and initialize integer variables len, a=0. Step 3: Input the string from the User. Step 4: Find the length of the string. Step 5: Repeat step 6 to 7 till a<len. Step 6: If the str[a] is numeric character then a++. Step 7: Else it is not a constant, break from the loop and go to step 9. Step 8: if a==len then print that the entered string is a constant Step 9: Else it is not a constant. Step 10: Stop.
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 33
PROGRAM: #include<stdio.h> #include<conio.h> #include<ctype.h> #include<string.h> void main() { char str[10]; int len,a; clrscr(); printf("\n Input a string :"); gets(str); len=strlen(str); a=0; while(a<len) { if(isdigit(str[a])) { a++; } else { printf(" It is not a Constant"); break; } } if(a==len) { printf(" It is a Constant"); } getch(); } RESULT: Input a string : 23 It is a Constant Input a string : a_123 It is not a Constant
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 34
10) Name of the Experiment AIM: To identify whether a given string is identifier or not S/W & H/W Requirements:
S/W: Yet Another Compiler to Compiler (YACC) Compiler, Office –XP,
H/W: Pentium IV Processor, 256 MB RAM, 40 GB Hard Disk
ALGORITHM: Step 1.Start Step 2.Check if the first char is a letter goto step 3 with rest of the string else goto 5. Step 3.Check if the first character of the given string is a letter or a digit repeat step 3 with rest of the string else goto step 5. Step 4.Print “The given string is an identifier” and goto step 6. Step 5.Print “The given string is not an identifier”. Step 6.Exit
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 35
PROGRAM:
#include< stdio.h > #include<conio.h> int is iden(char*); int second(char*); int third(); void main() { char *str; int i = -1;
if(isident(str))
printf(“\n\n\t\t The given string is an identifier”);
else
printf(“\n\n\t\t The given string is not an identifier”);
getch();
}
//To check whether the given string is identifier or not
//This function acts like first stage of dfa
int isident(char *str)
{
if((str[0]>=’a’&&str[0]<=’z’)||(str[0]>=’A’&&str[0]<=’Z’))
clrscr(); printf ("\n\n\t\tEnter the desired String: "); do { ++i; str[i] = getch(); if(str[i]!=10 && str[i]!=13) printf("%c",str[i]); if(str[i] == '\b') { --i; printf(" \b"); } }while(str[i] != 10 && str[i] != 13);
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 36
{
return(second(str+1));
}
else
return 0;
}
//This function acts like second stage of dfa
int second(char *str)
{
if((str[0]>=’0’&&str[0]<=’9’)||(str[0]>=’a’&&str[0]<=’Z’))||(str[0]>=’A’&&str[0]<=’Z’))
{
return(second(str+1));
}
else
{
if(str[0]==10||str[0]==13)
{
return(third(str));
}
else
{
return 0;
}
}
}
//This function acts as third stage of dfa
int third()
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 37
{
return 1;
}
RESULT:
Enter the desired String: a123 The given string is an identifier Enter the desired String: shailesh The given string is an identifier
Enter the desired String: 1asd The given string is not an identifier
Enter the desired String: as-*1 The given string is not an identifier
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 38
FAQ
1) What are lex and Yacc tools.
2) Difference between Lex and Yacc tools
3) Construct an NFA for the regular expression ab*
4) Are NFA and DFA equivalent
5) What is the role of Lexical Analyser.
6) What is a token? 7) What is an Identifier? 8) Give the result and the reason behind that for following statement. If(-1) printf(“you are in if”); else printd(“you are in else”);
9) State the no.of t
a=b+c*d 10) What is CFG. 11) State three rules of FIRST.
12) State three rules of FOLLOW. 13) Compute the leading of E,T,F in the following CFG E->E+T/T T->T*F/F F->(E)/id 14) State error in each phase of compiling. 15) State the O/P of each phase of Compiler.
.tokens in the following expressions: .
St. Martin’s Engineering College Compiler Design
Computer Science and Engineering Page 39