39
SYSTEM PROGRAMMING LABORATORY [10MCA36] [email protected] fb.com/hegde.ganesh7 Page 1 LEX:- 1. Lex: a tool for automatically generating a lexer or scanner given a lex specification (.l file) 2. A lexer or scanner is used to perform lexical analysis, or the breaking up of an input stream into meaningful units, or tokens. 3. For example, consider breaking a text file up into individual words. Skeleton of a lex specification (.l file) x.l %{ < C global variables, prototypes, comments > %} [DEFINITION SECTION] %% [RULES SECTION] %% < C auxiliary subroutines> The rules section %% [RULES SECTION] <pattern> { <action to take when matched> } <pattern> { <action to take when matched> } %% Patterns are specified by regular expressions. For example: %% [A-Za-z]* { printf(“this is a word”); } %% Regular Expression Basics . : matches any single character except \n * : matches 0 or more instances of the preceding regular expression + : matches 1 or more instances of the preceding regular expression ? : matches 0 or 1 of the preceding regular expression

SYSTEMS PROGRAMMING LABORATORY … PROGRAMMING LABORATORY [10MCA36] ... Write a LEX program to recognize a valid arithmetic ... The LEX program to recognize valid arithmetic expression

Embed Size (px)

Citation preview

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 1

LEX:-

1. Lex: a tool for automatically generating a lexer or scanner given a lex

specification (.l file)

2. A lexer or scanner is used to perform lexical analysis, or the breaking up of an

input stream into meaningful units, or tokens.

3. For example, consider breaking a text file up into individual words.

Skeleton of a lex specification (.l file)

x.l

%{

< C global variables, prototypes, comments >

%}

[DEFINITION SECTION]

%%

[RULES SECTION]

%%

< C auxiliary subroutines>

The rules section

%%

[RULES SECTION]

<pattern> { <action to take when matched> }

<pattern> { <action to take when matched> }

%%

Patterns are specified by regular expressions.

For example:

%%

[A-Za-z]* { printf(“this is a word”); }

%%

Regular Expression Basics

. : matches any single character except \n

* : matches 0 or more instances of the preceding regular expression

+ : matches 1 or more instances of the preceding regular expression

? : matches 0 or 1 of the preceding regular expression

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 2

| : matches the preceding or following regular expression

[ ] : defines a character class

() : groups enclosed regular expression into a new regular expression

“…”: matches everything within the “ “ literally

x|y : x or y

{i} : definition of i

x/y : x, only if followed by y (y not removed from input)

x{m,n} : m to n occurrences of x

x : x, but only at beginning of line

x$ : x, but only at end of line

"s" : exactly what is in the quotes (except for "\" and following character)

A regular expression finishes with a space, tab or newline

Meta-characters

– meta-characters (do not match themselves, because they are used in

the preceding reg exps):

• ( ) [ ] { } < > + / , ^ * | . \ " $ ? - %

– to match a meta-character, prefix with "\"

– to match a backslash, tab or newline, use \\, \t, or \n

Steps for executing a lex program:-

1. Create a file.

2. Save it with .l extension.

3. Use the lex command with the file name.

4. Then compile it using C compiler (cc) using, cc lex.yy.c –lfl.

5. Execute the file using ./a.out (the standard output file).

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 3

PNO: 1 COUNTING NUMBER OF VOWELS & CONSONANTS

DATE: 02/08/2011

AIM:

To write a LEX program to count no of vowels and consonants in a given string.

COMMAND DESCRIPTION:

yylex(): It is a c routine which scans the input string and recognizes the

tokens. Definition of this sub-routine is available in lex.yy.c, which is a C file

automatically generated by lex.

SOURCE CODE:

%{

int vow=0,con=0;

%}

%%

[aeiouAEIOU] {vow++;}

[a-zA-Z] {con++;}

%%

main()

{

printf("\n---------------------------------------------------------\n");

printf("\nenter the sentences : ");

yylex();

printf("\n no of vowels =%d, no of consonent =%d\n",vow,con);

printf("\n----------------------------------------------------------\n");

}

OUTPUT:

$ lex lex1.l

$ cc lex.yy.c –lfl

$ ./a.out

---------------------------------------------------------

enter the sentences : This is mca system software lab

no of vowels =8,

no of consonent =18

----------------------------------------------------------

RESULT:

The LEX program for counting number of vowels and consonants in a given

string is executed successfully and the output is verified.

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 4

PNO: 2 COUNTING NUMBER OF CHARACTERS, WORDS, SPACES & LINES

DATE: 02/08/2011

AIM:

To write a LEX program to count the number of characters, words ,spaces and

lines in a given input file.

COMMAND DESCRIPTION:

[ ] :A character class which matches any character within brackets.

yylex(): It is a c routine which scans the input string and recognizes the

tokens. Definition of this sub-routine is available in lex.yy.c, which is a C file

automatically generated by lex.

SOURCE CODE:

%{

int c,w,s,l;

%}

%%

[a-zA-Z] c++;

[a-zA-Z][ ] c++,w++,s++;

[a-zA-Z][\n] c++,w++,l++;

[ ] s++;

[\n] l++;

%%

main()

{ printf("\n------------------------------\n");

printf("Enter the string:\n");

yylex();

printf("The no of characters=%d\n",c);

printf("The no of words=%d\n", w);

printf("The no of spaces=%d\n", s);

printf("The no of lines=%d\n", l);

printf("---------------------------------\n");

return 0;

}

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 5

OUTPUT:

$ lex lex2.l

$ cc lex.yy.c –lfl

$ ./a.out

------------------------------

Enter the string:

We are studying in Sir.MVIT

The no of characters=22

The no of words=5

The no of spaces=4

The no of lines=1

---------------------------------

RESULT:

The LEX program for counting number of characters, words, spaces and lines

in a given input file is executed successfully and the output is verified.

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 6

PNO: 3 COUNTING NUMBER OF INTEGERS AND FRACTIONS

DATE: 09/08/2011

AIM:

To write a LEX program to count the number of

a) Positive and negative integers.

b) Positive and negative fractions.

COMMAND DESCRIPTION:

[ ] : A character class which matches any character within brackets.

\ : Used to escape metacharacters.

* : Matches zero or more copies of the preceding expression.

. : Matches zero or more copies of the preceding expression.

yylex(): It is a c routine which scans the input string and recognizes the

tokens. Definition of this sub-routine is available in lex.yy.c, which is a C file

automatically generated by lex.

SOURCE CODE:

%{

int pos,neg;

float posf,negf;

%}

%%

[0-9]+ pos++;

-[0-9]+ neg++;

[0-9]*\.[0-9]+ posf++;

-[0-9]*\.[0-9]+ negf++;

%%

main()

{

printf("enter input\n");

yylex();

printf("The number of positive integers =%d\n",pos);

printf("The number of negative integers =%d\n",neg);

printf("The number of positive fraction =%d\n",posf);

printf("The number of negative integers =%d\n",negf);

printf("-------------------------------------------------\n");

return 0;}

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 7

OUTPUT :

$ lex lex3.l

$ cc lex.yy.c –lfl

$ ./a.out

-------------------------------------------

Enter input

34

-68

89

-9.53

0.876

9.765

-89

The number of positive integers=2

The number of negative integers=2

The number of positive fractions=2

The number of negative fractions=1

-------------------------------------------

RESULT:

The LEX program for counting number of positive and negative integers and

fractions is executed successfully and the output is verified.

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 8

PNO: 4 COUNTING NUMBER OF COMMENT LINES

DATE: 16/08/2011

AIM:

To write a LEX program to count the numbers of comment lines in a given C

program. Also eliminate them and copy that program into separate file.

COMMAND DESCRIPTION:

/ : Matches the preceding regular expression.

* : Matches zero or more copies of the preceding expression.

. : Matches zero or more copies of the preceding expression.

SOURCE CODE:

%{

int i=0;

%}

%%

^"/*".*"*/"\n {i++;}

"/*".*[\n]*.*"*/" {i++;}

[a-z A-Z 0-9]"/*" { printf( " ");}

