Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  A Language For Specifying Lexical Analyzer - Lexical Analysis, Computer Science and IT Engineering

A Language For Specifying Lexical Analyzer - Lexical Analysis, Computer Science and IT Engineering - Computer Science Engineering (CSE) PDF Download

A LANGUAGE FOR SPECIFYING LEXICAL ANALYZER

There is a wide range of tools for constructing lexical analyzers.

Lex

YACC

Lex is a computer program that generates lexical analyzers. Lex is commonly used with the yacc parser generator.


Creating a lexical analyzer

  • First, a specification of a lexical analyzer is prepared by creating a program lex.l in the Lex language. Then, lex.l is run through the Lex compiler to produce a C program lex.yy.c.
  • Finally, lex.yy.c is run through the C compiler to produce an object progra m a.out, which is the lexical analyzer that transforms an input stream into a sequence of tokens.

A Language For Specifying Lexical Analyzer - Lexical Analysis, Computer Science and IT Engineering - Computer Science Engineering (CSE)

 

Lex Specification

A Lex program consists of three parts:

{ definitions }

%%

{ rules }

%%

{ user subroutines }

Definitions include declarations of variables, constants, and regular definitions

 

  •  Rules are statements of the form

p1 {action1}

p2 {action2}

pn {actionn}

where pi is regular expression and actioni describes what action the lexical analyzer should take

when pattern pi matches a lexeme. Actions are written in C code.

 

  • User subroutinesare auxiliary procedures needed by the actions. These can be compiledseparately and loaded with the lexical analyzer.

 

YACC- YET ANOTHER COMPILER-COMPILER

Yacc provides a general tool for describing the input to a computer program. The Yacc user specifies the structures of his input, together with code to be invoked as each such structure is recognized.

Yacc turns such a specification into a subroutine that handles the input process; frequently, it is convenient and appropriate to have most of the flow of control in the user's application handled by this subroutine.


Finite Automata

Finite Automata is one of the mathematical models that consist of a number of states and edges. It is a transition diagram that recognizes a regular expression or grammar.

There are tow types of Finite Automata :

  • Non-deterministic Finite Automata (NFA)
  • Deterministic Finite Automata (DFA)

A Language For Specifying Lexical Analyzer - Lexical Analysis, Computer Science and IT Engineering - Computer Science Engineering (CSE)

Deterministic Finite Automata

DFA is a special case of a NFA in which i) no state has an ε-transition.

ii)  there is at most one transition from each state on any input.

DFA has five tuples denoted by

M = {Qd, Ʃ, δ, q0, fd}

Qd – finite set of states

Ʃ            – finite set of input symbols

A Language For Specifying Lexical Analyzer - Lexical Analysis, Computer Science and IT Engineering - Computer Science Engineering (CSE)

 

Construction of DFA from regular expression

The following steps are involved in the construction of DFA from regular expression:

Convert RE to NFA using Thomson’s rules

Convert NFA to DFA

Construct minimized DFA

The document A Language For Specifying Lexical Analyzer - Lexical Analysis, Computer Science and IT Engineering - Computer Science Engineering (CSE) is a part of Computer Science Engineering (CSE) category.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)

FAQs on A Language For Specifying Lexical Analyzer - Lexical Analysis, Computer Science and IT Engineering - Computer Science Engineering (CSE)

1. What is a lexical analyzer?
Ans. A lexical analyzer is a program or tool that scans the input source code and breaks it down into a sequence of tokens. It is the first phase of a compiler and plays a crucial role in the process of lexical analysis.
2. What is the purpose of lexical analysis in computer science and IT engineering?
Ans. The purpose of lexical analysis in computer science and IT engineering is to analyze the source code and identify the basic building blocks, known as tokens. These tokens can include keywords, identifiers, constants, operators, and other symbols. Lexical analysis helps in organizing the source code for further processing by the compiler or interpreter.
3. How does a lexical analyzer work?
Ans. A lexical analyzer works by reading the input source code character by character and grouping them together into tokens. It uses a set of rules, called lexical rules or regular expressions, to identify and classify different tokens. The analyzer recognizes patterns in the input code and generates a sequence of tokens that can be used for further analysis or translation.
4. What is the role of regular expressions in specifying a lexical analyzer?
Ans. Regular expressions play a significant role in specifying a lexical analyzer. They define the patterns or rules that help in identifying different tokens in the input source code. By using regular expressions, the lexical analyzer can match the characters in the input code against these patterns and generate the corresponding tokens.
5. How important is a lexical analyzer in the overall compilation process?
Ans. A lexical analyzer is crucial in the overall compilation process as it is the first step in transforming the source code into a format that can be understood by the compiler. It helps in identifying the individual tokens and separating them from the input code. The output of the lexical analyzer, a sequence of tokens, serves as input for the subsequent phases of the compiler, such as parsing and semantic analysis.
Download as PDF

Top Courses for Computer Science Engineering (CSE)

Related Searches

Important questions

,

Extra Questions

,

practice quizzes

,

study material

,

Sample Paper

,

ppt

,

A Language For Specifying Lexical Analyzer - Lexical Analysis

,

video lectures

,

pdf

,

mock tests for examination

,

Summary

,

A Language For Specifying Lexical Analyzer - Lexical Analysis

,

A Language For Specifying Lexical Analyzer - Lexical Analysis

,

Objective type Questions

,

shortcuts and tricks

,

Semester Notes

,

Computer Science and IT Engineering - Computer Science Engineering (CSE)

,

Computer Science and IT Engineering - Computer Science Engineering (CSE)

,

past year papers

,

Computer Science and IT Engineering - Computer Science Engineering (CSE)

,

Exam

,

Viva Questions

,

MCQs

,

Free

,

Previous Year Questions with Solutions

;