22
[COMPILER DESIGN LAB] 5 th Sem Autonomous Scheme 1 of 22 COMPILER DESIGN LABORATORY Sub. Code: 13CS306 IA Marks: 50 Hrs. / Week: 03 Exam Hours: 03 Total Hrs: 42 Exam Marks: 50 LIST OF PROGRAMS Part A Execute the following programs using LEX: 1. Convert the upper case characters to lower case characters in a file. 2. Display the ASCII values of non-printing characters which are present in a file. 3. Count number of characters, words, lines, and spaces in a given file. 4. Count the number of comment lines in a given C program. Also eliminate them and copy the resulting program into a separate file. 5. Convert an octal number to its equivalent decimal number. 6. Recognize and count the number of identifiers in a given input file. Execute the following programs using YACC: 1. Recognize IF control statements and display number of levels of nesting. 2. Recognize a valid variable name, which starts with a letter, followed by any number of letters or digits? 3. Check whether the given string a n b m (n>0, m>=0) is accepted by the grammar or not. 4. Check for the validity of a given arithmetic expression. 5. Evaluate an arithmetic expression involving operators +, -, and /. 6. Check whether the given string a n b n (n>0) is accepted by the grammar or not. Part B Execute the following programs using ANTLR: 1. Combined grammar for implementing simple calculator. 2. Check whether the given sentence in English is valid or not. 3. Implement input construct in C/Java language. 4. Write a combined grammar to implement output construct in C/Java language. 5. Write a combined grammar to implement ASSIGNMENT construct in C++ language. 6. Write a combined grammar to implement FOR construct in FORTRAN / C++ language. Part C (Mini Projects) 1. Project#1a Development of Lexical analyzer for C, C++, Python, FORTRAN, PASCAL or any other programming language. Project#1b Development of Lexical analyzer for HTML, XML, Perl, PHP or any other scripting language. 2. Project#2 Development of Parser / code optimizer for any programming / scripting language. 3. Project#3 Development of Syntax directed translator / semantic analyzer for any language.

CD Lab Manual 2015_PESIT

  • Upload
    sai

  • View
    17

  • Download
    1

Embed Size (px)

DESCRIPTION

PESIT LAB MANUAL 2015

Citation preview

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 1 of 22

COMPILER DESIGN LABORATORY Sub. Code: 13CS306 IA Marks: 50 Hrs. / Week: 03 Exam Hours: 03 Total Hrs: 42 Exam Marks: 50

LIST OF PROGRAMS

Part A Execute the following programs using LEX:

1. Convert the upper case characters to lower case characters in a file.

2. Display the ASCII values of non-printing characters which are present in a file.

3. Count number of characters, words, lines, and spaces in a given file.

4. Count the number of comment lines in a given C program. Also eliminate them and copy the resulting program into a separate file.

5. Convert an octal number to its equivalent decimal number.

6. Recognize and count the number of identifiers in a given input file.

Execute the following programs using YACC:

1. Recognize IF control statements and display number of levels of nesting.

2. Recognize a valid variable name, which starts with a letter, followed by any number of letters or digits?

3. Check whether the given string an bm (n>0, m>=0) is accepted by the grammar or not.

4. Check for the validity of a given arithmetic expression.

5. Evaluate an arithmetic expression involving operators +, -, and /.

6. Check whether the given string an bn (n>0) is accepted by the grammar or not.

Part B

Execute the following programs using ANTLR:

1. Combined grammar for implementing simple calculator.

2. Check whether the given sentence in English is valid or not.

3. Implement input construct in C/Java language.

4. Write a combined grammar to implement output construct in C/Java language.

5. Write a combined grammar to implement ASSIGNMENT construct in C++ language.

6. Write a combined grammar to implement FOR construct in FORTRAN / C++ language.

Part C (Mini Projects)

1. Project#1a

Development of Lexical analyzer for C, C++, Python, FORTRAN, PASCAL or any other programming language.

Project#1b Development of Lexical analyzer for HTML, XML, Perl, PHP or any other scripting language.

2. Project#2 Development of Parser / code optimizer for any programming / scripting language.

3. Project#3 Development of Syntax directed translator / semantic analyzer for any language.

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 2 of 22

Write a program to convert the upper case characters into lower case characters in a file.

%{

/* Lex specification to convert upper case characters to lower case. */

#include<stdio.h>

%}

%%

[A-Z] {putchar ( 'a' + ( yytext[0] - 'A' ) ); }

.|\n {ECHO;}

%%

int main(int argc , char **argv )

{

/* Input is taken from stdin, output is to stdout */

yylex();

return 0;

}

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 3 of 22

