Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
69 views

Databases and Compiler LAB

This document contains the details of a Databases And Compiler lab course for an M.Tech in Computer Science Engineering program. It includes the contents, various SQL commands used for creating tables, inserting, selecting, updating, deleting records and using aggregate functions. It also demonstrates the use of DDL, DML, and TCL commands for schema definition, data manipulation and transaction control.

Uploaded by

Yerri Swamy
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Databases and Compiler LAB

This document contains the details of a Databases And Compiler lab course for an M.Tech in Computer Science Engineering program. It includes the contents, various SQL commands used for creating tables, inserting, selecting, updating, deleting records and using aggregate functions. It also demonstrates the use of DDL, DML, and TCL commands for schema definition, data manipulation and transaction control.

Uploaded by

Yerri Swamy
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 96

Databases And Compiler LAB

M.Tech(CSE)

Aushapur(V), Ghatkesar(M), R.R.Dist-501301

JNTU M.Tech(CSE) IInd-SEMESTER

Databases And Compiler Design Lab

(Academic Year 2010-2011)

Department of Computer Science and Engineering

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

LIST OF CONTENTS
Sr. Program Description No. 1 DDL,DML,TCL commands 2 3. 4 5 6 7 8 9 10 11 12 13 14 15 16 Queries using aggregate operators, SQL functions Constraints SQL JOINS, PLsql programs Program on lexical analysis C program to find whether the given input string is comment or not C program to find out whether the given input string is valid or not C Program to find out the no.of identifiers of the given input string c program to implement Lexical Analyzer PredictiveLL(1) Parser Acceptance of the input string by using Predictive LL(1) parser Recursive Descent Parser Shift Reduce Parser Three Address Code SLR PARSER Page No. 1-17 18-27 28-35 36-42 42-62 63 72 73 74 75 77 80 83 87 90 93

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

CREATE:-

SQL> create table person(driver_id varchar(10),address varchar(10)); Table created.

varchar(5),name

SQL> desc person; Name Null? Type

----------------------------------------- -------- ---------------------------DRIVER_ID NAME ADDRESS VARCHAR2(5) VARCHAR2(10) VARCHAR2(10)

SQL> create table varchar(10),dateofjoin date) Table created.

student(name

varchar(10),address

SQL> desc student; Name Null? Type

----------------------------------------- -------- ---------------------------NAME ADDRESS


VBIT, Ghatkesar

VARCHAR2(10) VARCHAR2(10)
Dept of CSE Page 12

Databases And Compiler LAB DATEOFJOIN ALTER:SQL> alter table student drop column name Table altered. DATE

M.Tech(CSE)

SQL> desc student; Name Null? Type

----------------------------------------- -------- ---------------------------ADDRESS DATEOFJOIN VARCHAR2(10) DATE

SQL> alter table person add(license number(5),model varchar(5),year number(4));

Table altered. SQL> desc person; Name Null? Type

----------------------------------------- -------- ---------------------------DRIVER_ID NAME ADDRESS LICENSE MODEL VARCHAR2(5) VARCHAR2(10) VARCHAR2(10) NUMBER(5) VARCHAR2(5)

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB YEAR NUMBER(4)

M.Tech(CSE)

SQL> alter table person drop column year;

Table altered.

SQL> alter table person modify(driver_id number(4));

Table altered.

SQL> desc person; Name Null? Type

----------------------------------------- -------- ---------------------------DRIVER_ID NAME ADDRESS LICENSE MODEL NUMBER(4) VARCHAR2(10) VARCHAR2(10) NUMBER(5) VARCHAR2(5)

Drop:SQL> drop table student Table dropped.

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

SQL> desc student; ERROR: ORA-04043: object student does not exist INSERT:SQL> create table customer(customer_name varchar(10),customer_st varchar(10),customer_city varchar(10)); Table created.

SQL>insert into customer values('&customer_name','&customer_st','&customer_city') Enter value for customer_name: raja Enter value for customer_st: perryridge Enter value for customer_city: detriod old 1: insert into customer values('&customer_name','&customer_st','&customer_city') new 1: insert into customer values('raja','perryridge','detriod') 1 row created. SQL> / Enter value for customer_name: venki Enter value for customer_st: downtown Enter value for customer_city: mexico old 1: insert into customer values('&customer_name','&customer_st','&customer_city') new 1: insert into customer values('venki','downtown','mexico')
VBIT, Ghatkesar Dept of CSE

Page 12

Databases And Compiler LAB 1 row created.

M.Tech(CSE)

SQL> / Enter value for customer_name: pinky Enter value for customer_st: hollywood Enter value for customer_city: west old 1: insert into customer values('&customer_name','&customer_st','&customer_city') new 1: insert into customer values('pinky','hollywood','west')

1 row created.

SQL> / Enter value for customer_name: scott Enter value for customer_st: velly Enter value for customer_city: minkt old 1: insert into customer values('&customer_name','&customer_st','&customer_city') new 1: insert into customer values('scott','velly','minkt')

1 row created.

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

SQL>/ Enter value for customer_name: kish Enter value for customer_st: tomgat Enter value for customer_city: brazil old 1: insert into customer values('&customer_name','&customer_st','&customer_city') new 1: insert into customer values('kish','tomgat','brazil')

1 row created.

SQL> select * from customer;

CUSTOMER_N ---------raja venki pinky scott kish

CUSTOMER_S ---------perryridge downtown hollywood velly tomgat

CUSTOMER_C ---------detriod mexico west minkt brazil

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

SQL> create table emp_works(emp_name varchar(10),cmp_name varchar(10),salary number(4)) Table created.

SQL>insert into emp_works values('vikram','wipro',3000) 1 row created. SQL> insert into emp_works values('will','satyam',5000) 1 row created.

SQL>insert into emp_works values('john','microsoft',5000) 1 row created.

SQL>insert into emp_works values('jack','tcs',9000) 1 row created. SQL>insert into emp_works values('katty','infosis',8000) 1 row created. Select:SQL> select * from emp_works;

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

EMP_NAME CMP_NAME ---------- ---------- ---------vikram will john katty jack wipro satyam microsoft infosis tcs

SALARY

3000 5000 5000 8000 9000

SQL> select emp_name from emp_works;

EMP_NAME ---------vikram will john katty jack

SQL> select cmp_name company from emp_works;

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

COMPANY ---------wipro satyam microsoft tcs infosis

SQL> select unique(customer_name) from customer;

CUSTOMER_N ---------raja scott kish venki pinky

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

SQL> select * from emp_works order by salary;

EMP_NAME CMP_NAME ---------- ---------- ---------vikram will john katty jack wipro satyam microsoft infosis tcs 3000 5000 5000 8000 9000

SALARY

SQL> select * from emp_works order by salary desc;

EMP_NAME CMP_NAME ---------- ---------- ---------jack katty will john vikram tcs infosis satyam microsoft wipro 9000 8000 5000 5000 3000

SALARY

SQL> create table loan varchar(10),amount number(4));

(loan_number

varchar(4),b_name

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB Table created.

M.Tech(CSE)

SQL>insert into loan values('l-11','RoundHill',900); 1 row created. SQL> insert into loan values('l-14','downtown',1500); 1 row created. SQL> insert into loan values('l-15','perryridge',1500); 1 row created. SQL> insert into loan values('l-17','perryridge',1300); 1 row created. SQL> insert into loan values('l-23','redwood',2000); 1 row created. SQL> select * from loan; LOAN B_NAME ---- ---------- ---------l-11 RoundHill l-14 downtown l-15 perryridge l-17 perryridge l-23 redwood 5 rows selected. SQL> select b_name,amount from loan 2 where b_name='perryridge'; 900 1500 1500 1300 2000 AMOUNT

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

B_NAME

AMOUNT

---------- ---------perryridge perryridge 1500 1300

SQL> select b_name,amount from loan 2 where not b_name='perryridge';

B_NAME

AMOUNT

---------- ---------RoundHill downtown redwood 900 1500 2000

SQL> select b_name,amount from loan 2 where amount<1000;

B_NAME

AMOUNT

---------- ---------RoundHill 900

SQL> select b_name,amount from loan

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB 2 where amount>=1000 and amount<=1500;

M.Tech(CSE)

B_NAME

AMOUNT

---------- ---------downtown perryridge perryridge 1500 1500 1300

DELETE:SQL> delete from customer; 5 rows deleted. SQL> select * from customer; no rows selected SQL> delete from loan where b_name='downtown'; 1 row deleted.

SQL> select * from loan; LOAN B_NAME ---- ---------- ---------l-11 RoundHill l-15 perryridge l-17 perryridge l-23 redwood 900 1500 1300 2000 AMOUNT

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

Update:SQL> alter table emp_works add(hra number(4)); Table altered.

SQL> update emp_works 2 set hra=1000; 5 rows updated.

SQL> select * from emp_works; EMP_NAME CMP_NAME ---------- ---------- ---------- ---------vikram will john jack katty wipro satyam microsoft tcs infosis 3000 5000 5000 9000 8000 1000 1000 1000 1000 1000 SALARY HRA

SQL> update emp_works 2 set hra=1500 3 where salary=9000; 1 row updated.

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

SQL> 1 select * from emp_works 2 where emp_name='jack'

EMP_NAME CMP_NAME ---------- ---------- ---------- ---------jack tcs 9000 1500

SALARY

HRA

Commit:SQL> create table computer(cmp_name varchar(10),price varchar(10)); Table created. SQL> insert into computer values('Dell',40000); 1 row created. SQL> insert into computer values('HP',30000); 1 row created. SQL> insert into computer values('lenevo',28000); 1 row created. SQL> commit; Commit complete. SQL>exit;

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

SQL> select * from computer; CMP_NAME PRICE ---------- ---------Dell HP lenevo 40000 30000 28000

RollBack:SQL> delete from computer; 3 rows deleted.

SQL> select * from computer; no rows selected

SQL> rollback; Rollback complete.

SQL> select * from computer;

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

CMP_NAME PRICE ---------- ---------Dell HP lenevo 40000 30000 28000

Aggregate Functions:AVG:SQL> select avg(amount) from loan

AVG(AMOUNT) ----------1440 MIN:SQL> select min(amount) from loan;

MIN(AMOUNT) ----------900 MAX:SQL> select max(amount) from loan

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

MAX(AMOUNT) ----------2000 COUNT:SQL> select count(loan_number) from loan;

COUNT(LOAN_NUMBER) -----------------5 SUM:SQL> select sum(amount) from loan SUM(AMOUNT) ----------7200 Group By:SQL> select b_name,sum(amount) from loan group by b_name B_NAME SUM(AMOUNT)

---------- ----------downtown roundhill perryridge 1500 900 2800

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB redwood 2000

M.Tech(CSE)

Having:SQL> select b_name,sum(amount) from loan group by b_name having sum(amount)>1000

B_NAME

SUM(AMOUNT)

---------- ----------downtown perryridge redwood 1500 2800 2000

StringFunctions:Lower:SQL> select lower('HELLO') from dual LOWER ----hello Upper:SQL> select upper('hai') from dual;

UPP ---

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB HAI Initcap:SQL> select initcap(name) from stud1; INITCAP(NA ---------Raja Amith Bunty Mintu Nitu Substr:SQL> select substr('unwanted',3,6) from dual; SUBSTR -----wanted Ascii:SQL> select ascii('a') from dual ASCII('A') ---------9 Instr SQL> select instr('hell','l',1,2) from dual

M.Tech(CSE)

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

INSTR('HELL','L',1,2) --------------------4 Translate:SQL> select translate('Madam','a','c') from dual TRANS ----Mcdcm Length:SQL> select length('Sir') from dual;

LENGTH('SIR') ------------3 Ltrim:SQL> select ltrim('Hello','He') from dual;

LTR --llo Rtrim:SQL> select rtrim('hello','o') from dual;

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

RTRI ---hell Trim:SQL> select trim( ' mam ') from dual; TRI --Mam lpad:SQL> select lpad('raja',10,'1') from dual LPAD('RAJA ---------111111raja

Rpad:SQL> select rpad('raja',10,'1') from dual

RPAD('RAJA ---------raja111111

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

Vsize:SQL> select vsize('hello')from dual;

VSIZE('HELLO') -------------5 MATH functions:ABS:SQL> select abs(-15) from dual; ABS(-15) ---------15 Power:SQL> select power(2,3) from dual; POWER(2,3) ---------8 ROUND:SQL> select round(15.19,1) from dual; ROUND(15.19,1) -------------15.2

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

SQRT:SQL> select sqrt(9) from dual; SQRT(9) ---------3 GREATEST:SQL> select greatest(13,14,15) from dual GREATEST(13,14,15) -----------------15

LEAST:SQL> select least(6,5,19) from dual

LEAST(6,5,19) ------------5

MOD:SQL> select mod(10,3) from dual;

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

MOD(10,3) ---------1 TRUNC:SQL> select trunc(123.567,2) from dual;

TRUNC(123.567,2) ---------------123.56 FLOOR :SQL> select floor(24.89) from dual; FLOOR(24.89) -----------24 CEIL:SQL> select ceil(23.36) from dual CEIL(23.36) ----------24 Constraints:Primary Key:At column level:-

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

SQL> create table employee(empno number(3),name varchar(10),office varchar(10),age number(2),primary Table created. SQL> insert into employee values('593','Pinky','DSNR',25) 1 row created. SQL> insert into employee values('593','venki','Mat',26); insert into employee values('593','venki','Mat',26) * ERROR at line 1:

ORA-00001: unique constraint (SYSTEM.SYS_C005670) violated

SQL> select * from employee; EMPNO NAME OFFICE AGE

---------- ---------- ---------- ---------593 Pinky DSNR 25

At table level:SQL> create table borrow(empno number(3),isbn number(3),datebor date,primary key(empno,isbn)); Table created. SQL> insert into borrow values('574','482','12 jan 2010');

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB 1 row created.

M.Tech(CSE)

SQL> insert into borrow values('589','482','16 jan 2010');

1 row created.

SQL> insert into borrow values('574','659','13 feb 2010');

1 row created. SQL> insert into borrow values('574','482','1 jan 2010'); insert into borrow values('574','482','1 jan 2010') * ERROR at line 1: ORA-00001: unique constraint (SYSTEM.SYS_C005671) violated Foreign Key:SQL> create table books(isbn number(3) varchar(10),empno number(3) references empl oyee); Table created. SQL> insert into books values(467,'c56',593) 1 row created. SQL> insert into books values(986,'sql',596); insert into books values(986,'sql',596) primary key,title

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB * ERROR at line 1:

M.Tech(CSE)

ORA-02291: integrity constraint (SYSTEM.SYS_C005673) violated parent key not Found On delete cascade:SQL> insert into employee values(586,'Mandy','Upl',63); 1 row created. SQL> insert into employee values(236,'Hyma','Ecil',32); 1 row created. SQL> insert into employee values(598,'Divya','Koti',14); 1 row created.

SQL> select * from employee; EMPNO NAME OFFICE AGE

---------- ---------- ---------- ---------593 Pinky 586 Mandy 236 Hyma 598 Divya DSNR Upl Ecil Koti 25 63 32 14

SQL> create table sal(empno number(3) references employee on delete cascade,salary number(5)); Table created.
VBIT, Ghatkesar Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

SQL> insert into sal values(593,5000); 1 row created. SQL> insert into sal values(236,1000); 1 row created. SQL> select * from sal; EMPNO SALARY

---------- ---------593 236 5000 1000

SQL> delete from employee where empno=236; 1 row deleted.

SQL> select * from sal; EMPNO SALARY

---------- ---------593 5000

Unique:SQL> create table st(name varchar(10),roll number(3) unique); Table created.

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

SQL>insert into st values('Potter',593); 1 row created.

SQL> insert into st values('Hermoine',593); insert into st values('Hermoine',593) * ERROR at line 1: ORA-00001: unique constraint (SYSTEM.SYS_C005674) violated Check:SQL> create table st1(roll number(5),m1 number(3) check(m1<100)); Table created.

SQL> insert into st1 values(586,63); 1 row created.

SQL> insert into st1 values(536,999); insert into st1 values(536,999) * ERROR at line 1: ORA-02290: check constraint (SYSTEM.SYS_C005676) violated Not Null Constraints:SQL> create table st2(name varchar(10) not null,roll number(3));

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB Table created.

M.Tech(CSE)

SQL> insert into st2 values('Raja','') 1 row created. SQL> insert into st2 values('mandy',89); 1 row created.

SQL> insert into st2 values('',564); insert into st2 values('',564) * ERROR at line 1: ORA-01400: cannot insert NULL into ("SYSTEM"."ST2"."NAME")

SQL> select * from st2;

NAME

ROLL

---------- ---------Raja mandy Views:SQL> create view em 2 as select * from employee; View created. 89

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

SQL> select * from em;

EMPNO NAME

OFFICE

AGE

---------- ---------- ---------- ---------593 Pinky 586 Mandy 598 Divya DSNR Upl Koti 25 63 14

SQL> delete from employee where name='Divya';

1 row deleted.

SQL> select * from em;

EMPNO NAME

OFFICE

AGE

---------- ---------- ---------- ---------593 Pinky 586 Mandy Set operations:SQL> select name from res 2 union 3 select emp_name from emp ; DSNR Upl 25 63

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

NAME ---------Deepika Divya Mandy Pinky Ponky Raja Sandy Vicky Vineela

9 rows selected.

SQL> select name from res 2 intersect 3 select emp_name from emp;

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

NAME ---------Divya SQL> select name from res 2 minus 3 select emp_name from emp; NAME ---------Mandy Raja Vicky Vineela Joins:SQL> create table emp(emp_name varchar(10),street varchar(10),city varchar(10)); Table created. SQL> create table ft_work(emp_name varchar(10),salary number(5)); Table created. SQL> insert into emp values('Sandy','DSNR','hyd') 1 row created. SQL> insert into emp values('Deepika','VMD','chennai');
VBIT, Ghatkesar Dept of CSE

varchar(10),b_name

Page 12

Databases And Compiler LAB 1 row created. SQL> insert into emp values('Pinky','MSK','Delhi'); 1 row created. SQL> insert into emp values('Ponky','vsk','goa'); 1 row created. SQL> insert into emp values('divya','Pod','NewYork'); 1 row created.

M.Tech(CSE)

SQL> select * from emp; EMP_NAME STREET ---------- ---------- ---------Sandy Deepika Pinky Ponky divya DSNR VMD MSK vsk Pod hyd chennai Delhi goa NewYork CITY

SQL> insert into ft_work values('Sandy','Vsk',1500); 1 row created.

SQL> insert into ft_work values('Deepika','gim',2000); 1 row created. SQL> insert into ft_work values('Pinky','row',3000);

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB 1 row created.

M.Tech(CSE)

SQL> insert into ft_work values('divya','min',5000); 1 row created. SQL> insert into ft_work values('Raja','vis',6000); 1 row created. Inner Join:SQL>1 select * from emp,ft_work 2* where emp.emp_name=ft_work.emp_name

EMP_NAME SALARY

STREET

CITY

EMP_NAME

B_NAME

---------- ---------- ---------- ---------- ---------- ---------Sandy Deepika Pinky divya DSNR VMD MSK Pod hyd chennai Delhi NewYork Sandy Vsk gim 1500 2000 3000 5000

Deepika

Pinky divya

row min

Outer Join:SQL> select * from emp,ft_work 2 where emp.emp_name(+)=ft_work.emp_name;

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

EMP_NAME STREET CITY

EMP_NAME B_NAME

SALARY

---------- ---------- ---------- ---------- ---------- ---------Sandy Deepika Pinky divya DSNR VMD MSK Pod hyd chennai Delhi Sandy Deepika Pinky Vsk gim row min vis 1500 2000 3000 5000 6000

NewYork divya Raja

SQL> 1 select * from emp,ft_work 2* where emp.emp_name=ft_work.emp_name(+);

EMP_NAME STREET CITY EMP_NAME B_NAME ---------- ---------- ---------- ---------- ---------- ---------Sandy Deepika Pinky divya Ponky DSNR VMD MSK Pod vsk hyd chennai Delhi NewYork goa Sandy Deepika Pinky divya Vsk gim row min

SALARY

1500 2000 3000 5000

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

Cross Join:SQL> select * from emp,ft_work;

EMP_NAME SALARY

STREET

CITY

EMP_NAME

B_NAME

---------- ---------- ---------- ---------- ---------- ---------Sandy Sandy Sandy Sandy Sandy Deepika Deepika Deepika Deepika Deepika Pinky DSNR DSNR DSNR DSNR DSNR VMD VMD VMD VMD VMD MSK hyd hyd hyd hyd hyd chennai chennai chennai chennai chennai Delhi Sandy Deepika Pinky divya Raja Sandy Deepika Pinky divya Raja Vsk gim row min vis Vsk gim row min vis Vsk 1500 2000 3000 5000 6000 1500 2000 3000 5000 6000 1500

Sandy

EMP_NAME SALARY

STREET

CITY

EMP_NAME

B_NAME

---------- ---------- ---------- ---------- ---------- ---------Pinky Pinky MSK MSK Delhi Delhi Deepika Pinky gim row 2000 3000
Dept of CSE

VBIT, Ghatkesar

Page 12

Databases And Compiler LAB Pinky Pinky Ponky Ponky Ponky Ponky Ponky divya divya MSK MSK vsk vsk vsk vsk vsk Pod Pod Delhi Delhi goa goa goa goa goa divya Raja Sandy Deepika Pinky divya Raja min vis Vsk gim row min vis Vsk gim 5000 6000 1500 2000 3000 5000 6000 1500 2000

M.Tech(CSE)

NewYork NewYork

Sandy Deepika

EMP_NAME SALARY

STREET

CITY

EMP_NAME

B_NAME

---------- ---------- ---------- ---------- ---------- ---------divya divya divya Pod Pod Pod NewYork NewYork NewYork Pinky divya Raja row min vis 3000 5000 6000

25 rows selected. Self Join:SQL> create table res(name varchar(10),reside varchar(10)) Table created. SQL> insert into res values('Raja','DSNR'); 1 row created.

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB SQL> insert into res values('Divya','DSNR'); 1 row created. SQL> insert into res values('Vineela','UPL'); 1 row created. SQL> insert into res values('Mandy','upl'); 1 row created. SQL> insert into res values('Vicky','gtksr'); 1 row created. SQL> select b.name,b.reside 2 from res a,res b 3 where a.name='Raja' and a.reside=b.reside;

M.Tech(CSE)

NAME

RESIDE

---------- ---------Raja Divya DSNR DSNR

PlSQL Programs:Simple plsql program SQL> declare 2 a number(3):=&a;

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB 3 b number(3):=&b; 4 c number(3); 5 begin 6 c:=a-b; 7 dbms_output.put_line('The difference of '||a||' and '||b|| ' is '||c); 8 end; 9 / Enter value for a: 6 old 2: a number(3):=&a; new 2: a number(3):=6; Enter value for b: 3 old 3: b number(3):=&b; new 3: b number(3):=3; The difference of 6 and 3 is 3

M.Tech(CSE)

PL/SQL procedure successfully completed. Factorial of a number SQL> declare 2 a number(3):=&a; 3 fact number(8); 4 begin 5 fact:=1; 6 for i in 1..a

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB 7 loop 8 fact:=fact*i; 9 end loop; 10 dbms_output.put_line('The factorial of '||a||'is '||fact); 11 end; 12 / Enter value for a: 5 old 2: a number(3):=&a; new 2: a number(3):=5; The factorial of 5is 120

M.Tech(CSE)

PL/SQL procedure successfully completed. Reverse of a number SQL> declare 2 Num number(3):=&Num; 3 rev number(3):=0; 4 m number(3); 5 begin 6 while(Num>0) 7 loop 8 m:=mod(num,10); 9 rev:=rev*10+m; 10 num:=floor(num/10);

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB 11 end loop; 12 dbms_output.put_line('The reverse of the number is '||rev); 13 end; 14 / Enter value for num: 56 old 2: Num number(3):=&Num; new 2: Num number(3):=56; The reverse of the number is 65 PL/SQL procedure successfully completed. Checking wheter the given number is prime Number or not SQL> declare 2 num number(3):=&num; 3 fac number(2); 4 begin 5 fac:=0; 6 for j in 1..num 7 loop 8 if(mod(num,j)=0) then 9 fac:=fac+1; 10 end if; 11 end loop; 12 if(fac=2) then 13 dbms_output.put_line('The number '||num||' is prime');

M.Tech(CSE)

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB 14 else 15 dbms_output.put_line('The number '||num||' is not prime'); 16 end if; 17 end; 18 / Enter value for num: 5 old 2: num number(3):=&num; new 2: num number(3):=5; The number 5 is prime

M.Tech(CSE)

PL/SQL procedure successfully completed.

SQL> / Enter value for num: 6 old 2: num number(3):=&num; new 2: num number(3):=6; The number 6 is not prime String Palindrome:SQL> declare 2 name varchar(10):='&name'; 3 res int; 4 i int; 5 j int:=1;

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB 6 begin 7 i:=length(name); 8 while(i>=j) 9 loop 10 if(substr(name,j,1)=substr(name,i,1)) then 11 res:=0; 12 else 13 res:=1; 14 exit; 15 end if; 16 j:=j+1; 17 i:=i-1; 18 end loop; 19 if(res=0) then 20 dbms_output.put_line('The string is palindrome'); 21 else 22 dbms_output.put_line('The string is not palindrome'); 23 end if; 24 end; 25 / Enter value for name: madam old 2: name varchar(10):='&name'; new 2: name varchar(10):='madam';

M.Tech(CSE)

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB The string is palindrome

M.Tech(CSE)

PL/SQL procedure successfully completed.

Square root from 1 to 100:SQL> create table sqrt(x number(3), squareroot number(10,5)); Table created. SQL> declare 2 x number(3):=&x; 3 begin 4 for i in 1..x 5 loop 6 insert into sqrt values(i,sqrt(i)); 7 end loop; 8 end; 9 / Enter value for x: 100 old 2: x number(3):=&x; new 2: x number(3):=100;

PL/SQL procedure successfully completed.

SQL> select * from sqrt;

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

X SQUAREROOT ---------- ---------1 2 3 4 5 6 7 8 9 10 11 1 1.41421 1.73205 2 2.23607 2.44949 2.64575 2.82843 3 3.16228 3.31662

X SQUAREROOT ---------- ---------12 13 14 3.4641 3.60555 3.74166

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB 15 16 17 18 19 20 21 22 3.87298 4 4.12311 4.24264 4.3589 4.47214 4.58258 4.69042

M.Tech(CSE)

X SQUAREROOT ---------- ---------23 24 25 26 27 28 29 30 31 32 33 4.79583 4.89898 5 5.09902 5.19615 5.2915 5.38516 5.47723 5.56776 5.65685 5.74456

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

X SQUAREROOT ---------- ---------34 35 36 37 38 39 40 41 42 43 44 5.83095 5.91608 6 6.08276 6.16441 6.245 6.32456 6.40312 6.48074 6.55744 6.63325

X SQUAREROOT ---------- ---------45 46 47 6.7082 6.78233 6.85565

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB 48 49 50 51 52 53 54 55 6.9282 7 7.07107 7.14143 7.2111 7.28011 7.34847 7.4162

M.Tech(CSE)

X SQUAREROOT ---------- ---------56 57 58 59 60 61 62 63 64 65 66 7.48331 7.54983 7.61577 7.68115 7.74597 7.81025 7.87401 7.93725 8 8.06226 8.12404

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB X SQUAREROOT ---------- ---------67 68 69 70 71 72 73 74 75 76 77 8.18535 8.24621 8.30662 8.3666 8.42615 8.48528 8.544 8.60233 8.66025 8.7178 8.77496

M.Tech(CSE)

X SQUAREROOT ---------- ---------78 79 80 81 82 83 84 85 8.83176 8.88819 8.94427 9 9.05539 9.11043 9.16515 9.21954

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB 86 87 88 9.27362 9.32738 9.38083

M.Tech(CSE)

X SQUAREROOT ---------- ---------89 90 91 92 93 94 95 96 97 98 99 9.43398 9.48683 9.53939 9.59166 9.64365 9.69536 9.74679 9.79796 9.84886 9.89949 9.94987

X SQUAREROOT ---------- ---------100 10

100 rows selected.

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

/*PLsql program to read rollno and display name m1,m2,m3*/

SQL> create table stud1(Name varchar(10),Roll number(3),M2 number(3),M3 number(3)); Table created. SQL> insert into stud1 values('Raja',574,88,69,99); 1 row created. SQL> insert into stud1 values('Amith',520,65,87,32); 1 row created. SQL> insert into stud1 values('Bunty',598,54,72,30); 1 row created. SQL> insert into stud1 values('Mintu',593,56,98,99); 1 row created. SQL> insert into stud1 values('Nitu',500,98,96,95); 1 row created. SQL> commit; Commit complete.

number(4),M1

SQL> declare

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB 2 sn varchar(10); 3 r number(4):=&r; 4 sm1 number(3); 5 sm2 number(3); 6 sm3 number(3); 7 begin 8 select name,m1,m2,m3 into sn,sm1,sm2,sm3 9 from stud1 10 where roll=r;

M.Tech(CSE)

11 dbms_output.put_line('Name '||sn||' M1 '||sm1||' M2 '||sm2||' M3 '|| sm3); 12 end; 13 / Enter value for r: 574 old 3: r number(4):=&r; new 3: r number(4):=574; Name Raja M1 88 M2 69 M3 99

PL/SQL procedure successfully completed.

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

Accept student rollno display the result as first,second,third

SQL> declare 2 sn varchar(10); 3 r number(4):=&r; 4 sm1 number(3); 5 sm2 number(3); 6 sm3 number(3); 7 avg1 number(3); 8 begin 9 select name,m1,m2,m3 into sn,sm1,sm2,sm3 10 from stud1 11 where roll=r; 12 avg1:=(sm1+sm2+sm3)/3; 13 if(avg1>=60) then 14 dbms_output.put_line('The student '||sn||' got First class'); 15 elsif(avg1<60 and avg1>=50) then 16 dbms_output.put_line('The student '||sn||'got Second class'); 17 elsif(avg1<50) then

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB 18 dbms_output.put_line('The student '||sn||'got Third class'); 19 end if; 20 end; 21 / Enter value for r: 574 old 3: r number(4):=&r; new 3: r number(4):=574; The student Raja got First class PL/SQL procedure successfully completed. Accept student rollno and update result field as first,second,third SQL> alter table stud1 add(result varchar(7)); Table altered.

M.Tech(CSE)

SQL> declare 2 sn varchar(10); 3 r number(4):=&r; 4 sm1 number(3); 5 sm2 number(3); 6 sm3 number(3); 7 res number(3); 8 av number(3); 9 begin 10 select name,m1,m2,m3 into sn,sm1,sm2,sm3

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB 11 from stud1 12 where roll=r; 13 av:=(sm1+sm2+sm3)/3; 14 if (av>=60) then 15 update stud1 set result='First' where roll=r; 16 elsif (av<60 and av>=50) then 17 update stud1 18 set result='Second' 19 where roll=r; 20 elsif (av<50) then 21 update stud1 22 set result='Third' 23 where roll=r; 24 end if; 25 end; 26 / Enter value for r: 574 old 3: r number(4):=&r; new 3: r number(4):=574; PL/SQL procedure successfully completed.

M.Tech(CSE)

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

SQL> select * from stud1; NAME ROLL M1 M2 M3 RESULT

---------- ---------- ---------- ---------- ---------- ------Raja Amith Bunty Mintu Nitu 574 520 598 593 500 88 65 54 56 98 69 87 72 98 96 99 First 32 30 99 95

Using cursor to display the fields SQL> declare 2 cursor c 3 is select * from stud1; 4 r stud1 %rowtype; 5 begin 6 open c; 7 loop 8 fetch c into r; 9 exit when c%notfound;

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB 10 dbms_output.put_line('Name '||r.name); 11 end loop;

M.Tech(CSE)

12 close c; 13 end; 14 / Name Raja Name Amith Name Bunty Name Mintu Name Nitu PL/SQL procedure successfully completed. Program to display the result of the students using cursors:SQL> declare 2 cursor c 3 is select * from stud1; 4 r stud1 %rowtype; 5 av number(3); 6 begin 7 open c; 8 loop 9 fetch c into r; 10 exit when c%notfound; 11 av:=(r.m1+r.m2+r.m3)/3;

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB 12 if(r.m1>40 and r.m2>40 and r.m3>40) then 13 if(av>=60) then 14 dbms_output.put_line('The student '||r.name||' First class'); 15 elsif(av<60 and av>=50) then 16 dbms_output.put_line('The student '||r.name||' Second class'); 17 else 18 dbms_output.put_line('The student '||r.name||' Third class'); 19 end if; 20 else 21 dbms_output.put_line('The student '||r.name||' is Fail'); 22 end if; 23 end loop; 24 close c; 25 end; 26 / The student Raja First class The student Amith is Fail The student Bunty is Fail The student Mintu First class The student Nitu First class

M.Tech(CSE)

PL/SQL procedure successfully completed.

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

/* Program on lexical analysis */ #include<stdio.h> #include<conio.h> #define MAX 30 void main() { char str[MAX]; int state=0; int i=0, j, startid=0, endid, startcon, endcon; clrscr(); for(j=0; j<MAX; j++) str[j]=NULL; //Initialise NULL printf("*** Program on Lexical Analysis ***\n"); printf(" Enter the string:\n "); gets(str); //Accept input string str[strlen(str)]=' '; printf("Analysis:\n "); while(str[i]!=NULL) { while(str[i]==' ') //To eliminate spaces i++; switch(state) { case 0: if(str[i]=='i') state=1; //if
VBIT, Ghatkesar Dept of CSE

Page 12

Databases And Compiler LAB else if(str[i]=='w') state=3; //while else if(str[i]=='d') state=8; //do else if(str[i]=='e') state=10; //else else if(str[i]=='f') state=14; //for else if(isalpha(str[i]) || str[i]=='_') { state=17; startid=i; } //identifiers else if(str[i]=='<') state=19; //relational '<' or '<=' else if(str[i]=='>') state=21; //relational '>' or '>=' else if(str[i]=='=') state=23; //relational '==' or assignment '=' else if(isdigit(str[i])) { state=25; startcon=i; } //constant else if(str[i]=='(') state=26; //special characters '(' else if(str[i]==')') state=27; //special characters ')' else if(str[i]==';') state=28; //special characters ';' else if(str[i]=='+') state=29; //operator '+' else if(str[i]=='-') state=30; //operator '-'

M.Tech(CSE)

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB break; //States for 'if' case 1: if(str[i]=='f') state=2; else { state=17; startid=i-1; i--; } break; case 2: if(str[i]=='(' || str[i]==NULL) { printf("if : Keyword \t"); state=0; i--; } else { state=17; startid=i-2; i--; } break; //States for 'while' case 3: if(str[i]=='h') state=4; else { state=17; startid=i-1; i--; } break; case 4: if(str[i]=='i') state=5; else { state=17; startid=i-2; i--; } break; case 5: if(str[i]=='l') state=6; else { state=17; startid=i-3; i--; } break; case 6: if(str[i]=='e') state=7; else { state=17; startid=i-4; i--; } break; case 7: if(str[i]=='(' || str[i]==NULL) { printf("while : Keyword \t"); state=0; i--; } else { state=17; startid=i-5; i--; } break; //States for 'do' case 8: if(str[i]=='o') state=9; else { state=17; startid=i-1; i--; }
VBIT, Ghatkesar Dept of CSE

M.Tech(CSE)

Page 12

Databases And Compiler LAB break; case 9: if(str[i]=='{' || str[i]==' ' || str[i]==NULL || str[i]=='(') { printf("do : Keyword\t"); state=0; i--; } break; //States for 'else' case 10: if(str[i]=='l') state=11; else { state=17; startid=i-1; i--; } break; case 11: if(str[i]=='s') state=12; else { state=17; startid=i-2; i--; } break; case 12: if(str[i]=='e') state=13; else { state=17; startid=i-3; i--; } break; case 13: if(str[i]=='{' || str[i]==NULL) { printf("else : Keyword \t"); state=0; i--; } else { state=17; startid=i-4; i--; } break; //States for 'for' case 14: if(str[i]=='o') state=15; else { state=17; startid=i-1; i--; } break; case 15: if(str[i]=='r') state=16; else { state=17; startid=i-2; i--; } break; case 16: if(str[i]=='(' || str[i]==NULL) { printf(" for : Keyword\t"); state=0; i--;
VBIT, Ghatkesar Dept of CSE

M.Tech(CSE)

Page 12

Databases And Compiler LAB } else { state=17; startid=i-3; i--; } break; //States for identifiers case 17: if(isalnum(str[i]) || str[i]=='_') { state=18; i++; } else if(str[i]==NULL||str[i]=='<'||str[i]=='>'||str[i]=='('||str[i]==')'|| str[i]==';'||str[i]=='='||str[i]=='+'||str[i]=='-') state=18; i--; break; case 18: if(str[i]==NULL || str[i]=='<' || str[i]=='>' || str[i]=='(' || str[i]==')' || str[i]==';' || str[i]=='=' || str[i]=='+' ||str[i]=='-') { endid=i-1; printf(" "); for(j=startid; j<=endid; j++) printf("%c", str[j]); printf("\t : Identifier"); state=0; i--; } break; //States for relational operator '<' & '<=' case 19: if(str[i]=='=') state=20; else if(isalnum(str[i]) || str[i]=='_') { printf("< : Relational operator\t"); i--; state=0; } break;
VBIT, Ghatkesar Dept of CSE

M.Tech(CSE)

Page 12

Databases And Compiler LAB case 20: if(isalnum(str[i]) || str[i]=='_') { printf("<= : Relational operator\t"); i--; state=0; } break; //States for relational operator '>' & '>=' case 21: if(str[i]=='=') state=22; else if(isalnum(str[i]) || str[i]=='_') { printf("> : Relational operator\t"); i--; state=0; } break; case 22: if(isalnum(str[i]) || str[i]=='_') { printf(">= : Relational operator"); i--; state=0; } break; //States for relational operator '==' & assignment operator '=' case 23: if(str[i]=='=') state=24; else { printf(" = : Assignment operator\t"); i--; state=0; } break; case 24: if(isalnum(str[i])) { printf(" == : Relational operator\t"); state=0; i--; }
VBIT, Ghatkesar Dept of CSE

M.Tech(CSE)

Page 12

Databases And Compiler LAB break;

M.Tech(CSE)

//States for constants case 25: if(isalpha(str[i])) { printf(" ** ERROR ***"); puts(str); for(j=0; j<i; j++) printf(" "); printf("^"); printf("Error at position %d Alphabet cannot follow digit\t", i); state=99; } else if(str[i]=='(' || str[i]==')' || str[i]=='<' || str[i]=='>' || str[i]==NULL || str[i]==';' || str[i]=='=') { endcon=i-1; printf(" "); for(j=startcon; j<=endcon; j++) printf("%c", str[j]); printf(" : Constant\t"); state=0; i--; } break; //State for special character '(' case 26: printf(" ( : Special character"); startid=i; state=0; i--; break; //State for special character ')' case 27: printf(" ) : Special character"); state=0; i--; break;

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB //State for special character ';' case 28: printf(" ; : Special character"); state=0; i--; break; //State for operator '+' case 29: printf(" + : Operator"); state=0; i--; break; //State for operator '-' case 30: printf(" + : Operator"); state=0; i--; break; //Error State case 99: goto END; } i++; } printf(" End of program"); END: getch(); } /* Output

M.Tech(CSE)

Correct input ------------*** Program on Lexical Analysis *** Enter the string: for(x1=0; x1<=10; x1++); Analysis:
VBIT, Ghatkesar Dept of CSE Page 12

Databases And Compiler LAB

M.Tech(CSE)

for ( x1 = 0 ; x1 <= 10 ; x1 + + ) ;

: Keyword : Special character : Identifier : Assignment operator : Constant : Special character : Identifier : Relational operator : Constant : Special character : Identifier : Operator : Operator : Special character : Special character

End of program Wrong input ----------*** Program on Lexical Analysis *** Enter the string: for(x1=0; x1<=19x; x++); Analysis: for : Keyword ( : Special character x1 : Identifier = : Assignment operator 0 : Constant ; : Special character x1 : Identifier <= : Relational operator Token cannot be generated
VBIT, Ghatkesar Dept of CSE Page 12

Databases And Compiler LAB

M.Tech(CSE)

/* C program to find whether the given input string is comment or not*/ #include<stdio.h> #include<conio.h> #include<string.h> void main() { char s[20]; int t,i,c; clrscr(); printf("\n enter a string"); scanf("%s",&s); if(s[0]=='/') { if(s[1]=='/') printf("\n comment"); else if(s[1]=='*') { for(i=2;i<strlen(s)-1;i++) { if(s[1]=='*' && s[i+1]=='/') t=1; } if(t==1) printf("comment"); else printf("invalid input"); } } else printf("/n not a comment"); getch(); } OUTPUT:
VBIT, Ghatkesar Dept of CSE Page 12

Databases And Compiler LAB

M.Tech(CSE)

enter a string//abcd comment enter a string /*abcd*/ comment enter a string xyz not a comment /* C program to find out whether the given input string is valid or not*/ #include<stdio.h> #include<conio.h> #include<string.h> void main() { char s[20]; clrscr(); printf("enter the string \n"); gets(s); if(isdigit(s[0])) printf("the given string is not valid"); else printf("the given string is valid"); getch(); }

OUTPUT: enter the string abc34w the given string is valid enter the string 1dfgh the given string is not valid

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

/*C Program to find out the no.of identifiers of the given input string*/ #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[100]; int i,t; t=0; clrscr(); printf("Enter string \n"); gets(a); for(i=0;i<strlen(a);i++) { if(a[i]>='A'&&a[i]<='Z' || a[i]>='a'&&a[i]<='z') { if(a[i+1]=='+'||a[i+1]=='=' || a[i+1]=='*' || a[i+1]=='\0'||a[i+1]=='-' ||a[i+1]=='/') { t=t+1; } } } printf("no. of identifiers are %d",t); getch(); } OUTPUT: Enter string position=initial+rate*60
VBIT, Ghatkesar Dept of CSE Page 12

Databases And Compiler LAB no. of identifiers are 3

M.Tech(CSE)

/* c program to implement Lexical Analyzer */ #include<stdio.h> #include<conio.h> #include<string.h> void main() { char s[120]; int i=0,n=1; clrscr(); printf("enter the string:\n"); gets(s); for(i=0;i<=strlen(s);i++) { if((s[i]>='A'&&s[i]<='Z')||(s[i]>='a'&&s[i]<='z')) { if(s[i+1]=='='||s[i+1]=='+'||s[i+1]=='*'||s[i+1]=='/'||s[i+1]=='\0') { printf("id%d",n); n++; } } if(s[i]>=48&&s[i]<=57) { printf("%c",s[i]); } else { switch(s[i]) { case'+':printf("+"); break;
VBIT, Ghatkesar Dept of CSE Page 12

Databases And Compiler LAB case'-':printf("-"); break; case'/':printf("/"); break; case'%':printf("%"); break; case'=':printf("="); break; case'*':printf("*"); break; } } } getch(); }

M.Tech(CSE)

OUTPUT: enter the string: position=initial+rate*60 id1=id2+id3*60

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

/*PredictiveLL(1) Parser*/ #include<stdio.h> #include<conio.h> #include<string.h> char prol[7][10]={"S","A","A","B","B","C","C"}; char pror[7][10]={"A","Bb","Cd","aB","@","Cc","@"}; char prod[7][10]={"S->A","a->Bb","A->Cd","B->aB","B->@","C->Cc","C->@"}; char first[7][10]={"abcd","ab","cd","a@","@","c@","@"}; char follow[7][10]={"$","$","$","a$","b$","c$","d$"}; char table[5][6][10]; numr(char c) { switch(c) { case 'S':return 0; case 'A':return 1; case 'B':return 2; case 'C':return 3; case 'a':return 0; case 'b':return 1; case 'c':return 2; case 'd':return 3; case '$':return 4; } return(2); } void main() { int i,j,k; clrscr(); for(i=0;i<=5;i++) for(j=0;j<=6;j++)
VBIT, Ghatkesar Dept of CSE Page 12

Databases And Compiler LAB

M.Tech(CSE)

strcpy(table[i][j]," "); printf("\n the following is the predictive parsing table for the following grammer:\n"); for(i=0;i<=7;i++) printf("%s\n",prod[i]); printf("\n predictive parsing table is\n"); fflush(stdin); for(i=0;i<=7;i++) { k=strlen(first[i]); for(j=0;j<10;j++) if(first[i][j]!='@') strcpy(table[numr(prol[i][0])+1][numr(first[i][j])+1],prod[i]); } for(i=0;i<7;i++) { if(strlen(pror[i])==1) { if(pror[i][0]=='@') { k=strlen(follow[i]); for(j=0;j<k;j++) strcpy(table[numr(prol[i][0])+1][numr(follow[i][j])+1],prod[i]); } } } strcpy(table[0][0]," "); strcpy(table[0][1]," a"); strcpy(table[0][2]," b"); strcpy(table[0][3]," c"); strcpy(table[0][4]," d"); strcpy(table[0][5]," $"); strcpy(table[1][0]," S"); strcpy(table[2][0]," A"); strcpy(table[3][0]," B"); strcpy(table[4][0]," C"); printf("\n_______________________________________________\n"); for(i=0;i<5;i++) for(j=0;j<6;j++) {
VBIT, Ghatkesar Dept of CSE Page 12

Databases And Compiler LAB printf("%-10s",table[i][j]); if(j==5) printf("\n_______________________________________________\n"); } getch(); }

