Lab
Lab
Lab
DEPARTMENT
OF
INFORMATION TECHNOLOGY
LAB FILE
Semester: 6th
LAB MANUAL
Compiler Design (IT-6004) 2019
DEPARTMENT
OF
INFORMATION TECHNOLOGY
LAB FILE
Semester: 6th
LAB MANUAL
Compiler Design (IT-6004) 2019
DEPARTMENT
OF
INFORMATION TECHNOLOGY
LAB FILE
Semester: 6th
LAB MANUAL
Compiler Design (IT-6004) 2019
DEPARTMENT
OF
INFORMATION TECHNOLOGY
LAB FILE
Semester: 6th
LAB MANUAL
Compiler Design (IT-6004) 2019
List of Experiments
S.
Experiments Date Sign Remark
No
LAB MANUAL
Compiler Design (IT-6004) 2019
Experiment No: 1
Study of Lex Tool With Example
Lex is a tool in lexical analysis phase to recognize tokens using regular expression.
• Lex tool itself is a lex compiler.
Use of Lex
• 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.
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.
LAB MANUAL
Compiler Design (IT-6004) 2019
Auxiliary functions This section holds additional functions which are used in actions. These
functions are compiled separately and loaded with lexical analyzer.
Lexical analyzer produced by lex starts its process by reading one character at a time until a
valid match for a pattern is found. Once a match is found, the associated action takes place to
produce token. The token is then given to parser for further processing.
Lookahead Operator
• Lookahead operator is the additional operator that is read by lex in order to distinguish
additional pattern for a token.
• Lexical analyzer is used to read one character ahead of valid lexeme and then retracts
to produce token.
• At times, it is needed to have certain characters at the end of input to match with a
pattern. In such cases, slash (/) is used to indicate end of part of pattern that matches
the lexeme.
Example: In some languages keywords are not reserved. So the statements
IF (I, J) = 5 and IF(condition) THEN
results in conflict whether to produce IF as an array name or a keyword. To resolve this the
lex rule for keyword IF can be written as,
IF/\ (.* \) {
letter }
LAB MANUAL
Compiler Design (IT-6004) 2019
LAB MANUAL
Compiler Design (IT-6004) 2019
Explanation
Process starts with s-closure of initial state 0. After processing all the input symbols, no state
is found as there is no transition out of state 8 on input a. Hence, look for accepting state by
retracting to previous state. From Fig. state 2 which is an accepting state is reached after
reading input symbol a and therefore the pattern a has been matched. At state 8, string aab has
been matched with pattern avb": By Lex rule, the longest matching prefix should be
considered. Hence, action Ag corresponds to pattern p3 will be executed for the string aab.
LAB MANUAL
Compiler Design (IT-6004) 2019
DFAs are also used to represent the output oflex. DFA is constructed from NFA, by
converting all the patterns into equivalent DFA using subset construction algorithm. If there
are one or more accepting NFA states, the first pattern whose accepting state is represented in
each DFA state is determined and displayed as output of DFA state. Process of DFA is
similar to that of NFA. Simulation of DFA is continued until no next state is found. Then
retraction takes place to find the accepting state of DFA. Action associated with the pattern
for that state is executed.
Implementing Lookahead Operator
Lookahead operator r1/r2 is needed because the pattern r1 for a particular token may need to
describe some trailing context r2 in order to correctly identify the actual lexeme.
For the pattern r1/r2, ‘/’ is treated as Ɛ.
If some prefix ab, is recognized by NFA as a match for regular expression then the lexeme is
not ended as NFA reaches the accepting state.
The end of lexeme occurs when NFA enters a state p such that
• p has an Ɛ -transition on I,
• There is a path from start state to state p, that spells out a.
• There is a path from state p to accepting state that spells out b.
• a is as Jong as possible for any ab satisfying conditions 1 - 3.
Figure shows the NFA for recognizing the keyword IF with lookahead. Transition from state
2 to state 3 represents the lookahead operator (-transition).
Accepting state is state 6, which indicates the presence of keyword IF. Hence, the lexeme IF
is found by looking backwards to the state 2, whenever accepting state (state 6) is reached.
LAB MANUAL
Compiler Design (IT-6004) 2019
Experiment No: 2
Program:-
LAB MANUAL
Compiler Design (IT-6004) 2019
LAB MANUAL
Compiler Design (IT-6004) 2019
Experiment No: 3
Program:-
LAB MANUAL
Compiler Design (IT-6004) 2019
Experiment No: 4
Read the input string. Check whether the string is starting with ‘/’ and check next character is
‘/’ or’*’. If condition satisfies print comment. Else not a comment.
PROGRAM:
LAB MANUAL
Compiler Design (IT-6004) 2019
Experiment No: 5
PROGRAM:
LAB MANUAL
Compiler Design (IT-6004) 2019
LAB MANUAL
Compiler Design (IT-6004) 2019
Experiment No: 6
Write a C program to test whether a given identifier is valid or not
RESOURCE: Turbo C++
PROGRAM LOGIC:
Read the given input string. Check the initial character of the string is numerical or any special
character except ‘_’ then print it is not a valid identifier. Otherwise print it as valid identifier if
remaining characters of string doesn’t contains any special characters except ‘_’.
PROGRAM:
LAB MANUAL
Compiler Design (IT-6004) 2019
Experiment No: 7
Read the input string. Using predictive parsing table parse the given input using stack .If stack
[i] matches with token input string pop the token else shift it repeat the process until it reaches
to $.
PROGRAM:
LAB MANUAL
Compiler Design (IT-6004) 2019
LAB MANUAL
Compiler Design (IT-6004) 2019
LAB MANUAL
Compiler Design (IT-6004) 2019
Experiment No: 8
LAB MANUAL
Compiler Design (IT-6004) 2019
LAB MANUAL
Compiler Design (IT-6004) 2019
LAB MANUAL
Compiler Design (IT-6004) 2019
Experiment No: 9
Program:
LAB MANUAL
Compiler Design (IT-6004) 2019
LAB MANUAL
Compiler Design (IT-6004) 2019
LAB MANUAL
Compiler Design (IT-6004) 2019
LAB MANUAL
Compiler Design (IT-6004) 2019
Experiment No: 10
Write a program for computation of first
Program:
LAB MANUAL
Compiler Design (IT-6004) 2019
LAB MANUAL
Compiler Design (IT-6004) 2019
LAB MANUAL
Compiler Design (IT-6004) 2019
Experiment No: 11
PROGRAM:
LAB MANUAL
Compiler Design (IT-6004) 2019
LAB MANUAL