Write a program to count the number of comment lines in a given C program. Also eliminate them and copy

the resulting program into a separate file.

%{

/* Lex specification to count number of comment lines in a file and eliminate them. */

int count=0, flag=0;

%}

%%

[\t ]+ {fprintf(yyout, yytext);}

"/*" {flag=1; count++ ;}

. {if(flag) fprintf(yyout, " ");

else fprintf(yyout, yytext);}

"*/" {flag=0;}

%%

int main(int argc, char **argv)

{

char sname[25], dname[25];

if(argc!=3)

{

printf("Enter the source file\n");

scanf("%s", sname);

printf("Enter the destination file\n");

scanf("%s", dname);

yyin=fopen(sname, "r");

yyout=fopen(dname, "w");

}

else

{

yyin=fopen(argv[1], "r");

yyout=fopen(argv[2], "w");

}

yylex();

printf("The number of comments are=%d\n", count);

return 0;

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 4 of 22

}

Write a program to display the ASCII values of non-printing characters which are present in a file.

%{

/* Lex Specification to print the ASCII values of the non-printable characters from the input

file. The input file name is passed as a command-line argument. */

#include<stdio.h>

#include<ctype.h>

/* For isprint() function */

#include<stdlib.h>

/* To store the non-printable characters found so far */

/* The size of the array is the max number of different ASCII characters possible */

int nonPrintable[256];

int arrayIndex=0, i;

%}

%%

.|\n {/* Check if the character is non-printable or not */

if(!isprint(yytext[0]))

{ /* Check if we have already found this character */

for(i=0;i<arrayIndex;++i)

if(yytext[0]==nonPrintable[i])

break;

/* If we have not found this character before, then output it */

if(i<=arrayIndex)

{

printf("\n%5d\n", yytext[0]);

nonPrintable[arrayIndex++]=yytext[0];

}

}

}

%%

int main(int argc, char **argv)

{ /* Check if the arguments are OK */

if(argc!=2)

{

fprintf(stderr, "Usage: %s filename\n", argv[0]);

exit(1);

}

/* Open the file on yyin */

yyin=fopen(argv[1], "r");

if(!yyin)

{

fprintf(stderr, "Could not open %s\n", argv[1]);

exit(1);

}

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 5 of 22

/* Call the lexer */

printf("The following non-printable ASCIIs were found in the file --\n");

yylex();

/* Close the file */

fclose(yyin);

return 0;

}

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 6 of 22

Write a program to convert an octal number to its equivalent decimal number.

%{

/* Lex specification to convert an octal number to its equivalent decimal number. */

#include<stdio.h>

int octal=0, decimal=0;

int valid=0;

%}

%%

[0-7]+ {valid=1; octal=atoi(yytext);}

[\n] {;}

. {valid=0;}

%%

yywrap()

{

if(valid)

{

decimal=octalToDecimal(octal);

printf("Decimal Equivalent is: %d", decimal);

}

else

{

printf("Invalid Number");

}

}

int octalToDecimal(int n)

{

int digit=0, sum=0, i=0;

while(n)

{

digit=n%10;

sum+=digit*power(8, i);

n=n/10;

i++;

}

return sum;

}

int power(int a, int n)

{

int i=0, tempsum=1;

for(i=0; i<n; i ++)

tempsum=tempsum*a;

return tempsum;

}

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 7 of 22

int main()

{

printf("Enter the octal number :");

yylex();

return 0;

}

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 8 of 22

Write a program to count number of characters, words, lines, and spaces in a given file.

%{

/* Lex specification to count spaces, lines, words, characters. */

int nword=0,nline=0,nchar=0,nspace=0;

%}

%%

[a-zA-Z0-9]+ {nword++;nchar+=yyleng;}

[ ]+ {nspace++;}

\n {nline++;}

. {nchar++;}

%%

int main(int argc, char **argv)

{

char sname[25];

if(argc!=2)

{

printf("Enter the source file\n");

scanf("%s", sname);

yyin=fopen(sname, "r");

}

else

yyin=fopen(argv[1], "r");

yylex();

printf("Number of words=%d\n", nword);

printf("Number of characters=%d\n", nchar);

printf("Number of spaces=%d\n", nspace);

printf("Number of lines=%d\n", nline);

return 0;

}

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 9 of 22

Write a program to recognize and count the number of identifiers in a given input file.

%{

/* Lex specification to count identifiers in a file. */

int flag=0,id=0;

%}

%%

int|long|float|double|char {flag=1;}

[,] {

if(flag)

id++;

}