M.Tech(CSE)

OUTPUT: the following is the predictive parsing table for the following grammer: S->A a->Bb A->Cd B->aB B->@ C->Cc C->@ abcd predictive parsing table is __________________________________________________ a b c d $ __________________________________________________ S S->A S->A S->A S->A __________________________________________________ A a->Bb a->Bb abcd A->Cd abcd __________________________________________________ B B->aB B->@ B->@ B->@ __________________________________________________ C C->@ C->@ C->@ __________________________________________________

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

/*Acceptance of the input string by using Predictive LL(1) parser*/ #include<stdio.h> #include<conio.h> #include<string.h> char s[20],stack[20]; void main() { Char m[5][6] [3]={"tb","","","tb","","","","+tb","","","n","n","fc","","","fc","","","","n"," *fc", "","n","n","i","","","(e)","",""}; int size[5][6]={0,0,2,0,0,0,3,0,0,1,1,2,0,0,2,0,0,0,1,2,0,1,1,1,0,0,3,0,0,}; int i,j,k,n,str1,str2; clrscr(); printf("i\p string:\n"); gets(s); strcat(s,"$"); stack[0]='$'; n=strlen(s); stack[1]='e'; i=1; j=0; printf("\n stack input\n--------------------\n"); while((stack[i]!='$')&&(s[j]!='$')) { if(stack[i]==s[j]) { i++; j++; } switch(stack[i])
VBIT, Ghatkesar Dept of CSE Page 12

