Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CS 4110 Compiler Design - Project Phase 1. Lexical analysis Total Points: 15 In

ID: 3880630 • Letter: C

Question

CS 4110 Compiler Design - Project Phase 1. Lexical analysis Total Points: 15 In this project, you need to implement a compiler for a language defined here. The programming language you need to use is C or C++ (and the language defined by the corresponding tools). The project includes two phases, lexical analysis, and syntax analysis. In the following, we first define the language syntax a nd tokens. The definitions are given in BNF form. 1 Language Definitions 1.1 Syntax Definitions program : . "?”-program., program-name function-definitions statements “ }” program-nameidentifier function-definitions ::= (function-definition)" function-definition “(" “Function” function-name arguments statements "return.. retum-arg “}" function-name identifier arguments-(argument)" argument :identifier return-arg ::-identifier | statements(statement)+ statement ::- assignment-stmt function-call if-stmt while-stmt assignment-stmt-:= “(" 'identifier param eter "I' function-call ::-“{"function-name parameters ..}"I “{"predefined-function parameters ..!" predefined-function-“+” I “-” I “*” 1, 1 .%", I "print'. parameters (parameter)* parameter function-call | identifier number character-string Boolearn number-integer l float if-stmt : := “(" "if" expression"then" statements “else" statements" while-stmt ::-“{” ..while" expression “do" statements";" expression ::-“ {"comparison-operator parameter parameter “I'l “{"Boolean-operator expression expression")" | Boolean comparison-operator ::-"m" 11 Boolean-operator "or"| , 1=.. I "G" I ""s" "and"

Explanation / Answer

.lex file will scan .cpp code and print symbol table

program.lex contains following code

%{

#include<string.h>

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 [ ]+

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} {insert(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()

{

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

head = NULL;

yylex();

yywrap();

display();

}

void insert(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;

//printf(" INSERTED %s %s in head",head->name,head->type);

}

else

{

current = head;

while(current->next!=NULL)

{

current = current->next;

}

current->next = new;

strcpy(new->name,str);

strcpy(new->type,type);

new->next = NULL;

//printf(" INSERTED %s %s other than head",new->name,new->type);

}

}

void display()

{

printf(" NAME TYPE ");

printf("%s %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 %s",current->name,head->type);

printf(" ");

}

}

extern int yywrap()

{

return 1;

}

===================================================================================

int a,ab,a23;

this is sample code

which will act as input