[;] {

if(flag)

{

id++;

flag=0;

}

}

. {;}

%%

int main(int argc, char **argv)

{

char *source;

if(argc>1)

{

yyin=fopen(argv[1], "r");

if(!yyin)

{

printf("Error in opening the file %s\n", argv[1]);

exit(0);

}

}

else

{

printf("Enter source\n");

scanf("%s", source);

yyin=fopen(source, "r");

if(!yyin)

{

printf("Error in opening the file %s\n", source);

exit(0);

}

}

yylex();

printf("Number of identifiers=%d\n", id);

return 0;

}

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 10 of 22

Write a program to check for the validity of a given arithmetic expression.

%{

/* Yacc specification to test the validity of a simple expression. */

#include <stdio.h>

%}

%token NUMBER

%left '+' '-'

%left '*' '/'

%%

lines:

|lines '\n'

|lines exp '\n' {printf("\nValid expression\n");}

;

exp:'-' exp

|exp '+' exp

|exp '-' exp

|exp '*' exp

|exp '/' exp

|'(' exp ')'

|NUMBER

;

%%

yyerror()

{

printf("\nError");

exit(0);

}

yylex()

{

char ch;

ch=getchar();

if(isdigit(ch))

{

ungetc(ch, stdin);

scanf("%d", &yylval);

return NUMBER;

}

return ch;

}

int main()

{

printf("Enter an expression");

yyparse();

return 0;

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 11 of 22

}Write a program to recognize IF control statements and display number of levels of nesting.

%{

/* Yacc specification to recognize IF control statements and display number of levels of

nesting. */

#include<stdio.h>

%}

%token IF COND OB CB

%%

lines :

| lines ifstmt;

ifstmt: IF COND OB lines CB;

%%

yyerror()

{

printf(“\nInvalid IF structure.”);

exit(0);

}

int main(int argc, char **argv)

{

extern int maxlevel;

extern FILE *yyin;

char source[30];

if(argc==1)

{

printf(“\nEnter the source file name : “);

scanf(“%s”, source);

yyin=fopen(source, “r”);

}

else

yyin=fopen(argv[1], “r”);

yyparse();

printf(“\nValid IF structure”);

printf(“\nDeapth of the structure = %d\n”, maxlevel);

return 0;

}

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 12 of 22

Write a program to recognize IF control statements and display number of levels of nesting.

%{

/* Lex specification to recognize IF control statement and display number of levels of

nesting. */

#include"y.tab.h"

int level=0,maxlevel=0,flag=0,flag1=0,flag2=0,flag3=0;

%}

%%

[ \n\t]*"if"[ \n\t]* {flag=1;flag1=1;flag2=1;return IF;}

[ \n\t]*"(".*")"[ \n\t]* {if(flag==1) {flag=0;return COND;}}

[ \n\t]*"{"[ \n\t]* {if((flag1==1)&&(flag2==1))

{flag2==0; level++;

if(maxlevel<level)

{maxlevel=level;}

return OB;}

else flag3=1;}

[ \n\t]*"}" [ \n\t]* {if((flag1==1)&&(flag3==0))

{level--; if(level==0)

{flag1=0;}

return CB;}

else flag3=0;}

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 13 of 22

%%Write a program to evaluate an arithmetic expression involving operators +, -, *, and /.

%{

/* Yacc specification to evaluate an arithmetic expression involving operators +, -, *,

and /. */

#include<stdio.h>

%}

%token DIGIT, IDENTIFIERS

%left '*' '/'

%left '+' '-'

%right UMINUS

%%

lines:

|lines '\n'

|lines exp '\n' { printf("\nValid expression\n");}

;

exp:'-' exp

|exp '+' exp

|exp '-' exp

|exp '*' exp

|exp '/' exp

|'(' exp ')'

|NUMBER

|IDENTIFIER

;

%%

yyerror()

{

printf("\nError");

exit(0);

}

yylex()

{

int ch;

while((ch=getchar())==' ');

if(isdigit(ch))

return NUMBER;

else if(isalpha(ch))

return IDENTIFIER;

else

return ch;

}

int main()

{

printf("Enter an expression\n");

yyparse();

return 0;

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 14 of 22

}Write a program to recognize a valid variable name, which starts with a letter, followed by any number

of letters or digits.

%{

/* Yacc specification to recognize a valid variable name. */

#include<stdio.h>

%}

%token letter digit

%%

start:

|letter A '\n'

|letter ;

A :letter A

|digit A

|letter

|digit ;

%%

yyerror()

{

printf("The variable is not in valid format\n");

exit(1);

}

yylex()