%%

main()

{

printf("-------------------------------------\n");

FILE *fp;

fp=fopen("input.c", "r");

yyin=fp;

fp=fopen("out.c","w");

yyout=fp;

yylex();

printf("The number of comment lines =%d\n", i);

printf("---------------------------------------\n");

}

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 9

Input file:

input.c

/*fourth program*/

void main()

{

printf("hello");

/*output

display hello*/

}

OUTPUT:

$ lex lex4.l

$ cc lex.yy.c –lfl

$ ./a.out

------------------------------------------

The number of comment lines =2

-------------------------------------------

out.c

void main()

{

printf("hello");

}

RESULT:

The LEX program for counting number of comment lines in a given C program

is executed successfully and the output is verified.

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 10

PNO: 5 RECOGNIZING PRINTF AND SCANF STATEMENTS

DATE: 23/08/2011

AIM:

To write a LEX program to count the no of „scanf‟ and „printf‟ statements in a

given C program. Replace them with „readf‟ and „writef‟ statements respectively.

COMMAND DESCRIPTION:

yylex(): It is a c routine which scans the input string and recognizes the

tokens. Definition of this sub-routine is available in lex.yy.c, which is a C file

automatically generated by lex.

yyin :A lex lexer reads its input from yyin, which points to the standard I/O

file.

SOURCE CODE:

%{

int sc,pf;

%}

%%

"scanf" {sc++,fprintf(f2,"readf");}

"printf" {pf++,fprintf(f2,"writef");}

%%

main()

{

FILE *fpf,*f2;

printf("-------------------------------\n");

fpf=fopen("input.c","r");

f2=fopen(“output.c”, “w”);

yyin=fpf;

yyout=f2;

yylex();

printf("No of scanf statements=%d\n",sc);

printf("No of printf statements=%d\n",pf);

printf("--------------------------------\n");

}

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 11

Input file:

input.c

void main()

{

char a[30];

printf("welcome");

printf("enter your name");

scanf("%s",a);

}

OUTPUT:

$ lex lex5.l

$ cc lex.yy.c –lfl

$ ./a.out

-------------------------------

No of scanf statements=1

No of printf statements=2

--------------------------------

output.c

void main()

{

char a[30];

writef("welcome");

writef("enter your name");

readf("%s",a);

}

RESULT:

The LEX program for counting the number of printf and scanf statements in a

C program and replacing them with readf and writef statements respectively is

executed successfully and the output is verified.

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 12

PNO: 6 RECOGNIZING VALIDITY OF ARITHMETIC EXPRESSION

DATE: 30/08/2011

AIM:

Write a LEX program to recognize a valid arithmetic expression and identify the

identifiers and operators present .Print them separately.

COMMAND DESCRIPTION:

/ : Matches the preceding regular expression.

* : Matches zero or more copies of the preceding expression.

\ : Used to escape metacharacters.

+ : matches one or more occurrence of the preceding regular

expression.

yylex(): It is a c routine which scans the input string and recognizes the

tokens. Definition of this sub-routine is available in lex.yy.c, which is a C file

automatically generated by lex.

yyin :A lex lexer reads its input from yyin, which points to the standard I/O

file.

SOURCE CODE:

%{

int id=0,op=0;

%}

%%

[a-zA-Z][a-zA-Z0-9]* {id++,printf("%s is a operand\n",yytext);};

[*/\-+] {op++;printf("%s is a operator\n",yytext);}

. {printf(" ");}

%%

main()

{ FILE *fp;

printf("---------------------------------\n");

fp=fopen("lab6.c","r");

yyin=fp;

yylex();

printf("No. of identifiers=%d\n",id);

printf("No. of operators=%d\n",op);

printf("-----------------------------------\n");

}

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 13

INPUT FILE:

Lab6.c

a*bvb+3m/f*34

OUTPUT:

$ lex lex6.l

$ cc lex.yy.c –lfl

$ ./a.out

---------------------------------

a is a operand

* is a operator

bvb is a operand

+ is a operator

m is a operand

/ is a operator

f is a operand

* is a operator

