+ All Categories
Home > Documents > ssmanual

ssmanual

Date post: 27-Mar-2015
Category:
Upload: abhishek-m-shivalingaiah
View: 151 times
Download: 5 times
Share this document with a friend
44
SS LAB MANNUAL PART A LEX PROGRAMS 1)Program to count the number of vowels and consonants in a given string. %{ #include<stdio.h> Int ccnt=0,vcnt=0; %} %% [AEIOUaeiou] {vcnt++;} [B-DF-HJ-NP-TV-Zb-df-hj-np-tv-z] {ccnt++;} .|\n ; %% Int main() { Printf(“ENTER A STRING AND PRESS<CTL+D>\\N”); Yylex(); Printf(“Number of VOWELS=%d\n,vcnt); Printf(“Number of CONSONANTS=%d\n,ccnt); Return 0; } COMMANDS: $ vi p1.l $ lex p1.l $ cc lex.yy.c –ll SIT,CSE 1
Transcript

SS LAB MANNUAL

PART A

LEX PROGRAMS

1)Program to count the number of vowels and consonants in a given string.

%{

#include<stdio.h>

Int ccnt=0,vcnt=0;

%}

%%

[AEIOUaeiou] {vcnt++;}

[B-DF-HJ-NP-TV-Zb-df-hj-np-tv-z] {ccnt++;}

.|\n ;

%%

Int main()

{

Printf(“ENTER A STRING AND PRESS<CTL+D>\\N”);

Yylex();

Printf(“Number of VOWELS=%d\n,vcnt);

Printf(“Number of CONSONANTS=%d\n,ccnt);

Return 0;

}

COMMANDS:

$ vi p1.l

$ lex p1.l

$ cc lex.yy.c –ll

SIT,CSE 1

SS LAB MANNUAL

2)Program to count the number of characters,words,spaces and linesin a given input file.

%{

#include<stdio.h>

Int ccnt=0,wcnt=0,scnt=0,lcnt=0;

%}

Word [^ \t\n ]+

Eol [\n]

Space [ ]

%%

{eol} {ccnt++;lcnt++;}

[\t] {ccnt++;scnt+=5;}

{space} {ccnt++,scnt++;}

. {ccnt++;}

{word} {ccnt+=yyleng;wcnt++;}

%%

Int main()

{

Yyin=fopen(“input.txt”,”r”);

If(yyin)

{

Yylex();

Printf(“Number of characters =%d\n,vcnt);

Printf(“Number of words,=%d\n,vcnt);

Printf(“Number of spaces =%d\n,vcnt);

Printf(“Number of lines =%d\n,ccnt);

}

Else

Printf(“ERROR IN OPENING THE FILE”);

SIT,CSE 2

SS LAB MANNUAL

Return 0;

}

COMMANDS:

$ vi p2.l

$lex p2.l

$ cc lex.yy.c –ll

$ vi input.txt

$./a.out

SIT,CSE 3

SS LAB MANNUAL

3)program to count number of

a) Positive and negative integers

b) Positive and negative fractions

%{

#include<stdio.h>

Int pint=0,nint=0,pftn=0,nftn=0;

%}

%%

\+?[0-9]+ {pint++;}

-[0-9]+ {nint++;}

\+?[0-9]*\.[0-9]+ | \+?[0-9]+\/[0-9]+ {pftn++;}

-[0-9]*\.[0-9]+ | -[0-9]+\/[0-9]+ {nftn++;}

[+-]?[0-9]*\.[0-9]* | [+-]?[0-9]*\/[0-9]* ;

. |\n ;

Int main()

{

Printf(“ENTER SOME NUMBERS AND PRESS<CTL+D>\N”);

Yylex();

Printf(“Number of +ve int=%d\n Number of -ve int =%d\n,pint,nint);

Printf(“Number of +ve frctn=%d\n Number of -ve frctn=%d,pftn,nftn);

Return 0;

}

COMMANDS:

$ vi p3.l

$ lex p3.l

$ cc lex.yy.c –ll

SIT,CSE 4

SS LAB MANNUAL

4) program to count number of comment lines in a given c program. Also eliminate them and copy that program into separate file.

%{

#include<stdio.h>

Int cnt=0;

%}

%x COMMENT

%%

“//”(.)*[ \t]*\n {cnt++; fprintf(yyout,” “);}

“/*”(.)*”*/”[ \t]*\n {cnt++;fprintf(yyout,” “);}

