📄 lab2.l
字号:
%{/*********************************************************************** Gerald Carter CSE 521 Spring 1994 lab2.l Description : This file contains the flex source code for a 'simple' C lexical analyzer.***********************************************************************//* INCLUDE FILES */#include <stdio.h> /* printf() */#include <malloc.h> /* malloc() */#include <string.h> /* strdup(), strcmp (), etc... */#include <stdlib.h> /* exit() */#include <time.h> /* localtime(), asctime(), ... */#include "lab2.h" /* node declaration an typedef *//* BOOLEAN MACROS */#define AND &&#define NOT !/* TOKEN MACROS */#define OPERATOR 260#define RES_WORD 261#define ID 262#define NUMBER 263/* GLOBAL VARIABLES */tree_node *root = NULL; /* pointer to root of tree */%}op ">="|"<="|"=="|"!="|"+="|"-="|"*="|"/="|"%="|"++"|"--"|";"|"("|")"|"{"|"}"|"+"|"-"|"*"|"/"|"%"|">"|"<"|"&"|"^"|"|"|"="|","reserved "break"|"continue"|"do"|"else"|"for"|"if"|"input"|"int"|"output"|"return"|"while"identifier [A-Z|a-z|_]([A-Z|a-z|0-9|_]{0,7})number ([0-9]{1,5})ws [ \t\n]+typo [^{op}{reseved}{identifier}{number}{ws}]%%{op} { return OPERATOR; }{reserved} { return RES_WORD; }{identifier} { return ID; }{number} { return NUMBER; }{ws} ;{typo} { printf ("Undefined symbol %s\n", yytext); exit (-1); }%%/* Insert() This function inserts the char* arguement into a binary search tree. PARAMTERS : new_lexeme : char * to token string GLOBAL ACCESS : root : pointer to root of binary search tree that holds the word RETURNS : int => 0 for correct completion*/int Insert (char* new_lexeme, int new_token_val){ tree_node *temp, *next; int found; /* No tree to insert into. 1. The root node must be allocated 2. char* must be assigned to node lexeme 3. leftchild and rightchild pointers must be iniatilized to NULL 4. node count set equal to 1 */ if (root==NULL) { if ((root = (tree_node*) malloc (sizeof(tree_node))) == NULL) { fprintf (stderr, "Unable to allocate memory for root.\n"); exit (-1); } root->leftchild = root->rightchild = NULL; if ((root->lexeme = strdup (new_lexeme)) == NULL) { fprintf (stderr, "Unable to allocate memory for lexeme in tree.\n"); exit (-1); } root->token_val = new_token_val; if (new_token_val == NUMBER) root->symbol_val = atoi (new_lexeme); root->count = 1; return 0; } /* set initial values */ temp = next = root; found = 0; /* loop to find if new_lexeme is in tree if new_lexeme is there increment count by 1 else find leaf node to attach new node to */ while ((NOT found) AND (next)) { temp = next; if (strcmp(temp->lexeme, new_lexeme) == 0) { ++(temp->count); found = 1; } else if (strcmp(temp->lexeme, new_lexeme) > 0) next = temp->leftchild; else next = temp->rightchild; } /* new_lexeme not in tree 1. allocate new node 2. strdup new_lexeme to node lexeme 3. set child pointers to NULL 4. set node count to 1 */ if (NOT found) { if (strcmp(temp->lexeme, new_lexeme) > 0) /* go left */ { if ((temp->leftchild=(tree_node*)malloc(sizeof(tree_node))) == NULL) { fprintf(stderr, "Unable to allocate memory for tree node.\n"); exit (-1); } temp = temp->leftchild; if ((temp->lexeme=strdup(new_lexeme)) == NULL) { fprintf(stderr, "Unable to allocate memory for lexeme in tree.\n"); exit (-1); } temp->leftchild = temp->rightchild = 0; temp->token_val = new_token_val; if (new_token_val == NUMBER) temp->symbol_val = atoi (new_lexeme); temp->count = 1; } else /* go right */ { if ((temp->rightchild=(tree_node*)malloc(sizeof(tree_node))) == NULL) { fprintf(stderr, "Unable to allocate memory for tree node.\n"); exit (-1); } temp = temp->rightchild; if ((temp->lexeme = strdup(new_lexeme)) == NULL) { fprintf(stderr, "Unable to allocate memory for lexeme in tree.\n"); exit (-1); } temp->leftchild = temp->rightchild = 0; temp->token_val = new_token_val; if (new_token_val == NUMBER) temp->symbol_val = atoi (new_lexeme); temp->count = 1; } } return 0; /* successful completion */}/* This function prints the binary search tree pointed to by root in order. The tree is printed to standard out. PARAMETERS : root : current tree node pointed to by root (ie. root of each sub-tree of recursive call. word_num : (int) sum of word count seen thus far GLOBAL ACCESS : none RETURNS : sum of node->count seen by function*/int PrintTable (tree_node *root, int word_num){ if (root) { word_num = PrintTable (root->leftchild, word_num); printf ("%-15s ", root->lexeme); switch (root->token_val) { case OPERATOR : printf ("Operator/Punctuation "); break; case RES_WORD : printf ("Reserved Word "); break; case ID : printf ("Identifier "); break; case NUMBER : printf ("Number "); break; } printf ("%-8d\n", root->count); word_num += root->count; word_num = PrintTable (root->rightchild, word_num); } return word_num;}/*************************************************************************** MAIN DRIVER***************************************************************************/int main (void){ int total_symbol_count = 0; /* total number of symbols in input */ int token_val = 0; /* value returned from yylex() */ struct tm *local; time_t t; /* Print Header Information */ t = time (NULL); local = localtime (&t); printf ("\t\t\tGerald Carter\n\t\t\tCSE 521"); printf ("\n\t\t\tAssignment #2\n\t\t\t%s\n", asctime(local)); /* loop to get next token and print corresponding description */ printf ("\t Lexeme Classification\n\n"); while (token_val = yylex()) { printf ("\t%15s ", yytext); switch (token_val) { case OPERATOR : printf ("Operator/Punctuation\n"); break; case RES_WORD : printf ("Reserved Word\n"); break; case ID : printf ("Identifier\n"); Insert (yytext, ID); break; case NUMBER : printf ("Number\n"); Insert (yytext, NUMBER); break; } } /* Print symbol table */ printf ("\n\nSymbol Table :\n\n"); total_symbol_count = PrintTable (root, total_symbol_count); printf ("\n\nThe total number of symbols read = %d", total_symbol_count);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -