Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Project Phase 1 -- Lexical Analysis Using lex Define the tokens in the FP language in lex definitions and feed it to lex to generate

Project Phase 1 -- Lexical Analysis Using lex Define the tokens in the FP language in lex definitions and feed it to lex to generate a scanner (lexer). Then, use the sample program as well as other testing programs written in FP to test your scanner. Note that it is your responsibility to convert the BNF (or verbal) definitions to lex definitions. The tokens in FP include: The set of keywords: Program, Function, return, if, then, else, while, do, or, and, print. The set of special symbols: {, }, (, ), =, +, , *, /, %, ==, >, <, >=, <=, !=. The set of other tokens: identifier, integer, float, character-string, Boolean. It is also your responsibility to insert appropriate statements (actions) in your lex definition file so that the lexer (created by lex) will generate a symbol table and print the output token names appropriately. You should design the symbol table such that it contains the necessary fields for this as well as the future projects.

Code Given:

%option noyywrap %{ #include #include #include struct node

{

char name[5];

char type[5];

struct node* next;

};

struct node* head;

%}

dt int|float|real|boolean

L [a-zA-Z]

D [0-9]

builtin main|print|scanf

id {L}({L}|{D})*

contdionalop {<|>|<=|>=|==}

whitesp [ \t]+

newline [ ]+

%%

if/"(" {printf("\"%s\" if loop. ",yytext);}

while/"{" {printf("\"%s\"while loop start. ",yytext);}

do/"{" {printf("\"%s\"do while loop start. ",yytext);}

while/"(" {printf("\"%s\"do while loop end. ",yytext);}

{D}+ {printf(" \"%s\" is a digit. ",yytext);}

contdionalop {printf("\"%s\" is a conditional operator. ",yytext);}

{dt} {printf(" \"%s\" is a datatype. ",yytext);}

{id}/"[" {printf("\"%s\" is an array. ",yytext);}

{id} {insertid(yytext,"id");}

{builtin}"(" {printf("\"%s\" is a built in function. ",yytext);}

{id}"(" {printf("\"%s\" is a user defined function. ",yytext);}

[);,]

"//"({L}({L}|{D})*|{whitesp})* {printf("\"%s\" is a single line comment. ",yytext);}

"\*"({L}({L}|{D})*|{whitesp}|{newline})*"*/" {printf("\"%s\" is a multiline line comment. ",yytext);}

%%

main()

{ //src.c yyin = fopen("src.c","r");

head = NULL;

yylex();

yywrap();

display();

}

insertid(char *str,char *type)

{

struct node* current = (struct node*)malloc(sizeof(struct node));

struct node* new = (struct node*)malloc(sizeof(struct node));

if(head==NULL)

{

head = (struct node*)malloc(sizeof(struct node));

strcpy(head->name,str);

strcpy(head->type,type);

head->next = NULL;

}

else

{

current = head;

while(current->next!=NULL)

{

current = current->next;

}

current->next = new;

strcpy(new->name,str);

strcpy(new->type,type);

new->next = NULL;

}

}

display()

{

printf(" NAME\t\tTYPE ");

printf("%s\t\t%s ",head->name,head->type);

struct node* current = (struct node*)malloc(sizeof(struct node));

struct node* new = (struct node*)malloc(sizeof(struct node));

current = head;

while(current->next!=NULL)

{

current = current->next;

printf("%s\t\t%s",current->name,head->type);

printf(" ");

}

}

extern int yywrap()

{

return 1;

}

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

Spatial Databases With Application To GIS

Authors: Philippe Rigaux, Michel Scholl, Agnès Voisard

1st Edition

1558605886, 978-1558605886

More Books

Students also viewed these Databases questions