{

int ch;

ch=getchar();

if(isalpha(ch) && islower(ch))

return letter;

else if(isdigit(ch))

return digit;

else

return ch;

}

int main()

{

printf("Enter the variable\n");

yyparse();

printf("Valid variable\n");

return 0;

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 15 of 22

}Write a program to check whether the given string an bn (n>0) is accepted by the grammar or not.

%{

/* Yacc specification to recognize the given string anbn (n>=0) is accepted by the

grammar or not. */

#include <stdio.h>

%}

%token A B

%%

s:A s B

|A B

|

;

%%

yyerror()

{

printf("The string does not belong to the grammar provided\n");

exit(1);

}

int yylex()

{

int ch;

ch=getchar();

if(ch=='a')

return A;

else if(ch=='b')

return B;

else if(ch=='\n')

return 0;

else return ch;

}

int main()

{

printf("Enter the expression\n");

yyparse();

printf("The string is a part of the defined grammar\n");

return 0;

}

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 16 of 22

Write a combined grammar for implementing simple calculator.

grammar SimpleCalc;

options

{

language = Java;

}

tokens

{

PLUS = '+';

MINUS = '-';

MULT = '*';

DIV = '/';

}

@members

{

publicstaticvoid main(String[] args) throws Exception

{

SimpleCalcLexer lex = new SimpleCalcLexer(new ANTLRFileStream(args[0]));

CommonTokenStream tokens = new CommonTokenStream(lex);

SimpleCalcParser parser = new SimpleCalcParser(tokens);

try

{

parser.expr();

}

catch(RecognitionException e)

{

e.printStackTrace();

}

}

}

/*Parser Rules*/

expr : term((PLUS|MINUS)term)*; term : factor((MULT|DIV)factor)*; factor : NUMBER;

/*Lexer Rules*/

NUMBER : (DIGIT)+; WHITESPACE : ('\t'|' '|'\n')+ {$channel = HIDDEN;};

fragment DIGIT : '0'..'9';

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 17 of 22

Write a combined grammar to check whether the given sentence in English is valid or not.

grammar Sentence;

options

{

language = Java;

}

tokens

{

GET = 'get';

PUT = 'put';

CHANGE = 'change';

DATA = 'data';

METADATA = 'metadata';

DEPENDENCIES = 'dependencies';

DEPENDENTS = 'dependents';

FROM = 'from';

IN = 'in';

ABOUT = 'about';

OF = 'of';

}

@members

{

publicstaticvoid main(String[] args) throws Exception

{

SentenceLexer lex = new SentenceLexer(new ANTLRFileStream(args[0]));

CommonTokenStream tokens = new CommonTokenStream(lex);

SentenceParser parser = new SentenceParser(tokens);

try

{

parser.expr();

}

catch(RecognitionException e)

{

e.printStackTrace();

}

}

}

command : sentence (NEWLINEsentence)* NEWLINE? EOF; sentence : WS? verbWSobjectWSprepositionWStargetWS?; verb : GET | PUT | CHANGE; object : DATA | METADATA | DEPENDENCIES | DEPENDENTS | STATISTICS; preposition : IN | ABOUT | OF | FROM; target : FILE;

FILE : ('a'..'z' | 'A'..'Z' | '0'..'9' | '.')+; NEWLINE : '\r'?'\n'; WS : (' ' | '\r' | '\n' | '\t')+ {SKIP();};

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 18 of 22

Write a combined grammar to implement READ construct in PASCAL language.

grammar read;

options {

language = Java;

}

tokens {

PLUS = '+';

MINUS = '-';

MULT = '*';

DIV = '/';

EQUAL = ':=';

OPENB = '(';

CLOSEB = ')';

COMMA = ',';

}

@members {

publicstaticvoid main(String[] args) throws Exception

{

readLexer lex = new readLexer(new ANTLRFileStream(args[0]));

CommonTokenStream tokens = new CommonTokenStream(lex);

readParser parser = new readParser(tokens);

try

{

parser.expr();

}

catch(RecognitionException e)

{

e.printStackTrace();

}

}

}

/* Lexer rules */

NUMBER: (DIGIT)+('\.'(DIGIT)+)?; WS: ('\t'|' '|'\r'|'\n')+ {$channel = HIDDEN}; READ: 'read'; ID: (ALPHA)+; fragment DIGIT: '0'..'9'; fragment ALPHA: 'a'..'z';

/* Parser rules */

read: READOPENBidlistCLOSEB; idlist: ID (COMMAID)*; lhs: IDEQUALexpr; expr: term((PLUS|MINUS)term)*; term: factor((MULT|DIV)factor)*; factor: NUMBER|ID;

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 19 of 22