No. of identifiers=4

No. of operators=4

RESULT:

The LEX program to recognize valid arithmetic expression and to count

number of identifiers and operators is executed successfully and the output is

verified.

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 14

PNO: 7 RECOGNIZING AND COUNTING THE NUMBER OF IDENTIFIERS

DATE: 30/08/2011

AIM:

To write a LEX program to recognize and count the number of identifiers in a

given input file.

COMMAND DESCRIPTION:

[ ] : A character class which matches any character within the

brackets.

. : Matches zero or more copies of the preceding expression.

( ) : Groups a series of regular expressions together into a new

regular expression.

yylex(): It is a c routine which scans the input string and recognizes the

tokens. Definition of this sub-routine is available in lex.yy.c, which is a C

file automatically generated by lex.

yyin :A lex lexer reads its input from yyin, which points to the standard

I/O file.

SOURCE CODE:

%{

int id=0,noi=0;

%}

%%

[a-zA-Z][a-zA-Z0-9]* {id++;printf("%s",yytext);}

[0-9]+[!a-zA-Z]* {noi++;printf("%s",yytext);}

%%

main()

{

FILE *fp;

printf("-----------------------------------\n");

fp=fopen("lab6.c","r");

yyin=fp; yylex();

printf("No. of identifiers=%d\n",id);

printf("No. of not valid identifiers=%d\n",noi);

printf("-----------------------------------\n");

}

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 15

Input file:

Lab6.c

a*bvb+3m/f*34

OUTPUT:

$ lex lex7.l

$ cc lex.yy.c –lfl

$ ./a.out

-----------------------------------

a*bvb+3m/f*34

No. of identifiers=3

No. of not valid identifiers=2

-----------------------------------

RESULT:

The LEX program for counting number of identifiers in a given input file is

executed successfully and the output is verified.

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 16

YACC: YET ANOTHER COMPILER-COMPILER

Yacc provides a general tool for imposing structure on the input to a computer

program. The Yacc user prepares a specification of the input process; this includes

rules describing the input structure, code to be invoked when these rules are

recognized, and a low-level routine to do the basic input. Yacc then generates a

function to control the input process. This function, called a parser, calls the user-

supplied low-level input routine (the lexical analyzer) to pick up the basic items

(called tokens) from the input stream. These tokens are organized according to the

input structure rules, called grammar rules; when one of these rules has been

recognized, then user code supplied for this rule, an action, is invoked; actions have

the ability to return values and make use of the values of other actions.

1. Based on its current state, the parser decides whether it needs a lookahead

token to decide what action should be done; if it needs one, and does not have one,

it calls yylex to obtain the next token.

2. Using the current state, and the lookahead token if needed, the parser

decides on its next action, and carries it out. This may result in states being pushed

onto the stack, or popped off of the stack, and in the lookahead token being

processed or left alone.

3: Ambiguity and Conflicts

A set of grammar rules is ambiguous if there is some input string that can be

structured in two or more different ways. For example, the grammar rule

expr : expr '-' expr

is a natural way of expressing the fact that one way of forming an arithmetic

expression is to put two other expressions together

with a minus sign between them. Unfortunately, this grammar rule does not

completely specify the way that all complex inputs should be structured. For

example, if the input is

expr - expr - expr

the rule allows this input to be structured as either

( expr - expr ) - expr

or as

expr - ( expr - expr )

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 17

Skeleton of a yacc specification (.y file)

x.y

%{

< C global variables, prototypes, comments >

%}

[DEFINITION SECTION]

%%

[RULES SECTION]

%%

< C auxiliary subroutines>

The rules section

%%

[RULES SECTION]

<pattern> { <action to take when matched> }

<pattern> { <action to take when matched> }

%%

Patterns are specified by regular expressions.

For example:

%%

[A-Za-z]* { printf(“this is a word”); }

%%

Regular Expression Basics

. : matches any single character except \n

* : matches 0 or more instances of the preceding regular expression

+ : matches 1 or more instances of the preceding regular expression

? : matches 0 or 1 of the preceding regular expression

| : matches the preceding or following regular expression

[ ] : defines a character class

() : groups enclosed regular expression into a new regular expression

“…”: matches everything within the “ “ literally

x|y x or y

{i} definition of i

x/y x, only if followed by y (y not removed from input)

x{m,n} m to n occurrences of x

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 18

x x, but only at beginning of line

x$ x, but only at end of line

"s" exactly what is in the quotes (except for "\" and

following character)

A regular expression finishes with a space, tab or newline

Meta-characters

– meta-characters (do not match themselves, because they are used in

the preceding reg exps):

• ( ) [ ] { } < > + / , ^ * | . \ " $ ? - %

– to match a meta-character, prefix with "\"

– to match a backslash, tab or newline, use \\, \t, or \n

Steps to execute a yacc program:-

1. Create a lex file save it with an extension (.l)

2. Create a yacc file save it with an extension (.y)

3. Create a lexer using lex command.

4. Create a parser using yacc –d command.(-d specifies file of token definitions

created by the –d switch.)

5. Compile using lex.yy.c y.tab.c –lfl(y.tab.h or y.tab.c does parsing)

6. yyerror() specifies error code on fail of a condition.

7. Then execute using ./a.out (the standard output file).

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 19

PNO: 1 CHECKING VALIDITY OF ARITHMETIC EXPRESSION

DATE: 06/09/2011

AIM:

Program to test the validity of simple expression involving operators +,-,* and /.

COMMAND DESCRIPTION:

+ : matches one or more occurrence of the preceding regular expr.

* : Matches zero or more copies of the preceding expression.

. : Matches zero or more copies of the preceding expression.

yyparse() : The enter point to a yacc-generated parser is yyparse(). when

your program calls yyparse(), the parser attempts to parse an input stream.

The parser returns a value of zero if the parse succeeds and non-zero if not.

yyerror() : Whenever a yacc parser detects a syntax error, it calls

yyerror() to report the error to the user, passing it a single argument, a string

describing error.

SOURCE CODE:

lex11.l

%{

#include"y.tab.h"

%}

%%

[0-9]+(\.[0-9]+)? {return NUM;}

[A-Za-z]+[A-Za-z0-9]* {return ID;}

. {return yytext[0];}

%%

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 20

yacc1.y

%{

#include<stdio.h>

%}

%token NUM,ID;

%left '+' '-'

%left '*' '/'

%%

expn:expn '+' expn

| expn '-' expn

| expn '*' expn

| expn '/' expn

| '(' expn ')'

| NUM

| ID

;

%%

int yyerror()

{

printf("Invalid expression \n");

exit(0);

}

main()

{

printf("---------------------------\n");

printf("Enter the expression\n");

yyparse();

printf("----------------------------\n");

printf("Valid expression\n");

printf("------------------------------\n");

}

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 21

OUTPUT:

$ lex lex11.l

$ yacc –d yacc1.y

$ cc y.tab.c lex.yy.c –lfl

$ ./a.out

1

---------------------------

Enter the expression

1+2

----------------------------

Valid expression

------------------------------

2./a.out

---------------------------

Enter the expression

a*3+(4-2)

----------------------------

Valid expression

------------------------------

3./a.out

---------------------------

Enter the expression

*l

Invalid expression

4./a.out

---------------------------

Enter the expression

a+c*

Invalid expression

RESULT:

The YACC program for checking validity of arithmetic expression is executed

successfully and the output is verified.

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 22

PNO: 2 RECOGNIZING NESTED IF STATEMENTS

DATE: 20/09/2011

AIM:

To write a yacc program to recognize nested IF control statements and display the no

of level of nesting.

COMMAND DESCRIPTION:

* : Matches zero or more copies of the preceding expression.

. : Matches zero or more copies of the preceding

expression.

( ) : Groups a series of regular expressions together into a new

regular

yyparse() : The enter point to a yacc-generated parser is yyparse().

when your program calls yyparse(), the parser attempts to parse an input

stream. The parser returns a value of zero if the parse succeeds and non-

zero if not.

yyerror() : Whenever a yacc parser detects a syntax error, it calls

