Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

System Programming and Operating system, Assignments of Microprocessors

System Programming and Operating system

Typology: Assignments

2020/2021

Uploaded on 12/27/2021

ravi-bhanushali
ravi-bhanushali 🇮🇳

1 document

1 / 78

Toggle sidebar

Related documents


Partial preview of the text

Download System Programming and Operating system and more Assignments Microprocessors in PDF only on Docsity! INDEX Sr. Title of Assignment Page No. No. GroupA 1 Design suitable data structures and implement pass-I of a two-pass assembler 1 for pseudo-machine in Java using object oriented feature. Implementation should consist of a few instructions from each category and few assembler directives. 2 Implement Pass-II of two pass assembler for pseudo-machine in Java using 7 object oriented features. The output of assignment-1 (intermediate file and symbol table) should be input for this assignment. 3 Design suitable data structures and implement pass-I of a two-pass macro 10 processor using OOP features in Java 4 Write a Java program for pass-II of a two-pass macro-processor. The output 14 of assignment-3 (MNT, MDT and file without any macro definitions) should be input for this assignment. Group B 5 Write a program to create Dynamic Link Library for any mathematical 17 operation and write an application program to test it. (Java Native Interface / Use VB or VC++). 6 Write a program using Lex specifications to implement lexical analysis phase 23 of compiler to generate tokens of subset of ‘Java’ program. 7 Write a program using Lex specifications to implement lexical analysis phase 30 of compiler to count no. of words, lines and characters of given input file. 8 Write a program using YACC specifications to implement syntax analysis 42 phase of compiler to validate type and syntax of variable declaration in Java. 9 Write a program using YACC specifications to implement syntax analysis 31 phase of compiler to recognize simple and compound sentences given in input file. Group C 10 Write a Java program (using OOP features) to implement following 64 scheduling algorithms: FCFS , SJF (Preemptive), Priority (Non-Preemptive) and Round Robin (Preemptive) 11 Write a Java program to implement Banker’s Algorithm 76 12 Implement UNIX system calls like ps, fork, join, exec family, and wait for 81 process management (use shell script/ Java/ C programming). 13 Study assignment on process scheduling algorithms in Android and Tizen. 92 Group D 14 Write a Java Program (using OOP features) to implement paging simulation 95 using 1. Least Recently Used (LRU) 2. Optimal algorithm System Programming & OS Laboratory TE Computer Engineering (2019 Course) Group - A Assignment No. 1 Title: Design suitable data structures and implement pass-I of a two-pass assembler for pseudo-machine in Java using object oriented feature. Implementation should consist of a few instructions from each category and few assembler directives. 2. Objectives: - To understand data structures to be used in pass I of an assembler. - To implement pass I of an assembler 3. Problem Statement: Write a program to create pass-I Assembler 4, Outcomes: After completion of this assignment students will be able to: - Understand the concept of Pass-I Assembler - Understand the Programming language of Java 5. Software Requirements: + Linux OS, JDK1.7 6. Hardware Requirement: - 4GB RAM ,500GB HDD 7. Theory Concepts: A language translator bridges an execution gap to machine language of computer system. An assembler is a language translator whose source language is assembly language. An Assembler is a program that accepts as input an Assembly language program and converts it into machine language. Language processing activity consists of two phases, Analysis phase and synthesis phase. Analysis of source program consists of three components, Lexical rules, syntax rules and semantic rules. Lexical rules govern the formation of valid statements in source language. Semantic rules associate the formation meaning with valid statements of language. Synthesis SYMBOL TABLE have following fields : Name of Symbol Value symbol Address Il. LITERAL TABLE (LITTAB): it contains entries such as literal and its value. Literal Table has following fields : literal Address of Literal Ill . POOL TABLE (POOLTAB): Contains literal number of the starting literal of each literal pool. Pool TABLE (pooltab) have following fields. LITERAL_NO IV: Location Counter which contains address of next instruction by calculating length of each instruction. Design of a Two Pass Assembler: - Tasks performed by the passes of two-pass assembler are as follows: Pass I: - Separate the symbol, mnemonic opcode and operand fields. Determine the storage-required foe every assembly language statement and update the location counter. Build the symbol table and the literal table. Construct the intermediate code for every assembly language statement. System Programming & OS Laboratory TE Computer Engineering (2019 Course) Pass II: - Synthesize the target code by processing the intermediate code generated during pass! INTERMEDIATE CODE REPRESENTATION The intermediate code consists of a set of IC units, each IC unit consisting of the following three fields 1. Address 2. Representation of the mnemonic opcode 3. Representation of operands Mnemonic field The mnemonic field contains a pair of the form: (statement class, code) Where statement class can be one of IS,DL and AD standing for imperative statement, declaration statement and assembler directive , respectively. For imperative statement, code is the instruction opcode in the machine language For declaration and assembler directives , following are the codes Declaration Statements Assembler directives DC 01 START O01 DS 02 END 02 ORIGIN 03 EQU 04 LTORG 05 Algorithms(procedure) : PASS 1 ¢ Initialize location counter, entries of all tables as zero. ¢ Read statements from input file one by one. ¢ While next statement is not END statement System Programming & OS Laboratory TE Computer Engineering (2019 Course) I. Tokenize or separate out input statement as label, numonic,operand 1 ,operand2 II. If label is present insert label into symbol table. III. If the statement is LTORG statement processes it by making its entry into literal table, pool table and allocate memory. IV. If statement is START or ORIGEN Process location counter accordingly. V. If an EQU statement, assign value to symbol by correcting entry in symbol table. VI. For declarative statement update code, size and location counter. VII. Generate intermediate code. VIII. Pass this intermediate code to pass -2. Conclusion: Thus, I have studied visual programming and implemented dynamic link library application for arithmetic operation References: J.J. Donovan, “Systems Programming”, McGraw Hill.[ chapter 3 topic 3.0,3.1, 3.2.1 in brief »3.2.2 figure 3.3 &3.5] Oral Questions: [Write short answer] 1. Explain what is meant by pass of an assembler. 2. Explain the need for two pass assembler. 3. Explain terms such as Forward Reference and backward reference. 4. Explain various types of errors that are handled in passé-I. 5. Explain the need of Intermediate Code generation and the variants used. 6. State various tables used and their significance in the design of two pass Assembler. 7. What is the job of assembler? 8. What are the various data structures used for implementing Pass-I of a two-pass assembler. 9. What feature of assembly language makes it mandatory to design a two pass assembler? 10. How are literals handled in an assembler? 11. How assembler directives are handled in pass I of assembler? System Programming & OS Laboratory TE Computer Engineering (2019 Course) Group - A Assignment No. 2 1. Title: Implement Pass-II of two pass assembler for pseudo-machine in Java using object oriented features. The output of assignment-1 (intermediate file and symbol table) should be input for this assignment. 2. Objectives: - To understand data structures to be used in pass II of an assembler. - To implement pass I of an assembler library application for arithmetic operation References: J.J. Donovan, “Systems Programming”, McGraw Hill.[ chapter 3 ] Oral Questions: [Write short answer] 1. Explain various types of errors that are handled in passé-II. 3. Draw flowchart of passé-II. 4. State various tables used and their significance in the design of two pass Assembler. 5. How LTORG statement is handled in pass II of assembler? 6. How Declarative statement is handled in pass II of assembler? 7. What is the significance of pool table? 8. Which data structures of pass I are used in pass II of assembler? 9. Explain the handling of imperative statement. 10. What feature of assembly language makes it mandatory to design a two pass assembler? 11. How are literals handled in an assembler? 12. How assembler directives are handled in pass I of assembler? System Programming & OS Laboratory TE Computer Engineering (2019 Course) Group - A Assignment No. 3 1. Title: Design suitable data structures and implement pass-I of a two-pass macro-processor using OOP features in Java 2. Objectives: - To Identify and create the data structures required in the design of macro processor. - To Learn parameter processing in macro - To implement pass I of macroprocessor 3. Problem Statement: Write a program to create pass-I Macro-processor 4, Outcomes: After completion of this assignment students will be able to: - Understand the Programming language of Java - Understand the concept of Pass-I Macro-processor 5. Software Requirements: o Linux OS, JDK1.7 6. Hardware Requirement: - 4GB RAM ,500GB HDD 7. Theory Concepts: MACRO Macro allows a sequence of source language code to be defined once & then referred to by name each time it is to be referred. Each time this name occurs is a program the sequence of codes is substituted at that point. A macro consist of 1. Name of the macro 2. Set of parameters 3. Body of macro System Programming & OS Laboratory TE Computer Engineering (2019 Course) Macros are typically defined at the start of program. Macro definition consists of 1. MACRO pseudo 2. MACRO name 3. Sequence of statements 4. MEND pseudo opcode terminating A macro is called by writing the macro name with actual parameter is an assembly program. The macro call has following syntax <macro name>. MACRO PROCESSOR MACRO PROCESSOR Prog with macro ASSMBLER Target —————* Prog ng =p defination C Fig 1. Assembly language program without macro Macro processor takes a source program containing macro definition & macro calls and translates into an assembly language program without any macro definition or calls. This program can now be handled over to a conventional assembler to obtain the target language. MACRO DEINITION Macros are typically defined at the start of a program. A macro definition consists of 1. MACRO pseudo code 2. MACRO name 3. Sequence of statement 4. MEND pseudo opcode terminating macro definition Structure of a macro Example MACRO INCR & ARG ADD AREG,& ARG ADD BRA,& ARG System Programming & OS Laboratory TE Computer Engineering (2019 Course) ADD CREG, & ARG MEND MACRO Expansion: During macro expansion each statement forming the body of the macro as picked up one by one sequentially. a. Each statement inside macro may have as it is during expansion. b. The name of a formal parameter which is preceded by the character ,,&“ during macro expansion an ordinary starting is retained without any modification. Formal parameters are replaced by actual parameters value. When a call is found the call processor sets a pointer the macro definition table pointer to the corresponding macro definition started in MDT. The initial value of MDT is obtained from MDT index. \Design of macro processor: Pass-I Generate Macro Name Table (MNT) Generate Macro Definition Table (MDT) Generate IC i.e. a copy of source code without macro definitions. MNT: Sr.No Macro Name MDT Index and MENDs defines a single macro instruction. 2. Save the definition: The processor must store the macro instruction definition, which it will need for System Programming & OS Laboratory TE Computer Engineering (2019 Course) expanding macro calls. 3. Recognize calls: The processor must recognize the macro calls that appear as operation mnemonics. This suggests that macro names be handled as a type of op-code. 4, Expand calls and substitute arguments: The processor must substitute for dummy or macro definition arguments the corresponding arguments from a macro call; the resulting symbolic text is then substitute for macro call. This text may contain additional macro definition or call. Implementation of a 2 pz algorithm 1. We assume that our macro processor is functionally independent of the assembler and that the output text from the macro processor will be fed into the assembler. 2. The macro processor will make two independent scans or passes over the input text , searching first for macro definitions and then for macro calls 3. The macro processor cannot expand a macro call before having found and saved the corresponding macro definitions. 4. Thus we need two passes over the input text , one to handle macro definitions and other to handle macro calls. 5. The first pass examines every operation code, will save all macro definitions in a macro Definition Table and save a copy of the input text, minus macro definitions on the secondary storage. 6. The first pass also prepares a Macro Name Table along with Macro Definition Table as seen in the previous assignment that successfully implemented pass — I of macro pre-processor. The second pass will now examine every operation mnemonic and replace each macro name with the appropriate text from the macro definitions. 8.SPECIFICATION OF DATABASE Pass 2 database: 1. The copy of the input source deck obtained from Pass- I 2. The output expanded source deck to be used as input to the assembler System Programming & OS Laboratory TE Computer Engineering (2019 Course) 3. The Macro Definition Table (MDT), created by pass | 4. The Macro Name Table (MNT), created by pass 1 5. The Macro Definition Table Counter (MNTC), used to indicate the next line of text to be used during macro expansion 6. The Argument List Array (ALA), used to substitute macro call arguments for the index markers in stored macro definition 9. Conclusion: Thus we have successfully implemented pass-II of a two-pass macro-processor References: J.J. Donovan, “Systems Programming”, McGraw Hill[chapter 4 ] Oral Questions: [Write short answer] 1. Write an algorithm for PASS-II of a two pass macro processor. 2. Explain macro expansion. 3. Draw flowchart of PASS-II of a two pass macro processor. 4. Explain nested Macro calls 5. Explain data structures that are used for implementing Pass II of a macro processor. 6. What is the input and output of pass II of macro processor? 7. What functions are involved in pass II of macro processor? 8. What is Conditional Macro Expansion? 9. Explain the contents of all the tables of pass II of macro processor. System Programming & OS Laboratory TE Computer Engineering (2019 Course) Group - B Assignment No. 1 1. Title: Write a program to create Dynamic Link Library for any mathematical operation and write an application program to test it. (Java Native Interface / Use VB or VC++). 2. Objectives: - To understand Dynamic Link Libraries Concepts - To implement dynamic link library concepts - To study about Visual Basic 3. Problem Statement: Write a program to create Dynamic Link Library for Arithmetic Operation in VB.net 4, Outcomes: After completion of this assignment students will be able to: - Understand the concept of Dynamic Link Library - Understand the Programming language of Visual basic 5. Software Requirements: + Visual Studio 2010 6. Hardware Requirement: - MIC Lenovo Think center M700 Ci3,6100,6th Gen. H81, 4GB RAM ,500GB HDD 7. Theory Concepts: Dynamic Link Library: A dynamic link library (DLL) is a collection of small programs that can be loaded when needed by larger programs and used at the same time. The small program lets the larger program communicate with a specific device, such as a printer or scanner. It is often packaged as a DLL program, which is usually referred to as a DLL file. DLL files that support specific device operation are known as device drivers. A DLL file is often given a" .dll" file name suffix. DLL files are dynamically linked with the program that uses them during program execution rather than being compiled into the main program. The advantage of DLL files is space is saved in random access memory (RAM) because the files don't get loaded into RAM together with the main program. When a DLL file is needed, it is loaded and run. For example, as long as a user is editing a document in Microsoft Word, the printer DLL file does not need to be loaded into RAM. If the user decides to print the document, the Word application causes the printer DLL file to be loaded and run. System Programming & OS Laboratory TE Computer Engineering (2019 Course) A program is separated into modules when using a DLL. With modularized components, a program can be sold by module, have faster load times and be updated without altering other parts of the program. DLLs help operating systems and programs run faster, use memory efficiently and take up less disk space. Feature of DLL: - DLLs are essentially the same as EXEs, the choice of which to produce as part of the linking process is for clarity, since it is possible to export functions and data from either. - It is not possible to directly execute a DLL, since it requires an EXE for the operating system to load it through an entry point, hence the existence of utilities like RUNDLL.EXE or RUNDLL32.EXE which provide the entry point and minimal framework for DLLs that contain enough functionality to execute without much support. - DLLs provide a mechanism for shared code and data, allowing a developer of shared code/data to upgrade functionality and the DLL function must be compatible in the following ways: the order in which the function expects its arguments to be pushed onto the stack, whether the function or the application is responsible for cleaning up the stack, and whether any arguments are passed in registers. - Provides a mechanism to extend the MFC library classes. You can derive classes from the existing MFC classes and place them in an MFC extension DLL for use by MFC applications. - Eases the creation of international versions. By placing resources in a DLL, it is much easier to create international versions of an application. You can place the strings for each language version of your application in a separate resource DLL and have the different language versions load the appropriate resources. Disadvantage: - A potential disadvantage to using DLLs is that the application is not self-contained; it depends on the existence of a separate DLL module. Visual Basic: Visual Basic is a third-generation event-driven programming language first released by Microsoft in 1991. It evolved from the earlier DOS version called BASIC. BASIC means Beginners’ All- purpose Symbolic Instruction Code. Since then Microsoft has released many versions of Visual Basic, from Visual Basic 1.0 to the final version Visual Basic 6.0. Visual System Programming & OS Laboratory TE Computer Engineering (2019 Course) Basic is a user-friendly programming language designed for beginners, and it enables anyone to develop GUI window applications easily. In 2002, Microsoft released Visual Basic.NET (VB.NET) to replace Visual Basic 6. Thereafter, Microsoft declared VB6 a legacy programming language in 2008. Fortunately, Microsoft still provides some form of support for VB6. VB.NET is a fully object-oriented programming language implemented in the .NET Framework. It was created to cater for the development of the web as well as mobile applications. However, many developers still favor Visual Basic 6.0 over its successor Visual Basic.NET - Design (architecture): Stack a. bynamic libraries: Heap Dynamic Uereries Application file Application code ! —EEEEEE — Bynamic library a Dynamic library references | references a 8. Algorithms(procedure) : Note: you should write algorithm & procedure as per program/concepts 9. Flowchart : Note: you should draw flowchart as per algorithm/procedure 10. Conclusion: Thus, I have studied visual programming and implemented dynamic link library application for arithmetic operation References: https://en.wikipedia.org/wiki/Visual_Basic https://www.: le.co.in/search? g=dynamic+link+library +: e=lnms System Programming & OS Laboratory TE Computer Engineering (2019 Course) &tbm=isch&sa=X &ved=0ahUKEwjqubTAuJvZAhWHQ48KHRZbD7sQ_AUICigB&biw = 136 https://msdn.microsoft.com/en-us/library/9yd93633.aspx Oral Questions: [Write short answer] 1. What Is Dll And What Are Their Usages And Advantages? 2. What Are The Sections In A Dil Executable/binary? 3. Where Should We Store Dils ? 4. Who Loads And Links The Dils? 5. How Many Types Of Linking Are There? 6. What Is Implicit And Explicit Linking In Dynamic Loading? 7. How to call a DLL function from Visual Besic? System Programming & OS Laboratory TE Computer Engineering (2019 Course) Group - B Assignment No. 2 1. Title: Write a program using Lex specifications to implement lexical analysis phase of compiler to generate tokens of subset of ,,Java™ program 2. Objectives : - To understand LEX Concepts - To implement LEX Program - To study about Lex & Java - To know important about Lexical analyzer 3. Problem Statement : Write a program using Lex specifications to implement lexical analysis phase of compiler to generate tokens of subset of ,Java™ program 4, Outcomes: - Understand the concept of LEX Tool - Understand the lexical analysis part - It can be used for data mining concepts. 5. Software Requirements: + LEX Tool (flex) 6. Hardware Requirement: - MIC Lenovo Think center M700 Ci3,6100,6th Gen. H81, 4GB RAM ,500GB HDD 7. Theory Concepts: Lex stands for Lexical Analyzer. Lex is a tool for generating Scanners. Scanners are programs that recognize lexical patterns in text. These lexical patterns (or regular Expressions) are defined in a particular syntax. A matched regular expression may have an associated action. This action may also include returning a token. When Lex receives input in the form of a file or text, it takes input one character at a time and continues until a pattern is matched, then lex performs the associated action (Which may include returning a token). If, on the other hand, no regular expression can be matched, further processing stops and Lex displays an error message. Lex and C are tightly coupled. A .lex file (Files in lex have the extension .lex) is passed through the lex utility, and produces output files in C. These file(s) are coupled to produce an executable version of the lexical analyzer. Lex turns the user“s expressions and actions into the host general —purpose language; the generated program is named yylex. The yylex program will recognize expressions in a System Programming & OS Laboratory TE Computer Engineering (2019 Course) stream (called input in this memo) and perform the specified actions for each expression as it is detected. + Regular Expression in Lex:- A Regular expression is a pattern description using a meta language. An expression is made up of symbols. Normal symbols are characters and numbers, but there are other symbols that have special meaning in Lex. The following two tables define some of the symbols used in Lex and give a few typical examples. + Defining regular expression in Lex:- Character Meaning A-Z, 0-9,a-z Character and numbers that form of the pattern. Matches any character except \n. Table 3: Character Class Pattern Matches [abc] one of:a bc [a-z] any letter a through z [a\- one of:a - z z] [-az] one of: - a z [A-Za-z0- one or more alphanumeric 9]+ characters [ \t\n] + whitespace [*ab] anything except: a b [a*b] one of: a * b [alb] one of: alb Regular expressions are used for pattern matching. A character class defines a single character and normal operators lose their meaning. Two operators supported in a character class are the hyphen ("-") and circumflex ("4"). When used between two characters the hyphen represents a range of characters. The circumflex, when used as the first character, negates the expression. If two patterns match the same string the longest match wins. In case both matches are the same length, then the first pattern listed is used. .. definitions ... %% . rules ... %% .. Subroutines ... Input to Lex is divided into three sections with % % dividing the sections. This is best illustrated by example. The first example is the shortest possible lex file: %% Input is copied to output one character at a time. The first % % is always required as there must always be a rules section. However if we don“t specify any rules then the default action is to match everything and copy it to output. Defaults for input and output are stdin and stdout, respectively. Here is the same example with defaults explicitly coded: %% System Programming & OS Laboratory TE Computer Engineering (2019 Course) /* match everything except newline */ .- ECHO; /* match newline */ \n ECHO; %% int yywrap(void) { return 1; } int main(void) { yylex(); return 0; } Two patterns have been specified in the rules section. Each pattern must begin in column one. This is followed by whitespace (space, tab or newline) and an optional action associated with the pattern. The action may be a single Cstatement, or multiple C statements, enclosed in braces. Anything not starting in column one is copied verbatim to the generated C file. We may take advantage of this behavior to specify comments in our lex file. In this example there are two patterns, "." and "\n", with an ECHO action associated for each pattern. Several macros and variables are predefined by lex. ECHO is a macro that writes code matched by the pattern. This is the default action for any unmatched strings. Typically, ECHO is defined as: #define ECHO fwrite(yytext, yyleng, 1, yyout) Variable yytext is a pointer to the matched string (NULL-terminated) and yyleng is the length of the matched string. Variable yyout is the output file and defaults to stdout. Function yywrap is called by lex when input is exhausted. Return | if you are done or 0 if more processing is required. Every C program requires a main function. In this case we simply call yylex that is the main entry-point for lex . Some implementations of lex include copies of main andyywrap in a library thus eliminating the need to code them explicitly. This is why our first example, the shortest lexprogram, functioned properly. Table 4: Lex Predefined Variables Name Function int yylex(void) call to invoke lexer, returns token char *yytext pointer to matched string yyleng length of matched string yylval value associated with token System Programming & OS Laboratory TE Computer Engineering (2019 Course) int yywrap(void) wrapup, return | if done, 0 if not done FILE *yyout output file FILE *yyin input file INITIAL initial start condition BEGIN condition switch start condition ECHO write matched string Here is a program that does nothing at all. All input is matched but no action is associated with any pattern so there will be no output. % % \n The following example prepends line numbers to each line in a file. Some implementations of lex predefine and calculate yylineno. The input file for lex is yyin and defaults to stdin. Fok int yylineno; %} %% A(.*)\n printf(""%4d\t%s", ++yylineno, yytext); %% int main(int arge, char *argy[]) { yyin = fopen(argy[1], "r''); yylex(); felose(yyin); } 8. Design (architecture) : tokens — en lexemes Lexical Syntax Analyzer Analyzer - Se request for tokens source-code System Programming & OS Laboratory TE Computer Engineering (2019 Course) regular expressions: = repetition, expressed by the "*" operator = alternation, expressed by the "I" operator = concatenation Any regular expression expressions may be expressed as a finite state automaton (FSA). We can represent an FSA using states, and transitions between states. There is one start state and one or more final or accepting states. letter or digit start a letter Ly ofher iF >) >(2) In Figure 3 state 0 is the start state and state 2 is the accepting state. As characters are read we make a transition from one state to another. When the first letter is read we transition to state 1. We remain in state | as more letters or digits are read. When we read a character other than a letter or digit we transition to accepting state 2. Any FSA may be expressed as a computer program. For example, our 3-state machine is easily programmed: start: goto stateO state0: read c if c = letter goto statel goto stateO System Programming & OS Laboratory TE Computer Engineering (2019 Course) statel: read c if c = letter goto statel if c = digit goto statel goto state2 state2: accept string This is the technique used by lex. Regular expressions are translated by lex to a computer program that mimics an FSA. Using the next input character and current state the next state is easily determined by indexing into a computer-generated state table. Now we can easily understand some of lex“'s limitations. For example, lex cannot be used to recognize nested structures such as parentheses. Nested structures are handled by incorporating a stack. Whenever we encounter a "(" we push it on the stack. When a)" is encountered we match it with the top of the stack and pop the stack. However lex only has states and transitions between states. Since it has no stack it is not well suited for parsing nested structures. Yacc augments an FSA with a stack and can process constructs such as parentheses with ease. The important thing is to use the right tool for the job. Lex is good at pattern matching. Yacc is appropriate for more challenging tasks. + Regular Expression in Lex:- A Regular expression is a pattern description using a meta language. An expression is made up of symbols. Normal symbols are characters and numbers, but there are other symbols that have special meaning in Lex. The following two tables define some of the symbols used in Lex and give a few typical examples. + Defining regular expression in Lex:- Character Meaning A-Z, 0-9,a-z Character and numbers that form of the pattern. Matches any character except \n. Used to denote range. Example: A-Z implies all characters from A to Z. Matches zero or more occurences of the preceiding pattern. Matches one or more occurences of the preceiding pattern. Matches zero or one occurences of the preceiding pattern. Matches end of line as the last character of the pattern. System Programming & OS Laboratory TE Computer Engineering (2019 Course) {} Indicates how many times a pattern can be present. Example: A {1,3} implies one or three occurences of A may be present. Used to escape meta characters. Also used to remove the special meaning of characters as defined in this table. Negation Logical OR between expressions. “<some symbols>” Literal meaning of characters. Meta characters hold. / Look ahead matches the preceding pattern only if followed by the succeeding expression. Example: AO/1 matches AO only if AO1 is the input. O Groups a series of regular expressions. + Examples of regular expressions Regular Meaning expression Joke[rs] Matches either jokes or joker A {1,2}shis+ Matches Aashis, Ashis, Aashi, Ashi. (A[b-e])+ Matches zero or one occurrences of A followed by any character from b to e. Tokens in Lex are declared like variable names in C.Every token has an associated expression.(Examples of tokens and expression are given in the following table). Using the examples in our tables, we“ll build a word-counting program. Our first task will be to show how tokens are declared. + Examples of token declaration Token | Associated expression Meaning Number | ([0-9])+ 1 or more occurences of a digit Chars [A-Za-z] Any character Blank — A blank space Word (chars)+ 1 or more occurences of chars Variable | (chars)+(number)*(chars)*(number)* Programming in Lex:- Programming in Lex can be divided into three steps: 1. Specify the pattern-associated actions in a form that Lex can understand. 2. Run Lex over this file to generate C code for the scanner. System Programming & OS Laboratory TE Computer Engineering (2019 Course) Two patterns have been specified in the rules section. Each pattern must begin in column one. This is followed by whitespace (space, tab or newline) and an optional action associated with the pattern. The action may be a single Cstatement, or multiple C statements, enclosed System Programming & OS Laboratory TE Computer Engineering (2019 Course) in braces. Anything not starting in column one is copied verbatim to the generated C file. We may take advantage of this behavior to specify comments in our lex file. In this example there are two patterns, "." and "\n", with an ECHO action associated for each pattern. Several macros and variables are predefined by lex. ECHO is a macro that writes code matched by the pattern. This is the default action for any unmatched strings. Typically, ECHO is defined as: #define ECHO fwrite(yytext, yyleng, 1, yyout) Variable yytext is a pointer to the matched string (NULL-terminated) and yyleng is the length of the matched string. Variable yyout is the output file and defaults to stdout. Function yywrap is called by lex when input is exhausted. Return | if you are done or 0 if more processing is required. Every C program requires a main function. In this case we simply call yylex that is the main entry-point for lex . Some implementations of lex include copies of main andyywrap in a library thus eliminating the need to code them explicitly. This is why our first example, the shortest lexprogram, functioned properly. Name Function int yylex(void) call to invoke lexer, returns token char *yytext pointer to matched string yyleng length of matched string yylval value associated with token int yywrap(void) wrapup, return | if done, 0 if not done FILE *yyout output file FILE *yyin input file INITIAL initial start condition BEGIN condition switch start condition ECHO write matched string Here is a program that does nothing at all. All input is matched but no action is associated with any pattern so there will be no output. %% \n The following example prepends line numbers to each line in a file. Some implementations of lex predefine and calculate yylineno. The input file for lex is yyin and defaults to stdin. System Programming & OS Laboratory TE Computer Engineering (2019 Course) Ft int yylineno; %} %% A(.*)\n printf(""%4d\t%s", ++yylineno, yytext); %% int main(int arge, char *argv[]) { yyin = fopen(argv[1], "r''); yylex(); fclose(yyin); } Global C and Lex declaration In this section we can add C variable declaration. We will declare an integer variable here for our word-counting program that holds the number of words counted by the program. We'll also perform token declaration of Lex. + Declaration for the word-counting program Fo int wordcoun t=0; %} Chars [A-za ZANAN'] Number ([0- 9]) + Delim [*’\n\t] Whitespace {delim} + Words {chars } + 0% The double percent sign implies the end of this section and the beginning of the three sections in Lex programming. + Lex rules for matching patterns: Lets look at the lex rules for describing the token that we want to match.(well use c to define to do when a token is matched).continuing with word counting program,here are the rules for matching token. + Lex rules for word counting program: System Programming & OS Laboratory TE Computer Engineering (2019 Course) {words} {word count++; /* Increase the word count by one*/} {whitespace } {/*do Nothing*/} {Number} {/*one may want to add some processing here*/} %% * C code The third and final section of programming in lex cover c function declaration (and occasionally the main function) Note that this section has to include the yywrap() function. Lex has set the function and variable that are available to the user. One of them is yywrap.Typically, yywrap () is define as shown in the example below. C code section for word counting program void main() { Yylex();/*start the analysis*/ printf(“No of words:%d\n”,wordCount); } int yywrap() { returnl; } In the processing section we have the basic element of lex programming.which should help in the writing simple lexical analysis programs. * Putting it all together The lex file is Lex scanner. It is represented to lex program as $ lex <file name.lex> This produce the lex.yy.c file which can be compiled using a C compile. It can also be used with parser to produce executable or you can include the Lex library in the link step with the option A-11. + Here some of Lex’s flags: o -c Indicate C action and is the default. o -t causes the lex.yy.c program to be written instead to standard output. o -v Provide a two-line summary of statistic. o -n will not print out the —-v summary. System Programming & OS Laboratory TE Computer Engineering (2019 Course) Lex variable and Function Note: you should write algorithm & procedure as per program/concepts 10. Flowchart : Note: you should draw flowchart as per algorithm/procedure 11. Conclusion: Thus, I have studied lexical analyzer and implemented an application for lexical analyzer to count total number of words, chard and line etc References : https://en.wikipedia.org/wiki/Lex (software http://epaperpress.com/lexandyacec/prl.html hitps://www,ibm.com/developerworks/library/1 lexyac/index.html Oral Questions: [Write short answer ] 1. What is Lex. 2. What is Compiler and phases of compiler. 3. What is Lex specification. 4. What is the difference between Lex and YACC. 5. What is Regular Expression. 6. How to run a Lex program. 7. What is yytext, yyin, yyout. 8. What is yywrap(). 9. What is yylex(). 10. token, lexemes, pattern? Group - B Assignment No. 5 1. Title: Write a program using YACC specifications to implement syntax analysis phase of compiler to validate type and syntax of variable declaration in Java. 2. Objectives : - To understand LEX & YACC Concepts - To implement LEX Program & YACC program - To study about Lex & Yaac specification - To know important about Lexical analyzer and Syntax analysis 3. Problem Statement : Write a program using YACC specifications to implement syntax analysis phase of compiler to validate infix expression & arithmetic expression in Java. 4, Outcomes: - Understand the concept of LEX & YACC Tool - Understand the lexical analysis & Syntax analysis part - It can be used for data mining and checking(validation) concepts. 5. Software Requirements: FLEX, YACC (LEX & YACC) 6. Hardware Requirement: - MIC Lenovo Think center M700 Ci3,6100,6th Gen. H81, 4GB RAM ,500GB HDD 7. Theory Concepts: Yace (Yet Another Compiler-Compiler) is a computer program for the Unix operating system developed by Stephen C. Johnson. It is a Look Ahead Left-to-Right (LALR) parser generator, generating a parser, the part of a compiler that tries to make syntactic sense of the source code, specifically a LALR parser, based on an analytic grammar written in a notation similar to Backus— Naur Form (BNF). Yacc is supplied as a standard utility on BSD and AT&T Unix. GNU- based Linux distributions include Bison, a forward-compatible Yacc replacement. Yacc is one of the automatic tools for generating the parser program. Basically Yacc is a LALR parser generator. The Yacc can report conflicts or ambiguities (if at all) in the form of error messages. LEX and Yacc work together to analyse the program syntactically. 42 Yacc is officially known as a “parser”. Its job is to analyze the structure of the input stream, and operate of the “big picture”. In the course of its normal work, the parser also verifies that the input is syntactically sound. YACC stands for “Yet Another Compiler Compiler” which is a utility available from Unix. Structure of a yacc file: A yacc file looks much like a lex file: ..definitions.. %% rules... %% code... Definitions As with lex, all code between %{ and %} is copied to the beginning of the resulting C file. Rules As with lex, a number of combinations of pattern and action. The patterns are now those of a context-free grammar, rather than of a regular grammar as was the 3 case with lex code. This can be very Elaborate, but the main ingredient is the call to yyparse, the grammatical parse. Input to yacc is divided into three sections. The definitions section consists of token declarations and C code bracketed by “%{“ and “%}". The BNF grammar is placed in the rules section and user subroutines are added in the subroutines section. This is best illustrated by constructing a small calculator that can add and subtract numbers. We“ll begin by examining the linkage between lex and yacc. Here is the definitions section for the yacc input file: Grammars for yacc are described using a variant of Backus Naur Form (BNF). This technique, pioneered by John Backusand Peter Naur, was used to describe ALGOLO0. A BNF grammar can be used to express context-free languages. Most constructs in modern programming languages can be represented in BNF. For example, the grammar for an expression that multiplies and adds numbers is 1E-E+E 2E->E*E 3E->id Three productions have been specified. Terms that appear on the left-hand side (hs) of a production, such as E, are nonterminals. Terms such as id (identifier) are terminals (tokens returned by lex) and only appear on the right-hand side (rhs) of a production. This grammar specifies that an expression may be the sum of two expressions, the product of two expressions, or an identifier. We can use this grammar to generate expressions: E->E*E (r2) -> E *z (r3) ->E+E*z(rl) ->E+y*z(r3) 43 Lex includes this file and utilizes the definitions for token values. To obtain tokens yacc calls yylex. Function yylex has a return type of int that returns a token. Values associated with the token are returned by lex in variable yylval. For example, [0-9]+ { yylval = atoi(yytext); return INTEGER; } would store the value of the integer in yylval, and return token INTEGER to yacc. The type of yylval is determined by YYSTYPE. Since the default type is integer this works well in this case. Token values 0-255 are reserved for character values. For example, if you had a rule such as [-+] return *yytext; /* return operator */ the character value for minus or plus is returned. Note that we placed the minus sign first so that it wouldnt be mistaken for a range designator. Generated token values typically start around 258 because lex reserves several values for end-of-file and error processing. Here is the complete lex input specification for our calculator: Ft #include "y.tab.h" #include <stdlib.h> void yyerror(char *); %} %% Internally yacc maintains two stacks in memory; a parse stack and a value stack. The parse stack contains terminals and nonterminals that represent the current parsing state. The value stack is an array of YYSTYPE elements and associates a value with each element in the parse stack. For example when lex returns an INTEGER token yacc shifts this token to the parse stack. At the same time the corresponding yylval is shifted to the value stack. The parse and value stacks are always synchronized so finding a value related to a token on the stack is easily accomplished. Here is the yacc input specification for our calculator: 46 The rules section resembles the BNF grammar discussed earlier. The left-hand side of a production, or nonterminal, is entered left-justified and followed by a colon. This is followed by the right-hand side of the production. Actions associated with a rule are entered in braces. With left-recursion we have specified that a program consists of zero or more expressions. Each expression terminates with a newline. When a newline is detected we print the value of the expression. When we apply the rule expr: expr '+' expr { $$ = $1 + $3; } we replace the right-hand side of the production in the parse stack with the left-hand side of the same production. In this case we pop "expr '+' expr" and push "expr". We have reduced the stack by popping three terms off the stack and pushing back one term. We may reference positions in the value stack in our C code by specifying "$1" for the first term on the right hand side of the production, "$2" for the second, and so on. "$$" designates the top of the stack after reduction has taken place. The above action adds the value associated with two expressions, pops three terms off the value stack, and pushes back a single sum. As a consequence the parse and value stacks remain synchronized. Numeric values are initially entered on the stack when we reduce from INTEGER to expr. After INTEGER is shifted to the stack we apply the rule expr: INTEGER { $$ = $1; } The INTEGER token is popped off the parse stack followed by a push of expr. For the value stack we pop the integer value off the stack and then push it back on again. In other words we do nothing. In fact this is the default action and need not be specified. Finally, when a newline is encountered, the value associated with expr is printed. In the event of syntax errors yacc calls the user-supplied function yyerror. If you need to modify the interface toyyerror then alter the canned file that yacc includes to fit your needs. The last function in our yacc specification ismain ... in case you were wondering where it was. This example still has an ambiguous grammar. Although yacc will issue shift-reduce warnings it will still process the grammar using shift as the default operation. In this section we will extend the calculator from the previous section to incorporate some new functionality. New features include arithmetic operators multiply and divide. Parentheses may be used to over-ride operator precedence, and single-character variables may be specified in assignment statements. The following illustrates sample input and calculator output: 47 user: 3 * (4+ 5) cale: 27 user: x =3 * (4+5) user: y =5 user: X cale: 27 user: y cale: 5 user: x + 2¥y cale: 37 48 7. Design (architecture) : (yyparse) source bas.y ———»_ yacc -———= y.tab.c y- tab. cc /}——» bas.exe bas.| lex > lex.yy.c (yylex) ” compiled output 8. Algorithms(procedure) : Note: you should write algorithm & procedure as per program/concepts 9. Flowchart : Note: you should draw flowchart as per algorithm/procedure - Understand the concept of LEX & YACC Tool - Understand the lexical analysis & Syntax analysis part - Understand the Simple and Compound sentense. 5. Software Requirements: FLEX, YACC (LEX & YACC) 6. Hardware Requirement: - M/C Lenovo Think center M700 Ci3,6100,6th Gen. H81, 4GB RAM ,500GB HDD 7. Theory Concepts: Yace 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. The heart of the input specification is a collection of grammar rules. Each rule describes an allowable structure and gives it a name. For example, one grammar rule might be Department of Computer Engineering, MMIT, Lohgaon, Pune-47 51 System Programming & OS Laboratory TE Computer Engineering (2015 Course) date : month_name day ',' year ; Here, date, month_name, day, and year represent structures of interest in the input process; presumably, month_name, day, and year are defined elsewhere. The comma ~~," is enclosed in single quotes; this implies that the comma is to appear literally in the input. The colon and semicolon Merely serve as punctuation in the rule, and have no significance in controlling the input. Thus, with proper definitions, the input July 4, 1776 might be matched by the above rule. An important part of the input process is carried out by the lexical analyzer. This user routine reads the input stream, recognizing the lower level structures, and communicates these tokens to the parser. For historical reasons, a structure recognized by the lexical analyzer is called a terminal symbol, while the structure recognized by the parser is called a nonterminal symbol. To avoid confusion, terminal symbols will usually be referred to as tokens. There is considerable leeway in deciding whether to recognize structures using the lexical analyzer or grammar rules. For example, the rules month_name : 'J' 'a'''n' ; month_name : 'F' 'e''b'; month_name : 'D''e''c' ; might be used in the above example. The lexical analyzer would only need to recognize individual letters, and month_name would be a nonterminal symbol. Such low-level rules tend to waste time and space, and may complicate the specification beyond Yacc's ability to deal with it. Usually, the lexical analyzer would recognize the month names, and return an indication that a month name was seen; in this case, month name would be a token. Literal characters such as **," must also be passed through the lexical analyzer, and are also considered tokens. Specification files are very flexible. It is relatively easy to add to the above example the rule In most cases, this new rule could be “‘slipped in" to a working system with minimal effort, and little danger of disrupting existing input. Department of Computer Engineering, MMIT, Lohgaon, Pune-47 52 System Programming & OS Laboratory TE Computer Engineering (2015 Course) The input being read may not conform to the specifications. These input errors are detected as early as is theoretically possible with a left-to-right scan; thus, not only is the chance of reading and computing with bad input data substantially reduced, but the bad data can usually be quickly found. Error handling, provided as part of the input specifications, permits the reentry of bad data, or the continuation of the input process after skipping over the bad data. In some cases, Yacc fails to produce a parser when given a set of specifications. For example, the specifications may be self-contradictory, or they may require a more powerful recognition mechanism than that available to Yacc. The former cases represent design errors; the latter cases can often be corrected by making the lexical analyzer more powerful, or by rewriting some of the grammar rules. Basic Specifications: Names refer to either tokens or non-terminal symbols. Yacc requires token names to be declared as such. In addition, it is often desirable to include the lexical analyzer as part of the specification file; it may be useful to include other programs as well. Thus, every specification file consists of three sections: the declarations, (grammar) rules, and programs. The sections are separated by double percent “*%%" marks. (The percent **%" is generally used in Yacc specifications as an escape character.) In other words, a full specification file looks like declarations %% rules %% programs The declaration section may be empty. Moreover, if the programs section is omitted, the second %% mark may be omitted also; thus, the smallest legal Yacc specification is %% rules Department of Computer Engineering, MMIT, Lohgaon, Pune-47 53 System Programming & OS Laboratory TE Computer Engineering (2015 Course) Blanks, tabs, and newlines are ignored except that they may not appear in names or multi character reserved symbols. Comments may appear wherever a name is legal; they are enclosed in /* .. . */, as in C and PL/I. The rules section is made up of one or more grammar rules. A grammar rule has the form: A: BODY; A represents a non-terminal name, and BODY represents a sequence of zero or more names and Literals. The colon and the semicolon are Yacc punctuation. Names may be of arbitrary length, and may be made up of letters, dot **.", underscore “_", and non-initial digits. Upper and lower case letters are distinct. The names used in the body of a grammar rule may represent tokens or non-terminal symbols. A literal consists of a character enclosed in single quotes *“"'. As in C, the backslash *“\"is an escape character within literals, and all the C escapes are recognized. Thus For a number of technical reasons, the NUL character (‘\0' or 0) should never be used in grammar rules. In the examples above, all the actions came at the end of their rules. Sometimes, it is desirable to get control before a rule is fully parsed. Yacc permits an action to be written in the middle of a rule as well as at the end. This rule is assumed to return a value, accessible through the usual mechanism by the actions to the right of it. In turn, it may access the values returned by the symbols to its left. Thus, in the rule A:B { $$ =1;} { x = $2; y = $3; } the effect is to set x to 1, and y to the value returned by C. Actions that do not terminate a rule are actually handled by Yacc by manufacturing a new non-terminal symbol name, and a new rule matching this name to the empty string. The interior action is the action triggered off by recognizing this added rule. Yacc actually treats the above example as if it had been written: Department of Computer Engineering, MMIT, Lohgaon, Pune-47 56 System Programming & OS Laboratory TE Computer Engineering (2015 Course) SACT : /* empty */ {$$= 15} A:B$ACTC {x =$2; y = $3; } In many applications, output is not done directly by the actions; rather, a data structure, such as a parse tree, is constructed in memory, and transformations are applied to it before output is generated. Parse trees are particularly easy to construct, given routines to build and maintain the tree structure desired. For example, suppose there is a C function node, written so that the call node( L, nl, n2 ) creates a node with label L, and descendants n1 and n2, and returns the index of the newly created node. Then parse tree can be built by supplying actions such as: expr : expr '+' expr { $$ = node( '+', $1, $3 ); } in the specification. The user may define other variables to be used by the actions. Declarations and definitions can appear in the declarations section, enclosed in the marks “*%{" and ~*%}". These declarations and definitions have global scope, so they are known to the action statements and the lexical analyzer. For example, %{ int variable = 0; %} could be placed in the declarations section, making variable accessible to all of the actions. The Yacc parser uses only names beginning in “yy"; the user should avoid such names. Translating, Compiling and Executing A Yacc Program The Lex program file consists of Lex specification and should be named <file name>.1 and the Yacc program cons! of Yace sepecification and should be named <file name>.y. following command may be issued to generate the parser Lex <file name>.1 Yace —d <file name>.y cc lex.yy.c y.tab.c -Il Department of Computer Engineering, MMIT, Lohgaon, Pune-47 57 System Programming & OS Laboratory TE Computer Engineering (2015 Course) -/a.out Yacc reads the grammar description in <file name>.yand generates a parser, function yyparse, in file y.tab.c . the -d option causes yacc to generate the definitions for tokens that are declared in the <file name>.y and palce them in file y.tab.h. Lex reads the pattern descriptions in <file name>.l, includes file y.tab.h, and generates a lexical analyzer, function yylex, in the file lex.yy.c Finally, the lexer and the parser are compiled and linked (-I) together to form the output file, a.out(by default). The execution of the parser begins from the main function, which will be ultimately call yyparse() to run the parser. Function yyparse() automatically calls yylex() whenever it is in need of token . Lexical Analyzer for YACC The user must supply a lexical analyzer to read the input stream and communicate tokens (with values, if desired) to the parser. The lexical analyzer is an integer-valued function called yylex. The function returns an integer, the token number, representing the kind of token read. If there is a value associated with that token, it should be assigned to the external variable yylval. The parser and the lexical analyzer must agree on these token numbers in order for communication between them to take place. The numbers may be chosen by Yacc, or chosen by the user. In either case, the ~“# define" mechanism of C is used to allow the lexical analyzer to return these numbers symbolically. For example, suppose that the token name DIGIT has been defined in the declarations section of the Yacc specification file. The relevant portion of the lexical analyzer might look like: yylexQ{ extern int yylval; int c; ¢ = getchar(); switch(c ) { case case 'I': case '9': Department of Computer Engineering, MMIT, Lohgaon, Pune-47 58 System Programming & OS Laboratory TE Computer Engineering (2015 Course) yylval = c-'0'; return( DIGIT ); The intent is to return a token number of DIGIT, and a value equal to the numerical value of the digit. Provided that the lexical analyzer code is placed in the programs section of the specification file, the identifier DIGIT will be defined as the token number associated with the token DIGIT. This mechanism leads to clear, easily modified lexical analyzers; the only pitfall is the need to avoid using any token names in the grammar that are reserved or significant in C or the parser; for example, the use of token names if or while will almost certainly cause severe difficulties when the lexical analyzer is compiled. The token name error is reserved for error handling, and should not be used naively. As mentioned above, the token numbers may be chosen by Yacc or by the user. In the default situation, the numbers are chosen by Yacc. The default token number for a literal character is the numerical value of the character in the local character set. Other names are assigned token numbers starting at 257. When Yacc generates, the parser(by default y.tab.c, which is C file), it will assign token numbers for all the tokens defined in Yacc program.Token numbers will be assigned using” #define”and will be copied, by default, to y.tab.h file. The lexical analyzer will reasd from this file or any furthe use. Precedence Compound Sentences and Meaning When independent clauses are joined with coordinators (also called coordinating conjunctions) commas and semicolons, they do more than just join the clauses. They add meaning and flow to your writing. First let's look at the coordinators you can use to join independent clauses. They are: Note that they form the handy mnemonic FANBOYS. The three you will use most often are ‘and,' 'but' and 'or.' Here's an example of how coordinating conjunctions add meaning: Department of Computer Engineering, MMIT, Lohgaon, Pune-47 61 System Programming & OS Laboratory TE Computer Engineering (2015 Course) ‘I think you'd enjoy the party, but I don't mind if you stay home.' In this sentence, the coordinator 'but' shows a clear relationship between the two independent clauses, in this case, that the speaker is making a suggestion that the person being addressed isn't expected to follow it. Without the coordinator ‘but,’ the relationship isn't apparent, making the writing choppy and the meaning less clear: ‘L think you'd enjoy the party. I don't mind if you stay home.' You can also join independent clauses with a semicolon (;) , which looks something like a cross between a colon and a comma. If you join clauses with a semicolon, you add an abrupt pause, creating a different kind of effect, as shown in the sentence below: 'He said he didn't mind if I stayed home; it soon became clear he wasn't being honest.’ You should use a semicolon when the independent clauses are related, but contrast in a way that you want to stand out. In the sentence above, the contrast is that the person being talked about in the first clause sounded honest when he said he didn't mind if the speaker stayed home, but in the second clause, the speaker is telling you that the person being talked about was not honest. You could just as easily have written the sentence using a coordinating conjunction: 'He said he didn't mind if I stayed home, but it soon became clear he wasn't being honest.' The sentence still means the same as before, but using the coordinator 'but' softens the impact of the second clause. Comparing Sentence Types Sentences give structure to language, and in English, they come in four types: simple, compound, complex and compound-complex. When you use several types together, your writing is more interesting. Combining sentences effectively takes practice, but you'll be happy with the result. 1. The simple sentence is an independent clause with one subject and one verb. For example: we are the indian. 2. The Compound sentence is two or more independent clause, joined with comma, semicolon & conjuction. For example: I am student and indian Application: Department of Computer Engineering, MMIT, Lohgaon, Pune-47 62 System Programming & OS Laboratory TE Computer Engineering (2015 Course) - YACC is used to generate parsers, which are an integral part of compiler. Algorithms(procedure) : Note: you should write algorithm & procedure as per program/concepts Flowchart : Note: you should draw flowchart as per algorithm/procedure Conclusion: Thus, I have studied lexical analyzer, syntax analysis and implemented Lex & Yacc application for Syntax analyzer to validate the given infix expression. References : https://en.wikipedia.org/wiki/Lex_(software http://e) ress. lexand. rl.html https://www.ibm.com/developerworks/library/l-lexyac/index.htm 1 http://epaperpress.com/lexandyacc/pry2.html Oral Questions: [Write short answer ] 1. What is Lex & Yace . 2. What is Compiler and phases of compiler. 3. What is Lex & Yacc specification. 4. What is the difference between Lex and YACC. 5. What is Regular Expression & grammer. 6. How to run a Lex & Yacc program. 7. What is yytext, yyin, yyout. 8. What is yywrap(). 9. What is yylex(). 10. What is yyparse(). 11. Define token, lexemes, pattern & symbol error? 12. What is left, right & no associativity. 13. What is use of $$? 14. What is yylval. Department of Computer Engineering, MMIT, Lohgaon, Pune-47 63 System Programming & OS Laboratory TE Computer Engineering (2015 Course) Group - C Assignment No. 1 1. Title: Write a Java program (using OOP features) to implement following scheduling algorithms: FCFS, SJF (Preemptive), Priority (Non-Preemptive) and Round Robin (Preemptive). 2. Objectives: - To understand OS & SCHEDULLING Concepts - To implement Scheduling FCFS, SJF, RR & Priority algorithms - To study about Scheduling and scheduler 3. Problem Statement: Write a Java program (using OOP features) to implement following scheduling algorithms: FCFS, SJF, Priority and Round Robin. 4, Outcomes: - Knowledge Scheduling policies - Compare different scheduling algorithms 5. Software Requirements: JDK/Eclipse 6. Hardware Requirement: - MIC Lenovo Think center M700 Ci3,6100,6th Gen. H81, 4GB RAM ,500GB HDD 7. Theory Concepts: CPU Scheduling: * CPU scheduling refers to a set of policies and mechanisms built into the operating systems that govern the order in which the work to be done by a computer system is completed. Short Term Scheduler ¢ The short term scheduler allocates the processor among the pool of ready processes resident in the memory. ¢ Its main objective is to maximize system performance in accordance with the chosen set of criteria. * Some of the events introduced thus for that cause rescheduling by virtue of their ability to change the global system state are: * Clock ticks ¢ Interrupt and I/O completions * Most operational OS calls Sending and receiving of signals + Activation of interactive programs. ¢ Whenever one of these events occurs ,the OS involves the short term scheduler. Department of Computer Engineering, DYPSOEA, AMBI , Pune 66 System Programming & OS Laboratory TE Computer Engineering (2019 Course) Scheduling Criteria : CPU Utilization: Keep the CPU as busy as possible. It range from 0 to 100%. In practice, it range from 40 to 90%. Throughput: Throughput is the rate at which processes are completed per unit of time. Turnaround time: This is the how long a process takes to execute a process. It is calculated as the time gap between the submission of a process and its completion. Waiting time: Waiting time is the sum of the time periods spent in waiting in the ready queue. Response time: Response time is the time it takes to start responding from submission time. It is calculated as the amount of time it takes from when a request was submitted until the first response is produced. Non-preemptive Scheduling : In non-preemptive mode, once if a process enters into running state, it continues to execute until it terminates or blocks itself to wait for Input/Output or by requesting some operating system service. Preemptive Scheduling : In preemptive mode, currently running process may be interrupted and moved to the ready State by the operating system. When a new process arrives or when an interrupt occurs, preemptive policies may incur greater overhead than non-preemptive version but preemptive version may provide better service. It is desirable to maximize CPU utilization and throughput, and to minimize turnaround time, waiting time and response time. Types of scheduling Algorithms ¢ In general, scheduling disciplines may be pre-emptive or non-pre-emptive . ¢ In batch, non-pre-emptive implies that once scheduled, a selected job turns to completion. There are different types of scheduling algorithms such as: Department of Computer Engineering, DYPSOEA, AMBI , Pune 67 System Programming & OS Laboratory TE Computer Engineering (2019 Course) + FCFS (First Come First Serve) + SJF(Short Job First) + Priority scheduling * Round Robin Scheduling algorithm First Come First Serve Algorithm * FCFS is working on the simplest scheduling discipline. ¢ The workload is simply processed in an order of their arrival, with no pre-emption. ¢ FCFS scheduling may result into poor performance. Since there is no discrimination on the basis of required services, short jobs may considerable in ¢ turn around delay and waiting time. Advantages Better for long processes Simple method (i.e., minimum overhead on processor) No starvation Disadvantages Convoy effect occurs. Even very small process should wait for its turn to come to utilize the CPU. Short process behind long process results in lower CPU utilization. Throughput is not emphasized. Department of Computer Engineering, DYPSOEA, AMBI , Pune 68 System Programming & OS Laboratory TE Computer Engineering (2019 Course) Note : solve complete e.g. as we studied in practical(above is just sample e.g.). you can take any e.g. Shortest Job First Algorithm : his is also known as shortest job first, or SJF This is a non-preemptive, pre-emptive scheduling algorithm. Best approach to minimize waiting time. Easy to implement in Batch systems where required CPU time is known in advance. Impossible to implement in interactive systems where required CPU time is not known. The processer should know in advance how much time process will take. Advantages It gives superior turnaround time performance to shortest process next because a short job is given immediate preference to a running longer job. Throughput is high. Disadvantages Elapsed time (i.e., execution-completed-time) must be recorded, it results an additional overhead on the processor. Starvation may be possible for the longer processes. Round Robin Scheduling : Round Robin is the preemptive process scheduling algorithm. Each process is provided a fix time to execute, it is called a quantum. Once a process is executed for a given time period, it is preempted and other process executes for a given time period. Context switching is used to save states of preempted processe Advantages Round-robin is effective in a general-purpose, times-sharing system or transaction processing system. Fair treatment for all the processes. Overhead on processor is low. Overhead on processor is low. Good response time for short processes. Disadvantages Care must be taken in choosing quantum value. Processing overhead is there in handling clock interrupt. Throughput is low if time quantum is too small. Department of Computer Engineering, DYPSOEA, AMBI,, Pune 71 System Programming & OS Laboratory TE Computer Engineering (2019 Course) Note : solve complete e.g. as we studied in practical(above is just sample e.g.). you can take any e.g. Priority Scheduling : Priority scheduling is a non-preemptive algorithm and one of the most common scheduling algorithms in batch systems. Each process is assigned a priority. Process with highest priority is to be executed first and so on. Processes with same priority are executed on first come first served basis. Priority can be decided based on memory requirements, time requirements or any other resource requirement. Advantage Good response for the highest priority processes. Disadvantage Starvation may be possible for the lowest priority processes. Note : solve complete e.g. as we studied in practical(above is just sample e.g.). you can take any e.g. Department of Computer Engineering, DYPSOEA, AMBI , Pune 72 System Programming & OS Laboratory TE Computer Engineering (2019 Course) 8. Algorithms(proce dure) : FCFS: Step 1: Start the process Step 2: Accept the number of processes in the ready Queue Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time Step 4: Set the waiting of the first process as ,,0“ and its burst time as its turn around time Step 5: for each process in the Ready Q calculate (a) Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-1) (b) Turn around time for Process(n)= waiting time of Process(n)+ Burst time for process(n) Step 6: Calculate (a) Average waiting time = Total waiting Time / Number of process (b) Average Turnaround time = Total Turnaround Time / Number of process Step 7: Stop the process SJF: Step 1: Start the process Step 2: Accept the number of processes in the ready Queue Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time Step 4: Start the Ready Q according the shortest Burst time by sorting according to lowest to highest burst time. Step 5: Set the waiting time of the first process as ,,0“ and its turnaround time as its burst time. Step 6: For each process in the ready queue, calculate (c) Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-1) (d) Turn around time for Process(n)= waiting time of Process(n)+ Burst time for process(n) Step 6: Calculate (c) Average waiting time = Total waiting Time / Number of process (d) Average Turnaround time = Total Turnaround Time / Number of process Step 7: Stop the process Department of Computer Engineering, DYPSOEA, AMBI , Pune 73 System Programming & OS Laboratory TE Computer Engineering (2019 Course) RR: Step 1: Start the process Step 2: Accept the number of processes in the ready Queue and time quantum (or) time slice Step 3: For each process in the ready Q, assign the process id and accept the CPU burst - To understand safe and unsafe state of a system - To understand deadlock - Implementation of banker“s algorithm for deadlock detection and avoidance 3. Problem Statement : Write a Java program to implement Banker‘s Algorithm 4, Outcomes: - Knowledge Bankers Algorithms - Application of Bankers Algorithms 5. Software Requirem ents: JDK/Eclip se 6. Hardware Requirement: - MIC Lenovo Think center M700 Ci3,6100,6th Gen. H81, 4GB RAM ,500GB HDD 7. Theory Concepts: The Banker's algorithm, sometimes referred to as the detection algorithm, is a resource allocation and deadlock avoidance algorithm developed by Edsger Dijkstra that tests for safety by simulating the allocation of predetermined maximum possible amounts of all resources, and then makes an "s-state" check to test for possible deadlock conditions for all other pending activities, before deciding whether allocation should be allowed to continue. Department of Computer Engineering, DYPSOEA, AMBI , Pune 76 System Programming & OS Laboratory TE Computer Engineering (2019 Course) Banker's algorithm is a deadlock avoidance algorithm. It is named so because this algorithm is used in banking systems to determine whether a loan can be granted or not. Consider there are n account holders in a bank and the sum of the money in all of their accounts is S. Everytime a loan has to be granted by the bank, it subtracts the loan amount from the total money the bank has. Then it checks if that difference is greater than S. It is done because, only then, the bank would have enough money even if all the n account holders draw all their money at once Banker's algorithm works in a similar way in computers. Whenever a new process is created, it must exactly specify the maximum instances of each resource type that it needs. Let us assume that there are n processes and m resource types. Some data structures are used to implement the banker's algorithm. They are: + Available: It is an array of length m. It represents the number of available resources of each type. If Available[j] = k, then there are k instances available, of resource type Rj. * Max: It is an nx m matrix which represents the maximum number of instances of each resource that a process can request. If Max{i][j] = k, then the process Pi can request atmost k instances of resource type Rj. + Allocation: It is an n x m matrix which represents the number of resources of each type currently allocated to each process. If Allocation[i][j] = k, then process Pi is currently allocated k instances of resource type Rj. + Need: It is an n x m matrix which indicates the remaining resource needs of each process. If Need{i][{j] =k, then process Pi may need k more instances of resource type Rj to complete its task. Resource Request Algorithm: This describes the behavior of the system when a process makes a resource request in the form of a request matrix. The steps are: 1. If number of requested instances of each resource is less than the need (which was declared previously by the process), go to step 2. Department of Computer Engineering, DYPSOEA, AMBI , Pune 77 System Programming & OS Laboratory TE Computer Engineering (2019 Course) 2. If number of requested instances of each resource type is less than the available resources of each type, go to step 3. If not, the process has to wait because sufficient resources are not available yet. 3. Now, assume that the resources have been allocated. Accordingly do, This step is done because the system needs to assume that resources have been allocated. So there will be less resources available after allocation. The number of allocated instances will increase. ThO065 need of the resources by the process will reduce. That's what is represented by the above three operations. After completing the above three steps, check if the system is in safe state by applying the safety algorithm. If it is in safe state, proceed to allocate the requested resources. Else, the process has to wait longer. Safety Algorithm: 1. Let Work and Finish be vectors of length m and n, respectively. Initially, This means, initially, no process has finished and the number of available resources is represented by the Available array. 4. Find an index i such that both 5. Finish[i] ==false 6. Needi <= Work If there is no such i present, then proceed to step 4. It means, we need to find an unfinished process whose need can be satisfied by the available resources. If no such process exists, just go to step 4. Department of Computer Engineering, DYPSOEA, AMBI , Pune 78 System Programming & OS Laboratory TE Computer Engineering (2019 Course) 7. Perform the following: 8. Work = Work + Allocation; 9. Finish[i] = true; 10. Go to step 2.
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved