PROGRAM NO:-1
OBJECT: DESIGN AND IMPLEMENT A LEXICAL ANALYZER FOR GIVEN
LANGUAGE USING C AND THE LEXICAL ANALYZER SHOULD IGNORE REDUNDANT
SPACES, TABS AND NEW LINES
#include<string.h>
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
void main()
{
FILE *f1;
char c,str[10];
int lineno=1,num=0,i=0;
//clrscr();
printf("\nEnter the c program\n");
f1=fopen("input.txt","w");
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
f1=fopen("input.txt","r");
while((c=getc(f1))!=EOF) // TO READ THE GIVEN FILE
{
if(isdigit(c)) // TO RECOGNIZE NUMBERS
{
num=c-48;
c=getc(f1);
while(isdigit(c))
{
num=num*10+(c-48);
c=getc(f1);
}
printf("%d is a number \n",num);
ungetc(c,f1);
}
else if(isalpha(c)) // TO RECOGNIZE KEYWORDS AND IDENTIFIERS
{
str[i++]=c;
c=getc(f1);
while(isdigit(c)||isalpha(c)||c=='_'||c=='$')
{
str[i++]=c;
c=getc(f1);
}
str[i++]='\0';
if(strcmp("for",str)==0||strcmp("while",str)==0||strcmp("do",str)==0||
strcmp("int",str)==0||strcmp("float",str)==0||strcmp("char",str)==0||
strcmp("double",str)==0||strcmp("static",str)==0||
strcmp("switch",str)==0||strcmp("case",str)==0) // TYPE 32 KEYWORDS
printf("%s is a keyword\n",str);
else
printf("%s is a identifier\n",str);
ungetc(c,f1);
i=0;
}
else if(c==' '||c=='\t') // TO IGNORE THE SPACE
printf("\n");
else if(c=='\n') // TO COUNT LINE NUMBER
lineno++;
else // TO FIND SPECIAL SYMBOL
printf("%c is a special symbol\n",c);
}
printf("Total no. of lines are: %d\n",lineno);
fclose(f1);
PROGRAM N0:02
OBJECT: WRITE A LEX PROGRAM TO CHEAK WETHER INPUT STRING
CONTAINS NUMBER
%{
#include <stdio.h>
%}
%%
[0-9]+ { printf("entered value contains\ninteger: %s\n", yytext); }
. { ;}
%%
main( ){
printf("enter alphanumeric\n");
yylex();
}
int yywrap(){
return 1;
}
PROGRAM N0:03
OBJECT:WRITE A PROGRAM TO CHEAK INPUT IDENTEFIER IS VALID
OR NOT.
%{
#include<stdio.h>
%}
%%
([_]?[a-zA-Z][_]?[a-zA-Z0-9]*)* {printf("\n %s is valid identifier",yytext);}
.* { printf("\n\t%s is invalid Identifier",yytext);}
%%
int main()
{
printf("enter identifier\n");
yylex();
}
int yywrap()
{
return 1;
}
PROGRAM NO:04
Object:WRITE A LEX PROGRAM TO COUNT NUMBER OF CHARACTER,
WORDS, LINES AND NUMBER OF SPACES.
%{
#include<stdio.h>
int sc=0,wc=0,lc=0,cc=0;
%}
%%
[\n] { lc++; }
[ \t] { sc++; }
[^\t\n ]+ { wc++; cc+=yyleng;}
%%
main(){
printf("Enter the input\n");
yylex();
printf("The number of lines=%d\n",lc);
printf("The number of spaces=%d\n",sc);
printf("The number of words=%d\n",wc);
printf("The number of characters are=%d\n",cc);
}
int yywrap( ){
return 1;
}
PROGRAM NO:05
Object: IMPLEMENT LEXICAL ANALYSER USING LEX TOOL.
%{
int COMMENT=0;
%}
identifier [a-zA-Z][a-zA-Z0-9]*
%%
#.* {printf("\n%s is preprocessor directive",yytext);}
int |
float |
char |
double |
while |
for |
struct |
do |
if |
break |
continue |
void |
switch |
return |
else |
goto {printf("\n\t%s is a keyword",yytext);}
"/*" {COMMENT=1;}{printf("\n\t %s is COMMENT",yytext);}
{identifier}\( {if(!COMMENT)printf("\nFUNCTION \n\t%s",yytext);}
\{ {if(!COMMENT)printf("\n BLOCK BEGINS");}
\} {if(!COMMENT)printf("BLOCK ENDS ");}
{identifier}(\[[0-9]*\])? {if(!COMMENT) printf("\n %s IDENTIFIER",yytext);}
\".*\" {if(!COMMENT)printf("\n\t %s is STRING",yytext);}
[0-9]+ {if(!COMMENT) printf("\n %s is INTEGER NUMBER ",yytext);}
[0-9]*\.[0-9]+ {if(!COMMENT) printf("\n %s is FLOAT NUMBER ",yytext);}
\)(\:)? {if(!COMMENT)printf("\n\t");ECHO;printf("\n");}
\( ECHO;
= {if(!COMMENT)printf("\n\t %s is ASSIGNMENT OPERATOR",yytext);}
\<= |
\>= |
\< |
== |
\> {if(!COMMENT) printf("\n\t%s is RELATIONAL OPERATOR",yytext);}
%%
int main(int argc, char **argv)
{
FILE *file;
file=fopen("var.c","r");
if(!file)
{
printf("could not open the file");
exit(0);
}
yyin=file;
yylex();
printf("\n");
return 0;
}
int yywrap()
{
return 1;
}
var.c
int main()
{
int a=23;
int b=6.7;
printf("%d,%f",a,b);
return 0;
}
PROGRAM NO:06
Object: WRITE YACC PROGRAM TO RECOGNIZE A VALID ARITHMETIC
EXPRESSION THAT USES OPERATOR +,-, * AND /.
CREATE bas.l FILE-
%{
#include "bas.tab.h"
%}
%%
[a-zA-Z] { return ALPHA; }
[0-9]+ { return NUM; }
[\t\n]+ ;
. { return yytext[0]; }
%%
CREATE bas.y FILE-
%{
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int yylex();
int yyerror();
%}
%token NUM ALPHA
%left '+' '-'
%left '*' '/ '
%left '(' ')'
%%
expr:'+'expr
|'-'expr
|expr'+'expr
|expr'-'expr
|expr'*'expr
|expr'/'expr
|'('expr')'
|NUM
|ALPHA
;
%%
int main( )
{
printf("enter an arithmetic expression\n"); yyparse();
printf("arithmetic expression is valid\n"); return 0;
}
int yyerror( )
{
printf("\n arithmetic expression is invalid"); exit(0);
}
yywrap()
{
return 1;
}
PROGRAM NO:07
Object: WRITE YACC PROGRAM TO RECOGNIZE A VALID VARIABLE WHICH
STARTS WITH A LETTER FOLLOWED BY ANY NUMBER OF LETTERS OR DIGITS
CREATE bas.l FILE-
%{
#include "bas.tab.h"
%}
%%
[a-zA-Z][a-zA-Z0-9]* { return VARIABLE; }
[ \t\n]+ ;
. { return yytext[0]; }
%%
CREATE bas.y FILE-
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int yyerror();
int yylex();
%}
%token VARIABLE
%%
E:
VARIABLE {printf("Valid variable\n");}
;
%%
int main() {
printf("Enter a variable name: ");
yyparse();
return 0;
}
int yyerror() {
printf("invalid variable\n");
exit(0);
}
yywrap()
{
return 1;
}
PROGRAM NO:08
Object: WRITE A YACC PROGRAM TO IMPLEMENT GRAMMER FOR A SIMPLE
CALCULATOR
CREATE bas.l FILE-
%{
#include<stdio.h>
#include"bas.tab.h"
extern int yylval;
%}
%%
[0-9]+ { yylval=atoi(yytext); return NUM;}
[\t\n ] ;
. return yytext[0];
%%
CREATE bas.y FILE-
%{
#include<stdio.h >
#include<stdlib.h>
#include<ctype.h>
int yylex();
int yyerror();
%}
%token NUM
%left '+' '-'
%left '*' '/'
%%
expr: e { printf("Result:%d\n",$$); return 0; }
;
e:e'+'e {$$=$1+$3;}
| e'-'e {$$=$1-$3;}
| e'*'e {$$=$1*$3;}
| e'/'e {$$=$1/$3;}
| '('e')' {$$=$2;}
| NUM {$$=$1;}
;
%%
int main(){
printf("\n Enter the Arithmetic Expression:\n"); yyparse();
printf("\nValid Expression\n");
}
int yyerror()
{
printf("\n Invalid Expression\n"); exit(0);
}
int yywrap( )
{
return 1;
}