Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me with the program please thank you. This is Drive.cc file. 2)This is Make.file. 3)This is pl-scanner file. 4)This is token file CS

Please help me with the program please thank you.image text in transcribedimage text in transcribed

This is Drive.cc file.

image text in transcribed

2)This is Make.file.

image text in transcribed

3)This is pl-scanner file.

image text in transcribedimage text in transcribed

4)This is token file

image text in transcribed

CS 3323 - Principles of Programming Languages Spring 2021 - Assignment 1 Download the files pl-scanner.yy, driver.c, tokens.h and Makefile from the assignment directory space. Then, perform the necessary modifications to the rule's file to recognize the below strings and tokens. The token codes can be found in the tokens.h header file. The assignment is due on February 26th, 2021, 11:59pm CT. Files must be uploaded by then. Late policy deduction applies. You can work in groups of up to 3 students. Groups remain the same for the whole semester. All students must upload the corresponding files. 1. (1.5pt) Add the necessary rules to recognize the arithmetic operators: +,-,*,/,+=++, =,==, -=,. See the file tokens.h to determine the constants to be used (they start with the prefix 'OP_'. 2. (2pt) Modify the rule returning the T ID token to recognize all identifiers matching the following: identifiers must start with the character 'Q'. the character immediately following the '@' must be a letter. Our language will be case insensitive, so there will be no distinction between upper and lower case strings. For example, the identifiers "SUM_42" and "sum_42" will be considered the same. characters following the initial 'Q' and the first letter can be any letter (upper or lower case), digit or the underscore? symbol. Examples of valid identifiers are commas used separate identifiers): Qiter, Qiii, Qali, Ca xe, @b_p, Qb20, QA_B_C_2.. Examples of invalid identifiers are commas used separate identifiers): -2, at, 2., 12.5, abC24, @_b43, -delta, Q. 3. (2.5pt) Create a new rule to recognize floating point numbers. The rule should return the token L FLOAT: Can start with a digit, the '+' or a ?-? Both the integer and fractional part should consist of at least one digit The integer and fractional part should be separated by a?' Examples of acceptable floating point numbers are: "10.0", " +1.5", "-10.50000", "0.9". Examples of strings that should be rejected are: "0.", ".01" 1 4. (1.5pt) Create a rule to recognize the following keywords: integer, float, foreach, begin, end, repeat, until, while, declare, if, then, print. The tokens corresponding to the keywords are those with the prefix K_ in the tokens.h header file. For convenience, a Makefile is provided, but you are not required to use it. Run: make to rebuild the scanner generator (lex.yy.c), and to recompile the driver. Some recommendations: Always include an action to execute even if it's an empty one. i.e. { }. The action associated to a detected pattern must start in the same line as where you write the pattern. Remember the issue with pattern prefixes (See slides and or corresponding class recording of Lexical Analysis). If something doesn't work, isolate the pattern you want to test in another file and test just that pattern. Don't feel afraid to run flex -h in the command line and test other flags/options. Don't insert any type of line or block comments as part of the regular expression patterns nor the actions. It will likely get Flex (and you) confused. #include #include #include "tokens.h" extern int yylex (); int main () { int next; while ((next = yylex ()) != T_E0F) { printf ("token = %d ", next); } return 0; } SRCS = driver.cc lex.yy. BIN = pl-scanner.exe CFLAGS = -03 -I. SCANNER = pl-scanner. #SCANNER = example-flex FLEX = flex CC = g++ all: scanner bin bin: $ (SRCS) $(CC) $ (CFLAGS) -0 $ (BIN) $ (SRCS) scanner: $(SCANNER).yy. $(FLEX) $(SCANNER).yy. clean: rm *.exe lex.yy...C *.0 1 2 /ame, ID; 3. 4. 5 #include "tokens.h" # undef yywrap. # define yywrap() 1 6 7. 8. 9 10 #undef YY_DECL #define YY_DECL int yylex() YY_DECL; 11 12 13 14 15 16 // Code run each time a pattern is matched. #undef YY_USER_ACTION # define YY_USER_ACTION {} 17 18 19 20 %} %option yylineno %option noyywrap DIGIT [0-9] ALPHA [a-zA-Z] 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 W.*$ [ \t]+ [ ]+ { return; } 40 { 41 42 return ';'; 43 } "=" { 44 45 46 47 48 return OP_ASSIGN; } 49 "main" { 50 return K_MAIN; } {DIGIT}+ { return L_INTEGER; } 51 52 53 54 55 56 57 58 59 60 61 62 63 64 {ALPHA}+ { return T_ID; } > { return T_EOF ; } { printf ("Unexpected character "); exit (1); } 65 66 67 68 69 // keywords #define K_FOREACH 200 #define K_PRINT 201 #define K_WHILE 202 #define K_REPEAT 203 #define K_UNTIL 204 #define K_BEGIN 205 #define K_END 206 #define K_DECLARE 209 #define K_IF 210 #define K_THEN 211 #define K_MAIN 212 #define K_INTEGER 213 #define K_FLOAT 214 // operators #define OP_ASSIGN 220 #define OP_ADD 221 #define OP_SUB 222 #define OP_MUL 223 #define OP_DIV 224 #define OP_LT 225 #define OP_GT 226 #define OP_LEQ 227 #define OP_GEQ 228 #define OP_EQ 229 // OP_DIFF is ~= #define OP_DIFF 230 // OP_PLUSPLUS is ++ #define OP_PLUSPLUS 231 // OP_ADDINC is += #define OP_ADDINC 232 #define CAR_A 400 #define CAR_B 401 #define CAR_C 402 #define CAR_AB 403 // literals #define T_ID 240 #define L_INTEGER 241 #define L_FLOAT 242 #define T_EOF 280 CS 3323 - Principles of Programming Languages Spring 2021 - Assignment 1 Download the files pl-scanner.yy, driver.c, tokens.h and Makefile from the assignment directory space. Then, perform the necessary modifications to the rule's file to recognize the below strings and tokens. The token codes can be found in the tokens.h header file. The assignment is due on February 26th, 2021, 11:59pm CT. Files must be uploaded by then. Late policy deduction applies. You can work in groups of up to 3 students. Groups remain the same for the whole semester. All students must upload the corresponding files. 1. (1.5pt) Add the necessary rules to recognize the arithmetic operators: +,-,*,/,+=++, =,==, -=,. See the file tokens.h to determine the constants to be used (they start with the prefix 'OP_'. 2. (2pt) Modify the rule returning the T ID token to recognize all identifiers matching the following: identifiers must start with the character 'Q'. the character immediately following the '@' must be a letter. Our language will be case insensitive, so there will be no distinction between upper and lower case strings. For example, the identifiers "SUM_42" and "sum_42" will be considered the same. characters following the initial 'Q' and the first letter can be any letter (upper or lower case), digit or the underscore? symbol. Examples of valid identifiers are commas used separate identifiers): Qiter, Qiii, Qali, Ca xe, @b_p, Qb20, QA_B_C_2.. Examples of invalid identifiers are commas used separate identifiers): -2, at, 2., 12.5, abC24, @_b43, -delta, Q. 3. (2.5pt) Create a new rule to recognize floating point numbers. The rule should return the token L FLOAT: Can start with a digit, the '+' or a ?-? Both the integer and fractional part should consist of at least one digit The integer and fractional part should be separated by a?' Examples of acceptable floating point numbers are: "10.0", " +1.5", "-10.50000", "0.9". Examples of strings that should be rejected are: "0.", ".01" 1 4. (1.5pt) Create a rule to recognize the following keywords: integer, float, foreach, begin, end, repeat, until, while, declare, if, then, print. The tokens corresponding to the keywords are those with the prefix K_ in the tokens.h header file. For convenience, a Makefile is provided, but you are not required to use it. Run: make to rebuild the scanner generator (lex.yy.c), and to recompile the driver. Some recommendations: Always include an action to execute even if it's an empty one. i.e. { }. The action associated to a detected pattern must start in the same line as where you write the pattern. Remember the issue with pattern prefixes (See slides and or corresponding class recording of Lexical Analysis). If something doesn't work, isolate the pattern you want to test in another file and test just that pattern. Don't feel afraid to run flex -h in the command line and test other flags/options. Don't insert any type of line or block comments as part of the regular expression patterns nor the actions. It will likely get Flex (and you) confused. #include #include #include "tokens.h" extern int yylex (); int main () { int next; while ((next = yylex ()) != T_E0F) { printf ("token = %d ", next); } return 0; } SRCS = driver.cc lex.yy. BIN = pl-scanner.exe CFLAGS = -03 -I. SCANNER = pl-scanner. #SCANNER = example-flex FLEX = flex CC = g++ all: scanner bin bin: $ (SRCS) $(CC) $ (CFLAGS) -0 $ (BIN) $ (SRCS) scanner: $(SCANNER).yy. $(FLEX) $(SCANNER).yy. clean: rm *.exe lex.yy...C *.0 1 2 /ame, ID; 3. 4. 5 #include "tokens.h" # undef yywrap. # define yywrap() 1 6 7. 8. 9 10 #undef YY_DECL #define YY_DECL int yylex() YY_DECL; 11 12 13 14 15 16 // Code run each time a pattern is matched. #undef YY_USER_ACTION # define YY_USER_ACTION {} 17 18 19 20 %} %option yylineno %option noyywrap DIGIT [0-9] ALPHA [a-zA-Z] 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 W.*$ [ \t]+ [ ]+ { return; } 40 { 41 42 return ';'; 43 } "=" { 44 45 46 47 48 return OP_ASSIGN; } 49 "main" { 50 return K_MAIN; } {DIGIT}+ { return L_INTEGER; } 51 52 53 54 55 56 57 58 59 60 61 62 63 64 {ALPHA}+ { return T_ID; } > { return T_EOF ; } { printf ("Unexpected character "); exit (1); } 65 66 67 68 69 // keywords #define K_FOREACH 200 #define K_PRINT 201 #define K_WHILE 202 #define K_REPEAT 203 #define K_UNTIL 204 #define K_BEGIN 205 #define K_END 206 #define K_DECLARE 209 #define K_IF 210 #define K_THEN 211 #define K_MAIN 212 #define K_INTEGER 213 #define K_FLOAT 214 // operators #define OP_ASSIGN 220 #define OP_ADD 221 #define OP_SUB 222 #define OP_MUL 223 #define OP_DIV 224 #define OP_LT 225 #define OP_GT 226 #define OP_LEQ 227 #define OP_GEQ 228 #define OP_EQ 229 // OP_DIFF is ~= #define OP_DIFF 230 // OP_PLUSPLUS is ++ #define OP_PLUSPLUS 231 // OP_ADDINC is += #define OP_ADDINC 232 #define CAR_A 400 #define CAR_B 401 #define CAR_C 402 #define CAR_AB 403 // literals #define T_ID 240 #define L_INTEGER 241 #define L_FLOAT 242 #define T_EOF 280

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

Students also viewed these Databases questions

Question

Define span of management or define span of control ?

Answered: 1 week ago

Question

What is meant by formal organisation ?

Answered: 1 week ago

Question

What is meant by staff authority ?

Answered: 1 week ago

Question

Discuss the various types of policies ?

Answered: 1 week ago

Question

5. Structure your speech to make it easy to listen to

Answered: 1 week ago

Question

1. Describe the goals of informative speaking

Answered: 1 week ago