PROGRAM 3
/**************************************************************************/
/* author: anthony f. ortiz */
/* program: scanner.l */
/* class: cs 6110 theory and design of compilers */
/* date: november 9, 1998 */
/* description: this program implements the scanner and symbol table */
/* part of a compiler. */
/**************************************************************************/
#include "defs.h"
#include "y.tab.h"
#include
#include
#include
int hash_it (char * word);
struct word * add_symtable (char * word);
struct word * lookup_symtable (char * word);
void print_symtable ();
void update_program (char * str);
void update_procedure (char * str);
void update_function (char * str, type_t type);
void update_parameters (type_t type, int ref);
void destroy_paramlist ();
void decrement_address ();
void increment_address ();
void start_addressing (int start);
void set_dim0 ();
void increment_dim ();
void update_variables (char * str, type_t type, int d, int low, int high);
void update_varlist (char * str);
void destroy_varlist ();
void update_variables2 (type_t type, int d, int low, int high);
void write_file(void);
int a_type(type_t t1);
void write_file(void);
void print_quad (quad_t qq);
void print_prog(void);
int start_program ();
void quit_program (int top);
void handle_integers (type_t type, int value);
void handle_reals (type_t type, double value);
void handle_variables (type_t type, int first, int index);
void handle_assignments (type_t type, int first, int index, type_t type2,
int first2);
void handle_writes (type_t type, int first);
void handle_reads (type_t type, int first, int index);
void handle_calculations (type_t type, int first, int sign, type_t type2,
int first2);
void handle_inequalities (type_t type, int first, int sign, type_t
type2, int first2);
void handle_bool (type_t type, int first, int sign, type_t type2, int first2);
int handle_then (int first);
int handle_else (int instr);
void handle_end (int instr);
int save_loc ();
int start_while (int first);
void end_while (int start, int end);
int declaring = 0;
%}
%%
"and" {
yylval.ival = AND;
printf (" ");
return (AND);
}
"array" {
printf (" ");
return (ARRAY);
}
"boolean" {
printf (" ");
return (BOOL);
}
"break" {
printf (" ");
return (BREAK);
}
"continue" {
printf (" ");
return (CONT);
}
"case" {
printf (" ");
return (CASE);
}
"declare" {
declaring = 1;
printf (" ");
return (DECLARE);
}
"div" {
yylval.ival = DIV;
printf (" ");
return (MULTOP);
}
"do" {
printf (" ");
return (DO);
}
"else" {
printf (" ");
return (ELSE);
}
"end" {
if (declaring == 1)
declaring = 0;
printf (" ");
return (END);
}
"function" {
declaring = 1;
printf (" ");
return (FUNCTION);
}
"if" {
printf (" ");
return (IF);
}
"integer" {
printf (" ");
return (INT);
}
"mod" {
yylval.ival = MOD;
printf (" ");
return (MULTOP);
}
"not" {
yylval.ival = NOT;
printf (" ");
return (NOT);
}
"of" {
printf (" ");
return (OF);
}
"or" {
yylval.ival = OR;
printf (" ");
return (OR);
}
"procedure" {
declaring = 1;
printf (" ");
return (PROCEDURE);
}
"program" {
printf ("\nTOKENS: ");
return (PROGRAM);
}
"read" {
printf (" ");
return (READ);
}
"real" {
printf (" ");
return (REAL);
}
"ref" {
printf ("[ ");
return (REF);
}
"return" {
printf (" ");
return (RETURN);
}
"returns" {
printf (" ");
return (RETURNS);
}
"then" {
printf (" ");
return (THEN);
}
"while" {
printf (" ");
return (WHILE);
}
"write" {
printf (" ");
return (WRITE);
}
"true" {
yylval.ival = TRUE;
printf ("", yylval.ival);
return (BCONST);
}
"false" {
yylval.ival = FALSE;
printf ("", yylval.ival);
return (BCONST);
}
[ \n\t]+ {/* white space */}
"//".*"\n" {/* comments */}
":=" {
printf (" ");
return (ASSGN);
}
":" {
printf (" ");
return (COL);
}
"," {
printf (" ");
return (COMMA);
}
"=" {
yylval.ival = EQ;
printf (" ");
return (RELOP);
}
"/" {
yylval.ival = FDIV;
printf (" ");
return (MULTOP);
}
">=" {
yylval.ival = GEQ;
printf (" ");
return (RELOP);
}
">" {
yylval.ival = GR;
printf (" ");
return (RELOP);
}
[A-Za-z_][A-Za-z0-9_]* {
yylval.pval = lookup_symtable (yytext);
if (yylval.pval == NULL || declaring == 1)
{
yylval.pval = add_symtable (yytext);
}
printf (" ", yytext);
return (IDENT);
}
[0-9]+ {
yylval.ival = atoi (yytext);
printf (" ", yylval.ival);
return (INTCONST);
}
"[" {
printf (" ");
return (LBRACK);
}
"{" {
printf ("");
return (LBRACE);
}
"<=" {
yylval.ival = LEQ;
printf (" ");
return (RELOP);
}
"<" {
yylval.ival = LESS;
printf (" ");
return (RELOP);
}
"(" {
printf (" ");
return (LP);
}
"-" {
yylval.ival = MINUS;
printf (" ");
return (ADDOP);
}
"!=" {
yylval.ival = NEQ;
printf (" ");
return (RELOP);
}
"+" {
yylval.ival = PLUS;
printf (" ");
return (ADDOP);
}
".." {
printf (" ");
return (RANGE);
}
"]" {
printf (" ");
return (RBRACK);
}
"}" {
printf (" ");
return (RBRACE);
}
")" {
printf ("]
BACK TO CS6110 PAGE.