Write a combined grammar to implement WRITE construct in PASCAL language.

grammar write;

options {

language = Java;

}

tokens {

PLUS = '+';

MINUS = '-';

MULT = '*';

DIV = '/';

EQUAL = ':=';

OPENB = '(';

CLOSEB = ')';

COMMA = ',';

}

@members {

publicstaticvoid main(String[] args) throws Exception

{

writeLexer lex = new writeLexer(new ANTLRFileStream(args[0]));

CommonTokenStream tokens = new CommonTokenStream(lex);

writeParser parser = new writeParser(tokens);

try

{

parser.expr();

}

catch(RecognitionException e)

{

e.printStackTrace();

}

}

}

/* Lexer rules */

NUMBER: (DIGIT)+('\.'(DIGIT)+)?; WS: ('\t'|' '|'\r'|'\n')+ {$channel = HIDDEN;}; WRITE: 'write'; ID: (ALPHA)+; fragment DIGIT: '0'..'9'; fragment ALPHA: 'a'..'z';

/* Parser rules */

//write: WS? KEY WS? OPENB WS? ID WS? COMMA WS? ID WS? CLOSEB WS?;

write: WRITEOPENBidlistCLOSEB; idlist: ID (COMMAID)*; lhs: IDEQUALexpr; expr: term((PLUS|MINUS)term)*; term: factor((MULT|DIV)factor)*; factor: NUMBER|ID;

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 20 of 22

Write a combined grammar to implement ASSIGNMENT construct in PASCAL language.

grammar assign;

options {

language = Java;

}

tokens {

PLUS = '+';

MINUS = '-';

MULT = '*';

DIV = '/';

EQUAL = ':=';

OPENB = '(';

CLOSEB = ')';

COMMA = ',';

}

@members {

publicstaticvoid main(String[] args) throws Exception

{

assignLexer lex = new assignLexer(new ANTLRFileStream(args[0]));

CommonTokenStream tokens = new CommonTokenStream(lex);

assignLexer parser = new assignLexer(tokens);

try

{

parser.expr();

}

catch(RecognitionException e)

{

e.printStackTrace();

}

}

}

/* Lexer rules */

NUMBER: (DIGIT)+('\.'(DIGIT)+)?; WS: ('\t'|' '|'\r'|'\n')+ {$channel = HIDDEN;}; ID: (ALPHA)+; fragment DIGIT: '0'..'9'; fragment ALPHA: 'a'..'z';

/* Parser rules */

stmt: IDEQUALexpr; expr: term((PLUS|MINUS)term)*; term: factor((MULT|DIV)factor)*; factor: NUMBER|ID;

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 21 of 22

Write a combined grammar to implement FOR construct in PASCAL language & display number of levels of

nesting.

grammar forloop;

options {

language = Java;

}

tokens {

PLUS = '+';

MINUS = '-';

MULT = '*';

DIV = '/';

EQUAL = ':=';

OPENB = '(';

CLOSEB = ')';

COMMA = ',';

SEMICOLON = ';';

}

@members {

publicstaticvoid main(String[] args) throws Exception

{

forloopLexer lex = new forloopLexer(new ANTLRFileStream(args[0]));

CommonTokenStream tokens = new CommonTokenStream(lex);

forloopParser parser = new forloopParser(tokens);

try

{

parser.expr();

}

catch(RecognitionException e)

{

e.printStackTrace();

}

}

}

/* Lexer rules */

NUMBER: (DIGIT)+('\.'(DIGIT)+)?; WS: ('\t'|' '|'\r'|'\n')+ {$channel = HIDDEN;}; FOR: 'for'; DO: 'do'; TO: 'to'; BEGIN: 'begin'; END: 'end'; WRITE: 'write'; READ: 'read'; ID: (ALPHA)+; fragment DIGIT: '0'..'9'; fragment ALPHA: 'a'..'z';

[COMPILER DESIGN LAB]

5th Sem Autonomous Scheme 22 of 22

/* Parser rules */

for: FORindex_expDObody; index_exp: IDEQUALexprTOexpr; body: stmt | BEGINstmt_listEND; stmt: assign | read | write | for;

stmt_list: stmt (stmt)*;

write: WRITEOPENBidlistCLOSEBSEMICOLON; read: READOPENBidlistCLOSEBSEMICOLON; idlist: ID (COMMAID)*; assign: IDEQUALexprSEMICOLON; expr: term((PLUS|MINUS)term)*; term: factor((MULT|DIV)factor)*; factor: NUMBER|ID;