Databases And Compiler LAB { case 'e':str1=0; break; case 'b':str1=1; break; case 't':str1=2; break; case 'c':str1=3; break; case'f':str1=4; break; } switch(s[j]) { case'i':str2=0; break; case'+':str2=1; break; case'*':str2=2; break; case'(':str2=3; break; case')':str2=4; break; case'$':str2=5; break; } if(m[str1][str2][0]=='\0') { printf("\n err"); exit(0); } else if(m[str1][str2][0]=='n') i--; else if(m[str1][str2][0]=='i') stack[i]='i'; else { for(k=size[str1][str2]-1;k>=0;k--) {
VBIT, Ghatkesar Dept of CSE

M.Tech(CSE)

Page 12

Databases And Compiler LAB stack[i]=m[str1][str2][k]; i++; } i--; } for(k=0;k<=i;k++) printf("%c",stack[k]); printf(" "); for(k=0;k<=n;k++) printf("%c",s[k]); printf("/n"); } printf("\n success"); getch(); }

M.Tech(CSE)

OUTPUT: ip string: (a+b)*c stack input -------------------$ (a+b)*c $ /n success

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

/*Recursive Descent Parser*/ #include<stdio.h> #include<conio.h> #include<string.h> char input[100]; int i,l; void main() { clrscr(); printf("\n recursive decent parsing for the following grammar\n"); printf("\n E->TE'\n E'->+TE'/@ \n T->FT'\n T'->*FT'/@ \n F->(E)/id \n"); E(); EP(); T(); TP(); F(); printf("enter the input string to be checked:\n"); gets(input); if(E()) { if(input[i+1]=='\0') printf("\n string is accepted"); else printf("\n string is not accepted"); } else printf("\n string not accepted"); getch(); } E() { if(T()) {
VBIT, Ghatkesar Dept of CSE Page 12

Databases And Compiler LAB if(EP()) return(1); else return(0); } else return(0); } EP() { if(input[i]=='+') { i++; if(T()) { if(EP()) return(1); else return(0); } else return(0); } else return(1); } T() { if(F()) { if(TP()) return(1); else return(0); } else return(0); } TP()
VBIT, Ghatkesar Dept of CSE

M.Tech(CSE)

Page 12

Databases And Compiler LAB { if(input[i]=='*') { i++; if(F()) { if(TP()) return(1); else return(0); } else return(0); } else return(1); } F() { if(input[i]=='(') { i++; if(E()) { if(input[i]==')') { i++; return(1); } else return(0); } else return(0); } else if(input[i]>='a'&&input[i]<='z'||input[i]>='A'&&input[i]<='Z') { i++; return(1); }
VBIT, Ghatkesar Dept of CSE

M.Tech(CSE)

Page 12

Databases And Compiler LAB return(0); }

M.Tech(CSE)

OUTPUT: recursive decent parsing for the following grammar E->TE' E'->+TE'/@ T->FT' T'->*FT'/@ F->(E)/id enter the input string to be checked: (a+b)*c string is accepted

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

/* Shift Reduce Parser*/ void push(char *,int *,char); char red(char); void red1(char *); char pop(char *,int *); char *name[]={"e+e","e*e"}; int top=-1; #include<stdio.h> #include<conio.h> #include<string.h> void main() { char input[20],*s,d,a[20],b,c; clrscr();printf("enter the input"); scanf("%s",input); s=input; printf("\n"); while(*s) { d=red(*s); push(a,&top,d); if(top>=2) { red1(a); s++; } else s++; } b=pop(a,&top); c=pop(a,&top); if(b=='e' && c=='\0') printf("\t\t->accept the input");
VBIT, Ghatkesar Dept of CSE Page 12