yyerror() to report the error to the user, passing it a single argument, a

string describing error.

SOURCE CODE:

lex12.l

%{

#include"y.tab.h"

int level=0,maxlevel=0;

%}

%%

"if" return IF;

"(".*")" return CON;

"{" { level++;

if(maxlevel<level)

maxlevel=level;

return OB;

}

"}" { level--;return CB;}

. ;

\n ;

%%

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 23

yacc2.y

%{

#include<stdio.h>

#include<stdlib.h>

%}

%token IF CON OB CB

%%

line:

|line ifstmt

;

ifstmt: IF CON OB line CB

;

%%

int main()

{

extern int maxlevel;

printf("\nEnter the Program Block\n");

yyparse();

if(!yyparse())

{

printf("\nValid if Statement\n");

printf("\nNumber of level of nesting = %d\n",maxlevel);

}

return 0;

}

int yyerror(){

printf("\nINvalid if statement\n");

exit(0);

}

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 24

OUTPUT:

$ lex lex12.l

$ yacc –d yacc2.y

$ cc y.tab.c lex.yy.c –lfl

$ ./a.out

1.

--------------------------------------------------------

Enter IF statement

if(4<5){ if(9>0){a=a+b;}}

Valid if and nesting=2

--------------------------------------------------------------------

2.

$ ./a.out

---------------------------------------------------------------------

Enter IF statement

if(g>9)

{}

Not a nesting.

---------------------------------------------------------------------

RESULT:

The YACC program for recognizing nested IF statements and displaying the number

of level of nesting is executed successfully and the output is verified.

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 25

PNO: 3 RECOGNIZING VALIDITY OF AN ARITHMETIC EXPRESSION

DATE: 20/09/2011

AIM:

To write a yacc program to recognize a valid arithmetic expression that uses

operators +,-,* and /.

COMMAND DESCRIPTION:

* : Matches zero or more copies of the preceding expression.

| : Matches the preceding regular expression.

[] : A character class which matches any character within the

o brackets.

yytext[] : Whenever a lexer matches a token ,the text of the token is

stored in this null terminated string. Its content is replaced each time a new

token is matched.

yyparse() : The enter point to a yacc-generated parser is yyparse(). when

your program calls yyparse(), the parser attempts to parse an input stream.

The parser returns a value of zero if the parse succeeds and non-zero if not.

yyerror() : Whenever a yacc parser detects a syntax error, it calls

yyerror() to report the error to the user, passing it a single argument, a string

describing error.

SOURCE CODE:

lex13.l

%{

#include<stdio.h>

#include"y.tab.h"

%}

%%

"("|")" ;

[0-9]+|[a-z A-Z] {return A;}

[+] {return ADD;}

[-] {return SUB;}

[*] {return MUL;}

[/] {return DIV;}

%%

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 26

yacc3.y

%{

#include<stdio.h>

%}

%token A ADD SUB MUL DIV

%%

e: '('e')'

|e ADD A

|e MUL A

|e DIV A

|A;

%%

int yyerror()

{

printf("\nInvalid Expression\n");

printf("-------------------\n");

exit(0);

}

int main()

{

printf("--------------------------\n");

printf("Enter the expression:\n");

yyparse();

printf("\nValid expression\n");

printf("----------------------------\n");

}

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 27

OUTPUT:

$ lex lex13.l

$ yacc –d yacc3.y

$ cc y.tab.c lex.yy.c –lfl

$ ./a.out

1./a.out

--------------------------

Enter the expression:

a*4*(5+9)

Valid expression

----------------------------

2 ./a.out

--------------------------

Enter the expression:

q+c(5)

Invalid Expression

-------------------

3 ./a.out

--------------------------

Enter the expression:

a+b/4

Valid expression

----------------------------

RESULT:

The YACC program for recognizing valid arithmetic expression that uses operators

+,-,* and / is executed successfully and the output is verified.

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 28

PNO: 4 RECOGNIZING VALID VARIABLE NAMES

DATE: 04/10/2011

AIM:

To write a YACC program to recognize valid variable, which starts with a letter,

