Você está na página 1de 6

Ques1 Give the classification of grammars.

Ans Classification of Grammars


Grammars are classified on the basis of the nature of productions used in them (Chomsky, 1963). Each grammar class has its own characteristics and limitations. Type 0 Grammars These grammars, known as phrase structure grammars, contain productions of the form ::= where both and can be strings of Ts and NTs. Such productions permit arbitrary substitution of strings during derivation or reduction, hence they are not relevant to specification of programming languages Type 1 Grammars These grammars are known as context sensitive grammars because their productions specify that derivation or reduction of strings can take place only in specific contexts. A Type-1 production has the form A::= Type 2 Grammars These grammars impose no context requirements on derivations or reductions. A typical Type-2 production is of the form A:: = which can be applied independent of its context. These grammars are therefore known as context free grammars (CFG). Type 3 Grammars Type-3 grammars are characterized by productions of the form A::= tB | t or A ::= Bt | t Type-3 grammars are also known as linear grammars or regular grammars. Operator Grammars: Definition (Operator grammar (OG)) :An operator grammar is a grammar none of whose productions contain two or more consecutive NTs in any RHS alternative

Ques2 Explain language processing activities with suitable diagrams Ans2 Language Processing Activities
The fundamental language processing activities can be divided into those that bridge the specification gap and those that bridge the execution gap. These activities are : 1.Program Generation Activities 2.Program Execution Activities 3. Program Generation The program generator is a software system which accepts the specification of a program to be generated, and generates a program in the target PL. Figure depicts the program generation activity

Program Execution Two popular models for program execution are Translation Interpretation Program Translation Program translation model bridges the execution gap by translating a program written in a PL, called the source program (SP), into an equivalent program in the machine or assembly language of the computer system, called the target program (TP). Characteristics of the program translation model are A program must be translated before it can be executed. The translated program may be saved in a file. The saved program may be executed repeatedly. A program must be retranslated following modifications. Program Interpretation The interpreter reads the source program and stores it in its memory. During interpretation it takes a source statement, determines its meaning and performs actions which implement it. This includes computational and input-output actions. The interpretation cycle, consists of the following steps: 1. Fetch the statement. 2. Analyze the statement and determine its meaning, viz. The computation to be performed and its operands. 3.Execute the meaning of the statement. Characteristics of interpretation 1.The source program is retained in the source form itself, i.e. no target program form exists. 2.A statement is analyzed during its interpretation.

Ques3 Write pass1 and pass2 data structures in detail. Ans3 Data Structures
The second step in our design procedure is to establish the databases that we have to work with. Pass 1 Data Structures Input source program

A Location Counter (LC), used to keep track of each instructions location.A table, the Machine-operation Table (MOT), that indicates the symbolic mnemonic, for each instruction and its length (two, four, or six bytes) A table, the Pseudo-Operation Table (POT), that indicates the symbolic mnemonic and action to be taken for each pseudo-op in pass 1. A table, the Symbol Table (ST), that is used to store each label and its corresponding value. A table, the literal table (LT), that is used to store each literal encountered and its corresponding assignment location. A copy of the input to be used by pass 2. Pass 2 Data Structures Copy of source program input to pass1. Location Counter (LC) A table, the Machine-operation Table (MOT), that indicates for each instruction, symbolic mnemonic, length (two, four, or six bytes), binary machine opcode and format of instruction. A table, the Pseudo-Operation Table (POT), that indicates the symbolic mnemonic and action to be taken for each pseudo-op in pass 2. A table, the Symbol Table (ST), prepared by pass1, containing each label and corresponding value. A Table, the base table (BT), that indicates which registers are currently specified as base registers by USING pseudo-ops and what are the specified contents of these registers. A work space INST, that is used to hold each instruction as its various parts are being assembled together. A work space, PRINT LINE, used to produce a printed listing. A work space, PUNCH CARD, used prior to actual outputting for converting the assembled instructions into the format needed by the loader. An output deck of assembled instructions in the format needed by the loader. Format of Data Structures The third step in our design procedure is to specify the format and content of each of the data structures. Pass 2 requires a machine operation table (MOT) containing the name, length, binary code and format; pass 1 requires only name and length. Instead of using two different table, we construct single (MOT). The Machine operation table (MOT) and pseudo-operation table are example of fixed tables.The contents of these tables are not filled in or altered during the assembly process.The following Table shows the format of the machine-op table (MOT)

6 bytes per entry b represents blank

Mnemonic Opcode (4 bytes) characters Abbb Ahbb ALbb ALRB .

Binary opcode (hexadecimal) 5A 4A 5E 1E .

