Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
LIST OF PROGRAMS
IA Marks: 50
Exam Hours: 03
Exam Marks: 50
Part A
Execute the following programs using LEX:
1.
2.
Display the ASCII values of non-printing characters which are present in a file.
3.
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.
6.
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.
5.
6.
Check whether the given string an bn (n>0) is accepted by the grammar or not.
Part B
2.
3.
4.
5.
6.
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
3.
Project#3
1.
5th Sem
Autonomous Scheme
1 of 22
%{
/* Lex specification to convert upper case characters to lower case. */
#include<stdio.h>
%}
%%
[A-Z]
.|\n
%%
int main(int argc , char **argv )
{
/* Input is taken from stdin, output is to stdout */
yylex();
return 0;
}
5th Sem
Autonomous Scheme
2 of 22
{fprintf(yyout, yytext);}
{flag=1; count++ ;}
{if(flag) fprintf(yyout, " ");
else fprintf(yyout, yytext);}
{flag=0;}
5th Sem
Autonomous Scheme
3 of 22
%%
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);
}
5th Sem
Autonomous Scheme
4 of 22
5th Sem
Autonomous Scheme
5 of 22
{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;
}
5th Sem
Autonomous Scheme
6 of 22
5th Sem
Autonomous Scheme
7 of 22
of
of
of
of
words=%d\n", nword);
characters=%d\n", nchar);
spaces=%d\n", nspace);
lines=%d\n", nline);
5th Sem
Autonomous Scheme
8 of 22
{flag=1;}
[,]
{
if(flag)
id++;
}
[;]
{
if(flag)
{
id++;
flag=0;
}
}
{;}
%%
int main(int argc, char
{
char *source;
if(argc>1)
{
**argv)
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;
}
5th Sem
Autonomous Scheme
9 of 22
5th Sem
Autonomous Scheme
10 of 22
5th Sem
Autonomous Scheme
11 of 22
[ \n\t]*"}" [ \n\t]*
5th Sem
{flag=1;flag1=1;flag2=1;return IF;}
{if(flag==1) {flag=0;return COND;}}
{if((flag1==1)&&(flag2==1))
{flag2==0; level++;
if(maxlevel<level)
{maxlevel=level;}
return OB;}
else
flag3=1;}
{if((flag1==1)&&(flag3==0))
{level--; if(level==0)
{flag1=0;}
return CB;}
else flag3=0;}
Autonomous Scheme
12 of 22
5th Sem
Autonomous Scheme
13 of 22
5th Sem
Autonomous Scheme
14 of 22
}Write a program to check whether the given string a b (n>0) is accepted by the grammar or not.
%{
n
5th Sem
Autonomous Scheme
15 of 22
5th Sem
{$channel = HIDDEN;};
Autonomous Scheme
16 of 22
5th Sem
Autonomous Scheme
17 of 22
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;
5th Sem
Autonomous Scheme
18 of 22
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;
5th Sem
Autonomous Scheme
19 of 22
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;
5th Sem
Autonomous Scheme
20 of 22
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';
5th Sem
Autonomous Scheme
21 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;
5th Sem
Autonomous Scheme
22 of 22