Ex: 1.a Program To Recognize A Few Patterns in C Aim
Ex: 1.a Program To Recognize A Few Patterns in C Aim
Aim
Algorithm
Source Code
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
return (true);
return (false);
ch == '=')
return (true);
return (false);
return (false);
return (true);
{
if (!strcmp(str, "if") || !strcmp(str, "else") ||
!strcmp(str, "break") ||
return (true);
return (false);
if (len == 0)
return (false);
return (false);
return (true);
if (len == 0)
return (false);
return (false);
if (str[i] == '.')
hasDecimal = true;
return (hasDecimal);
}
// Extracts the SUBSTRING.
int i;
return (subStr);
if (isDelimiter(str[right]) == false)
right++;
if (isOperator(str[right]) == true)
left = right;
if (isKeyword(subStr) == true)
left = right;
return;
}
OUTPUT
RESULT
Thus the program for developing a lexical analyzer to recognize a few patterns in C
has been executed successfully.
Ex: 1.b
Aim
Algorithm
Step 3: Create an entry in the symbol table for each valid identifier.
Source Code
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
void main()
int i=0,j=0,x=0,n;
void *p,*add[5];
char ch,srch,b[15],d[15],c;
while((c=getchar())!='$')
b[i]=c;
i++;
n=i-1;
printf("Given Expression:");
i=0;
while(i<=n)
printf("%c",b[i]);
i++;
while(j<=n)
c=b[j];
if(isalpha(toascii(c)))
p=malloc(c);
add[x]=p;
d[x]=c;
printf("\n%c \t %d \t identifier\n",c,p);
x++;
j++;
else
ch=c;
if(ch=='+'||ch=='-'||ch=='*'||ch=='=')
p=malloc(ch);
add[x]=p;
d[x]=ch;
printf("\n %c \t %d \t operator\n",ch,p);
x++;
j++;
}}}
getch();
OUTPUT
RESULT
Thus the program for symbol table has been executed successfully.
Ex: 2
Aim
Procedure
1. A LEX program contains three sections: definitions, rules, and user subroutines. Each
section must be separated from the others by a line containing only the delimiter, %
%. The format is as follows: definitions %% rules %% user_subroutines
2. In definition section, the variables make up the left column, and their definitions make
up the right column. Any C statements should be enclosed in %{..}%. An identifier is
defined such that the first letter of an identifier is alphabet and the remaining letters
are alphanumeric.
3. In rules section, the left column contains the pattern to be recognized in an input file
named yylex(). The right column contains the C program fragment executed when
that pattern is recognized. The various patterns are keywords, operators, new line
character, number, string, identifier, beginning and end of block, comment statements,
preprocessor directive statements etc.
4. Each pattern may have a corresponding action, that is, a fragment of C source code to
execute when the pattern is matched.
5. When yylex() matches a string in the input stream, it copies the matched text to an
external character array, yytext, before it executes any actions in the rules section.
6. In user subroutine section, main routine calls yylex(). yywrap() is used to get more
input.
7. The LEX command uses the rules and actions contained in file to generate a program,
lex.yy.c, which can be compiled with the gcc command. That program can then
receive input, break the input into the logical pieces defined by the rules in file, and
run program fragments contained in the actions in file.
8. This LEX program reads a C file as input and displays the tokens of the input file as
output.
Source Code
%{
int COMMENT=0;
%}
identifier [a-zA-Z][a-zA-Z0-9]*
%%
int |
float |
char |
double |
while |
for |
struct |
typedef |
do |
if |
break |
continue |
void |
switch |
return |
else |
\)(\:)? {if(!COMMENT)printf("\n\t");ECHO;printf("\n");}
\( ECHO;
\<= |
\>= |
\< |
== |
%%
FILE *file;
file=fopen("input.txt","r");
if(!file)
exit(0);
yyin=file;
yylex();
printf("\n");
return(0);
int yywrap()
{
return(1);
INPUT
//input.txt
#include<stdio.h>
#include<conio.h>
void main()
int a,b,c;
a=1;
b=2;
c=a+b;
printf("Sum:%d",c);
OUTPUT
RESULT
Thus the program for implementation of a Lexical Analyzer using LEX tool has been
executed successfully.
Ex: 3.a
Aim
Algorithm
Step3: Check the validity of the given expression according to the rule using yacc.
Step4: Using expression rule print the result of the given values.
Source Code
%{
#include "y.tab.h"
%}
%%
. return yytext[0];
\n return 0;
%%
int yywrap()
return 1;
}
%{
#include<stdio.h>
int valid=1;
%}
%token num id op
%%
s: id x
| num x
| '-' num x
| '(' s ')' x
x: op s
| '-' s
%%
int yyerror()
valid=0;
printf("\nInvalid expression!\n");
return 0;
int main()
{
printf("\nEnter the expression:\n");
yyparse();
if(valid)
{ printf("\nValid expression!\n"); }
Output
RESULT
Thus the program to recognize a valid arithmetic expression using YACC has been
executed successfully.
Ex: 3.b
Aim
To write a C program to recognize a valid variable which starts with a letter followed
by any number of letters or digits.
Algorithm
Step3: Check the validity of the given variable according to the rule using YACC.
Source Code
%{
#include "y.tab.h"
%}
%%
[a-zA-Z_][a-zA-Z_0-9]* return letter;
. return yytext[0];
\n return 0;
%%
int yywrap()
return 1;
%{
#include<stdio.h>
int valid=1;
%}
%%
start : letter s
s: letter s
| digit s
%%
int yyerror()
valid=0;
return 0;
}
int main()
yyparse();
if(valid)
printf("\nIt is an identifier!\n");
Output
RESULT
Thus the program to recognize a valid variable using YACC has been executed
successfully.
Ex: 3.c
Aim
Algorithm
Step3: Checking the validity of the given control structure according to the rule using YACC.
Source Code
%{
#include<stdio.h>
#include<stdlib.h>
int count=0;
%}
%%
|S
cond: x RELOP x
;
x:ID | NUMBER
%%
exit(0);
main()
yyparse();
%option noyywrap
%{
#include "y.tab.h"
%}
%%
\n {return NL;}
. {return yytext[0];}
%%
Output
RESULT
Thus the program to recognize a valid control structure syntax of C Language using
YACC has been executed successfully.
Ex: 3.d
Aim
Algorithm
Step1: A Yacc source program has three part: Declarations %% translation rules %% C
routines
Step3: Rules Section: The rules section defines the rules that parse the input stream. Each
rule of
Step4: Programs Section: The programs section contains the following subroutines. Because,
these subroutines are included in this file, it is not necessary to use the yacc library
when
Step5: Main- The required main program that calls the yyparse subroutine to start the
program.
Step6: yyerror(s) -This error-handling subroutine only prints a syntax error message.
Step7: yywrap -The wrap-up subroutine that returns a value of 1 when the end of input
occurs.
The calc.l file contains include statements for standard input and output, as
programmer
file information if we use the -d flag with the yacc command. The y.tab.h file contains
Source Code
%{
/* Definition section */
#include<stdio.h>
#include "y.tab.h"
%}
/* Rule Section */
%%
[0-9]+ {
yylval=atoi(yytext);
return NUMBER;
[\t] ;
[\n] return 0;
. return yytext[0];
%%
int yywrap()
return 1;
/* Definition section */
#include<stdio.h>
int flag=0;
%}
%token NUMBER
/* Rule Section */
%%
ArithmeticExpression:
E{
printf("\nResult=%d\n", $$);
return 0;
};
E:E'+'E {$$=$1+$3;}
|E'-'E {$$=$1-$3;}
|E'*'E {$$=$1*$3;}
|E'/'E {$$=$1/$3;}
|E'%'E {$$=$1%$3;}
|'('E')' {$$=$2;}
| NUMBER {$$=$1;}
%%
//driver code
void main()
{
printf("\nEnter Any Arithmetic Expression which can have operations Add, Sub, Mul, Div, Modulus
and Round brackets:\n");
yyparse();
if(flag==0)
void yyerror()
flag=1;
Output
RESULT
Thus the program to implement a calculator using LEX and YACC has been
executed.