Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

code should be in C++ Language Write a scanner/lexical analyzer for MYC. You may write the program in C++ or in Java. Input to the

code should be in C++ Language
image text in transcribed
image text in transcribed
image text in transcribed
Write a scanner/lexical analyzer for MYC. You may write the program in C++ or in Java. Input to the Lexical Analyzer is obtained by getting input characters from the source provided by the instructor. You may create three different functions that (1) returns the next character from the input and moves the character pointer to the next character, (2) returns the next character without moving the pointer. (3) returns the second character without moving the pointer. These methods will help you to process input from the source file. These methods are optional and you may process the input the way that you prefer. The scanner returns a set of tokens or an error message. You may consider that a token object contains these following attributes: type is an integer (1..5) denoting the type of the token: OPTOK DELIMTOK KEYWORDTOK IDENTOK NUMTOK dataType is an integer that denotes the type of basic data: INT REAL. . Val contains the value of the token. You may save the value as a string. Later on you can convert it to your desired type based on its data type. Your scanner should have the ability to return tokens as requested by calling a function called getNextToken(). Note that your scanner shouldn't print any token on the screen if the program contains any errors. The output should be similar to the following: If the source file is the following: begin 1; while(* , , OPTOE >, , ,,, COPTOK , , ,,, ,OPTOK, ,CIDENTTOK, 2>, OPTOR , , , , OPTOK, ,, , , Here is a description of all language components, Whitespaces and Comments: Ends of lines, Blanks, tabs, and comments are supposed to be deleted from the source file. You will need to keep track of line numbers in case of error messages. The Lexical Analyzer skips over these characters, but does not return any token for them. Comments are contained between the characters /* and / or starts by / if it is a single line comment. Comments of course cannot be tested. Operators and Delimiters: Operators of MYC are: + - . / :- Delimiters of MYC are: () Reserved words: The key reserved words of MYC are: begin end it then else goto while label do integer real Identifiers: Identifiers begin with a letter, followed by any number of letters or digits and it may contain underscore. The length of MYC identifiers should not exceed 25. Longer identifiers should not be accepted. Note that MYC doesn't accept ans capitalized letters. Numbers: Unsigned Numbers must begin with a digit; if there is a decimal point, it must be followed by at least one digit. For simplicity, don't consider any numeric overflow or underflow. If there is an error, your program should display an error message accordingly. For example, the number 123. is not accepted as there is no digits after the decimal point attached Testing: Test your program on the files testi.myc, test2.aye.test).myc and test4.myc Files are Note: Don't over flood your main function with a lot of code. Use of object oriented is must. Your main function should be similar to the following, but it doesn't have to be exactly the same. What I meant is that keep your main function too short int maint Scanner ser; Tokvar - sent.getNextToken(); While (Tokvari null) cout Token tokvar; tokvar - ser.getNextToken() Your scanner must have a function to display the symbol table created by your scanner. The symbol table contains extra information about your tokens. Keep the symbol table very simple and contain the following attributes: the symbol, token type, data type, and value. For the data type, you don't have to write any value for this attribute. The data type attribute for identifiers will be written by the syntax analyzer. For the value this attribute must be filled with the value of literals that you have in your program. Notice that for literals numbers you must fill the data type attribute as well. For example: If you have a literal as 1265, then the symbol table must contain information as: Symbol Token Data Type Value "1265" NUMTOK integer 1,265 and if you have another literal as 12.345, then the symbol table must contain information as: Symbol Token Data Type Value "12.345" NUMTOK float 12.345 If you have a declaration statement as: integer my Var: Symbol Token Data Type Value myVar IDENTTOK NULL NULL Even if myVar is initialized, then the value and the data type must remain NULL, as this is the rule of the syntax analyzer

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

Building Database Driven Catalogs

Authors: Sherif Danish

1st Edition

0070153078, 978-0070153073

Students also viewed these Databases questions