COP 3402 Fall 2002 Lab #4 (20 pts - out of a total of 400 points for the course) Due: Monday, November 18 Note: The general comments at the beginning and end of the previous labs apply to this lab as well. So, you may refer to them to refresh your memory about guidelines. In this program, you will use lex (flex) and yacc (bison) to process simple assignment statements. Each assignment statement has the following syntax: variable = expression; Assume that each statement starts on a new line and terminates on the same line (i.e., it is not across several lines). Also assume that all statements are syntactically and semantically correct, i.e., no error checking. The variables will be only single upper-case letters, i.e., 26 variables. The expressions will contain integers, variables, and three binary operations: addition (indicated by '+'), min or smaller of two operands (indicated by '!'), and max or larger of two operands (indicated by '@'). Addition has precedence over min and max (min and max have the same precedence). Thus, the expression 10 ! 20 + 30 results in 10. The associativity for all three operations is left-to-right. Thus, the expression 10 ! 30 @ 20 results in 20. Parenthesized expressions have higher precedence, i.e., parentheses may be used in the expressions to override precedence/associativity. Thus, the expression (10 ! 20) + 30 results in 40 and the expression 10 ! (30 @ 20) results in 10. I have provided parts of the yacc (bison) and lex (flex) input below, but you don't have to use them if you don't want to. /********************************************************************/ /* lab4.y */ %{ /* definitions */ #include #include /* INT_MIN is defined in this header file */ #define LETTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZ" char symbols[27]; /* contains the 26 variable names */ int values[26]; /* contains values for the 26 variables */ %} %start startsym %token VARIABLE NUMBER %% /* rules */ startsym : assignlist ; /***** YOU DO THE REMAINING RULES! *****/ %% /* user routines */ main() { int k; strcpy(symbols, LETTERS); /* initialize the variables to a "dummy" value */ for ( k = 0; k < 26; ++k ) values[k] = INT_MIN; /* process the assignment statements */ yyparse(); /* print the final values for the variables */ printf(" Symbol Table \n"); printf("Symbol Value \n"); for ( k = 0; k < 26; ++k ) if ( values[k] != INT_MIN ) printf(" %c %5d \n", symbols[k], values[k]); }/* end of main */ /********************************************************************/ /* lab4.l */ %{ /* definitions */ #include "lab4.tab.h" extern char symbols[]; int findindex(); %} %% /* rules */ [ ]+ {/* skipping blanks */ ; } /***** YOU DO THE REMAINING RULES! *****/ If token is a number, the "action" part of the rule should set "yylval" to the value of the number and return NUMBER. If token is a variable, the "action" part of the rule should set "yylval" to the index of the variable and return VARIABLE. Other rules should be straightforward! %% /* user routines */ int findindex(sym) /* This routine finds the index for a variable. */ char sym; { int k; for ( k = 0; k < 26; ++k ) if ( symbols[k] == sym ) return(k); printf("*** ERROR: The variable %c is not A-Z *** \n", sym); }/* end of findindex */ /********************************************************************/ /* Sample lab4.inp */ B = 12; M = B + 8; P = 10 @ 30; Q = B + M @ P; R = P ! Q; S = 10 ! 20 + 30; T = 10 ! 30 @ 20; U = (10 ! 20) + 30; X = 10 ! (30 @ 20); /********************************************************************/ /* Sample lab4.out */ Symbol Table Symbol Value B 12 M 20 P 30 Q 32 R 30 S 10 T 20 U 40 X 10 /********************************************************************/ - Note that there is no error checking. - The input data will follow the exact format specified in Sample Input, and your output must follow the exact format specified in Sample Output. /********************************************************************/ What To Turn In --------------- - A disk containing lab4.y, lab4.l, executable code, and other relevant files (if any). - A hard copy of lab4.y and lab4.l. - One week before the lab is due, I will put the input on the course web site. Download the input file and run your program with this input to generate an output file. Turn in a hard copy of this output file as well. - Put all these in an envelope; they will be returned to you in the envelope. Please use a large envelope so that you don't have to fold your printouts. /********************************************************************/