driver.cc
makefile
pl-scanner.yy
tokens.h
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 10th, 2020, 11:59pm. Files must be uploaded by then. Late policy deduction applies. 1. (ipt) Add the necessary rules to recognize the arithmetic operators: +, -,,/,=,=-, !,. See the file tokens.h to determine the constants to be used. 2. (2pt) Modify the rule returning the T.ID token to recognize all identifiers matching the following: identifiers can start with an underscore ('_') or a letter, both lower or uppercase all identifiers must have a minimum length of 2 characters all identifiers must have at least one letter identifiers can consist of letters, the underscore character or digits Examples of valid identifiers are: ,, al, a, b, bl, .counter, ab.c.2., 13. Examples of invalid identifiers are: - 2, a, 2.. 3. (Ipt) Create a new rule to recognize floating point numbers. The rule should return the token T.FLOAT: . Can start with a digit, the '+'ora! . 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 string that should be rejected are: "0."."01" 4. (pt) Create a rule to recognize the following keywords: integer, float, foreach, begin, end, repeat, until, while, declare, if, then. 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. Several online resources can be found in the web, for instance: #include
#include #include "tokens.h" extern int yylex (; int main( int next; while ((next = yylex () != T_EOF) { printf ("token = %d ", next); return o; SRCS = driver.cc lex.YY.C BIN = pl-scanner.exe CFLAGS = -03 -I. SCANNER = pl-scanner 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 % 3 #include "tokens.h" 4 # undef yywrap 5 # define yywrap() 1 9 #undef YY_DECL 10 #define YY_DECL int yylex(); 11 YY_DECL; 14 // Code run each time a pattern is matched. 15 #undef YY_USER_ACTION 16 # define 18_USER_ACTION {} 22 %option yylineno %option noyywrap DIGIT [0-9] ALPHA [a-zA-Z] VN. $ [ \t]+ [ ]+ Xcode File Edit View Find Navigate Editor Product Debug Source Control pl-scanner.yy 3 pl-scanner.yy / No Selection return ';'; return 's'; return K MAIN; (DIGIT return L_INTEGER; (ALPHA) + return T_ID; > { return T.EOF; } printf ("Unexpected character "); exit (1); ) // 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 OPLEQ 227 #define OPGEQ 228 #define OP EQ 229 #define OP INEQ 230 // literals #define T ID 240 #define L INTEGER 241 #define L_FLOAT 242 #define TEOF 280 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 10th, 2020, 11:59pm. Files must be uploaded by then. Late policy deduction applies. 1. (ipt) Add the necessary rules to recognize the arithmetic operators: +, -,,/,=,=-, !,. See the file tokens.h to determine the constants to be used. 2. (2pt) Modify the rule returning the T.ID token to recognize all identifiers matching the following: identifiers can start with an underscore ('_') or a letter, both lower or uppercase all identifiers must have a minimum length of 2 characters all identifiers must have at least one letter identifiers can consist of letters, the underscore character or digits Examples of valid identifiers are: ,, al, a, b, bl, .counter, ab.c.2., 13. Examples of invalid identifiers are: - 2, a, 2.. 3. (Ipt) Create a new rule to recognize floating point numbers. The rule should return the token T.FLOAT: . Can start with a digit, the '+'ora! . 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 string that should be rejected are: "0."."01" 4. (pt) Create a rule to recognize the following keywords: integer, float, foreach, begin, end, repeat, until, while, declare, if, then. 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. Several online resources can be found in the web, for instance: #include #include #include "tokens.h" extern int yylex (; int main( int next; while ((next = yylex () != T_EOF) { printf ("token = %d ", next); return o; SRCS = driver.cc lex.YY.C BIN = pl-scanner.exe CFLAGS = -03 -I. SCANNER = pl-scanner 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 % 3 #include "tokens.h" 4 # undef yywrap 5 # define yywrap() 1 9 #undef YY_DECL 10 #define YY_DECL int yylex(); 11 YY_DECL; 14 // Code run each time a pattern is matched. 15 #undef YY_USER_ACTION 16 # define 18_USER_ACTION {} 22 %option yylineno %option noyywrap DIGIT [0-9] ALPHA [a-zA-Z] VN. $ [ \t]+ [ ]+ Xcode File Edit View Find Navigate Editor Product Debug Source Control pl-scanner.yy 3 pl-scanner.yy / No Selection return ';'; return 's'; return K MAIN; (DIGIT return L_INTEGER; (ALPHA) + return T_ID; > { return T.EOF; } printf ("Unexpected character "); exit (1); ) // 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 OPLEQ 227 #define OPGEQ 228 #define OP EQ 229 #define OP INEQ 230 // literals #define T ID 240 #define L INTEGER 241 #define L_FLOAT 242 #define TEOF 280