SYSTEM SOFTWARE LAB MANUAL (LEX PROGRAMS) 1. Program to count the number of vowels and consonants in a given string. %{ #include int vowels=0; int cons=0; %} %% [aeiouAEIOU] {vowels++;} [a-zA-Z] {cons++;} %% int yywrap() { return 1; } main() { printf(“Enter the string.. at end press ^d\n”); yylex(); printf(“No of vowels=%d\nNo of consonants=%d\n”,vowels,cons); }

-1-

2. Program to count the number of characters, words, spaces, end of lines in a given input file. %{ #include Int c=0, w=0, s=0, l=0; %} WORD [^ \t\n,\.:]+ EOL [\n] BLANK [ ] %% {WORD} {w++; c=c+yyleng;} {BLANK} {s++;} {EOL} {l++;} . {c++;} %% int yywrap() { return 1; } main(int argc, char *argv[]) { If(argc!=2) { printf(“Usage: <./a.out> \n”); exit(0); } yyin=fopen(argv[1],”r”); yylex(); printf(“No of characters=%d\nNo of words=%d\nNo of spaces=%d\n No of lines=%d”,c,w,s,l); }

-2-

3. Program to count no of: a) +ve and –ve integers b) +ve and –ve fractions %{ #include int posint=0, negint=0,posfraction=0, negfraction=0; %} %% [-][0-9]+ {negint++;} [+]?[0-9]+ {posint++;} [+]?[0-9]*\.[0-9]+ {posfraction++;} [-][0-9]* \.[0-9]+ {negfraction++;} %% int yywrap() { return 1; } main(int argc, char *argv[]) { If(argc!=2) { printf(“Usage: <./a.out> \n”); exit(0); } yyin=fopen(argv[1],”r”); yylex(); printf(“No of +ve integers=%d\n No of –ve integers=%d\n No of +ve fractions=%d\n No of –ve fractions=%d\n”, posint, negint, posfraction, negfraction); }

-3-

4. Program to count the no of comment line in a given C program. Also eliminate them and copy that program into separate file

%{ #include int com=0; %} %s COMMENT %% “/*”[.]*”*/” {com++;} “/*” {BEGIN COMMENT ;} ”*/” {BEGIN 0; com++ ;} \n {com++ ;} . {;} .|\n {fprintf(yyout,”%s”,yytext); %% int yywrap() { return 1; } main(int argc, char *argv[]) { If(argc!=2) { printf(“Usage: <./a.out> \n”); exit(0); } yyin=fopen(argv[1],”r”); yyout=fopen(argv[2],”w”); yylex(); printf(“No of comment lines=%d\n”,com); }

-4-

5. Program to count the no of ‘scanf’ and ‘printf’ statements in a C program. Replace them with ‘readf’ and ‘writef’ statements respectively. %{ #include int pc=0, sc=0; %} %% “printf” { fprintf(yyout,”writef”); pc++;} “scanf” { fprintf(yyout,”readf”); sc++;} %% int yywrap() { return 1; } main(int argc, char *argv[]) { if(argc!=2) { printf(“Usage: <./a.out> \n”); exit(0); } yyin=fopen(argv[1],”r”); yyout=fopen(argv[2],”w”); yylex(); printf(“No of printf statements = %d\n No of scanf statements=%d\n”, pc, sc); }

-5-

6. Program to recognize a valid arithmetic expression and identify the identifiers and operators present. Print them separately. %{ #include #include int noprt=0, nopnd=0, valid=1, top=-1, m, l=0, j=0; char opnd[10][10], oprt[10][10], a[100]; %} %% “(“ { top++; a[top]=’(‘ ; } “{“ { top++; a[top]=’{‘ ; } “[“ { top++; a[top]=’[‘ ; } “)” { if(a[top]!=’(‘) { valid=0; return; } else top--; } “}” { if(a[top]!=’{‘) { valid=0; return; } else top--; } “]” { if(a[top]!=’[‘) { valid=0; return; } else top--; } “+”|”-“|”*”|”/” { noprt++; strcpy(oprt[l], yytext); l++; } [0-9]+|[a-zA-Z][a-zA-Z0-9_]* {nopnd++; -6-

strcpy(opnd[j],yytext); j++; } %% int yywrap() { return 1; } main() { int k; printf(“Enter the expression.. at end press ^d\n”); yylex(); if(valid==1 && i==-1 && (nopnd-noprt)==1) { printf(“The expression is valid\n”); printf(“The operators are\n”); for(k=0;k
-7-

7. Program to recognize whether a given sentence is simple or compound. %{ #include Int is_simple=1; %} %% [ \t\n]+[aA][nN][dD][ \t\n]+ {is_simple=0;} [ \t\n]+[oO][rR][ \t\n]+ {is_simple=0;} [ \t\n]+[bB][uU][tT][ \t\n]+ {is_simple=0;} . {;} %% int yywrap() { return 1; } main() { int k; printf(“Enter the sentence.. at end press ^d”); yylex(); if(is_simple==1) { Printf(“The given sentence is simple”); } else { Printf(“The given sentence is compound”); }

-8-

8. Program to recognize and count the number of identifiers in a given input file. %{ #include int id=0; %} %% [a-zA-Z][a-zA-Z0-9_]* { id++ ; ECHO; printf(“\n”);}

.+ { ;} \n { ;} %% int yywrap() { return 1; } main (int argc, char *argv[]) { if(argc!=2) { printf(“Usage: <./a.out> \n”); exit(0); } yyin=fopen(argv[1],”r”); printf(“Valid identifires are\n”); yylex(); printf(“No of identifiers = %d\n”,id); }

-9-

YACC PROGRAMS 1. Program to test the validity of a simple expression involving operators +, -, * and / Yacc Part %token NUMBER ID NL %left ‘+’ ‘-‘ %left ‘*’ ‘/’ %% stmt : exp NL { printf(“Valid Expression”); exit(0);} ; exp : exp ‘+’ exp | exp ‘-‘ exp | exp ‘*’ exp | exp ‘/’ exp | ‘(‘ exp ‘)’ | ID | NUMBER ; %% int yyerror(char *msg) { printf(“Invalid Expression\n”); exit(0); } main () { printf(“Enter the expression\n”); yyparse(); }

- 10 -

Lex Part %{ #include “y.tab.h” %} %% [0-9]+ { return DIGIT; } [a-zA-Z][a-zA-Z0-9_]* { return ID; } \n { return NL ;}

. { return yytext[0]; } %%

- 11 -

2. Program to recognize nested IF control statements and display the levels of nesting. Yacc Part %token IF RELOP S NUMBER ID %{ int count=0; %} %% stmt : if_stmt { printf(“No of nested if statements=%d\n”,count); exit(0);} ; if_stmt : IF ‘(‘ cond ‘)’ if_stmt {count++;} | S; ; cond : x RELOP x ; x : ID | NUMBER ; %% int yyerror(char *msg) { printf(“Invalid Expression\n”); exit(0); } main () { printf(“Enter the statement”); yyparse(); }

- 12 -

Lex Part %{ #include “y.tab.h” %} %% “if” { return IF; } [sS][0-9]* {return S;} “<”|”>”|”==”|”!=”|”<=”|”>=” { return RELOP; } [0-9]+ { return NUMBER; } [a-zA-Z][a-zA-Z0-9_]* { return ID; } \n { ; }

. { return yytext[0]; } %%

- 13 -

3. Program to check the syntax of a simple expression involving operators +, -, * and / Yacc Part %token NUMBER ID NL %left ‘+’ ‘-‘ %left ‘*’ ‘/’ %% stmt : exp NL { printf(“Valid Expression”); exit(0);} ; exp : exp ‘+’ exp | exp ‘-‘ exp | exp ‘*’ exp | exp ‘/’ exp | ‘(‘ exp ‘)’ | ID | NUMBER ; %% int yyerror(char *msg) { printf(“Invalid Expression\n”); exit(0); } main () { printf(“Enter the expression\n”); yyparse(); }

- 14 -

Lex Part %{ #include “y.tab.h” %} %% [0-9]+ { return NUMBER; } [a-zA-Z][a-zA-Z0-9_]* { return ID; } \n { return NL ;}

. { return yytext[0]; } %%

- 15 -

4. Program to recognize a valid variable, which starts with a letter, followed by any number of letters or digits. Yacc Part %token DIGIT LETTER NL UND %% stmt : variable NL { printf(“Valid Identifiers\n”); exit(0);} ; variable : LETTER alphanumeric ; alphanumeric: LETTER alphanumeric | DIGIT alphanumeric | UND alphanumeric | LETTER | DIGIT | UND ; %% int yyerror(char *msg) { printf(“Invalid Expression\n”); exit(0); } main () { printf(“Enter the variable name\n”); yyparse(); }

- 16 -

Lex Part %{ #include “y.tab.h” %} %% [a-zA-Z] { return LETTER ;} [0-9] { return DIGIT ; } [\n] { return NL ;} [_] { return UND; }

. { return yytext[0]; } %%

- 17 -

5. Program to evaluate an arithmetic expression involving operating +, -, * and /. Yacc Part %token NUMBER ID NL %left ‘+’ ‘-‘ %left ‘*’ ‘/’ %% stmt : exp NL { printf(“Value = %d\n”,$1); exit(0);} ; exp : exp ‘+’ exp { $$=$1+$3; } | exp ‘-‘ exp { $$=$1-$3; } | exp ‘*’ exp { $$=$1*$3; } | exp ‘/’ exp { if($3==0) { printf(“Cannot divide by 0”); exit(0); } else $$=$1/$3; } | ‘(‘ exp ‘)’ { $$=$2; } | ID { $$=$1; } | NUMBER { $$=$1; } ; %% int yyerror(char *msg) { printf(“Invalid Expression\n”); exit(0); } main () { printf(“Enter the expression\n”); yyparse(); } - 18 -

Lex Part %{ #include “y.tab.h” extern int yylval; %} %% [0-9]+ { yylval=atoi(yytext); return NUMBER; } \n { return NL ;}

. { return yytext[0]; } %%

- 19 -

6. Program to recognize strings ‘aaab’, ‘abbb’, ‘ab’ and ‘a’ using grammar (anbn, n>=0) Yacc Part %token A B NL %% stmt : s NL { printf(“Valid String\n”); exit(0) ;} ; s:AsB | ; %% int yyerror(char *msg) { printf(“Invalid String\n”); exit(0); } main () { printf(“Enter the String\n”); yyparse(); }

Lex Part %{ #include “y.tab.h” %} %% [aA] { return A; } [bB] { return B; } \n { return NL ;}

. { return yytext[0]; } %%

- 20 -

7. Program to recognize the grammar (anb, n>=10) %token A B NL %% stmt : A A A A A A A A A A s B NL { Printf(“Valid”); exit(0); } ; s:sA | ; int yyerror(char *msg) { printf(“Invalid String\n”); exit(0); } main () { printf(“Enter the String\n”); yyparse(); }

Lex Part %{ #include “y.tab.h” %} %% [aA] { return A; } [bB] { return B; } \n { return NL ;}

. { return yytext[0]; } %%

- 21 -

Steps to Execute Lex Program: lex cc lex.yy.c –ll ./a.out

Steps to execute YACC program: yacc –d lex cc y.tab.c lex.yy.c –ly –ll ./a.out

- 22 -

Sample Lex and YAcc Programs.pdf

SYSTEM SOFTWARE LAB MANUAL. (LEX PROGRAMS). 1. Program to count the number of vowels and consonants in a given. string. %{. #include.

39KB Sizes 2 Downloads 241 Views

Recommend Documents

Sample Lex and YAcc Programs.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Sample Lex and ...

Lex and Yacc – 1 Tutorial 1.pdf
Tom Niemann. Portland, Oregon. epaperpress.com/lexandyacc. 3. Page 3 of 41. Lex and Yacc – 1 Tutorial 1.pdf. Lex and Yacc – 1 Tutorial 1.pdf. Open. Extract.

Lex-Claims.opinion.pdf
Bank of New York Mellon Corporation (collectively “defendants”).1. Among other things, the GO Bondholders seek declaratory and. injunctive relief pursuant to ...

O'Reilly - Lex and Yacc.pdf
Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. O'Reilly - Lex and Yacc.pdf. O'Reilly - Lex and Yacc.pdf. Open.

060816-LEX Directory.pdf
Enterprise Holdings. Management Trainee,. Service Agent ... [email protected]. Page 1 of 1. 060816-LEX Directory.pdf. 060816-LEX Directory.pdf.

lex impaler 1 8.pdf
Graphic novelresources bass reeves tales ofthetalented. tenth. Graphic novelresources ... lex impaler 1 8.pdf. lex impaler 1 8.pdf. Open. Extract. Open with.

lex talionis by yskim.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. lex talionis by ...

SAS Routines for Calendar and Clock Elements - Lex Jansen
Jan 11, 2002 - a few applications, time is actually measured this way. The 100- ... In U.S. business writing, a certain date is written as May 21,. 2001 — a month, day, ... To make time measurements easier for a computer program to work with, it is

binpac: A yacc for Writing Application Protocol ... - Events - acm sigcomm
possible to describe arrays and padding with a context-free gram- .... 2, #100. &exportsourcedata. Makes the source data for the type visible through a member ...

Lex Claims Second Ammended Complaint.pdf
Page 1 of 71. IN THE UNITED STATES DISTRICT COURT. FOR THE DISTRICT OF PUERTO RICO. LEX CLAIMS, LLC,. JACANA HOLDINGS I LLC,. JACANA ...

Lex Claims Second Ammended Complaint.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Lex Claims ...

binpac: A yacc for Writing Application Protocol ... - Events - acm sigcomm
a wider range of protocols, ASN.1 [3] is a language for describing the abstract ... provide a concise, yet incomplete, way to define a protocol, rather than for ...

Lex 2 Enroll Form Spanish 2016-2017.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Lex 2 Enroll ...

binpac: A yacc for Writing Application Protocol Parsers - International ...
must handle thousands of connections in real-time to cope with the traffic in large networks, and protocol ..... rors can be caused by irregularity in real-world traffic data (protocol deviations, corrupted contents) as well as .... we can see a clea

binpac: A yacc for Writing Application Protocol Parsers
tions [37], online gaming, and Internet attacks [29] require under- standing ... rect constant can of course result in an incorrect parsing of a pro- tocol, but is not ...... http://www.securiteam.com/exploits/5BP0O209PS.html. [43] Symantec Multiple 

Anti-Yacc: MOF-to-text - Enterprise Distributed Object ...
MOF-to-text tool like Anti-Yacc is its availability to be a general report writer for ... Proceedings of the Sixth International ENTERPRISE DISTRIBUTED OBJECT COMPUTING Conference (EDOC'02) ... Wide Web Consortium (W3C) [10]. The XMI ...

Lex 2 Enroll Form English 2016-2017.pdf
(Check primary phone; include area code) Home: ( ) Work: ( ) Cell: ( ). Address: E-mail Address: Employer: Parent/Legal Guardian #2: Relationship to student: ...

Lex-Amended-Complaint-7-10-16-pdf.pdf
September 2014 Wettbewerbskonzept. Dezember 2014 / Januar 2015 Vorentwurf. Februar bis April 2015 Entwurf. Whoops! There was a problem loading this page. Retrying... Whoops! There was a problem loading this page. Retrying... Lex-Amended-Complaint-7-1

Sample Language for Reporting and Confidentially ... - GitHub
misconduct policy.1 Schools must also consult applicable state laws (such as .... order to provide a safe, non-discriminatory environment for all students. ... the College encourages victims to talk to someone, the College provides an online [or.

Sample Interview Questions and Tips.pdf
Page 1 of 2. Interview Tips. Prior to the Interview Day: Research the company, their mission, and the job. If you are able to quote the mission or use their 'lingo', ...

(www.entrance-exam.net)-GUJCET Physics And Chemistry Sample ...
(www.entrance-exam.net)-GUJCET Physics And Chemistry Sample Paper 2.PDF. (www.entrance-exam.net)-GUJCET Physics And Chemistry Sample Paper 2.

LEX ONE Proficiency Outcomes - 1-20 scale.pdf
College coursework. Page 1 of 1. LEX ONE Proficiency Outcomes - 1-20 scale.pdf. LEX ONE Proficiency Outcomes - 1-20 scale.pdf. Open. Extract. Open with.