followed by any number of letters or digits?

COMMAND DESCRIPTION:

[] : A character class which matches any character within the

o brackets.

yytext[] : Whenever a lexer matches a token ,the text of the token is

stored in this null terminated string. Its content is replaced each time a new

token is matched.

yyparse() : The enter point to a yacc-generated parser is yyparse(). when

your program calls yyparse(), the parser attempts to parse an input stream.

The parser returns a value of zero if the parse succeeds and non-zero if not.

yyerror() : Whenever a yacc parser detects a syntax error, it calls

yyerror() to report the error to the user, passing it a single argument, a string

describing error.

SOURCE CODE:

lex14.l

%{

#include"y.tab.h"

%}

%%

[A-Za-z] { return ALPHA;}

[0-9] { return DIGIT;}

. { return yytext[0];}

%%

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 29

yacc4.y

%{

#include<stdio.h>

%}

%token ALPHA DIGIT

%%

variable: ALPHA

| variable DIGIT

| variable ALPHA;

%%

int yyerror()

{

printf("Invalid input\n");

printf("------------------------------\n");

exit(0);

}

int main()

{

printf("------------------------------\n");

printf("Enter the variable:\n");

yyparse();

printf("------------------------------\n");

printf("Valid variable\n");

printf("------------------------------\n");

}

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 30

OUTPUT:

$ lex lex14.l

$ yacc –d yacc4.y

$ cc y.tab.c lex.yy.c –lfl

$ ./a.out

1

Enter the variable:

12

Invalid input

------------------------------

2./a.out

------------------------------

Enter the variable:

q23f

------------------------------

Valid variable

------------------------------

3./a.out

------------------------------

Enter the variable:

qwww

------------------------------

Valid variable

------------------------------

4./a.out

------------------------------

Enter the variable:

2w

Invalid input

RESULT:

The YACC program for recognizing valid variable name which starts with a letter

followed by any no digit is executed successfully and the output is verified.

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 31

PNO: 5 EVALUATION OF ARITHMETIC EXPRESSION

DATE: 04/10/2011

AIM:

To write a YACC program to evaluate an arithmetic expression involving operators

+,-,* and /.

COMMAND DESCRIPTION:

[] : A character class which matches any character within the

brackets.

yytext[] : Whenever a lexer matches a token ,the text of the token is

stored in this null terminated string. Its content is replaced each time a new

token is matched.

yyparse() : The enter point to a yacc-generated parser is yyparse(). when

your program calls yyparse(), the parser attempts to parse an input stream.

The parser returns a value of zero if the parse succeeds and non-zero if not.

yyerror() : Whenever a yacc parser detects a syntax error, it calls

yyerror() to report the error to the user, passing it a single argument, a string

describing error.

SOURCE CODE:

lex15.l

%{

#include"y.tab.h"

#include<math.h>

extern yylval;

%}

%%

[0-9] {yylval=atoi(yytext);

return num;}

. {return yytext[0];}

%%

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 32

yacc5.y

%{

#include"stdio.h"

int valid=1;

%}

%token num

%left'+''-'

%left'*''/'

%nonassoc uminus

%%

statement: expr{if(valid)printf("%d\n",$$);}

expr: '(' statement ')'

|expr'+'expr{$$=$1+$3;}

|expr'-'expr{$$=$1-$3;}

|expr'*'expr{$$=$1*$3;}

|expr'/'expr{if($3==0)valid=0; else($$=$1/$3);}

|'-'expr%prec uminus{$$=-$2;}

|num{$$=$1;} ;

%%

int yyerror()

{

valid=0;

}

int main()

{

printf("---------------------------------\n");

printf("enter valid expression\n");

yyparse();

if(valid)

printf("valid expr\n");

else

printf("invalid expr\n");

printf("------------------------------------\n");

}

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 33

OUTPUT:

$ lex lex15.l

$ yacc –d yacc5.y

$ cc y.tab.c lex.yy.c –lfl

$ ./a.out

1 ./a.out

---------------------------------

enter valid expression

(5*)

invalid expr

------------------------------------

2 ./a.out

