Академический Документы
Профессиональный Документы
Культура Документы
SYSTEM PROGRAMMING
Laboratory Manual
Year: 2019-2020
AIT SP
System Programming (2150708)
INDEX
AIT SP
System Programming (2150708)
Practical-1 Date:-
Write a program to print the content of file and display file content using of file pointer.
#include <stdio.h>
#include <stdlib.h> // For exit() int
main()
{
FILE *fptr;
char filename[100], c;
// Open file
fptr = fopen(filename, "r");
if (fptr== NULL)
{
printf("Cannot open file \n");
exit(0);
}
fclose(fptr); return 0;
}
Output:
Exercise
C program to copy contents of one file to another file
EVALUATION:
Involvement Understanding / Problem solving Timely Completion Total
(4) (3) (3) (10)
AIT SP 1
System Programming (2150708)
Practical 2 Date:-
Software Required:
Turbo C Compiler
Knowledge Required:
Theory/Logic:
A lexical analyzer reads and converts the input into a stream of tokens to be analyzed
by the parser.
The sentences of a language consist of string of tokens.
A lexical Analyzer can insulate a parser from the lexeme representation of tokens.
A lexical analyzer generates tokens for any program that is given as an input to it.
CODE ANALYZER
AIT SP 2
System Programming (2150708)
Application:
Removal of White Spaces and comments.
Recognizing Identifiers and Keywords, Recognizing Constants.
Conclusion:
A Lexical Analyzer reads and converts the input into a stream of tokens to be
analyzed by the parser.
Question:
1. What is Lexical Analyzer?
Answer:
Lexical Analysis is a function in which the stream of characters making up the
source program is read from left to right and grouped into tokens that are sequences of
characters having a collective meaning.
Exercise
Write down a Program for Lexical Analyzer in C.
EVALUATION:
AIT SP 3
System Programming (2150708)
Practical 3 Date:-
Software Required:
Turbo C Compiler
Knowledge Required:
Theory/Logic:
The basic idea is that when it is not clear which of the two alternative
productions to use to expand a non terminal A ,we may be able to rewrite
the A production to defer the decision until we have seen enough of the
input to make the right choice.
Algorithm
Input: Grammar G with no cycles or productions.
for each non terminal A find the longest prefix a common to two or
more of its alternatives.
A->B|B|…..B
AIT SP 4
System Programming (2150708)
#include<stdio.h>
#include<string.h>
int main()
{
char gram[20],part1[20],part2[20],modifiedGram[20],newGram[20],tempGram[20];
int i,j=0,k=0,l=0,pos;
for(i=0;gram[i]!='|';i++,j++)
part1[j]=gram[i];
part1[j]='\0';
for(j=++i,i=0;gram[j]!='\0';j++,i++)
part2[i]=gram[j];
part2[i]='\0';
for(i=0;i<strlen(part1)||i<strlen(part2);i++)
{
if(part1[i]==part2[i])
{
modifiedGram[k]=part1[i];
k++;
pos=i+1;
}
}
for(i=pos,j=0;part1[i]!='\0';i++,j++)
{
newGram[j]=part1[i];
}
newGram[j++]='|';
for(i=pos;part2[i]!='\0';i++,j++)
{
newGram[j]=part2[i];
}
modifiedGram[k]='X';
modifiedGram[++k]='\0';
newGram[j]='\0';
printf("\n A->%s",modifiedGram);
printf("\n X->%s\n",newGram);
}
AIT SP 5
System Programming (2150708)
Advantage:
Left factoring is d grammar transformation that is useful for producing a grammar
suitable for predictive parsing.
We can eliminate repetitive grammar productions.
Disadvantage:
There might be in the grammar that no left factoring is there so in that case this
method is not useful in parsing.
EVALUATION:
AIT SP 6
System Programming (2150708)
Practical 4 Date:-
Aim: Write c program to remove left recursion from given grammar.
Knowledge Requirement:
Algorithm to solve immediate left recursion should be known and how to apply it
should be known.
Theory/Logic:
Top-down parsing methods can not handle left-recursive grammars.
So a transformation that eliminates left-recursion is needed.
This program will eliminate left-recursion problem of given grammar.
Algorithm:
Input: Grammar G with no cycles or null-productions.
Output: An equivalent grammar with no left recursion.
Method: Apply the algorithm given below to G.
Exercise:-Write down a program to remove left recursion from the following input
Input:- E→EA/A,A→AT/a,T→a,E->1
EVALUATION:
AIT SP 7
System Programming (2150708)
Knowledge Required:
Concept of Predictive parsing
The whole knowledge of the First set of the Grammar.
Knowledge about the look ahead symbol.
Theory/Logic:
Recursive-decent parsing is a top-down method of syntax analysis in which
we execute set of recursive procedures to process the input. A procedure is
associated with each non-terminal of a grammar.
AIT SP 8
System Programming (2150708)
Example:
Advantage:
It helps in successful parsing of the string without backtracking.
Application:
This application can be used for the parsing of this particular parser.
Conclusion:
This application successfully parses the valid string from the given grammer.
Exercise:-Write down a program to generate the recursive descent parser from the
given grammar.
E→iE’,E’->+iE’|epsilon
EVALUATION:
AIT SP 9
System Programming (2150708)
Practical-6 Date:-
Aim : Write a program to Implement Predictive Parser for the given grammar
T -> F * T / F
F -> ( E ) / i
E -> T + E / T
Knowledge Required:
AIT SP 10
System Programming (2150708)
EVALUATION:
AIT SP 11
System Programming (2150708)
Practical-7 Date:-
AIM:-Write a SAL program in text file and generate SYMTAB and LITTAB
Knowledge Required:
To Know about Assembly Language Pass-1 Assembler
Theory/Logic: Translate assembly language programs to object programs or machine
code is called an Assembler.
One-pass assemblers are used when
o it is necessary or desirable to avoid a second pass over the source program
o the external storage for the intermediate file between two passes is slow or is
inconvenient to use
Main problem: forward references to both data and instructions
One simple way to eliminate this problem: require that all areas be defined before they
are referenced.
o It is possible, although inconvenient, to do so for data items.
AIT SP 12
System Programming (2150708)
Practical-8 Date:-
Aim:Use macro features of C language.
Knowledge Required:
-Concept of subroutine and user define function
Theory/Logic:-
A macro name is an abbreviation, which stands for some related lines of code. Macros
are useful for the following purposes:
AIT SP 13
System Programming (2150708)
PRINT_MAX
END
Advantage:-
Macro Expansion.
A macro call leads to macro expansion. During macro expansion, the macro statement is
replaced by sequence of assembly statements.
EVALUATION:
AIT SP 14
System Programming (2150708)
Practical-9 Date:-
Aim:-Write a program which generates Quadruple Table for the given postfix String.
Knowledge Required:
-Concept of Postfix,Prefix
Theory/Logic:
The quadruple is a structure with at the most tour fields such as op,arg1,arg2 and
result.
The op field is used to represent the internal code for operator, the arg1 and arg2
represent the two operands used and result field is used to store the result of an
expression.
Moreover, Consider the input statement x:= -a*b + -a*b
Advantage:-
- Using quadruples, we can move a statement that computes A without requiring any
changes in the statements using A, because the result field is explicit.
EVALUATION:
Involvement Understanding / Problem solving Timely Completion Total
(4) (3) (3) (10)
AIT SP 15
System Programming (2150708)
Practical-10 Date:-
Aim: Write a C program to parse a given string using Predictive parsing for given
grammar.
type → simple | ↑id | array [ simple ] of type
simple → integer | char | num dotdot num
Software Required:
Turbo C Compiler
Knowledge Required:
Theory/Logic:
Recursive-decent parsing is a top-down method of syntax analysis in which
we execute set of recursive procedures to process the input. A procedure is
associated with each non-terminal of a grammar.
AIT SP 16
System Programming (2150708)
Exercise:-Write down a program to generate the following output with use of given
grammar.
Input: type=simple|^ id|array [ simple ]
simple=int|char|num..num
Output:The grammar is :
type=simple|^ id|array [ simple ]
simple=int|char|num..num
Advantage:
It helps in successful parsing of the string without backtracking.
Application:
This application can be used for the parsing of this particular parser.
Conclusion:
This application successfully parses the valid string from the given grammer.
Question:
1. What is predictive parser?
A non backtracking form of Top Down parsing is predictive parsing.
EVALUATION:
AIT SP 17