“/*” {BEGIN COMMENT; fprintf(yyout,” “);}

<COMMENT>(.)* { fprintf(yyout,” “);}

<COMMENT>\n {cnt++; fprintf(yyout,” “);}

<COMMENT>”*/”[ \t]*\n {cnt++; fprintf(yyout,” “);}

<COMMENT>(.)*”*/”\n {cnt++; fprintf(yyout,” “);}

%%

Int main()

{

Yyin=fopen(“f1.c”,”r”);

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

If(yyin && yyout)

{

Yylex();

Printf(“Number of comment lines =%d\n,cnt);

}

Else

Printf(“ERROR IN OPENING FILES”);

SIT,CSE 5

SS LAB MANNUAL

Fclose(yyin);

Fclose(yyout);

Return 0;

}

COMMANDS:

$ vi p4.l

$ lex p4.l

$ cc lex.yy.c –ll

$vi f1.c

//simple c program

#include<stdio.h>

/* calculate a=b+c */

main()

{

int a,b=20,c=50;

/* it’s the

declaration of a,b,c

now

calculate b+c

store into a

*/

a=b+c;

}

$ ./a.out

Number of comment lines=8

$vi f2.c

#include<stdio.h>

main()

SIT,CSE 6

SS LAB MANNUAL

{ int a,b,c; a=b+c; }

5) Program to count number of ‘scanf’ and ‘printf’ statements in a c program. Replace them with ;readf’ and ‘writef’ statements respectively.

%{

#include<stdio.h>

Int pcnt=0,scnt=0;

%}

%%

“printf” {pcnt++; fprintf(yyout,”writef”);}

“scanf” {scnt++; fprintf(yyout,”readf”);}

.|\n {fprintf(yyout,yytext);}

%%

Main()

{

yyin=fopen(“f1.c”,”r”);

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

if(yyin && yyout)

{

Yylex();

Printf(“number of printfs=%d\n”,pcnt);

Printf(“number of scanfs=%d\n”,scnt);

}

Else

Printf(“error in opening files”);

Fclose(yyin);

Fclose(yyout);

}

SIT,CSE 7

SS LAB MANNUAL

COMMANDS:

$ vi p5.l

$ lex p5.l

$ cc lex.yy.c –ll

$vi f1.c

$./a.out

$ vi f2.c

SIT,CSE 8

SS LAB MANNUAL

6)Program to recognize a valid arithmetic expression and identify the identifiers and operators present. Print them separately.

%{

#include<string.h>

#include<stdio.h>

Int tcnt=0,bcnt=0,i=0,j=0;

Char oprt[10],oprd[10][7];

%}

%%

