0% found this document useful (0 votes)
24 views35 pages

CD Unit 1

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 35

UNIT I INTRODUCTION TO COMPILERS & LEXICAL ANALYSIS

PART-A
1. Define Compiler (or) What does translator mean?
Compiler is a program that reads a program written in one language –the source language-
and translates it into an equivalent program in another language- the target language. In this
translation process, the complier reports to its user the presence of the errors in the
source program.
2. What is Cross complier? (Nov 21)
There may be a complier which run on one machine and produces the target code for
another machine. Such a complier is called cross complier. Thus by using cross complier
technique platform independency can be achieved.
3. Illustrate diagrammatically how a language is processed. (May 16)

4. What advantages are there to a language-processing system in which the compiler produces
assembly language rather than machine language? (Nov 20)
The compiler may produce an assembly-language program as its output, because assembly
language is easier to produce as output and is easier to debug.
5. State the two main parts of compilation and its function. (May 18) What
are the two parts of a compilation? Explain briefly. (May 16, 17)
The two parts of compilation are:
 Analysis: The analysis part breaks up the source program into constituent pieces and
creates an intermediate representation of the source program. The analysis part is
called the front end of the compiler.
 Synthesis: The synthesis part constructs the desired target program from the
intermediate representation. The synthesis part is called the back end of the compiler.
6. What are the phases included in front end of a compiler? What does the front end produce?
(May 22)
The analysis part is called the front end of the compiler; it includes the phases:
 Lexical analysis
 Syntax analysis
 Semantic analysis
 Intermediate code generation
The analysis part collects information about the source program and stores it in a data
structure called a symbol table, which is passed along with the intermediate representation
to the synthesis part.

St. Mother Theresa Engineering College Page 1 of 26


7. What is Lexical Analysis?
The first phase of compiler is Lexical Analysis or linear analysis in which the stream of
characters making up the source program is read from left-to-right and grouped into tokens
that are sequences of characters having a collective meaning.
8. What is meant by Semantic Analysis?
The semantic analysis phase checks the source program for semantic errors and gathers type
information for the subsequent code generation phase. It uses the hierarchical structure
determined by the syntax-analysis phase to identify the operators and operand of
expressions and statements.

9. What is Symbol Table or Write the purpose of symbol table. (May 14)
List the attributes stored in symbol table. (May 19)
Symbol table is a data structure containing a record for each identifier, with fields for the
attributes of the identifier. These attributes may provide information about the storage
allocated for a name, its type, its scope (where in the program its value may be used), and in
the case of procedure names, such things as the number and types of its arguments, the
method of passing each argument (for example, by value or by reference), and the type
returned. The data structure allows us to find the record for each identifier quickly and to
store or retrieve data from that record quickly.

10. List of some Compiler Construction Tools.


Some commonly used Compiler-Construction Tools include:
 Parser generators  Code generator generators
 Scanner generators  Data-flow analysis engines
 Syntax-directed translation engines  Compiler-construction toolkits
11. With a neat block diagram specify the interactions between the lexical analyser and the parser.
(Nov 20)
State the interaction between the lexical analyser and the parser. (Nov 21)

The interaction is implemented by having the parser call the lexical analyzer. The call,
suggested by the getNextToken command, causes the lexical analyzer to read characters
from its input until it can identify the next lexeme and produce for it the next token,
which it returns to the parser.

St. Mother Theresa Engineering College Page 2 of 26


12. What is the input to lexical analyzer generator? What is its output? (May 22)
The input to lexical analyzer generator is the source program. The main task of the lexical
analyzer is to read the input characters of the source program, group them into lexemes, and
produce as output a sequence of tokens for each lexeme in the source program. The stream of
tokens is sent to the parser for syntax analysis; which is the output of the lexical analyzer.
13. What is the need for separating the analysis phase into lexical analysis and parsing?
 Separation of lexical analysis from syntax allows the simplified design.
 Efficiency of complier gets increased. Reading of input source file is a time consuming
process and if it is been done once in lexical analyzer then efficiency get increased.
Lexical analyzer uses buffering techniques to improve the performances.
 Input alphabet peculiarities and other device specific anomalies can be restricted to
the lexical analyzer.
14. Define Tokens, Patterns and Lexemes. (May 13, 14, 19)
Tokens: A token is a pair consisting of a token name and an optional attribute value. The
token name is an abstract symbol representing a kind of lexical unit, e.g., a particular
keyword, or a sequence of input characters denoting an identifier.
Patterns: A pattern is a description of the form that the lexemes of a token may take. In the
case of a keyword as a token, the pattern is just the sequence of characters that form the
keyword. For identifiers and some other tokens, the pattern is a more complex structure that
is matched by many strings.
Lexemes: A lexeme is a sequence of characters in the source program that matches the pattern
for a token and is identified by the lexical analyzer as an instance of that token.
15. What are the possible error recovery actions in a lexical analyzer? (May 12, 15, 18)
 Panic mode recovery: Delete successive characters from the remaining input until the