---------------------------------

enter valid expression

(6*9)

54

valid expr

------------------------------------

3 ./a.out

---------------------------------

enter valid expression

9+9

18

valid expr

------------------------------------

RESULT:

The YACC program for evaluation of arithmetic expression is executed successfully

and the output is verified.

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 34

PNO: 6 RECOGNIZING GRAMMARS OF THE FORM AMBN (M,N>=0)

DATE: 18/10/2011

AIM:

To write a YACC program to recognize strings „aaab‟,‟abbb‟,‟ab‟ and „a‟ using

the grammar.

COMMAND DESCRIPTION:

[] : A character class which matches any character within the

brackets.

yytext[] : Whenever a lexer matches a token ,the text of the token is

stored in this null terminated string. Its content is replaced each time a new

token is matched.

yyparse() : The enter point to a yacc-generated parser is yyparse(). when

your program calls yyparse(), the parser attempts to parse an input stream.

The parser returns a value of zero if the parse succeeds and non-zero if not.

yyerror() : Whenever a yacc parser detects a syntax error, it calls

yyerror() to report the error to the user, passing it a single argument, a string

describing error.

SOURCE CODE:

lex16.l

%{

#include “y.tab.h”

%}

%%

a return A;

b return B;

. return yytext[0];

\n return 0;

%%

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 35

yacc6.y

%{#include<stdio.h>%}

%token A B

%%

start:sa sb

|sa

|sb;

sa: A sa | A;

sb: B sb

|B;

%%

int main()

{

printf(“\nEnter a string of A‟s followed by B: - ”);

printf(“For the following grammer a^nb^n,n,m>=0\n”);

yyparse();

printf(“\nValid input\n”);

return 0;

}

void yyerror(){ printf(“\nInvalid input\n”);exit(0);}

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 36

OUTPUT:

$ lex prog6b.l

$ yacc –d prog6b.y

$ cc lex.yy.c y.tab.c –ll

$ ./a.out

Enter a string of A‟s followed by B: - abbb

For the following grammer a^nb^n,n,m>=0

Valid input

$ ./a.out

Enter a string of A‟s followed by B: - bbba

For the following grammer a^nb^n,n,m>=0

Invalid input

RESULT:

The YACC program for recognizing grammar of the form anbm is executed

successfully and the output is verified.

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 37

PNO: 7 RECOGNIZING GRAMMARS OF THE FORM ANB (N>=10)

DATE: 18/10/2011

AIM:

To write a YACC program to recognize the grammar(anb,n>=10)

COMMAND DESCRIPTION:

[] : A character class which matches any character within the

o brackets.

yytext[] : Whenever a lexer matches a token ,the text of the token is

stored in this null terminated string. Its content is replaced each time a new

token is matched.

yyparse() : The enter point to a yacc-generated parser is yyparse(). when

your program calls yyparse(), the parser attempts to parse an input stream.

The parser returns a value of zero if the parse succeeds and non-zero if not.

yyerror() : Whenever a yacc parser detects a syntax error, it calls

yyerror() to report the error to the user, passing it a single argument, a string

describing error.

SOUCE CODE:

lex17.l

%{

#include"y.tab.h"

%}

%%

[a] return A;

[b] return B;

%%

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 38

yacc7.y

%{

#include<stdio.h>

%}

%token A B

%%

S: A A A A A A A A A P;

P : A P|B

;

%%

main()

{

printf("Enter the string\n");

if(yyparse()==0)

printf("Valid grammer\n");

}

yyerror(char *s)

{

printf("Invalid grammer\n");

}

SYSTEM PROGRAMMING LABORATORY [10MCA36]

[email protected] fb.com/hegde.ganesh7 Page 39

OUTPUT:

$lex prog7l.l

$yacc –d prog7y.y

$cc lex.yy.c y.tab.c –ll

$./a.out

Enter the string

aaaaaaaaaaab

Valid grammer

$./a.out

Enter the string

aab

Invalid grammar

a

RESULT:

The YACC program for recognizing grammar of the form (anb,n>=10) is executed

successfully and the output is verified.