Databases And Compiler LAB else printf("\t\t->do not accept the input"); getch(); } void push(char *a,int *sp,char item) { if(*sp==20) printf("\t\t->stack is full"); else { *sp=*sp+1; a[*sp]=item; } } char red(char item) { if(item=='i') return('e'); else return(item); } void red1(char *a) { char t[4],j=0,d='e';while(j<=2) { t[j]=pop(a,&top) j++ } t[j]='\0'; if(strcmp(t,*name)==0) push(a,&top,d); else { j--; while(j>=0) { push(a,&top,t[j]); j--; } }
VBIT, Ghatkesar Dept of CSE

M.Tech(CSE)

Page 12

Databases And Compiler LAB } char pop(char *a,int *sp) { char item; if(*sp==-1) return('\0'); else { item=a[*sp]; *sp=*sp-1; return(item); } } OUTPUT: enter the inputi+i ->accept the input

M.Tech(CSE)

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

/* Three Address Code */ #include<stdio.h> #include<conio.h> #include<ctype.h> #include<stdlib.h> typedef struct node { char name[16]; int op; struct node *left; struct node *right; }node; struct node *build(); void trav(); struct node *new1(); void main() { struct node *n; clrscr(); n=build(); printf("code generated:\n"); trav(n); getch(); } node *build() { char buf[80]; node *stack[10]; node **sp=stack-1; node *p; while(gets(buf)) { switch(*buf) {
VBIT, Ghatkesar Dept of CSE Page 12

Databases And Compiler LAB case '*': case '+': p=new1(); p->right=*sp--; p->left=*sp--; p->op=*buf; *++sp=p; break; default: p=new1(); strcpy(p->name,buf); *++sp=p; break; } } return *sp--; } node *new1() { node *p; if(!p==(node *)(calloc(1,sizeof(node)))) exit(0); return p; } void trav(struct node *root) { static int tnum=0; if(!root) return; if(!(root->op)) { printf("t%d=%s\n",tnum,root->name); sprintf(root->name,"t%d",tnum); ++tnum; } else { trav(root->left); if((root->left)!=(root->right)); trav(root->right);
VBIT, Ghatkesar Dept of CSE

M.Tech(CSE)

Page 12

Databases And Compiler LAB

M.Tech(CSE)

printf("%s%c=%s\n",root->right->name,root->op,root->left->name); strcpy(root->name,root->right->name); } }

OUTPUT: a+b*c t1=b*c t2=a+t1

VBIT, Ghatkesar

Dept of CSE

Page 12

Databases And Compiler LAB

M.Tech(CSE)

/*SLR PARSER*/ #include<stdio.h> char tab[12][9] [2]={"s5","N","N","S4","N","N","1","2","3","N","S6","N","N","N ","Z","0","0","0","N","R2","S7","N","R2","R2","0","0","0","N", "R4","R4","N","R4","R4","0","0","0","S5","N","N","S4","N","N ","8","2","3","N","R6","6","N","R6","R6","0","0","0","S5","N"," N","N","S4","N","0","0","X","N","S6","N","N","SY","N","0","0" ,"0","N","R1","S7","N","R1","R1","0","0","0","N","R3","R3","N ","R3","R3","0","0","0","N","R5","R5","N","R5","R5","0","0","0 "}; /*z=accept; N->error; X->10; Y->11;*/ char prod[7][4]={"0","E+T","T","T*F","F","(E)","d"}; char index[12]={'0','1','2','3','4','5','6','7','8','9','X','Y'}; char term[9]={'d','t','(',')','$','E','T','F'}; int row,col,st_pt=0,ip_pt=0; char input[10],stack[20]; clrscr(); void main() { int j,k; printf("\n enter input string"); scanf("%s",input); strcat(input,"$"); stack[0]='0'; for(j=0;j<7;j++) strcat(prod[j],"\0"); printf("\n STACK INPUT\n\n"); while(1) {
VBIT, Ghatkesar Dept of CSE Page 12