(1 byte)Instruction Instruction Not used length format here (2 bits) (binary) (3 bits) (binary) (3 bits) 10 10 10 01 . 001 001 001 000 .

Ques4 Explain the phases of compiler in detail Ans4 Phases of Compiler


A compiler takes as input a source program and produces as output an equivalent sequence of machine instructions. This process is complex it is not reasonable, either from a logical point of view or from an implementation point of view, to consider the compilation process as occurring in one single step Hence it is customary to partition the compilation process into a series of sub processes called phases.A phase is a logically cohesive operation that takes as input one representation of the source program and produces as output another representation.

Lexical Analysis
The lexical analyzer is the interface between the source program and the compiler. The lexical analyzer reads the source program one character at a time, carving the source program into a sequence of atomic units called tokens. Each token represents a sequence of characters that can be treated as a single logical entity. Identifiers, keywords, constants, operators, and punctuation symbols such as commas and parentheses are typical tokens. There are two kinds of token: specific strings such as IF or a semicolon, and classes of strings such as identifiers, constants, or labels. The lexical analyzer and the following phase, the syntax analyzer are often grouped together into the same pass. Lexical Analysis In that pass, the lexical analyzer operates either under the control of the parser or a coroutine with the parser.The parser asks the lexical analyzer returns to the parser a code for the token that it found. In the case that the token is an identifier or another token with a value, the value is also passed to the parser. The usual method of providing this information is for the lexical analyzer to call a bookkeeping routine which installs the actual value in the symbol table if it is not already there. The lexical analyzer then passes the two components of the token to the parser. The first is a code for the token type (identifier), and the second is the value, a pointer to the place in the symbol table reserved for the specific value found

Ques5 What are macros and macro processors ? Ans Macro Expansion
A macro call leads to macro expansion. During macro expansion, the macro statement is replaced by sequence of assembly statements .

A Macro is a unit of specification for program generation through expansion. A macro name is an abbreviation, which stands for some related lines of code. Macros are useful for the following purposes: To simplify and reduce the amount of repetitive coding To reduce errors caused by repetitive coding To make an assembly program more readable. A macro consists of name, set of formal parameters and body of code. The use of macro name with set of actual parameters is replaced by some code generated by its body. This is macro expansion Macros allow a programmer to define pseudo operations, typically operations that are generally desirable, are not implemented as part of the processor instruction, and can be implemented as a sequence of instructions. Macros can be used in many programming languages, like C, C++ etc. Example: Macro in C programming. Macros are commonly used in C to define small snippets of code. If the macro has parameters, they are substituted into the macro body during expansion; thus, a C macro can mimic a C function. Example: #define max (a, b) a>b? A: b Defines the macro max, taking two arguments a and b. This macro may be called like any C function, using identical syntax. Therefore, after preprocessing z = max(x, y); Becomes z = x>y? X:y; C macros are capable of mimicking functions, creating new syntax within some limitations, as well as expanding into arbitrary text (although the C compiler will require that text to be valid C source code, or else comments), but they have some limitations as a programming construct

Ques 6.Explain macro parameters sequence Ans Parameters in Macros:


Syntax: <macro name> MACRO <parameter 1>.<parameter n> <body line 1> <body line 2>

. <body line m> ENDM Valid macro arguments are: Arbitrary sequences of printable characters, not containing blanks, tabs, commas, or semicolons Quoted strings (in single or double quotes) Single printable characters, preceded by '!' as an escape character Character sequences, enclosed in literal brackets < ... >, which may be arbitrary sequences of valid macro blanks, commas and semicolons Arbitrary sequences of valid macro arguments Expressions preceded by a '%' character Example 3 MY_SECOND MACRO CONSTANT, REGISTER MOV A,#CONSTANT ADD A,REGISTER ENDM MY_SECOND 42, R5 After calling the macro MY_SECOND, the body lines MOV A,#42 ADD A,R5 During macro expansion, these actual arguments replace the symbols of the corresponding formal parameters, wherever they are recognized in the macro body. The first argument replaces the symbol of the first parameter, the second argument replaces the symbol of the second parameter, and so forth. This is called substitution .
Macro Parameters Instruction Needed Real Operations Mov Dx,0 Mov Bx, NDiv Bx Macro Definition .Div Macro N Mov Dx, 0 Mov Bx, &N Div Bx Endm Use in Program Macro Expansion GetDec$ Dx, 0 Bx,34 Bx PutDec$

Div N

Call Call Mov GetDec$ Mov .Div 34 Div Call PutDec$ Call

Você também pode gostar