Lex Program to simple or compound sentence

 

Lex Program to recognize whether a given sentence is simple or compound

Problem definition:

Write a Lex Program to recognize whether a given sentence is a simple or compound sentence. Print the result on standard output.

A simple sentence is one that does not contain another sentence as a component. These sentences are represented by capital letters A-Z.

A compound sentence contains at least one simple sentence as a component, along with connecting keywords such as and, if, but, because, then, etc.

Structure of LEX Program:

%{
Definition section
%}

%%
Rules section
%%

User Subroutine section  

Click here to learn – How to Compile & Run LEX / YACC Programs on Windows 8, 10, and 11?

Lex Program to recognize whether a given sentence is simple or compound

/*Program to recognize whether a given sentence is simple or compound.*/
%{
	#include<stdio.h>
	int flag=0;
%}

%%
and |
or |
but |
because |
if |
then |
nevertheless  { flag=1; }
.  ;
\n  { return 0; }
%%

int main()
{
	printf("Enter the sentence:\n");
	yylex();
	if(flag==0)
		printf("Simple sentence\n");
	else
		printf("compound sentence\n");
}

int yywrap( )
{
	return 1;
}

Output:

maheshgh@maheshgh:~/LexYacc$ lex demo.l

maheshgh@maheshgh:~/LexYacc$ gcc lex.yy.c

maheshgh@maheshgh:~/LexYacc$ ./a.out

Enter the sentence:

My name is Mahesh Huddar and I am from India

(Note: press control+d after input)

Compound sentence

maheshgh@maheshgh:~/LexYacc$ ./a.out

My name is Mahesh Huddar

(Note: press control+d after input)

Simple sentence

Summary:

This article discusses how to write a lex program to find whether a given statement is simple or compound If you like the article, do share it with your friends.

Leave a Comment

Your email address will not be published. Required fields are marked *

Welcome to VTUPulse.com


Computer Graphics and Image Processing Mini Projects -> Click Here

Download Final Year Project -> Click Here

This will close in 12 seconds