Question: Design and implement a scanner for a programming language whose lexical specifications are given below. The scanner identifies and outputs tokens ( valid words and
Design and implement a scanner for a programming language whose lexical 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 correct.
When invoked, the scanner should extract the next token from the source program and should be able to output a token even if the input does not form a correct program. The syntax of the language will be specified later in Part
Atomic lexical elements of the language:
id :: letter alphanum
alphanum :: letter digit
integer :: nonzero digit
float :: integer fraction e integer
fraction ::digit nonzero
letter :: az AZ
digit ::
nonzero ::
Operators, punctuation and reserved words:
if
then
else
for
class
&& integer
; float
read
write
: return
:: main
Notes:
You are responsible for providing appropriate test cases that test for a wide variety of valid and invalid cases.
It is up to you to analyze this lexical definition and figure out if there are ambiguities or other errors. Any changes to this definition, if any, must be justified and should not result in diminishing the expressive power of the language. Also, you have to design the lexical analyzer in a flexible way, so that it can easily be adapted if changes are needed during the designing of syntax analyzer.
The tokens and followed by denote comments in the source code
Deliverables:
Document
Lexical specifications: Identify the lexical specification you used in the design of the scanner, as well as any changes that you may have applied to original lexical specifications.
Finite state automaton: Finite state machine describing the operation of your scanner.
Error reporting and recovery: Identify all the possible lexical errors that the scanner can encounter. Identify and explain the error recovery technique that you implement.
Design: Give a brief overview of the overall structure of your solution, as well as a brief description of the role of each component of your implementation.
Implementation Tools: Identify all the toolslibrariestechniques that you have used in your analysis or implementation and justify why you have used these particular ones as opposed to others.
Implementation
Scanner:
Develop a scanner that recognizes the abovementioned tokens.
It should be a function that returns a data structure containing the information about the next token identified in the source program file.
The data structure should contain information such as the token type its value or lexeme when applicable and its location in the source code. Fully describe this structure in your documentation.
Driver:
Include a driver that repeatedly calls the lexical analysis function and prints the token type of each token until the end of the source program is reached.
The scanner should optionally print the token stream to a file in the AtoCC format.
Another file for verification purposes should contain representative error messages each time an error is encountered in the input program.
The scanner should not stop after encountering an error. Error messages should be clear and should identify the location of the error in the source code.
Test cases:
Include many test cases that test a wide variety of valid and invalid cases.
Selected examples:
Invalid number: OR integer:integer:
Invalid number: OR integer:float:
Invalid number: OR float:integer:
efloat:e
integer:
abc id:abc
abcid:abc
abcInvalid identifier:abc
abc Invalid identifier:abc OR integer:id:abc
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
