CSE340 Project 1 solution


Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment


5/5 - (2 votes)

AbstractThe goal of this project is to explore strings, pointers, and memory allocationin C and familiarize you with the platform and tools that will be used in allprogramming assignments in this course. You will use a lexical analyzer thatwe developed to learn about strings, linked lists and memory management inC. You need to master these topics in order to have a much easier time insubsequent projects.1 IntroductionYou are provided with a small lexer (lexical analyzer) that reads tokens from stan-dard input. Your job is to write a program that reads all tokens from the inputby calling the lexer function getToken() and store some tokens in a linked list andthen print out the contents of the linked list in a speci c order.The following section describes the provided code in detail. You should read itcarefully to learn how to use it in your code.2 Lexer APIThe lexer is composed of several functions most of which are only meant to be calledby the lexer itself and only two of the functions are intended to be used in other code.In other words, these two functions provide the application programming interface(API) of our lexer. These functions are declared in lexer.h:? getToken(): reads the next token from standard input and returns its typeas an int. In some cases the actual token is stored in a global variable namedtoken which is a null-terminated character array. The cases for which theactual token is stored in token are: ID, NUM, IF, WHILE, DO, THEN and PRINT.The possible values for token type returned by getToken() are de ned asmacros in lexer.h. The special value ERROR signi es that getToken() hasencountered an unrecognized character in the input (a lexical error) and EOFsigni es the end of input stream.1? ungetToken(): causes the next call to getToken() to return the last tokenread by the previous call to getToken(). It simply sets a ag so that next callto getToken() would not read another token from the input but rather returnthe last token. Note that it is an error to call ungetToken() before any callto getToken().There are 4 global variables declared in lexer.h:1? ttype: when getToken() is called, the token type is stored as an integer inthe variable ttype.? token: when getToken() is called, the token value is stored in the array token.If the token is one of ID, NUM, IF, WHILE, DO, THEN or PRINT, the variable tokencontains the token string. For other token types, the variable token containsthe empty string.? tokenLength: the length of the string stored in token? line: the current line number of the input3 RequirementsYour program should use the provided lexer and read all tokens from the input byrepeatedly calling the getToken() function. Some of the token strings should bestored in a linked list. Speci cally, if? The token is of type NUM, or? The token is of type ID and the actual token is equal to one of the followingvalues: “cse340”, “programming”, or “language”the token string and some other information need to be stored in a node of a linkedlist. The information that needs to be stored about each of these tokens in the linkedlist is the following:? Token type (copied from ttype)? Token value (copied from token)? Line number in the input where token was read (copied from line)After reading all tokens from the input and storing information about tokensthat match the above criteria, your program should go over the linked list and printthe information in reverse order with the following format for each node of thelinked list printed in a separate line:1These variables are declared as extern variables which enables code in other les that #includelexer.h to see these variables. The actual variables are declared in lexer.c.2<line <ttype_str <tokenNote that <ttype str is the textual representation of the token type. The possiblevalues are ID and NUM.Your submissions will be inspected by the TA to give you feedback on potentialerrors in your code. You are required to store the above information in a linked list(either single or double linked list). The nodes in the linked list should be allocatedon the heap (using malloc or other similar functions like calloc) and the allocatedmemory should be freed after printing the output.3.1 ExampleHere is an example input with 4 lines:cse340 < < + 123 *456 programming- cse 340 , LANGUAGE 100. ; WHILE 200 IFAnd here is the expected output:4 NUM 2003 NUM 1003 NUM 3402 ID programming2 NUM 4561 NUM 1231 ID cse3404 GradingAn important part of this project is to make sure that you implement the require-ments in the manner speci ed. We will make sure that the output is correct, butwe will also check to make sure that you implemented a linked list, allocated spacecorrectly, manipulated strings correctly and so on. If the implementation does notfollow the requirements, you will not get full credit for your work.The course website which should be used to submit your programs, has a numberof test cases for this project as well as future projects. Make sure your code passesall test cases. The course website is only accessible from inside the ASU network,3so you should either be on campus or connect to ASU network via VPN softwarein order to access the website. The URL for accessing the website will bepublished on Blackboard.5 Submission1. Only C or C++ can be used for this project2. You should submit your code on the course website by 11:59:59 pm on duedate. The website link will be posted on Blackboard3. Read the provided code carefully, you need to fully understand it since it isrequired for this project and it is used in future projects4. There are compiling instructions in the comments written at the end of lexer.c5. Make sure your submission has no compile problems. If you get a compileerror on the website, x the problem and submit again6. You can submit any number of times you need, but remember that we onlygrade your last submission7. Write your code in a separate le, include lexer.h in your code to be able touse lexer functions8. Don’t submit the provided code, only submit the code that you write. lexer.hand lexer.c will be added to your submission automatically on the server9. Don’t include test scripts or test cases with your submission10. Don’t use any whitespace characters in le names4