ACD LABORATORY MANUAL (AR17)
Blog: anilkumarprathipati.wordpress.com Page 1
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi, Permanently Affiliated to JNTU Kakinada,
Accredited by NBA & Accredited by NAAC with A grade)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
LAB MANUAL
(In accordance with AR17 syllabus)
SUBJECT : ACD LAB
STREAM : CSE
ACD LABORATORY MANUAL (AR17)
Blog: anilkumarprathipati.wordpress.com Page 2
LIST OF LAB EXERCISES
Sl.
No Name of the Program
Page
Number
1
Design a lexical analyzer for given language and the lexical
analyzer should ignore redundant spaces, tabs and new lines by
using C program. 3
2 Design a lexical analyzer for given language by using ‘lex’ tool. 5
3 Simulate First and Follow of a Grammar. 7
4 Construct a recursive descent parser for an expression 9
5 Construct a LL(1) parser for an expression. 10
6 Implementation of shift reduce parsing algorithm. 12
7 Design a LALR bottom up parser for the given language by
using YACC tool. 14
8 Viva Questions 16
ACD LABORATORY MANUAL (AR17)
Blog: anilkumarprathipati.wordpress.com Page 3
EXPERIMENT-1
Objective:
Design a lexical analyzer for given language and the lexical analyzer should ignore
redundant spaces, tabs and new lines by using C program.
Procedure:
It is the first phase of the compiler. It gets input from the source program and produces
tokens as output.
It reads the characters one by one, starting from left to right and forms the tokens.
Token: It represents a logically cohesive sequence of characters such as keywords,
operators, identifiers, special symbols etc.
o Example: a + b = 20
o Here, a,b,+,=,20 are all separate tokens.
o Group of characters forming a token is called the Lexeme.
The lexical analyzer not only generates a token but also enters the lexeme into the
symbol table if it is not already there.
Its main task is to read the input characters and produce as output a sequence of
tokens that the parser uses for syntax analysis.
Upon receiving a “get next token” command from the parser, the lexical analyzer
reads input characters until it can identify the next token.
Logic / Algorithm:
1. Read the C program as input and stores in a file.
2. Check all the characters from the file from left to right whether character is alphabet or
digit or special symbol.
3. If the input is operator prints as special symbol.
4. If the input is number prints as number.
5. If the input is identifier prints as identifier.
6. If the input is keyword prints as keyword.
Expected Input:
Enter the c program
int main()
{
int a=10,20;
charch;
float f;
}^D
ACD LABORATORY MANUAL (AR17)
Blog: anilkumarprathipati.wordpress.com Page 4
Expected Output:
The numbers in the program are: 10 20
The keywords and identifiers are:
int is a keyword
main is an identifier
int is a keyword
a is an identifier
char is a keyword
ch is an identifier
float is a keyword
f is an identifier
Special characters are ( ) { = , ; ; ; }
Total no. of lines are:5
ACD LABORATORY MANUAL (AR17)
Blog: anilkumarprathipati.wordpress.com Page 5
EXPERIMENT-2
Objective:
Design a lexical analyzer for given language by using ‘lex’ tool.
Procedure:
Lex is a program designed to generate scanners, also known as tokenizes, which
recognize lexical patterns in text. Lex is an acronym that stands for "lexical analyzer
generator." It is intended primarily for Unix-based systems.
Lex is a tool in lexical analysis phase to recognize tokens using regular expression.
Lex tool itself is a lex compiler.
lex.l is an a input file written in a language which describes the generation of lexical
analyzer. The lex compiler transforms lex.l to a C program known as lex.yy.c.
lex.yy.c is compiled by the C compiler to a file called a.out.
The output of C compiler is the working lexical analyzer which takes stream of input
characters and produces a stream of tokens.
yylval is a global variable which is shared by lexical analyzer and parser to return the
name and an attribute value of token.
The attribute value can be numeric code, pointer to symbol table or nothing. Another
tool for lexical analyzer generation is Flex.
Lex program will be in following form structure:
declarations
%%
translation rules
%%
auxiliary functions
Declarations This section includes declaration of variables, constants and regular
definitions.
Translation rules It contains regular expressions and code segments.
Form: Pattern {Action}
Pattern is a regular expression or regular definition.
Action refers to segments of code.
Auxiliary functions This section holds additional functions which are used in actions.
These functions are compiled separately and loaded with lexical analyzer.
ACD LABORATORY MANUAL (AR17)
Blog: anilkumarprathipati.wordpress.com Page 6
Expected Input and Output:
if //Input
if is a keyword //Output
number //Input
number is a identifier //Output
254 //Input
It is a number //Output
<> //Input
it is a relational operator not equal //Output
^D-
ACD LABORATORY MANUAL (AR17)
Blog: anilkumarprathipati.wordpress.com Page 7
EXPERIMENT-3
Objective:
Simulate First and Follow of a Grammar.
E TE’
E’ +TE’ | ϵ
T FT’
T’ *FT’ | ϵ
F (E) | id
Procedure:
The construction of a predictive parser is aided by two functions associated with a
grammar G:
1. FIRST
2. FOLLOW
Rules / Algorithm for FIRST( ):
1. If X is terminal, then FIRST(X) is {X}.
2. If X → ε is a production, then add ε to FIRST(X).
3. If X is non-terminal and X → aα is a production then add a to FIRST(X).
4. If X is non-terminal and X → Y1 Y2…Yk is a production, then place a in FIRST(X) if for
some i, a is in FIRST(Yi), and ε is in all of FIRST(Y1),…,FIRST(Yi-1); that is, Y1,….Yi-
1 => ε. If ε is in FIRST(Yj) for all j=1,2,..,k, then add ε to FIRST(X).
//EAXMPLE
Rules / Algorithm for FOLLOW( ):
1. If S is a start symbol, then FOLLOW(S) contains $.
2. If there is a production A → αBβ, then everything in FIRST(β) except ε is placed in
follow(B).
3. If there is a production A → αB, or a production A → αBβ where FIRST(β) contains ε,
then everything in FOLLOW(A) is in FOLLOW(B).
//EXAMPLE
Expected Input and Output: //FIRST()
How many number of productions?:8
enter the production string like E=E+T
Enter productions Number 1 : E=TX
Enter productions Number 2 : X=+TX
Enter productions Number 3 : X=$
Enter productions Number 4 : T=FY
Enter productions Number 5 : Y=*FY
Enter productions Number 6 : Y=$
Enter productions Number 7 : F=(E)
Enter productions Number 8 : F=i
Find the FIRST of :X
FIRST(X)= { + $ }
press 'y' to continue : Y
Find the FIRST of :F
FIRST(F)= { ( i }
press 'y' to continue : Y
Find the FIRST of :Y
FIRST(Y)= { * $ }
press 'y' to continue : Y
Find the FIRST of :E
FIRST(E)= { ( i }
press 'y' to continue : Y
Find the FIRST of :T
ACD LABORATORY MANUAL (AR17)
Blog: anilkumarprathipati.wordpress.com Page 8
FIRST(T)= { ( i }
press 'y' to continue : N
Expected Input and Output: //FOLLOW()
Enter the no. of productions: 8
enter the production string like E=E+T
Enter productions Number 1 : E=TX
Enter productions Number 2 : X=+TX
Enter productions Number 3 : X=$
Enter productions Number 4 : T=FY
Enter productions Number 5 : Y=*FY
Enter productions Number 6 : Y=$
Enter productions Number 7 : F=(E)
Enter productions Number 8 : F=i
Find FOLLOW of -->X
FOLLOW(X) = { $ ) }
Do you want to continue(Press 1 to continue....)?1
Find FOLLOW of -->E
FOLLOW(E) = {$ ) }
Do you want to continue(Press 1 to continue....)?1
Find FOLLOW of -->Y
FOLLOW(Y) = { + $ ) }
Do you want to continue(Press 1 to continue....)?1
Find FOLLOW of -->T
FOLLOW(T) = { + $ ) }
Do you want to continue(Press 1 to continue....)?1
Find FOLLOW of -->F
FOLLOW(F) = { * + $ ) }
Do you want to continue(Press 1 to continue....)?2
ACD LABORATORY MANUAL (AR17)
Blog: anilkumarprathipati.wordpress.com Page 9
EXPERIMENT-4
Objective:
Construct a recursive descent parser for an expression
Procedure:
Recursive descent parsing is one of the top-down parsing techniques, which uses a set
of recursive procedures to scan its input.
This parsing method may involve backtracking, that is, making repeated scans of the
input.
Logic / Algorithm:
1. Read the input string.
2. Write procedures for the every non-terminal.
3. Verify the next token equals to non-terminals if it satisfies match the non-terminal.
4. If the input string does not match print error message.
Expected Input and Output: 1)
Enter an arithmetic expression :
sum+month*interest
Accepted..!!!
2)
Enter an arithmetic expression :
sum+avg*+interest
Rejected..!!!
ACD LABORATORY MANUAL (AR17)
Blog: anilkumarprathipati.wordpress.com Page 10
EXPERIMENT-5
Objective:
Construct a LL(1) parser for an expression.
Procedure:
Input : A string w and a parsing table M for grammar G.
Output : If w is in L(G), a leftmost derivation of w; otherwise, an error indication.
Method : Initially, the parser has $S on the stack with S, the start symbol of G on top, and w$
in the input buffer. The program that utilizes the predictive parsing table M to produce a parse
for the input is as follows:
set ip to point to the first symbol of
w$; repeat
let X be the top stack symbol and a the symbol pointed to
by ip; if X is a terminal or $ then
if X = a then
pop X from the stack and
advance ip else error()
else /* X is a non-terminal */
if M[X, a] = X →Y1Y2 … Yk then begin
pop X from the stack;
push Yk, Yk-1, … ,Y1 onto the stack, with Y1 on
top;
output the production X → Y1 Y2 . . . Yk
End
until X = $
else error()
/* stack is empty */
Expected Input and Output:
1)
Enter any String(Append with $) i+i*i$
Stack Input Output
$E i+i*i$
$HT i+i*i$ E->TH
$HUF i+i*i$ T->FU
$HUi i+i*i$ F->i
$HU +i*i$ POP
$H +i*i$ U->ε
$HT+ +i*i$ H->+TH
$HT i*i$ POP
$HUF i*i$ T->FU
$HUi i*i$ F->i
$HU *i$ POP
$HUF* *i$ U->*FU
$HUF i$ POP
$HUi i$ F->i
$HU $ POP
$H $ U->ε
$ $ H->ε
Parser Accepted
ACD LABORATORY MANUAL (AR17)
Blog: anilkumarprathipati.wordpress.com Page 11
2)
Enter any String(Append with $)i+i**i$
Stack Input Output
$E i+i**i$
$HT i+i**i$ E->TH
$HUF i+i**i$ T->FU
$HUi i+i**i$ F->i
$HU +i**i$ POP
$H +i**i$ U->ε
$HT+ +i**i$ H->+TH
$HT i**i$ POP
$HUF i**i$ T->FU
$HUi i**i$ F->i
$HU **i$ POP
$HUF* **i$ U->*FU
$HUF *i$ POP
$HU$ *i$ F->$
Parser Rejected
ACD LABORATORY MANUAL (AR17)
Blog: anilkumarprathipati.wordpress.com Page 12
EXPERIMENT-6
Objective:
Implementation of shift reduce parsing algorithm.
Procedure:
Shift-reduce parsing is a type of bottom-up parsing that attempts to construct a
parse tree for an input string beginning at the leaves (the bottom) and working up
towards the root (the top).
Logic / Algorithm:
Shift reduce parsing is a process of reducing a string to the start symbol of a grammar.
Shift reduce parsing uses a stack to hold the grammar and an input tape to hold the stri
Sift reduce parsing performs the two actions: shift and reduce. That's why it is known
as shift reduces parsing.
At the shift action, the current symbol in the input string is pushed to a stack.
At each reduction, the symbols will replaced by the non-terminals. The symbol is the
right side of the production and non-terminal is the left side of the production.
Expected Input and Output:
1)
SHIFT REDUCE PARSER GRAMMER
E->E+E
E->E/E
E->E*E
E->E-E
E->id
enter the input symbol: a+b*c
stack implementation table
stack input symbol action
______ ____________ ______
$ a+b*c$ --
$ a +b*c$ shift a
$E +b*c$ E->a
$E+ b*c$ shift +
$E+b *c$ shift b
$E+E *c$ E->b
$E * c$ E->E+E
$E* c$ shift *
$E*c $ shift c
$E*E $ E->c
$E $ E->E*E
$E $ ACCEPT
ACD LABORATORY MANUAL (AR17)
Blog: anilkumarprathipati.wordpress.com Page 13
2)
SHIFT REDUCE PARSER GRAMMER
E->E+E
E->E/E
E->E*E
E->E-E
E->id
enter the input symbol: a+b*+c
stack implementation table
stack input symbol action
______ ____________ ______
$ a+b*+c$ --
$a +b*+c$ shift a
$E +b*+c$ E->a
$E+ b*+c$ shift +
$E+b *+c$ shift b
$E+E *+c$ E->b
$E *+c$ E->E+E
$E* +c$ shift *
$E*+ c$ shift +
$E*+c $ shift c
$E*+E $ E->c
$E*+E reject
ACD LABORATORY MANUAL (AR17)
Blog: anilkumarprathipati.wordpress.com Page 14
EXPERIMENT-7
Objective:
Design a LALR bottom up parser for the given language by using YACC tool.
Procedure:
Automatically generate a parser (LALR parser) for a context free grammar.
Allows syntax direct translation by writing grammar productions and semantic
actions LALR(1) is more powerful than LL(1).
Algorithm / Rukles:
declarations /* specify tokens, and non-terminals */
%%
translation rules /* specify grammar here */
%%
supporting C-routines
Expected Input and Output:
y.output contains the ouput
1 line : expr '\n'
2 expr : expr '+' term
3 | term
4 term : term '*' factor
5 | factor
6 factor : '(' expr ')'
7 | ID
^L
state 0
$accept : . line $end (0)
ID shift 1
'(' shift 2
. error
line goto 3
exprgoto 4
term goto 5
state 1
factor : ID . (7)
. reduce 7
state 2
factor : '(' . expr ')' (6)
ID shift 1
'(' shift 2
. error
exprgoto 7
term goto 5
factor goto 6
state 3
ACD LABORATORY MANUAL (AR17)
Blog: anilkumarprathipati.wordpress.com Page 15
$accept : line . $end (0)
$end accept
state 4
line :expr . '\n' (1)
expr :expr . '+' term (2)
'\n' shift 8
'+' shift 9
. error
state 5
expr : term . (3)
term : term . '*' factor (4)
'*' shift 10
'\n' reduce 3
'+' reduce 3
')' reduce 3
state 6
term : factor . (5)
. reduce 5
state 7
expr :expr . '+' term (2)
factor : '(' expr . ')' (6)
'+' shift 9
')' shift 11
. error
state 8
line :expr '\n' . (1)
. reduce 1
state 9
expr :expr '+' . term (2)
ID shift 1
'(' shift 2
. error
term goto 12
factor goto 6
state 10
term : term '*' . factor (4)
ID shift 1
'(' shift 2
. error
factor goto 13
state 11
factor : '(' expr ')' . (6)
. reduce 6
state 12
expr :expr '+' term . (2)
ACD LABORATORY MANUAL (AR17)
Blog: anilkumarprathipati.wordpress.com Page 16
term : term . '*' factor (4)
'*' shift 10
'\n' reduce 2
'+' reduce 2
')' reduce 2
state 13
term : term '*' factor . (4)
. reduce 4
8 terminals, 5 nonterminals
8 grammar rules, 14 states
ACD LABORATORY MANUAL (AR17)
Blog: anilkumarprathipati.wordpress.com Page 17
VIVA QUESTIONS
1. What is a compiler?
2. What are the two parts of a compilation?
3. List the subparts or phases of analysis part.
4. Depict diagrammatically how a language is processed?
5. What is linear analysis?
6. Explain the various phases of a compiler?
7. What are the classifications of a compiler?
8. What is a symbol table?
9. Mention some of the cousins of a compiler?
10. List the phases that constitute the front end of a compiler.
11. Mention the back-end phases of a compiler.
12. Define compiler-compiler.
13. List the various compiler construction tools.
14. Differentiate tokens, patterns, and lexeme.
15. List the operations on languages.
16. Write a regular expression for an identifier
17. Mention the various notational short hands for representing regular expressions.
18. What is the function of a hierarchical analysis?
19. What does a semantic analysis do?
20. List the various error recovery strategies for a lexical analysis.
21. Define parser.
22. Mention the basic issues in parsing.
23. Why lexical and syntax analyzers are separated out?
24. Define a context free grammar.
25. Define ambiguous grammar.
26. What is a operator precedence parser?
27. List the properties of LR parser.
28. Mention the types of LR parser.
29. What are the problems with top down parsing?
30. Write the algorithm for FIRST and FOLLOW.
31. List the advantages and disadvantages of operator precedence parsing.
32. What is dangling else problem?
33. What is YACC?
34. What is meant by handle pruning?
35. Define LR(0) items.
36. What is meant by viable prefixes?
37. Define handle.
38. What are kernel & non-kernel items?
39. What is phrase level error recovery?
40. What are the benefits of intermediate code generation?
41. What are the various types of intermediate code representation?
42. Define backpatching.
43. What are the various methods of implementing three address statements?
44. What is Basic block?
45. What is a flow graph?
46. What is a DAG? What are its applications?
47. Define peephole optimization.
48. List the characteristics of peephole optimization.
49. How do you calculate the cost of an instruction?
ACD LABORATORY MANUAL (AR17)
Blog: anilkumarprathipati.wordpress.com Page 18
50. Define symbol table.
51. What do you mean by machine dependent and machine independent optimization?
52. List the different storage allocation strategies.
53. What is dynamic scoping?
54. What is code motion?
55. What are the contents of activation record?