Question
Implement a scanner for the language over this grammar using the nested cases approach. Start with determining what should be the tokensin this grammar. Then,
Implement a scanner for the language over this grammar using the nested cases approach. Start with determining what should be the tokensin this grammar. Then, using a pencil and paper, draw an FSM for the scanner. Verify that the FSM indeed accepts the tokens of the language over this grammar. When you convince yourself that the FSM is correct, start coding. Do not use any library-based scanning facilities like strtok() or strsep()in your implementation. Test your scanner on the following program: firstvar := 1; secondvar := 2; repeat (10) thirdvar := 2 * (firstvar + secondvar) / (firstvar + 2); repeat (firstvar + 2 * secondvar) repeat (thirdvar) print firstvar; Print the tokens as they are recognized by the scanner as follows: {
#include
typedef enum { INVALID_TOKEN = 0, NUMBER_TOKEN, IDENT_TOKEN, ASSIGNMENT_TOKEN, SEMICOLON_TOKEN, LPAREN_TOKEN, RPAREN_TOKEN, PLUS_TOKEN, MINUS_TOKEN, MULT_TOKEN, DIV_TOKEN, MOD_TOKEN, REPEAT_TOKEN, PRINT_TOKEN, END_OF_INPUT_TOKEN } TOKEN_TYPE;
typedef struct token { TOKEN_TYPE type; char *strVal; } TOKEN;
TOKEN *scannerAdHoc();
void ungetToken(TOKEN **);
void freeToken(TOKEN **);
#define BUF_SIZE 128 #define MAX_LINE_LENGTH 256
#endif
#include "scanner.h"
int main(int argc, char **argv) { freopen(argv[1], "r", stdin);
TOKEN *token = NULL; char *token2str[] = {"INVALID", "NUMBER", "IDENT", "ASSIGNMENT", "SEMICOLON", "LPAREN", "RPAREN", "PLUS", "MINUS", "MULT", "DIV", "MOD", "REPEAT", "PRINT", "END_OF_INPUT"}; printf(" "); while ((token = scannerAdHoc()) != NULL) { if ( token->strVal == NULL) printf("{%s} ", token2str[token->type]); else printf("{%s, %s} ", token2str[token->type], token->strVal); freeToken(&token); fflush(stdout); } printf(" "); exit(EXIT_SUCCESS); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started