Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

scanner.lex %{ #include zcalc.h #include int ch; int flag = 0; #define NAME 257 #define SEMISYM 268 #define COMMASYM 269 #define LPARSYM 270 #define RPARSYM

image text in transcribed

scanner.lex

%{ #include "zcalc.h" #include

int ch; int flag = 0;

#define NAME 257 #define SEMISYM 268 #define COMMASYM 269 #define LPARSYM 270 #define RPARSYM 271 #define EQSYM 272 #define PLUSSYM 273 #define MULTSYM 274 #define ASGNSYM 275 #define MINUSSYM 276 #define NUMBER 277

typedef struct { double dval; struct symtab *symp; } my_type;

static my_type yylval; %}

/* definitions for lex analyzer */ letter [A-Za-z] digit [0-9]+ ident {letter}({letter}|{digit})* ws [ \t ]+ other .

%%

{ws} ; /*---- Tokens and Actions---- */ "//".* ; ";" return SEMISYM; "," return COMMASYM; "(" return LPARSYM; ")" return RPARSYM; "==" return EQSYM; "+" return PLUSSYM; "*" return MULTSYM; "=" return ASGNSYM; "-" return MINUSSYM; /* end names and delimiters */ {ident} { struct symtab *sp = symlook(yytext); yylval.symp = sp; return NAME; } {digit} { yylval.dval = atof(yytext); return NUMBER; } "$" { return 0; } {other} ; /* ignore other stuff */ %%

void yyerror(char *mesg); /* yacc error checker */

struct symtab * symlook(s) char *s; { char *p; struct symtab *sp;

for(sp = symtab; sp name && !strcmp(sp->name, s)) return sp;

/* is it free */ if (!sp->name) { sp->name = (char *)strdup(s); return sp; } /* otherwise continue to the next */ } yyerror("Too many symbols... "); exit(1); }

addfunc(name, func) char *name; double(*func)(); { struct symtab *sp = symlook(name); sp->funcptr = func; }

void yyerror(char *mesg) /* yacc error function */ { flag = 1; printf("%s " , mesg); /* If error prints error and Do not accept to signify bad syntax in program */ }

main() /* Not needed with a parser */ { printf("Lex \t\tToken\t\t "); /* header on columns */ printf("---------------------------- "); do { ch = yylex(); if (ch == SEMISYM) printf("%s\t\tSEMICOLON ", yytext); else if (ch == COMMASYM) printf("%s\t\tCOMMA ", yytext); else if (ch == LPARSYM) printf("%s\t\tL_PARENTHESIS ", yytext); else if (ch == RPARSYM) printf("%s\t\tR_PARENTHESIS ", yytext); else if (ch == EQSYM) printf("%s\t\tEQ_OP ", yytext); else if (ch == PLUSSYM) printf("%s\t\tPLUS_OP ", yytext); else if (ch == MULTSYM) printf("%s\t\tMULT_OP ", yytext); else if (ch == ASGNSYM) printf("%s\t\tASSIGNMENT_STMT ", yytext); else if (ch == MINUSSYM) printf("%s\t\tMINUS_OP ", yytext); else if (ch == NUMBER) printf("%s\t\tNUMBER ", yytext); else if (ch == NAME) printf("%s\t\tNAME\t\t", yytext); else printf("%c ",ch); printf(" "); /* end check token read */ } while(ch != 0); /* read until end of file */ }

int yywrap() { return 1; }

zcalc.h

define NSYMS 20

struct symtab {

char *name;

double (*funcptr)();

double value;

} symtab[NSYMS];

struct symtab *symlook();

On Linux, flex is the fast lexical analyzer generator. The file scanner.lex contains the description of tokens to generate a simple and very basic scanner using flex. To generate the scanner, use the following command: flex scanner.lex You should notice that flex created the file lex.yy.c in your current directory. Now compile this file into an executable program as follows: gcc-o scanner lex.yy.c Note that you must have the zcalc.h header file in the same directory for the compile to be successful. The scanner will output the type of symbol that it recognized for the input that you typed. Now you can run the scanner executable and see what it does by typing in text and checking if it is recognized as a token. This scanner accepts a limited number of symbols such as "(", ")", operator symbols (i.e., "+", "*", and "-"), numbers, and identifiers (i.e., variable names). Type in a few of these symbols and see how the scanner responds. Now, let's try to enter the division operator "/" and modulus operator "%". What is the result? If the scanner does not recognize a symbol, it will simply not respond with the token, so it is vitally important that any valid symbol is accounted for. You may use Ctrl-D to terminate the scanner. Your task for this recitation assignment is to add flex support for the division and modulus operators in a similar fashion as is done for other arithmetic operators. To do this, you will need to modify the scanner.lex file to add support for these operator symbols in three locations: Add the constant definitions for the division and modulus operators. You may use any integer literal values following the existing set that are already defined. Add support for each symbol with the appropriate return value that matches the constant definitions added above. Inside main, add the appropriate else if branch and corresponding printf statement for each newly added symbol. Now, run flex again and re-compile the newly created lex.yy.c file to make sure it works as expected. On Linux, flex is the fast lexical analyzer generator. The file scanner.lex contains the description of tokens to generate a simple and very basic scanner using flex. To generate the scanner, use the following command: flex scanner.lex You should notice that flex created the file lex.yy.c in your current directory. Now compile this file into an executable program as follows: gcc-o scanner lex.yy.c Note that you must have the zcalc.h header file in the same directory for the compile to be successful. The scanner will output the type of symbol that it recognized for the input that you typed. Now you can run the scanner executable and see what it does by typing in text and checking if it is recognized as a token. This scanner accepts a limited number of symbols such as "(", ")", operator symbols (i.e., "+", "*", and "-"), numbers, and identifiers (i.e., variable names). Type in a few of these symbols and see how the scanner responds. Now, let's try to enter the division operator "/" and modulus operator "%". What is the result? If the scanner does not recognize a symbol, it will simply not respond with the token, so it is vitally important that any valid symbol is accounted for. You may use Ctrl-D to terminate the scanner. Your task for this recitation assignment is to add flex support for the division and modulus operators in a similar fashion as is done for other arithmetic operators. To do this, you will need to modify the scanner.lex file to add support for these operator symbols in three locations: Add the constant definitions for the division and modulus operators. You may use any integer literal values following the existing set that are already defined. Add support for each symbol with the appropriate return value that matches the constant definitions added above. Inside main, add the appropriate else if branch and corresponding printf statement for each newly added symbol. Now, run flex again and re-compile the newly created lex.yy.c file to make sure it works as expected

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

Recommended Textbook for

The Major Accounting Firms Understanding The Role Of Global Auditing Giants

Authors: Seth Nashe

1st Edition

B0CGKZ5Y2Q, 979-8859081318

More Books

Students also viewed these Accounting questions

Question

Identify and control your anxieties

Answered: 1 week ago

Question

Understanding and Addressing Anxiety

Answered: 1 week ago