UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a...
-
Upload
florence-patrick -
Category
Documents
-
view
222 -
download
0
Transcript of UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a...
![Page 1: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/1.jpg)
UNIT-III
By
Mr. M. V. Nikum (B.E.I.T)
![Page 2: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/2.jpg)
Programming Language
• Lexical and Syntactic features of a programming Language are specified by its grammar
• Language:- collection of valid sentences.• Sentences: These are the sequence of words.• Word:- Sequence of letters of graphic symbols.• The Language specified in this manner is
called as formal Language.
![Page 3: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/3.jpg)
Programming Language Grammar
• A formal Language is a set of rules which precisely specify the sentence of L.
• Formal Grammar can be represented as :-• G= {∑ , NT, S, P}– ∑ set of terminals– NT set of non terminals– S Distinguished symbol (Starting symbol)– P set of productions.
![Page 4: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/4.jpg)
• Terminal Symbol:- These are the symbols which can not further sub divided– Ex:- ∑= { a, b,c,d…….0,1,2,3……}
• No terminal symbols:- These are the combination of terminal symbols which can be further sub divided.– Ex:- <A> or <Noun>
![Page 5: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/5.jpg)
• Production :- It is called as re-writing rule• It can be represented as :-– Each production consists of a No terminal
followed by an arrow (-) or equal to (=), followed by string of Non terminals and terminals
– Ex:- A b S Aa
![Page 6: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/6.jpg)
Syntax Tree
• A Graphical representation of any statement of a Language is called as syntax tree.
![Page 7: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/7.jpg)
id
id+id*id
![Page 8: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/8.jpg)
Derivation
• The replacement of Non terminal symbols according to the given production rule is called as derivation
• Types of Derivation:– Leftmost derivation– Rightmost derivation
![Page 9: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/9.jpg)
Rules for English Language
1) <Sentence> <NP><VP>2) <NP> <article><noun>3) <VP> <verb>4) <VP> <verb><adverb>5) <article> The6) <noun> student7) <verb> studies8) <adverb> hard9) <adverb> slowly
![Page 10: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/10.jpg)
Derivation
• Structure Rules applied• <Sentence>• <NP><VP> (1)• <article><noun>< VP> (2)• <article><noun> <verb><adverb> (4)• The <noun> <verb><adverb> (5)• The student <verb> <adverb> (6)• The student studies <adverb> (7)• The student studies hard (8)
![Page 11: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/11.jpg)
• Ex. Consider the full grammar, • E → E+E• E → E*E• E → id• Let us derive the string “ id + id * id”
![Page 12: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/12.jpg)
Using leftmost derivation:-
![Page 13: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/13.jpg)
Using Right most derivation:-
![Page 14: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/14.jpg)
Rules
![Page 15: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/15.jpg)
Required string is bbaa
![Page 16: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/16.jpg)
• Derive the string
–babbaaaba
![Page 17: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/17.jpg)
![Page 18: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/18.jpg)
Required string is
baab
![Page 19: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/19.jpg)
![Page 20: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/20.jpg)
Reduction
• It is the process of replacement of string or part of string by non terminal according to the production rule.
![Page 21: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/21.jpg)
• Structure Rules applied• The student studies hard• <article> student studies hard (5)• <article><noun> studies hard (6)• <article><noun> <verb> hard (7)• <article><noun> <verb> <adverb> (8)• <NP> <verb> <adverb> (2)• <NP><VP> (4)• <Sentence> (1)
![Page 22: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/22.jpg)
Ambiguity of Grammar
• The Grammar for a language is said to be ambiguous if there exists at least one string which can be generated (or, derived) in more than one way
• i. e. there can be more than one leftmost derivations, more than one rightmost derivations & also more than one derivation trees associated with such a string.
![Page 23: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/23.jpg)
• Ex. Consider the full grammar, • E → E+E• E → E*E• E → id• Let us derive the string “ id + id * id”
![Page 24: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/24.jpg)
![Page 25: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/25.jpg)
![Page 26: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/26.jpg)
id id
![Page 27: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/27.jpg)
Compiler
![Page 28: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/28.jpg)
• Complier:-• These are the system programs which will
automatically translate the High level language program in to the machine language program
Compiler
Database
Source programHigh level Lang. Prog.
Target program /M/C Lang. Prog.
![Page 29: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/29.jpg)
• Cross Compiler:-• These are the system programs which will automatically
translate the HLL program compatible with M/C A, in to the machine language program compatible with M/C A , but the underlying M/C is M/C B
Cross CompilerSource programHLL Prog. Compatible with M/C A
Target program /M/C Lang. Prog.
M/C B
![Page 30: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/30.jpg)
P Code CompilerSource
Program
Compiler
Obj Program
Exexute
P-Code interpreter
P-Code interpreter
![Page 31: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/31.jpg)
• Very similar in concept of Interpreter• In pseudo- code complier program is analyzed
and converted into an intermediate form, which is then executed interpretively.
• The source program is compiled, the resulted object program is in p-code. This p-code program is then read and executed under control of p-code interpreter.
![Page 32: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/32.jpg)
Compilation process overview
• Compilation process is vast and complex.• Hence it is divided into a series of subtasks
called as phases.• Each of which transform the source program
form one representation to another.• The compilation process involves two major
tasks.
![Page 33: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/33.jpg)
Compilation process overview
Analysis of + Synthesis of =Translation
source text Target text of prog in obj form
![Page 34: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/34.jpg)
• Analysis of source text– Lexical Analysis (Scanner)– Syntax Analysis (Parser)– Semantic analysis.
• Synthesis of target text– Storage allocation– Code generation
![Page 35: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/35.jpg)
Analysis of source text
• Determine validity of source statement from the analysis view point.
• Determine the content of source statement.• Construct the suitable representation of the
source statement f0r use by the subsequent analysis phase or by the synthesis phase of compiler.
![Page 36: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/36.jpg)
Synthesis of target text
• Memory allocation:- It is the simple task given to the presence of symbol table. The memory requirement of an identifier is computed from its length, type and dimension and memory is allocated to it.
• Code generation:- the code generation uses the knowledge of target architecture , knowledge of instruction and also the addressing modes in the target computer to select the appropriate instruction.
![Page 37: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/37.jpg)
Synthesis of target text
• The important issues in code generation are – Determine the space where the intermediate
results should ne kept i.e. whether they should be kept in memory location or held in machine register.
– Determine which instruction should be used for type conversion operation.
– Determine which addressing mode should be used.
![Page 38: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/38.jpg)
Phase Structure of Compiler
![Page 39: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/39.jpg)
![Page 40: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/40.jpg)
![Page 41: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/41.jpg)
Code Optimization
• This is an optional phase to improve the intermediate code so that ultimate object program can run faster and also take less phase.
• The ultimate aim of this technique is to improve the execution efficiency of a program by– Eliminating redundancies – Rearranging the computation in the program with
out affecting target machine.
![Page 42: UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-](https://reader031.fdocuments.us/reader031/viewer/2022032200/56649ce65503460f949b42a9/html5/thumbnails/42.jpg)
Code optimization techniques
1. Compile time evaluation2. Elimination of common sub expressions3. Frequency reduction4. Strength reduction5. Dead code elimination6. Boolean sub expressions optimization7. Local and Global optimization.