[+-/*%] {tcnt++; oprt[i++]=yytext[0];}

[a-zA-Z0-9]+ {tcnt++; strcpy(oprd[j++],yytext); }

[(] {b++;}

[)] {b--;}

.|\n ;

%%

Main()

{ int l;

Printf(“ENTER AN EXPRESSION\n”);

Yylex();

If((tcnt!=1)&&(tcnt%2!=0))

{

If(b==0)

{

Printf(“valid Expression\n”);

Printf(“the operators are\n”);

For(l=0;l<I;l++)

Printf(“%c\n”,oprt[l]);

Printf(“the identifiers are\n”);

SIT,CSE 9

SS LAB MANNUAL

For(l=0;l<j;l++)

Printf(“%s\n”,oprd[l]);

}

Else

Printf(“Unmatched braces”);

}

else

printf(“Invalid Expression”);

}

COMMANDS:

$ vi p6.l

$ lex p6.l

$ cc lex.yy.c –ll

$./a.out

SIT,CSE 10

SS LAB MANNUAL

7)Program to recognize whether a given sentence is simple or compound.

%{

#include<stdio.h>

Int flag=0;

%}

%%

“and” | “or” | “if” | “else” |”but” |”not” { flag=1;}

.|\n ;

%%

Main()

{

Printf(“ENTER A SENTENCE\n”);

Yylex();

If(flag)

Printf(“COMPOUND SENTENCE\n”);

Else

Printf(“SIMPLE SENTENCE\n”);

}

COMMANDS:

$ vi p7.l

$ lex p7.l

$ cc lex.yy.c –ll

$./a.out

SIT,CSE 11

SS LAB MANNUAL

8)Program to recognize and count the number of identifiers in a given input file.

%{

#include<stdio.h>

Int cnt=0;

%}

%%

“int” | “void” | “double” | “char” |”float” |”short” |”long” {

Char ch;

While(1)

{

Ch=iput();

If(ch==’,’) cnt++;

Else

If(ch==’;’)

{ cnt++; break; }

}

.|\n ;

%%

Main()

{

Yyin=fopen(“f1.c”,”r”);

If(yyin)

{

Yylex();

Printf(“NUMBER OF IDENTIFIERS=%d\n”,cnt);

}

Else

Printf(“ERROR IN OPENING FILE”);

SIT,CSE 12

SS LAB MANNUAL

fclose(yyin);

}

COMMANDS:

$ vi p8.l

$ lex p8.l

$ cc lex.yy.c –ll

$ vi f1.c

$./a.out

SIT,CSE 13

SS LAB MANNUAL

YACC PROGRAMS

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

Lex file(y1.l)

%{

#include”y.tab.h”

%}

%%

[a-zA-Z]+|[0-9]+ {return OP;}

\n {return ENTER;}

. {return yytext[0];}

%%

Yacc file(y1.y)

%{

#include<stdio.h>

Int flag=0;

%}

%token OP ENTER

%%

Start: exp ENTER {flag=1; return;}

;

exp:’+’ OP

|’-‘OP

|OP’+’ exp

| OP’-‘ exp

| OP’*’ exp

| OP’/’ exp

|’(‘exp’)’

SIT,CSE 14

SS LAB MANNUAL

| OP

;

%%

Main()

{

Printf(“ENTER AN EXPRESSION\n”);

Yyparse();

If(flag)

Printf(“VALID EXPRESSION”);

Else

Printf(“INVALID EXPRESSION”);

}

Yyerror(char *s)

{ }

COMMANDS:

$lex y1.l

$yacc –d y1.y

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

$./a.out

SIT,CSE 15

SS LAB MANNUAL

2)Program to recognize nested IF control statements and display the number of levels of nesting.

Lex file (y2.l)

%{

#include"y.tab.h"

%}

%%

"if" {return IF;}

['('] {return yytext[0];}

[')'] {return yytext[0];}

. {return TERMINAL;}

[\n] {return ENTER;}

%%

Yaccfile(y2.y)

%{

#include<stdio.h>

int flag=0,count=0;

%}

%token IF TERMINAL ENTER

%%

st :stmt {if(count>=10)flag=1;return;}

;

stmt :stmt IF '('cond')'ENTER {count++;}

|IF'('cond')'ENTER {count++;}

;

cond :cond TERMINAL

|TERMINAL

;

SIT,CSE 16

SS LAB MANNUAL

%%

main()

{

yyparse();

if(flag)

printf("Valid Nesting and Number of levels of nesting is:%d\n",count);

else

printf("Invalid\n");

}

yyerror(char *s)

{

}

COMMANDS:

$lex y1.l

$yacc –d y1.y

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

$./a.out

3) Program to recognize valid arithmetic expression that uses operators +,-,* and /.

SIT,CSE 17

SS LAB MANNUAL

Lex file(y3.l)

%{

#include”y.tab.h”

%}

%%

[a-zA-Z]+|[0-9]+ {return OP;}

\n {return ENTER;}

. {return yytext[0];}

%%

Yacc file(y3.y)

%{

#include<stdio.h>

Int flag=0;

%}

%token OP ENTER

%left ‘+’ ‘-‘

%left ‘*’ ‘/’

%%

Start: exp ENTER {flag=1; return;}

;

exp:exp’+’exp

|exp’-‘exp

|exp’*’exp

|exp’/’exp

|’(‘exp’)’

| OP

;

%%

SIT,CSE 18

SS LAB MANNUAL

Main()

{

Printf(“ENTER AN EXPRESSION\n”);

Yyparse();

If(flag)

Printf(“VALID EXPRESSION”);

Else

Printf(“INVALID EXPRESSION”);

}

Yyerror(char *s)

{ }

COMMANDS:

$lex y3.l

$yacc –d y3.y

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

$./a.out

4)Program to recognize a valid variable ,which starts with a letter ,followed by any number of letters or digits.

SIT,CSE 19

SS LAB MANNUAL

Lex file(y4.l)

%{

#include”y.tab.h”

%}

%%

[0-9]+ {return DG;}

[a-zA-Z]+| {return LTR;}

\n {return ENTER;}

. {return yytext[0];}

%%

Yacc file(y1.y)

%{

#include<stdio.h>

Int flag=0;

%}

%token DG LTR ENTER

%%

Start: LTR str ENTER { flag=1; return;}

;

str:LTR DG str

| DG LTR str

;

%%

Main()

{

Printf(“ENTER A VARIABLE\n”);

Yyparse();

If(flag)

SIT,CSE 20

SS LAB MANNUAL

Printf(“VALID VARIABLE”);

Else

Printf(“INVALID VARIABLE”);

}

Yyerror(char *s)

{ }

COMMANDS:

$lex y4.l

$yacc –d y4.y

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

$./a.out

5)Program to evaluate an arithmetic expression involving operators +,-,* and /.

Lex file(y5.l)

SIT,CSE 21

SS LAB MANNUAL

%{

#include”y.tab.h”

Extern int yylval;

%}

%%

[0-9]+ {yylval=atoi(yytext);return OP;}

\n {return ENTER;}

. {return yytext[0];}

%%

Yacc file(y5.y)

%{

#include<stdio.h>

Int flag=0;

%}

%token OP ENTER

%left ‘+’ ‘-‘

%left ‘*’ ‘/’

%%

Start: exp ENTER {flag=1;printf(“=%d”,$$); return;}

;

exp:exp’+’exp {$$=$1+$3;}

|exp’-‘exp {$$=$1-$3;}

|exp’*’exp {$$=$1*$3;}

|exp’/’exp {if($3==0)

yyerror(“DIVIDE BY ZERO ERROR”);

else

$$=$1/$3; }

|’(‘exp’)’ { $$=$1; }

SIT,CSE 22

SS LAB MANNUAL

| OP {$$=$1; }

;

%%

Main()

{

Printf(“ENTER AN EXPRESSION\n”);

Yyparse();

If(flag)

Printf(“VALID EXPRESSION”);

else

Printf(“INVALID EXPRESSION”);

}

Yyerror(char *s)

{ printf(“%s”,s); }

COMMANDS:

$lex y5.l

$yacc –d y5.y

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

$./a.out

6) Program to recognize strings ‘aaab’,’abbb’,’ab’and ‘a’ using the grammar (an bn n>=0).

A)Implementation (an bn,n>=0)

SIT,CSE 23

SS LAB MANNUAL

Y6.l

%{

#include”y.tab.h”

%}

%%

“a” {return CHARA;}

“b” {return CHARA;}

\n {return ENTER;}

. {return yytext[0];}

%%

Y6a.y

%{

#include<stdio.h>

Int ca=0,cb=0,flag=0;

%}

%token CHARA CHARB ENTER

%%

Start: exp1 exp2 ENTER {if(ca==cb) flag=1; return; }

;

exp1: CHARA exp1 {ca++;}

|CHARA {ca++;}

;

exp2: CHARB exp1 {cb+;}

|CHARB {cb++;}

;

%%

Main()

{

SIT,CSE 24

SS LAB MANNUAL

Printf(“ENTER AN EXPRESSION\n”);

Yyparse();

If(flag)

Printf(“VALID STRING”);

else

Printf(“INVALID STRING”);

}

Yyerror(char *s)

{ printf(“%s”,s); }

COMMANDS:

$lex y6.l

$yacc –d y6a.y

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

$./a.out

Y6b.y

%{

#include<stdio.h>

Int flag=0;

%}

%token CHARA CHARB ENTER

%%

Start: exp1 exp2 ENTER { flag=1; return; }

|CHARA ENTER { flag=1; return; }

;

exp1: CHARA exp1

|CHARA

;

exp2: CHARB exp1

SIT,CSE 25

SS LAB MANNUAL

|CHARB

;

%%

Main()

{

Printf(“ENTER AN EXPRESSION\n”);

Yyparse();

If(flag)

Printf(“VALID STRING”);

else

Printf(“INVALID STRING”);

}

Yyerror(char *s)

{ printf(“%s”,s); }

COMMANDS:

$lex y6.l

$yacc –d y6b.y

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

$./a.out

7) Program to recognize the grammar (an b ,n>=10).

Y7.l

SIT,CSE 26

SS LAB MANNUAL

%{

#include”y.tab.h”

%}

%%

“a” {return CHARA;}

“b” {return CHARA;}

\n {return ENTER;}

. {return yytext[0];}

%%

Y7.y

%{

#include<stdio.h>

Int ca=0,flag=0;

%}

%token CHARA CHARB ENTER

%%

Start: exp1 CHARB ENTER {if(ca>=10) flag=1; return; }

;

exp1: CHARA exp1 {ca++;}

|CHARA {ca++;}

;

%%

Main()

{

Printf(“ENTER AN EXPRESSION\n”);

Yyparse();

If(flag)

Printf(“VALID STRING”);

SIT,CSE 27

SS LAB MANNUAL

else

Printf(“INVALID STRING”);

}

Yyerror(char *s)

{ printf(“%s”,s); }

COMMANDS:

$lex y7.l

$yacc –d y7.y

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

$./a.out

PART B

SIT,CSE 28

SS LAB MANNUAL

Students(a batch must consists of 2 students) must do one of the below mini projects.However all the projects must be carried out by some batches in a class:

a) Implement a one-Pass Assembler:

Input: . asm file(assembly language code)

Output: . obj file,.exe file (Binary object program)

One pass assembler that produces object program for later execution.

A one pass assembler scans the input and simultaneously generates the object code for it.

Here forward references are entered into lists which will be pointing to the symbols that have undefined symbols in their definitions.

When the definition of a symbol is encountered, instructions that made forward references to that symbol may no longer be available in memory for modification hence the assembler must generate another text record in the object program with the correct operand address. When the program is loaded, this address will be inserted into the instruction by the action of the loader.

SIT,CSE 29

SS LAB MANNUAL

b)Implement a Two-Pass Assembler:

Input: . asm file(assembly language code)

Output: . obj file,.exe file (Binary object program)

Pass1: (Define symbols)

Assign addresses to all statements in the program.

Save the values assigned to all labels for use in pass 2.

SIT,CSE 30

SS LAB MANNUAL

SIT,CSE 31

SS LAB MANNUAL

SIT,CSE 32

SS LAB MANNUAL

b) Implement a Multi-Pass Assembler:

A multi-pass assembler is a solution to the problem with symbol definitions in terms of forward references which cannot be solved in a two-pass assembler.

In the first pass usual scanning of the input is done then

N number of passes can be done for resolving the definition until every symbol is defined properly.

Then a final pass will be done while we generate the actual object program.

For the forward reference we make use of following type of symbol table which will update in every pass till the final one.

Symbol name

Eg: A

&n (n-number of undefined symbols in current definition)

Eg: &1

Expression which gives the value for A

Eg: MAXLEN/2

NULL (if A is not having any other symbols depending on it) else

Ptr to list which will contain the list of symbols depending on A

MAXLEN * Ptr[HALFSZ,NULL]

We will repeat the procedure of defining symbols until there is no ‘*’ found in the symbol table.

SIT,CSE 33

SS LAB MANNUAL

c)Simulate a Linking Loader:

SIT,CSE 34

ESTAB = External symbol table

PROGADDR = Program load address

CSADDR = Control section address

begin

get PROGADDR from operating system

set CSADDR to PROGADDR {for first control section}

while not end of input do

begin

read next input record {Header record for control section}

set CSLTH to control section length

search ESTAB for control section name

if found then set error flag {duplicate external symbol}

else enter control section name into ESTAB with value CSADDR

while record type != ‘E’ do

begin

read next input symbol

if record type = ‘D’ then

for each symbol in the record do

begin

search ESTAB for symbol name

if found then set error flag (duplicate external symbol)

else enter symbol into ESTAB with value (CSADDR + indicated address)

end {for}

end {while != ‘E’}

add CSLTH to CSADDR {starting address for next control section}

end {while not EOF}

Linking Loader - Pass1

(Assigns addresses to all external symbols)

SS LAB MANNUAL

SIT,CSE 35

Pass2

(Perform actual loading, relocation, and linking)

Begin

Set CSADDR to PROGADDR

Set EXECADDR to PROGADDR

While not end of input do

begin

read next input record {Header record}

set CSLTH to control section length

while record type != ‘E’ do

begin

read next input record

if record type = ‘T’ then

begin

{if object code is in character form, convert into internal representation}

move object code from record to location (CSADDR + specified address)

end {if ‘T’}

else if record type = ‘M’ then

begin

search ESTAB for modifying symbol name

if found then add or subtract symbol value at location (CSADDR + specified address)

else set error flag (undefined external symbol)

end {if ‘M’}

end {while != ‘E’}

if an address is specified {in End record} then set EXECADDR to (CSADDR + specified address)

add CSLTH to CSADDR

end {while not EOF}

Jump to location given by EXECDDR {to start execution of loaded program}

SS LAB MANNUAL

d)Implement a one-Pass Macroprocessor:

SIT,CSE 36

SS LAB MANNUAL

SIT,CSE 37

SS LAB MANNUAL

d) Implement an interactive debugger for identifying syntax errors in a c program:lists the syntax errors in c and let the user to correct them interactively.

SIT,CSE 38

SS LAB MANNUAL

e) Implement a lexical analyzer using LEX:generate table of tokens for c or c++ program

SIT,CSE 39

SS LAB MANNUAL

f) Implement C SHELL for the following Commands:

g) Implement a Vi Editor with the following features:

SIT,CSE 40