lexical analyzer can find a well-formed token. This technique may confuse the parser.
 Other possible error recovery actions:
 Deleting an extraneous characters
 Inserting missing characters.
 Replacing an incorrect character by a correct character.
 Transposing two adjacent characters
16. Why is buffering used in lexical analysis? What are the commonly used buffering methods?
Lexical analyzer scans the sources program line by line. For storing the input string, which is
to be read, the lexical analyzer makes use of input buffer. The lexical analyzer maintains two
pointers: forward and backward pointers for scanning the input string. There are two types
of input buffering schemes- one buffer scheme and two buffer scheme.

17. Draw a transition diagram to represent relational operators.


The relational operators are: <, <=, >, >=, =, !=

St. Mother Theresa Engineering College Page 3 of 26


18. List the rules for constructing regular expressions?
The rules are divided into two major classifications(i) Basic rules (ii) Induction rules
Basic rules:
 ε is a regular expression that denotes {ε} (i.e.) the language contains only an empty
string.
 For each a in ∑, then a is a regular expression denoting {a}, the language with only one
string, which consists of a single symbol a.
Induction rules:
If R and S are regular expressions denoting languages L(R) and L(S) respectively then,
 (R)|(S) is a regular expression denoting L(R) U L(S)
 (R).(S) is a regular expression denoting L(R). L(S)
 (R)* is a regular expression denoting L(R)*

19. What are algebraic properties of regular expressions?


The algebraic law obeyed by regular expressions are called algebraic properties of regular
expression. The algebraic properties are used to check equivalence of two regular expressions.
S.No Properties Meaning
1 r|s=s|r | is commutative
2 r|(s|t)=(r|s)|t | is associative
3 r(st)=(rs)t Concatenation is associative
4 r(s|t)=rs|rt; (s|t) r=sr|tr Concatenation is distributive over |
5 єr=rє=r є is the identity for concatenation
6 r*=(r|є)* є is guaranteed in a closure
7 r* =r* *is idempotent
20. Write a short note on LEX.
An input file, which we call lex.1, is written in the Lex language and describes the lexical
analyzer to be generated. The Lex compiler transforms lex. 1 to a C program, in a file that is
always named lex. yy . c. The latter file is compiled by the C compiler into a file called a. out,
as always. The C-compiler output is a working lexical analyzer that can take a stream of input
characters and produce a stream of tokens.

PART – B
1. Describe the various phases of a compiler and tract it with the program segment (position=initial + rate *
60)(May 16, May 19, Nov 21, May 22)

St. Mother Theresa Engineering College Page 4 of 26


St. Mother Theresa Engineering College Page 5 of 26
St. Mother Theresa Engineering College Page 6 of 26
St. Mother Theresa Engineering College Page 7 of 26
St. Mother Theresa Engineering College Page 8 of 26
St. Mother Theresa Engineering College Page 9 of 26
St. Mother Theresa Engineering College Page 10 of 26
2. Describe the specification of tokens and how to recognize the tokens (May 13)

St. Mother Theresa Engineering College Page 11 of 26


St. Mother Theresa Engineering College Page 12 of 26
St. Mother Theresa Engineering College Page 13 of 26
St. Mother Theresa Engineering College Page 14 of 26
St. Mother Theresa Engineering College Page 15 of 26
St. Mother Theresa Engineering College Page 16 of 26
St. Mother Theresa Engineering College Page 17 of 26
St. Mother Theresa Engineering College Page 18 of 26
3. Prove that the following two regular expressions are equivalent by showing that
minimum state DFA’s are same. (May 15)
(i)(a|b)* (ii)(a*|b*)*

St. Mother Theresa Engineering College Page 19 of 26


St. Mother Theresa Engineering College Page 20 of 26
St. Mother Theresa Engineering College Page 21 of 26
St. Mother Theresa Engineering College Page 22 of 26
St. Mother Theresa Engineering College Page 23 of 26
4. Explain Cousins of Compiler in Detail.

St. Mother Theresa Engineering College Page 24 of 26


St. Mother Theresa Engineering College Page 25 of 26
St. Mother Theresa Engineering College Page 26 of 26
St. Mother Theresa Engineering College Page 27 of 26
St. Mother Theresa Engineering College Page 28 of 26
St. Mother Theresa Engineering College Page 29 of 26
5. Describe how to recognize the tokens?

St. Mother Theresa Engineering College Page 30 of 26


St. Mother Theresa Engineering College Page 31 of 26
St. Mother Theresa Engineering College Page 32 of 26
St. Mother Theresa Engineering College Page 33 of 26

You might also like