Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question 2 Your task is to design and implement a lexical anlayzer for a programming language whose specifications are given below. The scanner identifies and

Question 2
Your task is to design and implement a lexical anlayzer for a programming language whose specifications are given below. The scanner identifies and outputs tokens (valid words and punctuation)
in the source program. Its output is a token that can thereafter be used by the syntax analyzer to
verify that the program is syntactically valid. When called, the lexical analyzer should extract the
next token from the source program. The syntax of the language will be specified in TMA #2.
Punctuation Operators Reserved words
( ; +< else integer self
),> float let struct
{.*<= func private then
} : />= if public var
[->= or impl read void
]== and inherits return while
<> not write
1
Lexical elements
id -> letter alphanum
letter -> a ... z | A ... Z
alphanum -> letter | digit |_
digit ->0...9
integer -> nonzero digit
|0
nonzero ->1...9
float -> integer fraction [e [+|] integer]
fraction ->. digit nonzero |.0
Comments:
Inline comments start with // and end with the end of the line they appear in.
Block comments start with /* and end with */ and may span over multiple lines.
Answer to the following tasks:
(a) Identify the lexical specification, expressed as regular expressions, that you used in the design of the lexical analyzer.
(Note: You should completely describe the data structure of your design and any changes
that you may have applied to the original lexical specifications should be also included.)
(b) Describe the complete operation of your lexical analyzer using finite state diagram.
(c) Write Lex (and Yacc) files to construct a lexical analyzer that recognizes the above-stated
tokens. It should be a function that returns a token data structure containing the information
about the next token identified in the source program file. The token data structure should
contains: (1) the token type; (2) the lexeme; and (3) the location, of the token in the source
code.
(Note: The parser that Yacc generates should be able to call the lexical analyzer generated by
Lex and perform parsing of the code generated by the given grammar. Your Lex file should
be able to remove any white space from the code. All correspondences should be stored in a
symbol table and printed out as output.)
(d) Identify all the possible lexical errors that the lexical analyzer might encounter and the possible error recovery techniques you could use. Choose one of them and discuss why you
have chosen it.
(e) Include many test cases that test a wide variety of valid and invalid cases

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions