The Goal Of This Project Is To Build A Scanner For A Version Of The Core Language

Posted Under: C

Ask A Question
DESCRIPTION
Posted
Modified
Viewed 13
C language programing and read all instructions and requirements in the pdf carefully. make sure you meet all requirements and follow the submission instructions
Attachments
__MACOSX/._Project1-1 Project1-1/scanner.c #include <stdlib.h> #include <stdio.h> #include <string.h> #include <ctype.h> #include "scanner.h" #include "core.h" // Below are the values we want to persist as the scanner runs // This maintains a pointer to the file static FILE* fp; // This is the string making up the current token static char* tokenString; // This is the enum value for the current token static int token; // Below are the functions that make up the scanner // Opens the file, find the first token int scanner_open(char* filename) { fp = fopen(filename, "r"); if(fp == NULL) { printf("Error: File %s couldn't be opened!\n", filename); } tokenString = NULL; return nextToken(); } // Frees memory allocated for the scanner int scanner_close() { fclose(fp); free(tokenString); } // Returns the current token (enum value) int currentToken() { // You will need to fill this in with appropriate things } // Finds the next token (or the first token when called by scanner_open) int nextToken() { // You will need to fill this in with appropriate things } // Passes back the string value, so the parse tree (project 2) will be able to store identifiers void getId(char* identifier) { strcpy(identifier, tokenString); } // Passes back the constant value, so the parse tree (project 2) will be able to store const values int getConst() { return atoi(tokenString); } __MACOSX/Project1-1/._scanner.c Project1-1/core.h #ifndef CORE_H #define CORE_H // This enum serves as our tokens enum core { // Keywords AND, BEGIN, DO, ELSE, END, IF, IN, INTEGER, IS, NEW, NOT, OR, OUT, PROCEDURE, RECORD, THEN, WHILE, // Symbols ADD, SUBTRACT, MULTIPLY, DIVIDE, ASSIGN, EQUAL, LESS, COLON, SEMICOLON, PERIOD, COMMA, LPAREN, RPAREN, LBRACE, RBRACE, // Others CONST, ID, EOS, ERROR }; #endif __MACOSX/Project1-1/._core.h __MACOSX/Project1-1/._Correct Project1-1/tester.sh #!/bin/bash runner=$1 echo "Attempting to compile c code..." gcc -o main *.c runner="./main verbose" score=0 error=0 for value in {1..13} do echo "" echo "Running ${value}.code" timeout 5 ${runner} Correct/${value}.code > Correct/${value}.student echo "" echo "Comparing with ${value}.expected" #Check for correct print tr -d '[:space:]' < Correct/${value}.student > temp1 tr -d '[:space:]' < Correct/${value}.expected > temp2 echo "Comparing input and output" if cmp -s "temp1" "temp2"; then echo "Print looks good" score=$(($score + 1)) else echo "Student output and expected output are different" fi done rm temp1 rm temp2 echo "" echo "" echo "Running error cases:" echo "" echo "Running 1.error:" echo "----------" timeout 5 ${runner} Error/1.code echo "----------" read -n 1 -p "Error is '!' in file. Error message related to that? (y/n)" mainmenuinput if [ $mainmenuinput = "y" ]; then error=$(($error + 1)) fi echo "" echo "Running error cases:" echo "" echo "Running 2.error:" echo "----------" timeout 5 ${runner} Error/2.code echo "----------" read -n 1 -p "Error is '?' in file. Error message related to that? (y/n)" mainmenuinput if [ $mainmenuinput = "y" ]; then error=$(($error + 1)) fi echo "" echo "Running error cases:" echo "" echo "Running 3.error:" echo "----------" timeout 5 ${runner} Error/3.code echo "----------" read -n 1 -p "Error is '$' in file. Error message related to that? (y/n)" mainmenuinput if [ $mainmenuinput = "y" ]; then error=$(($error + 1)) fi echo "" echo "Running error cases:" echo "" echo "Running 4.error:" echo "----------" timeout 5 ${runner} Error/4.code echo "----------" read -n 1 -p "Error is too large constant in file. Error message related to that? (y/n)" mainmenuinput if [ $mainmenuinput = "y" ]; then error=$(($error + 1)) fi echo "" echo "Correct cases score out of 13:" echo $score echo "Error cases score out of 4:" echo $error echo Done! __MACOSX/Project1-1/._tester.sh Project1-1/main.c #include <stdio.h> #include <string.h> #include "scanner.h" #include "core.h" int main(int argc, char *argv[]) { // If the optional CLI argument "verbose" is given, will print out // string values instead of the enumeration value (for testing) int verbose = (argc > 2) && (strcmp(argv[1], "verbose") == 0); // Initialize the scanner scanner_open(argv[1 + verbose]); while (currentToken() != EOS && currentToken() != ERROR) { // Get the current token int current = currentToken(); // If verbose, convert enum value to char string if (verbose == 1) { char str[10]; //char* str; switch (current) { case AND : strcpy(str, "AND"); break; case BEGIN : strcpy(str, "BEGIN"); break; case DO : strcpy(str, "DO"); break; case ELSE : strcpy(str, "ELSE"); break; case END : strcpy(str, "END"); break; case IF : strcpy(str, "IF"); break; case IN : strcpy(str, "IN"); break; case INTEGER : strcpy(str, "INTEGER"); break; case IS : strcpy(str, "IS"); break; case NEW : strcpy(str, "NEW"); break; case NOT : strcpy(str, "NOT"); break; case OR : strcpy(str, "OR"); break; case OUT : strcpy(str, "OUT"); break; case PROCEDURE : strcpy(str, "PROCEDURE"); break; case RECORD : strcpy(str, "RECORD"); break; case THEN : strcpy(str, "THEN"); break; case WHILE : strcpy(str, "WHILE"); break; case ADD : strcpy(str, "ADD"); break; case SUBTRACT : strcpy(str, "SUBTRACT"); break; case MULTIPLY : strcpy(str, "MULTIPLY"); break; case DIVIDE : strcpy(str, "DIVIDE"); break; case ASSIGN : strcpy(str, "ASSIGN"); break; case EQUAL : strcpy(str, "EQUAL"); break; case LESS : strcpy(str, "LESS"); break; case COLON : strcpy(str, "COLON"); break; case SEMICOLON : strcpy(str, "SEMICOLON"); break; case PERIOD : strcpy(str, "PERIOD"); break; case COMMA : strcpy(str, "COMMA"); break; case LPAREN : strcpy(str, "LPAREN"); break; case RPAREN : strcpy(str, "RPAREN"); break; case LBRACE : strcpy(str, "LBRACE"); break; case RBRACE : strcpy(str, "RBRACE"); break; case CONST : strcpy(str, "CONST"); break; case ID : strcpy(str, "ID"); break; case EOS : strcpy(str, "EOS"); break; } printf("%s", str); // If ID/CONST, print that value if (currentToken() == ID) { char value[20]; getId(value); printf("[%s]", value); } else if (currentToken() == CONST) { int value = getConst(); printf("[%d]", value); } printf("\n"); } else { printf("%d\n", current); } // Advance to the next token nextToken(); } // Scanning is done, release memory scanner_close(); return 0; } __MACOSX/Project1-1/._main.c Project1-1/scanner.h #ifndef SCANNER_H #define SCANNER_H #include "core.h" // Opens the file, find the first token int scanner_open(char* filename); // Frees memory allocated for the scanner int scanner_close(); // Returns the current token (enum value) int currentToken(); // Finds the next token (or the first token when called by scanner_open) int nextToken(); // Passes back the string value, so the parse tree (project 2) will be able to store identifiers void getId(char* identifier); // Passes back the constant value, so the parse tree (project 2) will be able to store const values int getConst(); #endif __MACOSX/Project1-1/._scanner.h Project1-1/3341 Project 1.pdf CSE 3341 Project 1 - Core Scanner Overview The goal of this project is to build a scanner for a version of the Core language, a pretend language we will be discussing in class. For this project you are given the following: � “3341 Project 1.pdf” - This handout. Make sure you read it completely and handle all requirements in your implementation. You are encouraged to post any questions on Piazza. � “main.c”, “core.h”, “scanner.h”, “scanner.c” - I have outlines the project in this files, and give you some of the code you will need. Make no changes to to “core.h” file. You can make changes to “main.c” as long as those changes do not change break my tester.sh script. � You may create additional files to contain any additional classes or methods you want to create. � “tester.sh” - This is a script I wrote to help you test your project. It is very similar to the script that will be used to grade your project, so if your project works correctly with this script you are probably doing well. The only guarantee I give is that this script will work on stdlinux. � Folder “Correct” - This contains some correct inputs and their expected outputs. The “tester.sh” script will test your code against these examples. � Folder “Error” - This contains some inputs that should generate error messages. The “tester.sh” script will test your code against these examples. The following are some constraints on your implementation: � Do not use scanner generators (e.g. lex, flex, jlex, jflex, ect) or parser generators (e.g. yacc, CUP, ect) � Use only the standard libraries of C. This is the reference I like to use: https://en.cppreference.com/w/c/header Your submission should compile and run in the standard linux environment the CSE department provides (stdlinux). I will leave it up to you to decide what IDE you will use or if you will develope your code locally or remotely, but as a final step before submitting your code please make sure it works on stdlinux. Use the subscribe command - make sure you are subscribed to GCC-10.1.0. The graders will not spend any time fixing your code - if it does not compile on stdlinux, your project will not be graded and you will get a 0. 1 https://en.cppreference.com/w/c/header Your Scanner You are responsible for writing a scanner, which will take as input a text file and output a stream of “tokens” from the core.h enumeration. You scanner must implement the following functions: � scanner open and scanner close: These functions open the file, find the first token, and release memory when we are done scanning. � currentToken: This function should return the token the scanner is currently on, with- out consuming that token. � nextToken: This function should advance the scanner to the next token in the stream (the next token becomes the current token). � getId: If the current token is ID, then this function should return the string value of the identifier. If the current token is not ID, behavior is undefined. � getConst: If the current token is CONST, then this function should return the value of the constant. If the current token is not CONST, behavior is undefined. All of these functions will be necessary for the parser you will write in the second project. You are free to create additional functions. To make things easier for you, you may assume no token is made of more than 20 characters. Also, I suggest using calloc to allocate memory, instead of malloc. Input The input to the scanner will come from a single ASCII text file. The name of this file will be given as a command line argument to the main function. The scanner should process the sequence of ASCII characters in this file and should produce the appropriate sequence of tokens. There are two options for how your scanner can operate: (1) the scanner can read the entire character stream from the file, tokenize it, stores all the tokens in some list or array and calls to currentToken and nextToken simply walk through the list or (2) the scanner reads from the file only enough characters to construct the first token, and then later reads from the file on demand as the currentToken or nextToken functions are called. Real world scanners typically work as described in (2). In your implementation, you can implement (1) or (2), whichever you prefer. Once your scanner has scanned the entire file, it should return the EOS token (End Of Stream). 2 Invalid Input Your scanner should recognize and reject invalid input with a meaningful error message. The scanner should make sure that the input stream of characters represents a valid sequence of tokens. For example, characters such as ‘ ’ and ’%’ are not allowed in the input stream. If your scanner encounters a problem, it should print a meaningful error message to standard out (please use the format ”ERROR: Something meaningful here”) and return the ERROR token so the main program halts. The Language The Core language consists of 4 kinds of strings, which you will need to tokenize: � Keywords: and begin do else end if in integer is new not or out procedure record then while � Identifiers: Begins with a letter (uppercase or lowercase) followed by zero or more letters/digits. Refer to this regular expression once we cover regular expressions: (a| . . . |z|A| . . . |Z)(a| . . . |z|A| . . . |Z|0|1| . . . |9)* � Constants: Integers from 0 to 1009 (inclusive) � Symbols: + - * / := = < : ; . , ( ) [ ] Your scanner walk through the input character stream, recognize strings from the lan- guage, and return the appropriate token from the enumeration in “Core.java” or “Core.py”. If there is any situation in which it is unclear to you which token should be returned, please ask for clarification on Piazza. Write your scanner with these rules in mind: 1. The language is case sensitive, and the keywords take precedence over the identifiers. For example, “begin” should produce the token BEGIN (not ID), but “bEgIn” should produce the token ID. 2. Strings in the language may or may not be separated by whitespaces. For example the character stream may contain the string “x=10” or the string “x = 10”, and both of these should generate the token sequence ID EQUAL CONST. 3. Always take the greedy approach. For example, the string “whilewhile” should produce an ID token instead of two WHILE tokens, string “123” should produce a single CONST token, and string “:=” should produce ASSIGN. 3 4. Keyword/identifier strings end with either whitespace or a non-digit/letter character. For example: (a) the string “while (” and the string “while(” should both result in the WHILE and LPAREN tokens. (b) the string “while 12” should result in the WHILE and CONST tokens, but the string “while12” should result in the ID token. 5. Constant strings end with any non-digit character. For example: (a) the string “120while” or “120 while” should result in the CONST and WHILE tokens. 6. Symbols may or may not be separated from other strings by whitespace. For example: (a) String “++while<= =12=” should result in the token sequence ADD ADD WHILE LESS EQUAL EQUAL CONST EQUAL. Let me know if you think of any situations not covered here. Testing Your Project I have provided some test cases. For each correct test case there are two files (for example 4.code and 4.expected). On stdlinux you can redirect the output of the main program to a file, then use the diff command to see is there is any difference between your output and the expected output. For an example of how to do this you can take a look at the script file ”tester.sh”. The test cases are weak. You should do additional testing with your own test cases. Feel free to create and post additional test cases on piazza. Project Submission On or before 11:59 pm January 27th, you should submit to the Carmen dropbox for Project 1 a single zip file containing the following: � All your .java or .py files. � An ASCII text file named README.txt that contains: – Your name on top – The names of all files you are submitting and a brief description stating what each file contains – Any special features or comments on your project – Any known bugs in your scanner 4 If the time stamp on your submission is 12:00 am on January 28th or later, you will receive a 10% reduction per day, for up to three days. If your submission is more than 3 days late, it will not be accepted and you will receive zero points for this project. If you resubmit your project, only the latest submission will be considered. Grading The project is worth 100 points. Correct functioning of the scanner is worth 65 points. The handling of errors is worth 20 points. The implementation style and documentation are worth 15 points. Academic Integrity The project you submit must be entirely your own work. Minor consultations with others in the class are OK, but they should be at a very high level, without any specific details. The work on the project should be entirely your own; all the design, programming, testing, and debugging should be done only by you, independently and from scratch. Sharing your code or documentation with others is not acceptable. Submissions that show excessive similarities (for code or documentation) will be taken as evidence of cheating and dealt with accordingly; this includes any similarities with projects submitted in previous instances of this course. Academic misconduct is an extremely serious offense with severe consequences. Addi- tional details on academic integrity are available from the Committee on Academic Mis- conduct (see http://oaa.osu.edu/coamresources.html). If you have any questions about uni- versity policies or what constitutes academic misconduct in this course, please contact me immediately. 5 __MACOSX/Project1-1/._3341 Project 1.pdf __MACOSX/Project1-1/._Error Project1-1/Correct/9.code int x1, y123y,z9z9z9z; __MACOSX/Project1-1/Correct/._9.code Project1-1/Correct/4.expected ID[program] ID[int] ID[X] COMMA ID[Y] COMMA ID[XY] SEMICOLON ID[input] ID[X] COMMA ID[Y] COMMA ID[XY] SEMICOLON IF NOT LPAREN ID[X] EQUAL ID[Y] RPAREN THEN IF ID[X] LESS ID[Y] THEN ID[output] CONST[1] SEMICOLON ELSE ID[output] CONST[2] SEMICOLON SEMICOLON ID[output] CONST[3] SEMICOLON SEMICOLON ID[input] ID[X] COMMA ID[Y] COMMA ID[XY] SEMICOLON IF NOT LPAREN ID[X] EQUAL ID[Y] RPAREN THEN IF ID[X] LESS ID[Y] THEN ID[output] CONST[10] SEMICOLON ELSE ID[output] CONST[20] SEMICOLON SEMICOLON ID[output] CONST[30] SEMICOLON SEMICOLON ID[input] ID[X] COMMA ID[Y] COMMA ID[XY] SEMICOLON IF NOT ID[X] EQUAL ID[Y] RPAREN THEN IF ID[X] LESS ID[Y] THEN ID[output] CONST[100] SEMICOLON ELSE ID[output] CONST[200] SEMICOLON SEMICOLON ID[output] CONST[30] SEMICOLON SEMICOLON ID[input] ID[X] COMMA ID[Y] COMMA ID[XY] SEMICOLON IF ID[X] EQUAL ID[Y] THEN ID[output] CONST[199] SEMICOLON ELSE IF NOT LPAREN ID[X] LESS EQUAL ID[Y] RPAREN THEN ID[output] CONST[29] SEMICOLON ELSE ID[output] CONST[39] SEMICOLON SEMICOLON SEMICOLON END __MACOSX/Project1-1/Correct/._4.expected Project1-1/Correct/5.expected PROCEDURE RECORD ID[X] COMMA ID[Y] COMMA ID[XY] SEMICOLON IN ID[X] COMMA ID[Y] SEMICOLON WHILE LPAREN ID[X] EQUAL EQUAL EQUAL CONST[0] RPAREN BEGIN ID[Y] EQUAL ID[Y] MULTIPLY ID[Y] SEMICOLON ID[X] EQUAL ID[X] SUBTRACT CONST[1] SEMICOLON SEMICOLON OUT ID[Y] SEMICOLON END __MACOSX/Project1-1/Correct/._5.expected Project1-1/Correct/5.code procedure record X , Y , XY ; in X, Y; while (X === 0) begin Y = Y * Y; X = X - 1; ; out Y; end __MACOSX/Project1-1/Correct/._5.code Project1-1/Correct/4.code program int X , Y , XY ; input X, Y, XY; if not (X = Y) then if X < Y then output 1; else output 2; ; output 3; ; input X, Y, XY; if not (X = Y) then if X < Y then output 10; else output 20; ; output 30; ; input X, Y, XY; if not X = Y) then if X < Y then output 100; else output 200; ; output 30; ; input X, Y, XY; if X = Y then output 199; else if not (X <= Y) then output 29; else output 39; ; ; end __MACOSX/Project1-1/Correct/._4.code Project1-1/Correct/8.code int x, y,z; __MACOSX/Project1-1/Correct/._8.code Project1-1/Correct/13.code precedure int x, y, z; A (a, b, c) begin a = b < c; begin in x, y, z; A (x, y, z); end __MACOSX/Project1-1/Correct/._13.code Project1-1/Correct/3.code program int X , Y , XY ; a ( b, c, d) begin a=a+b+c; ;begin X = 0; output X; end and instance __MACOSX/Project1-1/Correct/._3.code Project1-1/Correct/11.expected WHILE NOT LPAREN ID[x] ADD ID[y] EQUAL EQUAL EQUAL EQUAL ID[z10] MULTIPLY CONST[101] BEGIN ID[test] SEMICOLON ID[test] COLON EQUAL ID[test1] SUBTRACT ID[test2] SEMICOLON END __MACOSX/Project1-1/Correct/._11.expected Project1-1/Correct/10.expected IF LPAREN ID[x] EQUAL EQUAL ID[y] RPAREN THEN IN ID[x] SEMICOLON ELSE OUT ID[x] SEMICOLON __MACOSX/Project1-1/Correct/._10.expected Project1-1/Correct/9.expected ID[int] ID[x1] COMMA ID[y123y] COMMA ID[z9z9z9z] SEMICOLON __MACOSX/Project1-1/Correct/._9.expected Project1-1/Correct/8.expected ID[int] ID[x] COMMA ID[y] COMMA ID[z] SEMICOLON __MACOSX/Project1-1/Correct/._8.expected Project1-1/Correct/3.expected ID[program] ID[int] ID[X] COMMA ID[Y] COMMA ID[XY] SEMICOLON ID[a] LPAREN ID[b] COMMA ID[c] COMMA ID[d] RPAREN BEGIN ID[a] EQUAL ID[a] ADD ID[b] ADD ID[c] SEMICOLON SEMICOLON BEGIN ID[X] EQUAL CONST[0] SEMICOLON ID[output] ID[X] SEMICOLON END AND ID[instance] __MACOSX/Project1-1/Correct/._3.expected Project1-1/Correct/2.expected RECORD ID[int] ID[X] COMMA ID[Y] COMMA ID[XY] SEMICOLON BEGIN ID[X] EQUAL CONST[0] SEMICOLON NEW LBRACE CONST[10] RBRACE ID[X] SEMICOLON END __MACOSX/Project1-1/Correct/._2.expected Project1-1/Correct/2.code record int X , Y , XY ; begin X = 0; new[10] X; end __MACOSX/Project1-1/Correct/._2.code Project1-1/Correct/12.code pRocedure In oUt iF TheN END BEGIN EnD ref EOS __MACOSX/Project1-1/Correct/._12.code Project1-1/Correct/1.code 123 and procedure record RECORD ( ; := token = ) 3 while __MACOSX/Project1-1/Correct/._1.code Project1-1/Correct/11.code while not (x+y = = == z10 * 101 begin test; test : = test1 - test2; end __MACOSX/Project1-1/Correct/._11.code Project1-1/Correct/7.expected ID[precedure] BEGIN END __MACOSX/Project1-1/Correct/._7.expected Project1-1/Correct/6.expected ID[program] ID[int] ID[X] COMMA ID[Y] COMMA ID[Z] SEMICOLON ID[input] ID[X] COMMA ID[Y] COMMA ID[Z] SEMICOLON WHILE NOT LPAREN ID[X] ASSIGN CONST[0] RPAREN BEGIN ID[output] ID[Y] SEMICOLON WHILE ID[Y] LESS CONST[10] BEGIN ID[Y] ASSIGN ID[Y] ADD CONST[1] SEMICOLON SEMICOLON ID[output] ID[Y] SEMICOLON ID[Y] ASSIGN ID[Z] ID[X] ASSIGN ID[X] SUBTRACT CONST[1] SEMICOLON SEMICOLON END __MACOSX/Project1-1/Correct/._6.expected Project1-1/Correct/10.code if (x == y) then in x; else out x; __MACOSX/Project1-1/Correct/._10.code Project1-1/Correct/1.expected CONST[123] AND PROCEDURE RECORD ID[RECORD] LPAREN SEMICOLON ASSIGN ID[token] EQUAL RPAREN CONST[3] WHILE __MACOSX/Project1-1/Correct/._1.expected Project1-1/Correct/7.code precedure begin end __MACOSX/Project1-1/Correct/._7.code Project1-1/Correct/12.expected ID[pRocedure] ID[In] ID[oUt] ID[iF] ID[TheN] ID[END] ID[BEGIN] ID[EnD] ID[ref] ID[EOS] __MACOSX/Project1-1/Correct/._12.expected Project1-1/Correct/13.expected ID[precedure] ID[int] ID[x] COMMA ID[y] COMMA ID[z] SEMICOLON ID[A] LPAREN ID[a] COMMA ID[b] COMMA ID[c] RPAREN BEGIN ID[a] EQUAL ID[b] LESS ID[c] SEMICOLON BEGIN IN ID[x] COMMA ID[y] COMMA ID[z] SEMICOLON ID[A] LPAREN ID[x] COMMA ID[y] COMMA ID[z] RPAREN SEMICOLON END __MACOSX/Project1-1/Correct/._13.expected Project1-1/Correct/6.code program int X , Y, Z; input X, Y, Z; while not (X := 0) begin output Y; while Y < 10 begin Y := Y + 1; ; output Y; Y := Z X := X - 1; ; end __MACOSX/Project1-1/Correct/._6.code Project1-1/Error/4.code 1000000000 __MACOSX/Project1-1/Error/._4.code Project1-1/Error/3.code program int X Y XY ; input X; case x of 1 2 3 1 4 5 6 2 else 3 end; output X$; end __MACOSX/Project1-1/Error/._3.code Project1-1/Error/2.code program int X Y XY ; X = 0; output X; end what is this doing here? __MACOSX/Project1-1/Error/._2.code Project1-1/Error/1.code program int X Y XY ; begin input X ; Y := 0 ; EOS input XY, Y ; output XY ; Y ::= Y + XY ; X := X - 1 ; while ! (0 < X) begin input XY ; output XY ; Y := Y + XY ; X := X - 1 ; ; output Y ; end __MACOSX/Project1-1/Error/._1.code
Explanations and Answers 0

No answers posted

Post your Answer - free or at a fee

Login to your tutor account to post an answer

Posting a free answer earns you +20 points.

Login

NB: Post a homework question for free and get answers - free or paid homework help.

Get answers to: The Goal Of This Project Is To Build A Scanner For A Version Of The Core Language or similar questions only at Tutlance.

Related Questions