SlideShare a Scribd company logo
1 of 75
Silver Oak Group of Institutes
Silver Oak College of Engineering & Technology
Aditya Silver Oak Institute of Technology
GUJARAT TECHNOLOGICAL UNIVERSITY BACHELOR OF
ENGINEERING
COMPILER DESIGN
(3170701)
COMPUTER ENGINEERING / INFORMATION TECHNOLOGY
DEPARTMENT
ii
COMPILER DESIGN PRACTICAL BOOK DEPARTMENT
OF COMPUTER ENGINEERING PREFACE
It gives us immense pleasure to present the first edition of Compiler Design Practical Book for the
B. Tech. 4th year students for SILVER OAK GROUP OFINSTITUTES.
The Compiler Design theory and laboratory course at SILVER OAK GROUP OF INSTITUTES,
AHMEDABAD is designed in such a way that students develop the basic understanding of the
Compiler Design and various applications in the theory classes and then try their hands on the
experiments to realize the various aspects of Compiler Design.
This laboratory course is intended to make the students experiment on the basic techniques of
compiler construction and tools that can used to perform syntax-directed translation of a high-
level programming language into an executable code. Students will design and implement
language processors in C by using tools to automate parts of the implementation process. This
will provide deeper insights into the more advanced semantics aspects of programming
languages, code generation, machine independent optimizations, dynamic memory allocation,
and objectorientation
Hopefully this Compiler Design Practical Book will serve the purpose for which it has been
developed.
Lab Manual Revisedby:Prof.Monali Suthar, SilverOakCollege of EngineeringandTechnology
Prof. Jalpa Shah, Aditya Silver Oak Institute of Technology
Lab Manual Revision No.: SOGI_2170701_LM_2019_1
iii
INSTRUCTIONS TO STUDENTS
1. While entering into the LAB students should wear their ID cards and arrange your shoes in
properway.
2. All the students must come with the lab manual in every practicalsession.
3. Students have to maintain their lab manual on regular basis(it means evaluation of practical
will be doneweekly)
4. Questions given in the lab manual must be attempted and solved on the givendate.
5. Feedback and conclusion section of each practical must be filled with utmostsincerity.
6. Lab manual must be filled only after performing thepractical.
7. Students should maintain decorum inside thelaboratory.
8. All the observations have to be neatly recorded in the Practical Class Notebook and verified
by the instructor before leaving thelaboratory.
9. Write the answers of all the questions at the end of each experiment in the PracticalBook.
10. Lab Manual records need to be submitted on or before date ofsubmission.
11. The grades for the Practical course work will be awarded based on your performance in the
laboratory, regularity, obtaining of experiments results, labquiz, regular viva-voice and end-term
examination.
12. Students are restricted to not to use mobile phones inside thelaboratory.
13. After completing the laboratory exercise make sure to shut down the systemand switch off
all the amities properly. Also rearrange the chair in the properway.
iv
CERTIFICATE
This is to certify that
Mr. Dhaval r tukadiya withenrolmentno.180770107240, Semester 7
DIV-D has successfully completed his laboratory experiments in the Compiler
Design (2170701) from the department of Computer Engineering during the
academic year ...2020............ -..2021.....
Date ofSubmission:..5-10-2020....................... Staff in charge:...........................
Head of Department: ...........................................
v
TABLE OF CONTENT
Sr.
No Experiment Title
Page No
Date of
Start
Date of
Completion Sign
Marks
(out of
10)
From To
1
To study the various types of
errors in compiler.
1 4
2
Study of Lex/Yacc of
Compiler Writing.
5 8
3
Write a Program to check
whether a string belongs to
grammar or not.
9 13
4
Write a Program to check
whether a string belongs to
grammar or not.
14 18
5
Write a program to compute
FIRST of non-terminals
19 24
6
Write a program to check
whether a string is keyword
and constant.
25 28
vi
7
Write a program to count
number of new lines and
white spaces.
29 30
8
Write a program to count the
number of vowels and
consonants in a given string.
31 33
9
Write a program to count
number of characters, words,
spaces, end of lines in a given
input file.
34 37
vi
10
Write a program to recognize
whether a given sentence is
simple 30/9/14 or compound.
38 40
11
Write a program to test the
validity of a simple
expression involving
operators +, -,* and /.
41 43
12
Write a program to recognize
nested IF control statements
and display the levels of
nesting.
44 47
13
Write a program to recognize
strings ‘aaab’, ‘abbb’, ‘ab’, and
‘a’ using grammar.
48 51
1
PRACTICAL SET-1
Types of program errors
We distinguish between the following types of errors:
1. Syntax errors: errors due to the fact that the syntax of the language is notrespected.
2. Semantic errors: errors due to an improper use of programstatements.
3. Logical errors: errors due to the fact that the specification is notrespected.
From the point of view of when errors are detected, we distinguish:
1. Compile time errors: syntax errors and static semantic errors indicated by thecompiler.
2. Runtime errors: dynamic semantic errors, and logical errors, that cannot be detected
by the compiler(debugging).
2
AIM: To study the various types of errors in compiler.
// C program to illustrate
// syntax error
#include<stdio.h>
void main()
{
int x = 10;
int y = 15;
printf("%d", (x, y)) // semicolon missed
}
Error:
error: expected ';' before '}' token
Syntax of a basic construct is written wrong. For example : while loop
// C program to illustrate
// syntax error
#include<stdio.h>
int main(void)
{
// while() cannot contain "." as an argument.
while(.)
{
printf("hello");
}
return 0;
3
}
Error:
error: expected expression before '.' token
while(.)
In the given example, the syntax of while loop is incorrect. This causes a syntax error.
Run-time Errors: Errors which occur during program execution(run-time) after successful compilation are called
run-time errors. One of the most common run-time error is division by zero also known as Division error. These
types of error are hard to find as the compiler doesn’t point to the line at which the error occurs. For more
understanding run the example given below.
// C program to illustrate
// run-time error
#include<stdio.h>
void main()
{
int n = 9, div = 0;
// wrong logic
// number is divided by 0,
// so this program abnormally terminates
div = n/0;
printf("resut = %d", div);
}
Error:
warning: division by zero [-Wdiv-by-zero]
div = n/0;
4
In the given example, there is Division by zero error. This is an example of run-time error i.e errors occurring while
running the program.
Linker Errors: These error occurs when after compilation we link the different object files with main’s object
using Ctrl+F9 key(RUN). These are errors generated when the executable of the program cannot be generated. This
may be due to wrong function prototyping, incorrect header files. One of the most common linker error is writing
Main() instead of main().
// C program to illustrate
// linker error
#include<stdio.h>
void Main() // Here Main() should be main()
{
int a = 10;
printf("%d", a);
}
Error:
(.text+0x20): undefined reference to `main'
Logical Errors : On compilation and execution of a program, desired output is not obtained when certain input
values are given. These types of errors which provide incorrect output but appears to be error free are called logical
errors. These are one of the most common errors done by beginners of programming. These errors solely depend on
the logical thinking of the programmer and are easy to detect if we follow the line of execution and determine why
the program takes that path of execution.
// C program to illustrate
// logical error
5
int main()
{
int i = 0;
// logical error : a semicolon after loop
for(i = 0; i < 3; i++);
{
printf("loop ");
continue;
}
getchar();
return 0;
}
No output
Semantic errors: This error occurs when the statements written in the program are not meaningful to the compiler.
// C program to illustrate
// semantic error
void main()
{
int a, b, c;
a + b = c; //semantic error
}
Error
Error: lvalue required as left operand of assignment
a + b = c; //semantic error
6
Question Answers:
1. Consider the following C declaration. int x[10], y,z; What kind of error does following
statement contain? z == x+y;
a. Syntaxerror
b. Semantic error
c. Logicalerror
d. All of theabove
2. Write down types of errorsgenerated.
Logical error
Semantic error
Syntax error
3. All syntax errors and some of the semantic errors (the static semantic errors) are
detected bythe Compiler.
4. Errors that occur when you violate the rules of writing C/C++ syntax are knownas
Syntax error .
7
5. Error is an illegal operation performed by the user which results in abnormal working of
the program. State TRUE orFALSE.
TRUE
8
PRACTICAL SET-2
Lex/Yacc compiler
A compiler or interpreter for a programming language is often decomposed into two parts:
1. Read the source program and discover itsstructure.
2. Process this structure, e.g. to generate the target program.
Lex and Yacc can generate program fragments that solve the first task.
The task of discovering the source structure again is decomposed into subtasks:
1. Split the source file intotokens(Lex).
2. Find the hierarchical structure of the program(Yacc).
9
AIM: Study of Lex/Yacc of Compiler Writing.
A compiler or interpreter for a programming language is often decomposed into two parts:
1. Read the source program and discover its structure.
2. Process this structure, e.g. to generate the target program.
Lex and Yacc can generate program fragments that solve the first task.
The task of discovering the source structure again is decomposed into subtasks:
1. Split the source file into tokens (Lex).
2. Find the hierarchical structure of the program (Yacc).
Description:
Some of the most time consuming and tedious parts of writing a compiler involve the lexical scanning and syntax
analysis. Luckily there is freely available software to assist in these functions. While they will not do everything for
you, they will enable faster implementation of the basic functions. Lex and Yacc are the most commonly used
packages with Lex managing the token recognition and Yacc handling the syntax. They work well together, but
conceivably can be used individually as well.
Both operate in a similar manner in which instructions for token recognition or grammar are written in a special file
format. The text files are then read by lex and/or yacc to produce c code. This resulting source code is compiled to
make the final application. In practice the lexical instruction file has a “.l” suffix and the grammar file has a “.y”
suffix. This process is shown in Figure 1.
Figure 1.Lex and Yacc Process (based on a diagram on page 5 of “A Compact Guide to Lex & Yacc” by Thomas
Niemann)
The file format for a lex file consists of (4) basic sections
• The first is an area for c code that will be place verbatim at the beginning of the generated source code. Typically
is will be used for things like #include, #defines, and variable declarations.
• The next section is for definitions of token types to be recognized. These are not mandatory, but in general makes
the next section easier to read and shorter.
• The third section set the pattern for each token that is to be recognized, and can also include c code to be called
when that token is identified
10
• The last section is for more c code (generally subroutines) that will be appended to the end of the generated c
code. This would typically include a main function if lex is to be used by itself.
• The format is applied as follows (the use and placement of the % symbols are necessary):
%
{
//header c code
%}
//definitions
%%
//rules
%%
//subroutines
The format for a yacc file is similar, but includes a few extras.
• The first area (preceded by a %token) is a list of terminal symbols. You do not need to list single character ASCII
symbols, but anything else including multiple ASCII symbols need to be in this list (i.e. “==”).
• The next is an area for c code that will be place verbatim at the beginning of the generated source code.Typically
is will be used for things like #include, #defines, and variable declarations.
• The next section is for definitions - none of the following examples utilize this area • The fourth section set the
pattern for each token that is to be recognized, and can also include c code to be called when that token is identified
• The last section is for more c code (generally subroutines) that will be appended to the end of the generated c
code. This would typically include a main function if lex is to be used by itself.
• The format is applied as follows (the use and placement of the % symbols are necessary):
%tokens RESERVED, WORDS, GO, HERE
%{
//header c code
%}
//definitions
%%
11
//rules
%%
//subroutines
These formats and general usage will be covered in greater detail in the following (4) sections. In general it is best
not to modify the resulting c code as it is overwritten each time lex or yacc is run. Most desired functionality can be
handled within the lexical and grammar files, but there are some things that are difficult to achieve that may require
editing of the c file.
12
Question Answers:
1. YACC is a computerprogramfor
operationsystem
.
b. DOS
c. Unix
d.
d. openSUSE
2. Input of Lexis
a. Set to regular
expression
b. Statement
c. Numeric data
d. ASCII data
3. Which of the following software tool is parser
generator? a.Lex
b. Yacc
c. Both of the
mentioned
d. None of
thementioned
a.Windows
13
4.
5. Yacc semantic action is a sequence of C Program State TRUE or FALSE.
TRUE
The YACC takes C code as inputandoutputsfile whoch contain souce code that can be combined
into actual parser. .
14
PRACTICAL SET-3
Grammar
Definition − A context-free grammar (CFG) consisting of a finite set of grammar rules is a
quadruple (N, T, P, S) where
N is a set of non-terminalsymbols.
 T is a set of terminals where N ∩ T =NULL.
 P is a set of rules, P: N → (N ∪T)*, i.e., the left-hand side of the production rule P does
have any right context or leftcontext.
S is the startsymbol.
Example
 The grammar ({A}, {a, b, c}, P, A), P : A → aA, A→abc.
 The grammar ({S, a, b}, {a, b}, P, S), P: S → aSa, S → bSb, S→ε
 The grammar ({S, F}, {0, 1}, P, S), P: S → 00S | 11F, F → 00F|ε
The set of all strings that can be derived from a grammar is said to be the language generated
from that grammar. A language generated by a grammar G is a subset formally definedby
 L(G)={W|W ∈ ∑*, S ⇒GW}
If L(G1) = L(G2), the Grammar G1 is equivalent to the Grammar
G2.Example
If there is a grammar
G: N = {S, A, B} T = {a, b} P = {S → AB, A → a, B → b}
15
Here S produces AB, and we can replace A by a, and B by b. Here, the only accepted
string is ab, i.e.,
L(G) = {ab}
Example
Suppose we have the following grammar −
G: N = {S, A, B} T = {a, b} P = {S → AB, A → aA|a, B → bB|b}
The language generated by this grammar −
L(G) = {ab, a2b, ab2, a2b2, ………}
= {am bn | m ≥ 1 and n ≥ 1}
16
AIM: Write a Program to check whether a string belongs to grammar ornot.
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main() {
char string[50];
int flag,count=o;
clrscr();
printf("The grammar is: S->aS, S->Sb, S->abn");
printf("Enter the string to be checked:n");
gets(string);
if(string[0]=='a') {
flag=0;
for (count=1;string[count-1]!='0';count++) {
if(string[count]=='b') {
flag=1;
continue;
} else if((flag==1)&&(string[count]=='a')) {
printf("The string does not belong to the specified grammar");
break;
} else if(string[count]=='a')
continue; else if(flag==1)&&(string[count]='0')) {
printf("String accepted…..!!!!");
break;
} else {
printf("String not accepted");
}
}
}
getch();
}
17
Output:
The grammar is
s-> as
s->sb
s->ab
Enter the string to be checked: aab
String accecpted…!!!
18
Question Answers:
1. Which of the following statement isfalse?
a. Context free language is the subset of context sensitive
language b. Regular language is the subset of context
sensitivelanguage
c. Recursively ennumerable language is the super set of regular language
d. Context sensitive language is a subset of context free language
2. The minimum number of productions required to produce a language
consistingof palindrome strings over ∑={a,b}is
a. 3
b. 7
c. 5
d. 6
3. Find a regular expression for a grammar which generates a language which states:
L contains a set of strings starting wth an a and ending with a b, with something in
the middle.
a. a(a*Ub*)
bb.
a*(aUb)b*
c.a(a*b*)b
d. None of the mentioned
19
4. Which of the following is the correct representation of grammar for the given
regular expression?a(aUb)*b
a. S →aMb
M → e
M → aM
M →bM
b. (1) S →aMb
(2) M →Mab
(3) M →aM
(4) M → bM
c. (1) S →aMb
(2) M →e
(3) M →aMb
(4) M → bMa
d. None of thementioned
5. Define of Context freeGrammar.
Context-free grammars(CFGs) are usedtodescribe context-free languages.A context-freegrammarisa setofrecursive
rulesusedtogenerate patterns of strings ....CFG's are usedto describe programminglanguagesandparserprograms in
compilerscanbe generatedautomaticallyfromcontext-free grammars.
20
PRACTICAL SET-4
Parse Tree:
A parse tree is an entity which represents the structure of the derivation of aterminal string from
some non-terminal (not necessarily the start symbol). The definition is as in the book. Key
features to define are the root ∈ V and yield ∈Σ* of each tree.
 For each σ ∈Σ, there is a tree with root σ and no children; its yield isσ
 For each rule A → ε, there is a tree with root A and one child ε; its yieldisε
 If t1, t2, ..., tn are parse trees with roots r1, r2, ..., rn and respective yields y1, y2, ..., yn, and
A → r1r2...rn is a production, then there is a parse tree with root A whose children are t1,
t2, ..., tn. Its root is A and its yield isy1y2...yn
Observe that parse trees are constructed from bottom up, not top down. The actual
construction of "adding children" should be made more precise, but we intuitively know what's
going on.
As an example, here are all the parse (sub) trees used to build the parse tree for the arithmetic
expression 4 + 3 * 2 using the expression grammar
E → E + T | E - T | T
T → T * F | F
F → a | ( E )
where a represents an operand of some type, be it a number or variable. The trees are grouped by
height.
The following code is supposed to generate a parse tree of the input expression, but the
problem is that the output E, T, F, S (functions used in the code).
21
AIM: Write a Program to generate a parse tree.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char next;
void E(void);void T(void);
void S(void);void F(void);
void error(int);void scan(void);
void enter(char);
void leave(char);
void spaces(int);
int level = 0;
//The main should always be very simple
//First scan the string
//second checkfor end of string reached , if yes success and if not error.
//P ---> E '#'
int main(void){
printf("Input:");
scan(); E();
if (next != '#') error(1);
else printf("***** Successful parse *****n");
}
22
//E ---> T {('+'|'-') T}
void E(void){
enter('E');
T();
while (next == '+' || next == '-') {
scan();
T();
}
leave('E');
}
//T ---> S {('*'|'/') S}
void T(void)
{
enter('T'); S();
while (next == '*' || next == '/') {
scan(); S();
}
leave('T');
}
//S ---> F '^' S | F
void S(void)
{
enter('S'); F();
if (next == '^') {
scan(); S();
}
leave('S');}
23
//F ---> char | '(' E ')'
void F(void)
{
enter('F');
if (isalpha(next))
{
scan();
}
else if (next == '(') {
scan(); E();
if (next == ')')
scan();
else
error(2);
}
else {
error(3);
}
leave('F');
}
//Scan the entire input
void scan(void){
while (isspace(next = getchar()));
}
void error(int n)
24
{
printf("n*** ERROR: %in", n);
exit(1);
}
void enter(char name)
{
spaces(level++);
printf("+-%cn", name);
}
void leave(char name)
{
spaces(--level);
printf("+-%cn", name);
}
//TO display the parse tree
void spaces(int local_level)
{
while (local_level-- > 0)
printf("| ");
}
25
Question Answers:
1. Which among the following is the root of the parse
tree? a. Production P
b. Terminal T
c. Variable V
d. Starting Variable S
2. A grammar with more than one parse tree is
called: a.Unambiguous
b. Ambiguous
c. Regular
d. None of the mentioned
3. Directed Acyclic graphs is the acyclic graphical representation of agrammar.
4. Grammar is ambiguous if
a. it has 2 parsetrees
b. it has two left mostderivations
c. it has two right most
derivations d. All of theabove
5. Draw parse tree from given grammar for 3*2+5string.
E -> I
E -> E + EE
-> E * EE->
(E)
I -> ε | 0 | 1 | … | 9
26
PRACTICAL SET-5
First of NonTerminals
The rules for finding FIRST of a given grammar is: If
X is terminal, FIRST(X) = {X}.
If X → ε is a production, then add ε to FIRST(X).
If X is a non-terminal, and X → Y1 Y2 … Yk is a production, and ε is in all of FIRST(Y1), …,
FIRST(Yk), then add ε to FIRST(X).
If X is a non-terminal, and X → Y1 Y2 … Yk is a production, then add a to FIRST(X) if for
some i, a is in FIRST(Yi), and ε is in all of FIRST(Y1), …, FIRST(Yi-1).
Assumptions:
Each Non-terminal character is represented by one Uppercase letter.
Each Terminal character is represented by one lowercase letter.
LHS and RHS of each production will be separated by a "=" symbol.
There will be no Blank space within a production string.
Assumed that Left recursion is avoided in all input productions.
27
AIM: Write a program to compute FIRST of non-terminals
Code:
#include<stdio.h>
#include<conio.h>
char array[10][20],temp[10];
int c,n;
void fun(int,int[]);
int fun2(int i,int j,int p[],int );
void main()
{
int p[2],i,j;
printf("Enter the no. of productions :");
scanf("%d",&n);
printf("Enter the productions :n");
for(i=0;i<n;i++)
scanf("%s",array[i]);
28
for(i=0;i<n;i++)
{
c=-1,p[0]=-1,p[1]=-1;
fun(i,p);
printf("First(%c) : [ ",array[i][0]);
for(j=0;j<=c;j++)
printf("%c,",temp[j]);
printf("b ].n");
getch();
}
}
int fun2(int i,int j,int p[],int key)
{
int k;
if(!key)
{
for(k=0;k<n;k++)
if(array[i][j]==array[k][0])
break;
29
p[0]=i;p[1]=j+1;
fun(k,p);
return 0;
}
else
{
for(k=0;k<=c;k++)
{
if(array[i][j]==temp[k])
break;
}
if(k>c)return 1;
else return 0;
}
}
void fun(int i,int p[])
{
int j,k,key;
for(j=2;array[i][j] != NULL; j++)
39
{
if(array[i][j-1]=='/')
{
if(array[i][j]>= 'A' && array[i][j]<='Z')
{
key=0;
fun2(i,j,p,key);
}
else
{
key = 1;
if(fun2(i,j,p,key))
temp[++c] = array[i][j];
if(array[i][j]== '@'&& p[0]!=-1) //taking '@' as null symbol
{
if(array[p[0]][p[1]]>='A' && array[p[0]][p[1]] <='Z')
{
key=0;
fun2(p[0],p[1],p,key);
31
Output:
}
else
if(array[p[0]][p[1]] != '/'&& array[p[0]][p[1]]!=NULL)
{
if(fun2(p[0],p[1],p,key))
temp[++c]=array[p[0]][p[1]];
}
}
}
}
}
}
32
Question Answers:
1. What is terminal in grammar? How can you identify it in thegivengrammar?
A terminal is a symbol which does not appear on the left-hand side of any production.
A grammar contains a set of terminal symbols (tokens) such as the plus sign, +, the times sign, *,
and other tokens defined by the lexical analyzer such as Identifiers.
Eg: E- E+T
E- a
T - b
Here a,b,+ are terminals.
2. What is non-terminal in grammar? How can you identify it in the givengrammar?
Explain withexample.
Non-terminals are syntactic variables that denote sets of strings. The non-
terminals define sets of strings that help define the language generated by
the grammar. A set of tokens, known as terminal symbols (Σ). Terminals are the
basic symbols from which strings are formed
Eg: E- E+T
E- a
T - b
Here E,T are non terminals
3. If you find NULL or ε in the grammar then whatwill be the procedure forfindingtheFIRST?
It will be directlyincludedinfirsttable
33
4. Find the FIRST of the givengrammar.
S ->cAd
A -> bc|a
S: c
A: a,b
5. Find FIRST of the givengrammar
S→ABa /bCA
A→cBCD / є
B→CdA / ad
C→eC / є
D→bsf / a
Ans:
First of s: b,c,e
First of A: c, Ƹ
First of B: a,e,Ƹ
First of C: e ,Ƹ
First of D : a,b
34
PRACTICAL SET-6
Constants in C
As the name suggests the name constants is given to such variables or values in C programming
language which cannot be modified once they are defined. They are fixed values in a program.
There can be any types of constants like integer, float, octal, hexadecimal, character constants
etc. Every constant has some range. The integers that are too big to fit into an int willbe taken as
a long. Now there are various ranges that differ from unsigned to signed bits. Under signed bit
the range of an int, varies from -128 to +127 and under unsigned bit int varies from 0to255.
How to define constants?
In C program we can define constants in two ways as shown below:
1. using #define preprocessordirective
2. Using a constkeyword.
Keywords in C
Keywords are specific reserved words in C each of which has a specific feature associated with
it. Almost all of the words which help us use the functionality of the C language are included in
the list of keywords. So you can imagine that the list of keywords is not going to be a small one!
There are a total of 32 keywords in C:
auto, break, case, char, const, continue, default , do, double, else, enum, extern , float , for,
goto, if, int , long , register ,return ,short, signed, sizeof, static , struct ,switch, typedef ,union
,unsigned , void, volatile , while
35
AIM : Write a program to check whether a string is keyword and constant. Code:
#include <stdio.h>
#include <string.h>
int main() {
char keyword[32][10]={
"auto","double","int","struct","break","else","long",
"switch","case","enum","register","typedef","char",
"extern","return","union","const","float","short",
"unsigned","continue","for","signed","void","default",
"goto","sizeof","voltile","do","if","static","while"
} ;
char str[]="which";
int flag=0,i;
for(i = 0; i < 32; i++) {
if(strcmp(str,keyword[i])==0) {
flag=1;
}
}
if(flag==1)
printf("%s is a keyword",str);
else
printf("%s is not a keyword",str);
}
Output:
Which is not a keyword
36
Question Answers:
1. Use of “struct”keyword.
struct is used to declare a new data-type. Basically this means grouping variables together. For
example, a struct data type could be used to declare the format of the file.
2. Which keyword is used for define decimalvalues?
Float is used.
3. Why we need constant in C? Explain withexample.
Constants refer to fixed values that the program may not alter during its execution. These fixed values
are also called literals. Constants can be of any of the basic data types like an integer constant, a
floating constant, a character constant, or a string literal. There are enumeration constants as well.
4. Use of “sizeof” keyword.
The sizeof operator is the most common operator in C. It is a compile-time unary operator and used tocompute
the size of its operand. It returns the size of a variable......When sizeof() is used with the data types, it simply
returns the amount of memory allocated to that data type.
5. Which one of the following is not a reserved keyword for C?
a.Auto
b.Case
c.Main
d.default
37
PRACTICAL SET-7
Lexical Analysis
Lexical analysis is the first phase of a compiler. It takes the modified source code from language
preprocessors that are written in the form of sentences. The lexical analyzer breaks these
syntaxes into a series of tokens, by removing any whitespace or comments in the sourcecode.
AIM: Write a program to count number of new lines and white spaces.
Code:
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
char ch;
int character = 0, space = 0, tab = 0, line = 0;
fp = fopen("/home/tusharsoni/Desktop/TestFile","r");
if(fp == NULL)
{
printf("File Not Foundn");
exit(1);
}
else
{
while(1)
{
ch = fgetc(fp);
if(ch == EOF)
{
break;
}
character++;
if(ch == ' ')
space++;
else if(ch == 't')
tab++;
else if(ch == 'n')
line++;
}
38
}
fclose(fp);
printf("nNumber of Characters = %dn", character);
printf("nNumber of Tabs = %dn", tab);
printf("nNumber of New Lines = %dn", line);
printf("nNumber of Spaces = %dn", space);
Output:
39
Question Answers:
1. The number of tokens in the following C statement is printf("i = %d, &i = %x", i,&i);
a.3 b.26 c.10 d.21
2. The output of a lexical analyzeris
a. A parsetree
b. Intermediate code
c. Machine code
d. A stream ofTokens
3. The number of tokens infollowing
program? # define M100
int main ( )
{
//
declarevariables
int n = 2020;
return n %M;
}
Answer:16
4. Number of tokens in the
statement: printf(“i=%d,&i=%x”,i,&bi);
Answer:
Printf(“i=%d,&i=%x”,I,&,I,);
40
5. Count the number of
tokens; voidmain()
{
i/* nt */a=10;
return;
}
Answer:13
41
42
PRACTICAL-8
lex
Lexis a programdesigned to generate scanners,also known as tokenizers, which recognize lexical
patterns in text. Lex is an acronym that stands for "lexical analyzer generator." It is intended
primarily for Unix-based systems. The code for Lex was originally developed by Eric Schmidt and
Mike Lesk.
FOR COMPILE AND EXECUTE:
lex file.l
gcc lex.yy.c -ly -ll
./a.out
AIM: Write a program to count the numberof vowels and consonants in a givenstring.
Code:
%{
int vow_count=0;
int const_count =0;
%}
%%
[aeiouAEIOU] {vow_count++;}
[a-zA-Z] {const_count++;}
%%
main()
43
{
printf("Enter the string of vowels and consonents:");
yylex();
printf("The number of vowels are: %dn",vow);
printf("The number of consonants are: %dn",cons);
return 0;
}
Output:
Enter the string of vowels and consonents
My name is Hina
The number of vowels are: 5
The number of consonants are: 7
44
Question Answers:
1. Which of the following is not a feature ofcompiler?
a. Scans the entire program first and then translate it into machinecode
b. When all the syntax errors are removed execution takesplace
c. Slow fordebugging
d. Execution time ismore
2. Input of Lexis?
a. Set to regularexpression
b. Statement
c. Numeric data
d. ASCIIdata
3. A Lex compilergenerates?
a. Lex objectcode
b. b.Transitiontables
c. CTokens
d. None ofabove
45
4. Analysis which determines the meaning of a statement onceitsgrammatical
e becomes known is termedas
a. Semantic analysis
b. Syntaxanalysis
c. Regularanalysis
d. Generalanalysis
structur
46
PRACTICAL SET-9
How to count Word,Character?
public int GetNumOfCharsInFile(string filePath)
{
int count = 0;
using (var sr = new StreamReader(filePath))
{
while (sr.Read() != -1)
count++;
}
return count;
}
FOR COMPILE AND EXECUTE:
lex file.l
gcc lex.yy.c -ly -ll
./a.out
47
AIM: Write a program to count number of characters, words, spaces, end of lines in a given
input file.
Code:
%{
int ch=0,wd=0,l=0,sp=0;
%}
%%
(" ") {sp++;}
[n] {l++;}
[^ t n]+ {wd++; ch=ch+yyleng;}
%%
main()
{
yyin=fopen("kit.txt","r");
yylex();
printf("char=%d t words=%d t spaces=%d t lines=%d",ch,wd,sp,l);
}
Output:
(Content in file)
I love program ming.
Working with files in C program ming is fun.
I am learning C program ming at Codeforwin.
(Output)
Total characters = 106
Total words = 18
Total lines = 3
48
Question Answers:
1. Inacompiler checks every character of the sourcetext.
a. The lexicalanalyzer
b. The syntaxanalyzer
c. The codegenerator
d. The codeoptimizer
2. How many tokens are there in the following Cstatement?
a. printf (“j=%d, &j=%x”,j&j)
b. 4
c. 5
d. 9
e. 10
3. In a compiler, when is the keyboards of a language arerecognized?
a. During the lexical analysis of aprogram
b. During parsing of theprogram
c. During the codegeneration
d. During the data flowanalysis
4. The lexical analysis forJavaneeds _ in a necessary and sufficientsense.
a. Turingmachine
b. Non-deterministic push downautomata
c. Deterministic push downautomata
d. Finite stateautomata
49
PRACTICAL SET-10
For Compile And Execute:
lex file.l
gcc lex.yy.c -ly -ll
./a.out
AIM: Write a program to recognize whether a given sentence is simple orcompound.
Code:
%{
int flag=0;
%}
%%
(""[aA][nN][dD]"")|(""[oO][rR]"")|(""[bB][uU][tT]"") {flag=1;}
%%
int main()
{
printf("Enter the sentencen");
yylex();
if(flag==1)
printf("nCompound sentencen");
else
printf("nSimple sentencen");
return 0;
}
50
Output:
$./a.out
Enter the sentence
I am Pooja
I am Pooja
[Ctrl-d]
Simple sentence
$./a.out
Enter the sentence
CSE or ISE
CSE or ISE
[Ctrl-d]
Compound sentence
51
Question Answers:
1. is not an advantage of using shared; dynamically linked libraries as
opposed to using statically linkedlibraries.
a. Lesser overall page fault rate inthesystem
b. Faster programstartup
c. Existing programs need not be relinked to take advantage of newerversions
d. Smaller sizes of executablefiles
2. The number of derivation trees for the correct answer strings to question 30is
a. 4
b. 1
c. 3
d. 2
3. Assuming that the input is scanned in left to right order, while parsing an input string
the top-down parseruse
a. Rightmostderivation
b. Leftmostderivation
c. Rightmost derivation that is traced out inreverse
d. Leftmost derivation that is traced out inreverse
4. is atop-downparser
a. Operator precedenceparser
b. An LALR (k)parser
c. An LR (k)parser
d. Recursive descentparser
52
PRACTICAL SET-11
YACC:
YACC program also consists of three sections, "Definitions", "Context Free Grammar and action
for each production","Subroutines/Functions".
In first section, we can mention C language code which may consist of header files inclusion,
globalvariables/Constants definition/declaration. C languagecode can bementioned inbetween
the symbols %{ and %}. Alsowe can define tokens in the first section. In above program, NUMBER
is the token. We can define the associativity of the operations (i.e. left associativity or right
associativity). In above yacc program, we have specified left associativity for all operators.
Priorities among the operators can also be specified. It is in the increasing order "from top to
bottom". For e.g. in our above yaccprogram, round brackets '(',')' has the higherpriority than '*',
'/', '%' which has higher priority than '+', '-'. Operators in the same statement have the same
priority. For e.g. in our above program all of the '*', '/', '%' have the samepriority.
In second section, we mention the grammar productions and the action for each production.
$$ refer to the top of the stack position while $1 for the first value, $2 for the second value
in the stack.
Third section consists ofthe subroutines. We have to call yyparse() to initiatethe parsing process.
yyerror() function is called when all productions in the grammar in second section do not match
to the inputstatement.
For compile and execute:-
yacc -d y2.y
lex y2.l
cc lex.yy.c y.tab.c -ll
./a.out
53
AIM: Write a program to test the validity of a simple expression involving operators +, -,*
and /.
Code:
%{
#include<stdio.h>
#include<stdlib.h>
%}
%token NUMBER ID NL
%left '+' '-'
%left '*' '/'
%%
stmt: exp NL {printf("valid expressionn"); exit(0);}
;
exp: exp '+' exp | exp '-' exp | exp '*' exp | exp '/' exp | '(' exp ')' | ID | NUMBER
;
%%
int yyerror(char *msg)
{
printf("Invalid expressionn");
exit(0);
}
54
main()
{
printf("enter the expression: n");
yyparse();
}
LEX PART:
CODE:(sarith.l)
%{
#include"y.tab.h"
%}
%%
[0-9]+ {return NUMBER;}
[a-zA-Z][a-zA-Z0-9_]* {return ID;}
n {return NL;}
. {return yytext[0];}
%%
55
Output:
yacc -d sarith.y
lex sarith.l
cc y.tab.clex.yy.c-ly-ll
./a.out
enterthe expression:
a+10
validexpression
yacc -d sarith.y
lex sarith.l
cc y.tab.clex.yy.c-ly-ll
./a.out
enterthe expression:
10-
Invalidexpression
56
Questions Answers:
1.
a. Windows
b. DOS
c. Unix
d. OpenSUSE
2. The table is created byYACC.
a. LALRparsing
b. LLparsing
c. GLRparsing
d. None of thementioned
3. The YACC takes C code as input andoutputs
a. Top downparsers
b. Bottom upparsers
c. Machine code
d. None of thementioned
4. Input of Lex is?
a. Set of regularexpression
b. Statement
c. Numeric data
YACC is a computerprogramfor operationsystem.
57
PRACTICAL SET-12
Nested if :
Syntax:
if (condition1){ Statement1; }
else_if(condition2)
{ Statement2; }
else Statement 3;Description:
If condition 1 is false, then condition 2 is checked and statements are
executed if it is true. If condition 2 also gets failure, then else part is executed.
AIM: Write a program to recognize nested IF control statements and display
the levels of nesting.
Code:
LEX CODE
%option noyywrap
%{
#include "y.tab.h"
%}
%%
"if" {return IF;}
[sS][0-9]* {return S;}
"<"|">"|"=="|"<="|">="|"!=" {return RELOP;}
[0-9]+ {return NUMBER;}
[a-z][a-zA-Z0-9_]* {return ID;}
n {return NL;}
. {return yytext[0];}
%%
58
YACC CODE
%{
#include<stdio.h>
#include<stdlib.h>
int count=0;
%}
%token IF RELOP S NUMBER ID NL
%%
stmt: if_stmt NL {printf("No. of nested if statements=%dn",count);exit(0);}
;
if_stmt : IF'('cond')''{'if_stmt'}' {count++;}
|S
;
cond: x RELOP x
;
x:ID | NUMBER
;
%%
int yyerror(char *msg)
{
printf("the statement is invalidn");
exit(0);
}
main()
{
printf("enter the statementn");
yyparse();
}
59
Output:
yacc -d nif.y
lex nif.l
cc y.tab.clex.yy.c-ly-ll
./a.out
enterthe statement
if(a<b){s}
No.of nestedif statements=1
./a.out
enterthe statement
if(a>b){if(a>b){s}}
No.of nestedif statements=2
60
Question Answers:
1. What will be the output of following program?
#include<stdio.h>i
ntmain()
{
int a=10;
if(a==10)
{
printf("Hello...");
break;
printf("Ok");
}
else
{
}
printf("Hii");
61
return 0;
}
a. Hello...
b. Hello...OK
c. OK
d. Error
2. What will be the output of following program
? #include <stdio.h>
int main()
{
if( (-100 && 100)||(20 && -20) )
printf("%s","Condition is true.");
else
printf("%s","Condition is false.");
return 0;
}
a. Condition istrue.
b. Condition isfalse.
c. No output
d. ERROR
62
3. Comment on the following codebelow
#include <stdio.h>
void main(){
int x = 5;
if (true);
printf("hello");
}
a. It will displayhello
b. It will throw anerror
c. Nothing will bedisplayed
d. Compilerdependent
4. The output of the code below
is #include<stdio.h>
void main()
{
int x = 0; if
(x == 0)
Printf("hi");
else
printf("how are
u"); printf("hello");
}
a. hi
63
b. how areyou
c. hello
d. hihello
64
PRACTICAL SET-13
String Recognization
Pattern searching is animportant problem in computer science.When we do searchfor astring
in notepad/word file or browser or database, pattern searching algorithms are used to show
the search results.
Matching Overview
txt = "AAAAABAAABA" pat =
"AAAA"
We compare first window of txt with pat
txt = "AAAAABAAABA"
pat = "AAAA" [Initial position]
We find a match. This is same as Naive String Matching. In the next step, we compare next
window of txt with pat.
txt = "AAAAABAAABA"
pat = "AAAA" [Pattern shifted one position]
This is where KMP does optimization over Naive. In this second window, we only compare fourth
A of pattern with fourth character of current window of text to decide whether current window
matches or not. Since we know first three characters will anyway match, we skipped matching
first three characters.
65
AIM: Write a program to recognize strings ‘aaab’, ‘abbb’, ‘ab’, and ‘a’ using grammar
Code:
LEX PART
YACC PART
%{
#include<stdio.h>
intvalid=1;
%}
%token A B
%%
str:S'n'{return0;}
S:A S B
|
;
%%
main()
{
printf("Enter the string:n");
yyparse();
if(valid==1)
printf("nvalid string");
}
%{
#include "y.tab.h"
%}
%%
a returnA;
b returnB;
.|n returnyytext[0];
%%
66
Output:
$ lex prog5b.l
$ yacc -d prog5b.y
$ cc -c lex.yy.c y.tab.c
$ cc -o a.out lex.yy.o y.tab.o -lfl
$ ./a.out
Enter the string: aaaabb
Invalid string
$ ./a.out
Enter the string: aaabbb
Valid string
67
Question Answers:
1. A language is represented by a regular expression (a)*(a+ ba). Which of the following
string does not belong to the regular set represented by the aboveexpression?
a. aaa
b. aba
c. ababa
d. aa
2. The following production rules of a regular grammar generate a language L: SaS /bS/
a / b The regular expression of Lis
a. a +b
b. (a +b)*
c. (a + b)(a +b)*
d. (aa + bb)a*b*
3. The regular expression (a/b)(a/b) denotes theset
a. { a, b, ab, aa}
b. { a, b, ba, bb}
c. Both a) andb)
d. None ofthese.
4. a*(a+b)* is equivalentto
a. a* +b*
b. a*b*
c. (ab)*
d. None ofthese

More Related Content

What's hot

An introduction to programming
An introduction to programmingAn introduction to programming
An introduction to programmingrprajat007
 
Applying Anti-Reversing Techniques to Machine Code
Applying Anti-Reversing Techniques to Machine CodeApplying Anti-Reversing Techniques to Machine Code
Applying Anti-Reversing Techniques to Machine CodeTeodoro Cipresso
 
CBCS 2018 Scheme I sem Lab Manual for 18CPL17
CBCS 2018 Scheme I sem Lab Manual for 18CPL17 CBCS 2018 Scheme I sem Lab Manual for 18CPL17
CBCS 2018 Scheme I sem Lab Manual for 18CPL17 manjurkts
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & RecoveryAkhil Kaushik
 
Cprogramming tutorial
Cprogramming tutorialCprogramming tutorial
Cprogramming tutorialعمر عبد
 
C interview questions for fresher
C interview questions for fresherC interview questions for fresher
C interview questions for fresherMYTHILIKRISHNAN4
 
C Programming and Coding Standards, Learn C Programming
C Programming and Coding Standards, Learn C ProgrammingC Programming and Coding Standards, Learn C Programming
C Programming and Coding Standards, Learn C ProgrammingTonex
 
265 ge8151 problem solving and python programming - 2 marks with answers
265   ge8151 problem solving and python programming - 2 marks with answers265   ge8151 problem solving and python programming - 2 marks with answers
265 ge8151 problem solving and python programming - 2 marks with answersvithyanila
 
Turbo C
Turbo CTurbo C
Turbo Cnat236
 
Compiler Design(Nanthu)
Compiler Design(Nanthu)Compiler Design(Nanthu)
Compiler Design(Nanthu)guest91cc85
 
Error hanadling in c programming presentation
Error hanadling in c programming presentationError hanadling in c programming presentation
Error hanadling in c programming presentationPranaliPatil76
 
Introduction to C Language (By: Shujaat Abbas)
Introduction to C Language (By: Shujaat Abbas)Introduction to C Language (By: Shujaat Abbas)
Introduction to C Language (By: Shujaat Abbas)Shujaat Abbas
 

What's hot (19)

An introduction to programming
An introduction to programmingAn introduction to programming
An introduction to programming
 
Applying Anti-Reversing Techniques to Machine Code
Applying Anti-Reversing Techniques to Machine CodeApplying Anti-Reversing Techniques to Machine Code
Applying Anti-Reversing Techniques to Machine Code
 
CBCS 2018 Scheme I sem Lab Manual for 18CPL17
CBCS 2018 Scheme I sem Lab Manual for 18CPL17 CBCS 2018 Scheme I sem Lab Manual for 18CPL17
CBCS 2018 Scheme I sem Lab Manual for 18CPL17
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & Recovery
 
Cprogramming tutorial
Cprogramming tutorialCprogramming tutorial
Cprogramming tutorial
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
C interview questions for fresher
C interview questions for fresherC interview questions for fresher
C interview questions for fresher
 
C Programming and Coding Standards, Learn C Programming
C Programming and Coding Standards, Learn C ProgrammingC Programming and Coding Standards, Learn C Programming
C Programming and Coding Standards, Learn C Programming
 
265 ge8151 problem solving and python programming - 2 marks with answers
265   ge8151 problem solving and python programming - 2 marks with answers265   ge8151 problem solving and python programming - 2 marks with answers
265 ge8151 problem solving and python programming - 2 marks with answers
 
Compiler
Compiler Compiler
Compiler
 
Cp week _2.
Cp week _2.Cp week _2.
Cp week _2.
 
Turbo C
Turbo CTurbo C
Turbo C
 
Abc c program
Abc c programAbc c program
Abc c program
 
Compiler Design(Nanthu)
Compiler Design(Nanthu)Compiler Design(Nanthu)
Compiler Design(Nanthu)
 
3.2
3.23.2
3.2
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Error hanadling in c programming presentation
Error hanadling in c programming presentationError hanadling in c programming presentation
Error hanadling in c programming presentation
 
Introduction to C Language (By: Shujaat Abbas)
Introduction to C Language (By: Shujaat Abbas)Introduction to C Language (By: Shujaat Abbas)
Introduction to C Language (By: Shujaat Abbas)
 
Book ppt
Book pptBook ppt
Book ppt
 

Similar to Here are the key points about Lex and Yacc:- Lex and Yacc are tools used to generate parts of a compiler. They help with the front-end processing of the source code.- Lex is used for lexical analysis. It reads the source code and generates tokens. Its job is to break the input into smallest elements called tokens. - Yacc is used for syntactic analysis or parsing. It takes the tokens generated by Lex and recognizes the grammatical structure of the program according to the language syntax rules. - Yacc uses grammar rules defined in Backus-Naur Form (BNF) to analyze the token stream and organize them into a parse tree which represents the hierarchical syntactic structure of the program

ProgFund_Lecture_7_Intro_C_Sequence.pdf
ProgFund_Lecture_7_Intro_C_Sequence.pdfProgFund_Lecture_7_Intro_C_Sequence.pdf
ProgFund_Lecture_7_Intro_C_Sequence.pdflailoesakhan
 
Stnotes doc 5
Stnotes doc 5Stnotes doc 5
Stnotes doc 5Alok Jain
 
Lab Report sample of c programming.docx
Lab Report sample of c programming.docxLab Report sample of c programming.docx
Lab Report sample of c programming.docxbheshchaudhary
 
C and its errors
C and its errorsC and its errors
C and its errorsJunaid Raja
 
Mark asoi ppt
Mark asoi pptMark asoi ppt
Mark asoi pptmark-asoi
 
INTRODUCTION TO C PROGRAMMING MATERIAL.pdf
INTRODUCTION TO C PROGRAMMING MATERIAL.pdfINTRODUCTION TO C PROGRAMMING MATERIAL.pdf
INTRODUCTION TO C PROGRAMMING MATERIAL.pdfSubramanyambharathis
 
PCCF UNIT 1.pptx
PCCF UNIT 1.pptxPCCF UNIT 1.pptx
PCCF UNIT 1.pptxDivyaKS12
 
Introduction to Computer Programming
Introduction to Computer ProgrammingIntroduction to Computer Programming
Introduction to Computer ProgrammingProf. Erwin Globio
 
CS8251_QB_answers.pdf
CS8251_QB_answers.pdfCS8251_QB_answers.pdf
CS8251_QB_answers.pdfvino108206
 
Contents Pre-requisites Approximate .docx
   Contents Pre-requisites  Approximate .docx   Contents Pre-requisites  Approximate .docx
Contents Pre-requisites Approximate .docxShiraPrater50
 
Computer programing 111 lecture 2
Computer programing 111 lecture 2Computer programing 111 lecture 2
Computer programing 111 lecture 2ITNet
 
Chapter 1 - Basic concepts of programming.pdf
Chapter 1 - Basic concepts of programming.pdfChapter 1 - Basic concepts of programming.pdf
Chapter 1 - Basic concepts of programming.pdfKirubelWondwoson1
 
Introduction to systems programming
Introduction to systems programmingIntroduction to systems programming
Introduction to systems programmingMukesh Tekwani
 

Similar to Here are the key points about Lex and Yacc:- Lex and Yacc are tools used to generate parts of a compiler. They help with the front-end processing of the source code.- Lex is used for lexical analysis. It reads the source code and generates tokens. Its job is to break the input into smallest elements called tokens. - Yacc is used for syntactic analysis or parsing. It takes the tokens generated by Lex and recognizes the grammatical structure of the program according to the language syntax rules. - Yacc uses grammar rules defined in Backus-Naur Form (BNF) to analyze the token stream and organize them into a parse tree which represents the hierarchical syntactic structure of the program (20)

Module 1 2 just basic-
Module 1 2  just basic-Module 1 2  just basic-
Module 1 2 just basic-
 
ProgFund_Lecture_7_Intro_C_Sequence.pdf
ProgFund_Lecture_7_Intro_C_Sequence.pdfProgFund_Lecture_7_Intro_C_Sequence.pdf
ProgFund_Lecture_7_Intro_C_Sequence.pdf
 
Stnotes doc 5
Stnotes doc 5Stnotes doc 5
Stnotes doc 5
 
Lab Report sample of c programming.docx
Lab Report sample of c programming.docxLab Report sample of c programming.docx
Lab Report sample of c programming.docx
 
C and its errors
C and its errorsC and its errors
C and its errors
 
Types of errors 2019
Types of errors 2019Types of errors 2019
Types of errors 2019
 
PROBLEM SOLVING
PROBLEM SOLVINGPROBLEM SOLVING
PROBLEM SOLVING
 
Mark asoi ppt
Mark asoi pptMark asoi ppt
Mark asoi ppt
 
INTRODUCTION TO C PROGRAMMING MATERIAL.pdf
INTRODUCTION TO C PROGRAMMING MATERIAL.pdfINTRODUCTION TO C PROGRAMMING MATERIAL.pdf
INTRODUCTION TO C PROGRAMMING MATERIAL.pdf
 
PCCF UNIT 1.pptx
PCCF UNIT 1.pptxPCCF UNIT 1.pptx
PCCF UNIT 1.pptx
 
Introduction to Computer Programming
Introduction to Computer ProgrammingIntroduction to Computer Programming
Introduction to Computer Programming
 
CS8251_QB_answers.pdf
CS8251_QB_answers.pdfCS8251_QB_answers.pdf
CS8251_QB_answers.pdf
 
C tutorials
C tutorialsC tutorials
C tutorials
 
Contents Pre-requisites Approximate .docx
   Contents Pre-requisites  Approximate .docx   Contents Pre-requisites  Approximate .docx
Contents Pre-requisites Approximate .docx
 
TYPES OF ERRORS.pptx
TYPES OF ERRORS.pptxTYPES OF ERRORS.pptx
TYPES OF ERRORS.pptx
 
Computer programing 111 lecture 2
Computer programing 111 lecture 2Computer programing 111 lecture 2
Computer programing 111 lecture 2
 
Cp 111 lecture 2
Cp 111 lecture 2Cp 111 lecture 2
Cp 111 lecture 2
 
Switch case looping
Switch case loopingSwitch case looping
Switch case looping
 
Chapter 1 - Basic concepts of programming.pdf
Chapter 1 - Basic concepts of programming.pdfChapter 1 - Basic concepts of programming.pdf
Chapter 1 - Basic concepts of programming.pdf
 
Introduction to systems programming
Introduction to systems programmingIntroduction to systems programming
Introduction to systems programming
 

Recently uploaded

High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...ranjana rawat
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 

Recently uploaded (20)

High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 

Here are the key points about Lex and Yacc:- Lex and Yacc are tools used to generate parts of a compiler. They help with the front-end processing of the source code.- Lex is used for lexical analysis. It reads the source code and generates tokens. Its job is to break the input into smallest elements called tokens. - Yacc is used for syntactic analysis or parsing. It takes the tokens generated by Lex and recognizes the grammatical structure of the program according to the language syntax rules. - Yacc uses grammar rules defined in Backus-Naur Form (BNF) to analyze the token stream and organize them into a parse tree which represents the hierarchical syntactic structure of the program

  • 1. Silver Oak Group of Institutes Silver Oak College of Engineering & Technology Aditya Silver Oak Institute of Technology GUJARAT TECHNOLOGICAL UNIVERSITY BACHELOR OF ENGINEERING COMPILER DESIGN (3170701) COMPUTER ENGINEERING / INFORMATION TECHNOLOGY DEPARTMENT
  • 2.
  • 3. ii COMPILER DESIGN PRACTICAL BOOK DEPARTMENT OF COMPUTER ENGINEERING PREFACE It gives us immense pleasure to present the first edition of Compiler Design Practical Book for the B. Tech. 4th year students for SILVER OAK GROUP OFINSTITUTES. The Compiler Design theory and laboratory course at SILVER OAK GROUP OF INSTITUTES, AHMEDABAD is designed in such a way that students develop the basic understanding of the Compiler Design and various applications in the theory classes and then try their hands on the experiments to realize the various aspects of Compiler Design. This laboratory course is intended to make the students experiment on the basic techniques of compiler construction and tools that can used to perform syntax-directed translation of a high- level programming language into an executable code. Students will design and implement language processors in C by using tools to automate parts of the implementation process. This will provide deeper insights into the more advanced semantics aspects of programming languages, code generation, machine independent optimizations, dynamic memory allocation, and objectorientation Hopefully this Compiler Design Practical Book will serve the purpose for which it has been developed. Lab Manual Revisedby:Prof.Monali Suthar, SilverOakCollege of EngineeringandTechnology Prof. Jalpa Shah, Aditya Silver Oak Institute of Technology Lab Manual Revision No.: SOGI_2170701_LM_2019_1
  • 4. iii INSTRUCTIONS TO STUDENTS 1. While entering into the LAB students should wear their ID cards and arrange your shoes in properway. 2. All the students must come with the lab manual in every practicalsession. 3. Students have to maintain their lab manual on regular basis(it means evaluation of practical will be doneweekly) 4. Questions given in the lab manual must be attempted and solved on the givendate. 5. Feedback and conclusion section of each practical must be filled with utmostsincerity. 6. Lab manual must be filled only after performing thepractical. 7. Students should maintain decorum inside thelaboratory. 8. All the observations have to be neatly recorded in the Practical Class Notebook and verified by the instructor before leaving thelaboratory. 9. Write the answers of all the questions at the end of each experiment in the PracticalBook. 10. Lab Manual records need to be submitted on or before date ofsubmission. 11. The grades for the Practical course work will be awarded based on your performance in the laboratory, regularity, obtaining of experiments results, labquiz, regular viva-voice and end-term examination. 12. Students are restricted to not to use mobile phones inside thelaboratory. 13. After completing the laboratory exercise make sure to shut down the systemand switch off all the amities properly. Also rearrange the chair in the properway.
  • 5. iv CERTIFICATE This is to certify that Mr. Dhaval r tukadiya withenrolmentno.180770107240, Semester 7 DIV-D has successfully completed his laboratory experiments in the Compiler Design (2170701) from the department of Computer Engineering during the academic year ...2020............ -..2021..... Date ofSubmission:..5-10-2020....................... Staff in charge:........................... Head of Department: ...........................................
  • 6. v TABLE OF CONTENT Sr. No Experiment Title Page No Date of Start Date of Completion Sign Marks (out of 10) From To 1 To study the various types of errors in compiler. 1 4 2 Study of Lex/Yacc of Compiler Writing. 5 8 3 Write a Program to check whether a string belongs to grammar or not. 9 13 4 Write a Program to check whether a string belongs to grammar or not. 14 18 5 Write a program to compute FIRST of non-terminals 19 24 6 Write a program to check whether a string is keyword and constant. 25 28
  • 7. vi 7 Write a program to count number of new lines and white spaces. 29 30 8 Write a program to count the number of vowels and consonants in a given string. 31 33 9 Write a program to count number of characters, words, spaces, end of lines in a given input file. 34 37
  • 8. vi 10 Write a program to recognize whether a given sentence is simple 30/9/14 or compound. 38 40 11 Write a program to test the validity of a simple expression involving operators +, -,* and /. 41 43 12 Write a program to recognize nested IF control statements and display the levels of nesting. 44 47 13 Write a program to recognize strings ‘aaab’, ‘abbb’, ‘ab’, and ‘a’ using grammar. 48 51
  • 9. 1 PRACTICAL SET-1 Types of program errors We distinguish between the following types of errors: 1. Syntax errors: errors due to the fact that the syntax of the language is notrespected. 2. Semantic errors: errors due to an improper use of programstatements. 3. Logical errors: errors due to the fact that the specification is notrespected. From the point of view of when errors are detected, we distinguish: 1. Compile time errors: syntax errors and static semantic errors indicated by thecompiler. 2. Runtime errors: dynamic semantic errors, and logical errors, that cannot be detected by the compiler(debugging).
  • 10. 2 AIM: To study the various types of errors in compiler. // C program to illustrate // syntax error #include<stdio.h> void main() { int x = 10; int y = 15; printf("%d", (x, y)) // semicolon missed } Error: error: expected ';' before '}' token Syntax of a basic construct is written wrong. For example : while loop // C program to illustrate // syntax error #include<stdio.h> int main(void) { // while() cannot contain "." as an argument. while(.) { printf("hello"); } return 0;
  • 11. 3 } Error: error: expected expression before '.' token while(.) In the given example, the syntax of while loop is incorrect. This causes a syntax error. Run-time Errors: Errors which occur during program execution(run-time) after successful compilation are called run-time errors. One of the most common run-time error is division by zero also known as Division error. These types of error are hard to find as the compiler doesn’t point to the line at which the error occurs. For more understanding run the example given below. // C program to illustrate // run-time error #include<stdio.h> void main() { int n = 9, div = 0; // wrong logic // number is divided by 0, // so this program abnormally terminates div = n/0; printf("resut = %d", div); } Error: warning: division by zero [-Wdiv-by-zero] div = n/0;
  • 12. 4 In the given example, there is Division by zero error. This is an example of run-time error i.e errors occurring while running the program. Linker Errors: These error occurs when after compilation we link the different object files with main’s object using Ctrl+F9 key(RUN). These are errors generated when the executable of the program cannot be generated. This may be due to wrong function prototyping, incorrect header files. One of the most common linker error is writing Main() instead of main(). // C program to illustrate // linker error #include<stdio.h> void Main() // Here Main() should be main() { int a = 10; printf("%d", a); } Error: (.text+0x20): undefined reference to `main' Logical Errors : On compilation and execution of a program, desired output is not obtained when certain input values are given. These types of errors which provide incorrect output but appears to be error free are called logical errors. These are one of the most common errors done by beginners of programming. These errors solely depend on the logical thinking of the programmer and are easy to detect if we follow the line of execution and determine why the program takes that path of execution. // C program to illustrate // logical error
  • 13. 5 int main() { int i = 0; // logical error : a semicolon after loop for(i = 0; i < 3; i++); { printf("loop "); continue; } getchar(); return 0; } No output Semantic errors: This error occurs when the statements written in the program are not meaningful to the compiler. // C program to illustrate // semantic error void main() { int a, b, c; a + b = c; //semantic error } Error Error: lvalue required as left operand of assignment a + b = c; //semantic error
  • 14. 6 Question Answers: 1. Consider the following C declaration. int x[10], y,z; What kind of error does following statement contain? z == x+y; a. Syntaxerror b. Semantic error c. Logicalerror d. All of theabove 2. Write down types of errorsgenerated. Logical error Semantic error Syntax error 3. All syntax errors and some of the semantic errors (the static semantic errors) are detected bythe Compiler. 4. Errors that occur when you violate the rules of writing C/C++ syntax are knownas Syntax error .
  • 15. 7 5. Error is an illegal operation performed by the user which results in abnormal working of the program. State TRUE orFALSE. TRUE
  • 16. 8 PRACTICAL SET-2 Lex/Yacc compiler A compiler or interpreter for a programming language is often decomposed into two parts: 1. Read the source program and discover itsstructure. 2. Process this structure, e.g. to generate the target program. Lex and Yacc can generate program fragments that solve the first task. The task of discovering the source structure again is decomposed into subtasks: 1. Split the source file intotokens(Lex). 2. Find the hierarchical structure of the program(Yacc).
  • 17. 9 AIM: Study of Lex/Yacc of Compiler Writing. A compiler or interpreter for a programming language is often decomposed into two parts: 1. Read the source program and discover its structure. 2. Process this structure, e.g. to generate the target program. Lex and Yacc can generate program fragments that solve the first task. The task of discovering the source structure again is decomposed into subtasks: 1. Split the source file into tokens (Lex). 2. Find the hierarchical structure of the program (Yacc). Description: Some of the most time consuming and tedious parts of writing a compiler involve the lexical scanning and syntax analysis. Luckily there is freely available software to assist in these functions. While they will not do everything for you, they will enable faster implementation of the basic functions. Lex and Yacc are the most commonly used packages with Lex managing the token recognition and Yacc handling the syntax. They work well together, but conceivably can be used individually as well. Both operate in a similar manner in which instructions for token recognition or grammar are written in a special file format. The text files are then read by lex and/or yacc to produce c code. This resulting source code is compiled to make the final application. In practice the lexical instruction file has a “.l” suffix and the grammar file has a “.y” suffix. This process is shown in Figure 1. Figure 1.Lex and Yacc Process (based on a diagram on page 5 of “A Compact Guide to Lex & Yacc” by Thomas Niemann) The file format for a lex file consists of (4) basic sections • The first is an area for c code that will be place verbatim at the beginning of the generated source code. Typically is will be used for things like #include, #defines, and variable declarations. • The next section is for definitions of token types to be recognized. These are not mandatory, but in general makes the next section easier to read and shorter. • The third section set the pattern for each token that is to be recognized, and can also include c code to be called when that token is identified
  • 18. 10 • The last section is for more c code (generally subroutines) that will be appended to the end of the generated c code. This would typically include a main function if lex is to be used by itself. • The format is applied as follows (the use and placement of the % symbols are necessary): % { //header c code %} //definitions %% //rules %% //subroutines The format for a yacc file is similar, but includes a few extras. • The first area (preceded by a %token) is a list of terminal symbols. You do not need to list single character ASCII symbols, but anything else including multiple ASCII symbols need to be in this list (i.e. “==”). • The next is an area for c code that will be place verbatim at the beginning of the generated source code.Typically is will be used for things like #include, #defines, and variable declarations. • The next section is for definitions - none of the following examples utilize this area • The fourth section set the pattern for each token that is to be recognized, and can also include c code to be called when that token is identified • The last section is for more c code (generally subroutines) that will be appended to the end of the generated c code. This would typically include a main function if lex is to be used by itself. • The format is applied as follows (the use and placement of the % symbols are necessary): %tokens RESERVED, WORDS, GO, HERE %{ //header c code %} //definitions %%
  • 19. 11 //rules %% //subroutines These formats and general usage will be covered in greater detail in the following (4) sections. In general it is best not to modify the resulting c code as it is overwritten each time lex or yacc is run. Most desired functionality can be handled within the lexical and grammar files, but there are some things that are difficult to achieve that may require editing of the c file.
  • 20. 12 Question Answers: 1. YACC is a computerprogramfor operationsystem . b. DOS c. Unix d. d. openSUSE 2. Input of Lexis a. Set to regular expression b. Statement c. Numeric data d. ASCII data 3. Which of the following software tool is parser generator? a.Lex b. Yacc c. Both of the mentioned d. None of thementioned a.Windows
  • 21. 13 4. 5. Yacc semantic action is a sequence of C Program State TRUE or FALSE. TRUE The YACC takes C code as inputandoutputsfile whoch contain souce code that can be combined into actual parser. .
  • 22. 14 PRACTICAL SET-3 Grammar Definition − A context-free grammar (CFG) consisting of a finite set of grammar rules is a quadruple (N, T, P, S) where N is a set of non-terminalsymbols.  T is a set of terminals where N ∩ T =NULL.  P is a set of rules, P: N → (N ∪T)*, i.e., the left-hand side of the production rule P does have any right context or leftcontext. S is the startsymbol. Example  The grammar ({A}, {a, b, c}, P, A), P : A → aA, A→abc.  The grammar ({S, a, b}, {a, b}, P, S), P: S → aSa, S → bSb, S→ε  The grammar ({S, F}, {0, 1}, P, S), P: S → 00S | 11F, F → 00F|ε The set of all strings that can be derived from a grammar is said to be the language generated from that grammar. A language generated by a grammar G is a subset formally definedby  L(G)={W|W ∈ ∑*, S ⇒GW} If L(G1) = L(G2), the Grammar G1 is equivalent to the Grammar G2.Example If there is a grammar G: N = {S, A, B} T = {a, b} P = {S → AB, A → a, B → b}
  • 23. 15 Here S produces AB, and we can replace A by a, and B by b. Here, the only accepted string is ab, i.e., L(G) = {ab} Example Suppose we have the following grammar − G: N = {S, A, B} T = {a, b} P = {S → AB, A → aA|a, B → bB|b} The language generated by this grammar − L(G) = {ab, a2b, ab2, a2b2, ………} = {am bn | m ≥ 1 and n ≥ 1}
  • 24. 16 AIM: Write a Program to check whether a string belongs to grammar ornot. Code: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char string[50]; int flag,count=o; clrscr(); printf("The grammar is: S->aS, S->Sb, S->abn"); printf("Enter the string to be checked:n"); gets(string); if(string[0]=='a') { flag=0; for (count=1;string[count-1]!='0';count++) { if(string[count]=='b') { flag=1; continue; } else if((flag==1)&&(string[count]=='a')) { printf("The string does not belong to the specified grammar"); break; } else if(string[count]=='a') continue; else if(flag==1)&&(string[count]='0')) { printf("String accepted…..!!!!"); break; } else { printf("String not accepted"); } } } getch(); }
  • 25. 17 Output: The grammar is s-> as s->sb s->ab Enter the string to be checked: aab String accecpted…!!!
  • 26. 18 Question Answers: 1. Which of the following statement isfalse? a. Context free language is the subset of context sensitive language b. Regular language is the subset of context sensitivelanguage c. Recursively ennumerable language is the super set of regular language d. Context sensitive language is a subset of context free language 2. The minimum number of productions required to produce a language consistingof palindrome strings over ∑={a,b}is a. 3 b. 7 c. 5 d. 6 3. Find a regular expression for a grammar which generates a language which states: L contains a set of strings starting wth an a and ending with a b, with something in the middle. a. a(a*Ub*) bb. a*(aUb)b* c.a(a*b*)b d. None of the mentioned
  • 27. 19 4. Which of the following is the correct representation of grammar for the given regular expression?a(aUb)*b a. S →aMb M → e M → aM M →bM b. (1) S →aMb (2) M →Mab (3) M →aM (4) M → bM c. (1) S →aMb (2) M →e (3) M →aMb (4) M → bMa d. None of thementioned 5. Define of Context freeGrammar. Context-free grammars(CFGs) are usedtodescribe context-free languages.A context-freegrammarisa setofrecursive rulesusedtogenerate patterns of strings ....CFG's are usedto describe programminglanguagesandparserprograms in compilerscanbe generatedautomaticallyfromcontext-free grammars.
  • 28. 20 PRACTICAL SET-4 Parse Tree: A parse tree is an entity which represents the structure of the derivation of aterminal string from some non-terminal (not necessarily the start symbol). The definition is as in the book. Key features to define are the root ∈ V and yield ∈Σ* of each tree.  For each σ ∈Σ, there is a tree with root σ and no children; its yield isσ  For each rule A → ε, there is a tree with root A and one child ε; its yieldisε  If t1, t2, ..., tn are parse trees with roots r1, r2, ..., rn and respective yields y1, y2, ..., yn, and A → r1r2...rn is a production, then there is a parse tree with root A whose children are t1, t2, ..., tn. Its root is A and its yield isy1y2...yn Observe that parse trees are constructed from bottom up, not top down. The actual construction of "adding children" should be made more precise, but we intuitively know what's going on. As an example, here are all the parse (sub) trees used to build the parse tree for the arithmetic expression 4 + 3 * 2 using the expression grammar E → E + T | E - T | T T → T * F | F F → a | ( E ) where a represents an operand of some type, be it a number or variable. The trees are grouped by height. The following code is supposed to generate a parse tree of the input expression, but the problem is that the output E, T, F, S (functions used in the code).
  • 29. 21 AIM: Write a Program to generate a parse tree. Code: #include <stdio.h> #include <stdlib.h> #include <ctype.h> char next; void E(void);void T(void); void S(void);void F(void); void error(int);void scan(void); void enter(char); void leave(char); void spaces(int); int level = 0; //The main should always be very simple //First scan the string //second checkfor end of string reached , if yes success and if not error. //P ---> E '#' int main(void){ printf("Input:"); scan(); E(); if (next != '#') error(1); else printf("***** Successful parse *****n"); }
  • 30. 22 //E ---> T {('+'|'-') T} void E(void){ enter('E'); T(); while (next == '+' || next == '-') { scan(); T(); } leave('E'); } //T ---> S {('*'|'/') S} void T(void) { enter('T'); S(); while (next == '*' || next == '/') { scan(); S(); } leave('T'); } //S ---> F '^' S | F void S(void) { enter('S'); F(); if (next == '^') { scan(); S(); } leave('S');}
  • 31. 23 //F ---> char | '(' E ')' void F(void) { enter('F'); if (isalpha(next)) { scan(); } else if (next == '(') { scan(); E(); if (next == ')') scan(); else error(2); } else { error(3); } leave('F'); } //Scan the entire input void scan(void){ while (isspace(next = getchar())); } void error(int n)
  • 32. 24 { printf("n*** ERROR: %in", n); exit(1); } void enter(char name) { spaces(level++); printf("+-%cn", name); } void leave(char name) { spaces(--level); printf("+-%cn", name); } //TO display the parse tree void spaces(int local_level) { while (local_level-- > 0) printf("| "); }
  • 33. 25 Question Answers: 1. Which among the following is the root of the parse tree? a. Production P b. Terminal T c. Variable V d. Starting Variable S 2. A grammar with more than one parse tree is called: a.Unambiguous b. Ambiguous c. Regular d. None of the mentioned 3. Directed Acyclic graphs is the acyclic graphical representation of agrammar. 4. Grammar is ambiguous if a. it has 2 parsetrees b. it has two left mostderivations c. it has two right most derivations d. All of theabove 5. Draw parse tree from given grammar for 3*2+5string. E -> I E -> E + EE -> E * EE-> (E) I -> ε | 0 | 1 | … | 9
  • 34. 26 PRACTICAL SET-5 First of NonTerminals The rules for finding FIRST of a given grammar is: If X is terminal, FIRST(X) = {X}. If X → ε is a production, then add ε to FIRST(X). If X is a non-terminal, and X → Y1 Y2 … Yk is a production, and ε is in all of FIRST(Y1), …, FIRST(Yk), then add ε to FIRST(X). If X is a non-terminal, and X → Y1 Y2 … Yk is a production, then add a to FIRST(X) if for some i, a is in FIRST(Yi), and ε is in all of FIRST(Y1), …, FIRST(Yi-1). Assumptions: Each Non-terminal character is represented by one Uppercase letter. Each Terminal character is represented by one lowercase letter. LHS and RHS of each production will be separated by a "=" symbol. There will be no Blank space within a production string. Assumed that Left recursion is avoided in all input productions.
  • 35. 27 AIM: Write a program to compute FIRST of non-terminals Code: #include<stdio.h> #include<conio.h> char array[10][20],temp[10]; int c,n; void fun(int,int[]); int fun2(int i,int j,int p[],int ); void main() { int p[2],i,j; printf("Enter the no. of productions :"); scanf("%d",&n); printf("Enter the productions :n"); for(i=0;i<n;i++) scanf("%s",array[i]);
  • 36. 28 for(i=0;i<n;i++) { c=-1,p[0]=-1,p[1]=-1; fun(i,p); printf("First(%c) : [ ",array[i][0]); for(j=0;j<=c;j++) printf("%c,",temp[j]); printf("b ].n"); getch(); } } int fun2(int i,int j,int p[],int key) { int k; if(!key) { for(k=0;k<n;k++) if(array[i][j]==array[k][0]) break;
  • 37. 29 p[0]=i;p[1]=j+1; fun(k,p); return 0; } else { for(k=0;k<=c;k++) { if(array[i][j]==temp[k]) break; } if(k>c)return 1; else return 0; } } void fun(int i,int p[]) { int j,k,key; for(j=2;array[i][j] != NULL; j++)
  • 38. 39 { if(array[i][j-1]=='/') { if(array[i][j]>= 'A' && array[i][j]<='Z') { key=0; fun2(i,j,p,key); } else { key = 1; if(fun2(i,j,p,key)) temp[++c] = array[i][j]; if(array[i][j]== '@'&& p[0]!=-1) //taking '@' as null symbol { if(array[p[0]][p[1]]>='A' && array[p[0]][p[1]] <='Z') { key=0; fun2(p[0],p[1],p,key);
  • 39. 31 Output: } else if(array[p[0]][p[1]] != '/'&& array[p[0]][p[1]]!=NULL) { if(fun2(p[0],p[1],p,key)) temp[++c]=array[p[0]][p[1]]; } } } } } }
  • 40. 32 Question Answers: 1. What is terminal in grammar? How can you identify it in thegivengrammar? A terminal is a symbol which does not appear on the left-hand side of any production. A grammar contains a set of terminal symbols (tokens) such as the plus sign, +, the times sign, *, and other tokens defined by the lexical analyzer such as Identifiers. Eg: E- E+T E- a T - b Here a,b,+ are terminals. 2. What is non-terminal in grammar? How can you identify it in the givengrammar? Explain withexample. Non-terminals are syntactic variables that denote sets of strings. The non- terminals define sets of strings that help define the language generated by the grammar. A set of tokens, known as terminal symbols (Σ). Terminals are the basic symbols from which strings are formed Eg: E- E+T E- a T - b Here E,T are non terminals 3. If you find NULL or ε in the grammar then whatwill be the procedure forfindingtheFIRST? It will be directlyincludedinfirsttable
  • 41. 33 4. Find the FIRST of the givengrammar. S ->cAd A -> bc|a S: c A: a,b 5. Find FIRST of the givengrammar S→ABa /bCA A→cBCD / є B→CdA / ad C→eC / є D→bsf / a Ans: First of s: b,c,e First of A: c, Ƹ First of B: a,e,Ƹ First of C: e ,Ƹ First of D : a,b
  • 42. 34 PRACTICAL SET-6 Constants in C As the name suggests the name constants is given to such variables or values in C programming language which cannot be modified once they are defined. They are fixed values in a program. There can be any types of constants like integer, float, octal, hexadecimal, character constants etc. Every constant has some range. The integers that are too big to fit into an int willbe taken as a long. Now there are various ranges that differ from unsigned to signed bits. Under signed bit the range of an int, varies from -128 to +127 and under unsigned bit int varies from 0to255. How to define constants? In C program we can define constants in two ways as shown below: 1. using #define preprocessordirective 2. Using a constkeyword. Keywords in C Keywords are specific reserved words in C each of which has a specific feature associated with it. Almost all of the words which help us use the functionality of the C language are included in the list of keywords. So you can imagine that the list of keywords is not going to be a small one! There are a total of 32 keywords in C: auto, break, case, char, const, continue, default , do, double, else, enum, extern , float , for, goto, if, int , long , register ,return ,short, signed, sizeof, static , struct ,switch, typedef ,union ,unsigned , void, volatile , while
  • 43. 35 AIM : Write a program to check whether a string is keyword and constant. Code: #include <stdio.h> #include <string.h> int main() { char keyword[32][10]={ "auto","double","int","struct","break","else","long", "switch","case","enum","register","typedef","char", "extern","return","union","const","float","short", "unsigned","continue","for","signed","void","default", "goto","sizeof","voltile","do","if","static","while" } ; char str[]="which"; int flag=0,i; for(i = 0; i < 32; i++) { if(strcmp(str,keyword[i])==0) { flag=1; } } if(flag==1) printf("%s is a keyword",str); else printf("%s is not a keyword",str); } Output: Which is not a keyword
  • 44. 36 Question Answers: 1. Use of “struct”keyword. struct is used to declare a new data-type. Basically this means grouping variables together. For example, a struct data type could be used to declare the format of the file. 2. Which keyword is used for define decimalvalues? Float is used. 3. Why we need constant in C? Explain withexample. Constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are enumeration constants as well. 4. Use of “sizeof” keyword. The sizeof operator is the most common operator in C. It is a compile-time unary operator and used tocompute the size of its operand. It returns the size of a variable......When sizeof() is used with the data types, it simply returns the amount of memory allocated to that data type. 5. Which one of the following is not a reserved keyword for C? a.Auto b.Case c.Main d.default
  • 45. 37 PRACTICAL SET-7 Lexical Analysis Lexical analysis is the first phase of a compiler. It takes the modified source code from language preprocessors that are written in the form of sentences. The lexical analyzer breaks these syntaxes into a series of tokens, by removing any whitespace or comments in the sourcecode. AIM: Write a program to count number of new lines and white spaces. Code: #include<stdio.h> #include<stdlib.h> void main() { FILE *fp; char ch; int character = 0, space = 0, tab = 0, line = 0; fp = fopen("/home/tusharsoni/Desktop/TestFile","r"); if(fp == NULL) { printf("File Not Foundn"); exit(1); } else { while(1) { ch = fgetc(fp); if(ch == EOF) { break; } character++; if(ch == ' ') space++; else if(ch == 't') tab++; else if(ch == 'n') line++; }
  • 46. 38 } fclose(fp); printf("nNumber of Characters = %dn", character); printf("nNumber of Tabs = %dn", tab); printf("nNumber of New Lines = %dn", line); printf("nNumber of Spaces = %dn", space); Output:
  • 47. 39 Question Answers: 1. The number of tokens in the following C statement is printf("i = %d, &i = %x", i,&i); a.3 b.26 c.10 d.21 2. The output of a lexical analyzeris a. A parsetree b. Intermediate code c. Machine code d. A stream ofTokens 3. The number of tokens infollowing program? # define M100 int main ( ) { // declarevariables int n = 2020; return n %M; } Answer:16 4. Number of tokens in the statement: printf(“i=%d,&i=%x”,i,&bi); Answer: Printf(“i=%d,&i=%x”,I,&,I,);
  • 48. 40 5. Count the number of tokens; voidmain() { i/* nt */a=10; return; } Answer:13
  • 49. 41
  • 50. 42 PRACTICAL-8 lex Lexis a programdesigned to generate scanners,also known as tokenizers, which recognize lexical patterns in text. Lex is an acronym that stands for "lexical analyzer generator." It is intended primarily for Unix-based systems. The code for Lex was originally developed by Eric Schmidt and Mike Lesk. FOR COMPILE AND EXECUTE: lex file.l gcc lex.yy.c -ly -ll ./a.out AIM: Write a program to count the numberof vowels and consonants in a givenstring. Code: %{ int vow_count=0; int const_count =0; %} %% [aeiouAEIOU] {vow_count++;} [a-zA-Z] {const_count++;} %% main()
  • 51. 43 { printf("Enter the string of vowels and consonents:"); yylex(); printf("The number of vowels are: %dn",vow); printf("The number of consonants are: %dn",cons); return 0; } Output: Enter the string of vowels and consonents My name is Hina The number of vowels are: 5 The number of consonants are: 7
  • 52. 44 Question Answers: 1. Which of the following is not a feature ofcompiler? a. Scans the entire program first and then translate it into machinecode b. When all the syntax errors are removed execution takesplace c. Slow fordebugging d. Execution time ismore 2. Input of Lexis? a. Set to regularexpression b. Statement c. Numeric data d. ASCIIdata 3. A Lex compilergenerates? a. Lex objectcode b. b.Transitiontables c. CTokens d. None ofabove
  • 53. 45 4. Analysis which determines the meaning of a statement onceitsgrammatical e becomes known is termedas a. Semantic analysis b. Syntaxanalysis c. Regularanalysis d. Generalanalysis structur
  • 54. 46 PRACTICAL SET-9 How to count Word,Character? public int GetNumOfCharsInFile(string filePath) { int count = 0; using (var sr = new StreamReader(filePath)) { while (sr.Read() != -1) count++; } return count; } FOR COMPILE AND EXECUTE: lex file.l gcc lex.yy.c -ly -ll ./a.out
  • 55. 47 AIM: Write a program to count number of characters, words, spaces, end of lines in a given input file. Code: %{ int ch=0,wd=0,l=0,sp=0; %} %% (" ") {sp++;} [n] {l++;} [^ t n]+ {wd++; ch=ch+yyleng;} %% main() { yyin=fopen("kit.txt","r"); yylex(); printf("char=%d t words=%d t spaces=%d t lines=%d",ch,wd,sp,l); } Output: (Content in file) I love program ming. Working with files in C program ming is fun. I am learning C program ming at Codeforwin. (Output) Total characters = 106 Total words = 18 Total lines = 3
  • 56. 48 Question Answers: 1. Inacompiler checks every character of the sourcetext. a. The lexicalanalyzer b. The syntaxanalyzer c. The codegenerator d. The codeoptimizer 2. How many tokens are there in the following Cstatement? a. printf (“j=%d, &j=%x”,j&j) b. 4 c. 5 d. 9 e. 10 3. In a compiler, when is the keyboards of a language arerecognized? a. During the lexical analysis of aprogram b. During parsing of theprogram c. During the codegeneration d. During the data flowanalysis 4. The lexical analysis forJavaneeds _ in a necessary and sufficientsense. a. Turingmachine b. Non-deterministic push downautomata c. Deterministic push downautomata d. Finite stateautomata
  • 57. 49 PRACTICAL SET-10 For Compile And Execute: lex file.l gcc lex.yy.c -ly -ll ./a.out AIM: Write a program to recognize whether a given sentence is simple orcompound. Code: %{ int flag=0; %} %% (""[aA][nN][dD]"")|(""[oO][rR]"")|(""[bB][uU][tT]"") {flag=1;} %% int main() { printf("Enter the sentencen"); yylex(); if(flag==1) printf("nCompound sentencen"); else printf("nSimple sentencen"); return 0; }
  • 58. 50 Output: $./a.out Enter the sentence I am Pooja I am Pooja [Ctrl-d] Simple sentence $./a.out Enter the sentence CSE or ISE CSE or ISE [Ctrl-d] Compound sentence
  • 59. 51 Question Answers: 1. is not an advantage of using shared; dynamically linked libraries as opposed to using statically linkedlibraries. a. Lesser overall page fault rate inthesystem b. Faster programstartup c. Existing programs need not be relinked to take advantage of newerversions d. Smaller sizes of executablefiles 2. The number of derivation trees for the correct answer strings to question 30is a. 4 b. 1 c. 3 d. 2 3. Assuming that the input is scanned in left to right order, while parsing an input string the top-down parseruse a. Rightmostderivation b. Leftmostderivation c. Rightmost derivation that is traced out inreverse d. Leftmost derivation that is traced out inreverse 4. is atop-downparser a. Operator precedenceparser b. An LALR (k)parser c. An LR (k)parser d. Recursive descentparser
  • 60. 52 PRACTICAL SET-11 YACC: YACC program also consists of three sections, "Definitions", "Context Free Grammar and action for each production","Subroutines/Functions". In first section, we can mention C language code which may consist of header files inclusion, globalvariables/Constants definition/declaration. C languagecode can bementioned inbetween the symbols %{ and %}. Alsowe can define tokens in the first section. In above program, NUMBER is the token. We can define the associativity of the operations (i.e. left associativity or right associativity). In above yacc program, we have specified left associativity for all operators. Priorities among the operators can also be specified. It is in the increasing order "from top to bottom". For e.g. in our above yaccprogram, round brackets '(',')' has the higherpriority than '*', '/', '%' which has higher priority than '+', '-'. Operators in the same statement have the same priority. For e.g. in our above program all of the '*', '/', '%' have the samepriority. In second section, we mention the grammar productions and the action for each production. $$ refer to the top of the stack position while $1 for the first value, $2 for the second value in the stack. Third section consists ofthe subroutines. We have to call yyparse() to initiatethe parsing process. yyerror() function is called when all productions in the grammar in second section do not match to the inputstatement. For compile and execute:- yacc -d y2.y lex y2.l cc lex.yy.c y.tab.c -ll ./a.out
  • 61. 53 AIM: Write a program to test the validity of a simple expression involving operators +, -,* and /. Code: %{ #include<stdio.h> #include<stdlib.h> %} %token NUMBER ID NL %left '+' '-' %left '*' '/' %% stmt: exp NL {printf("valid expressionn"); exit(0);} ; exp: exp '+' exp | exp '-' exp | exp '*' exp | exp '/' exp | '(' exp ')' | ID | NUMBER ; %% int yyerror(char *msg) { printf("Invalid expressionn"); exit(0); }
  • 62. 54 main() { printf("enter the expression: n"); yyparse(); } LEX PART: CODE:(sarith.l) %{ #include"y.tab.h" %} %% [0-9]+ {return NUMBER;} [a-zA-Z][a-zA-Z0-9_]* {return ID;} n {return NL;} . {return yytext[0];} %%
  • 63. 55 Output: yacc -d sarith.y lex sarith.l cc y.tab.clex.yy.c-ly-ll ./a.out enterthe expression: a+10 validexpression yacc -d sarith.y lex sarith.l cc y.tab.clex.yy.c-ly-ll ./a.out enterthe expression: 10- Invalidexpression
  • 64. 56 Questions Answers: 1. a. Windows b. DOS c. Unix d. OpenSUSE 2. The table is created byYACC. a. LALRparsing b. LLparsing c. GLRparsing d. None of thementioned 3. The YACC takes C code as input andoutputs a. Top downparsers b. Bottom upparsers c. Machine code d. None of thementioned 4. Input of Lex is? a. Set of regularexpression b. Statement c. Numeric data YACC is a computerprogramfor operationsystem.
  • 65. 57 PRACTICAL SET-12 Nested if : Syntax: if (condition1){ Statement1; } else_if(condition2) { Statement2; } else Statement 3;Description: If condition 1 is false, then condition 2 is checked and statements are executed if it is true. If condition 2 also gets failure, then else part is executed. AIM: Write a program to recognize nested IF control statements and display the levels of nesting. Code: LEX CODE %option noyywrap %{ #include "y.tab.h" %} %% "if" {return IF;} [sS][0-9]* {return S;} "<"|">"|"=="|"<="|">="|"!=" {return RELOP;} [0-9]+ {return NUMBER;} [a-z][a-zA-Z0-9_]* {return ID;} n {return NL;} . {return yytext[0];} %%
  • 66. 58 YACC CODE %{ #include<stdio.h> #include<stdlib.h> int count=0; %} %token IF RELOP S NUMBER ID NL %% stmt: if_stmt NL {printf("No. of nested if statements=%dn",count);exit(0);} ; if_stmt : IF'('cond')''{'if_stmt'}' {count++;} |S ; cond: x RELOP x ; x:ID | NUMBER ; %% int yyerror(char *msg) { printf("the statement is invalidn"); exit(0); } main() { printf("enter the statementn"); yyparse(); }
  • 67. 59 Output: yacc -d nif.y lex nif.l cc y.tab.clex.yy.c-ly-ll ./a.out enterthe statement if(a<b){s} No.of nestedif statements=1 ./a.out enterthe statement if(a>b){if(a>b){s}} No.of nestedif statements=2
  • 68. 60 Question Answers: 1. What will be the output of following program? #include<stdio.h>i ntmain() { int a=10; if(a==10) { printf("Hello..."); break; printf("Ok"); } else { } printf("Hii");
  • 69. 61 return 0; } a. Hello... b. Hello...OK c. OK d. Error 2. What will be the output of following program ? #include <stdio.h> int main() { if( (-100 && 100)||(20 && -20) ) printf("%s","Condition is true."); else printf("%s","Condition is false."); return 0; } a. Condition istrue. b. Condition isfalse. c. No output d. ERROR
  • 70. 62 3. Comment on the following codebelow #include <stdio.h> void main(){ int x = 5; if (true); printf("hello"); } a. It will displayhello b. It will throw anerror c. Nothing will bedisplayed d. Compilerdependent 4. The output of the code below is #include<stdio.h> void main() { int x = 0; if (x == 0) Printf("hi"); else printf("how are u"); printf("hello"); } a. hi
  • 71. 63 b. how areyou c. hello d. hihello
  • 72. 64 PRACTICAL SET-13 String Recognization Pattern searching is animportant problem in computer science.When we do searchfor astring in notepad/word file or browser or database, pattern searching algorithms are used to show the search results. Matching Overview txt = "AAAAABAAABA" pat = "AAAA" We compare first window of txt with pat txt = "AAAAABAAABA" pat = "AAAA" [Initial position] We find a match. This is same as Naive String Matching. In the next step, we compare next window of txt with pat. txt = "AAAAABAAABA" pat = "AAAA" [Pattern shifted one position] This is where KMP does optimization over Naive. In this second window, we only compare fourth A of pattern with fourth character of current window of text to decide whether current window matches or not. Since we know first three characters will anyway match, we skipped matching first three characters.
  • 73. 65 AIM: Write a program to recognize strings ‘aaab’, ‘abbb’, ‘ab’, and ‘a’ using grammar Code: LEX PART YACC PART %{ #include<stdio.h> intvalid=1; %} %token A B %% str:S'n'{return0;} S:A S B | ; %% main() { printf("Enter the string:n"); yyparse(); if(valid==1) printf("nvalid string"); } %{ #include "y.tab.h" %} %% a returnA; b returnB; .|n returnyytext[0]; %%
  • 74. 66 Output: $ lex prog5b.l $ yacc -d prog5b.y $ cc -c lex.yy.c y.tab.c $ cc -o a.out lex.yy.o y.tab.o -lfl $ ./a.out Enter the string: aaaabb Invalid string $ ./a.out Enter the string: aaabbb Valid string
  • 75. 67 Question Answers: 1. A language is represented by a regular expression (a)*(a+ ba). Which of the following string does not belong to the regular set represented by the aboveexpression? a. aaa b. aba c. ababa d. aa 2. The following production rules of a regular grammar generate a language L: SaS /bS/ a / b The regular expression of Lis a. a +b b. (a +b)* c. (a + b)(a +b)* d. (aa + bb)a*b* 3. The regular expression (a/b)(a/b) denotes theset a. { a, b, ab, aa} b. { a, b, ba, bb} c. Both a) andb) d. None ofthese. 4. a*(a+b)* is equivalentto a. a* +b* b. a*b* c. (ab)* d. None ofthese