Databases And Compiler LAB for(k=0;k<=st_pt;k++) printf("%c",stack[k]); printf(" "); for(k=ip_pt;input[k-1]!='$';k++) printf("%c",input[k]); printf("\n"); row=is_index(stack[st_pt]); col=is_term(input[ip_pt]); if(tab[row][col][0]=='s') shift(tab[row][col][1]); else if(tab[row][col][0]=='R') reduce(tab[row][col][1]); else if(tab[row][col][0]=='Z') { printf("\n success"); getch(); exit(0); } if(tab[row][col][0]=='N') { printf("\n error"); getch(); exit(0); } } } shift(char ch) { st_pt++; stack[st_pt++]=input[ip_pt++]; stack[st_pt]=ch; } reduce(char ch) { int k,prno,prlen,rowno,colno; for(k=1;k<7;k++) if(index[k]==ch) prno=k;
VBIT, Ghatkesar Dept of CSE

M.Tech(CSE)

Page 12

Databases And Compiler LAB prlen=strlen(prod[prno]); for(k=1;k<=2*prlen;k++) st_pt--; if(prno==1||prno==2) stack[st_pt]='T'; else if(prno==3||prno==4) stack[st_pt]='T'; else if(prno==5||prno==6) stack[st_pt]='F'; rowno=is_index(stack[st_pt-1]); colno=is_term(stack[st_pt]); stack[++st_pt]=tab[rowno][colno][0]; } is_index(char ch) { int k; for(k=0;k<=9;k++) if(index[k]==ch) return(k); if(index[k]=='X') return(10); else if(index[k]=='Y') return(11); } is_term(char ch) { int k; for(k=0;k<9;k++) if(term[k]==ch) return(k); } OUTPUT: Enter input string: id+id*id String is accepted
VBIT, Ghatkesar Dept of CSE

M.Tech(CSE)

Page 12

Databases And Compiler LAB

M.Tech(CSE)

VBIT, Ghatkesar

Dept of CSE

Page 12

You might also like