0% found this document useful (0 votes)
80 views

Language Processors

This document discusses language processors and their role in bridging semantic gaps between application domains and execution domains. It describes how language processors perform analysis on source programs through lexical analysis, syntax analysis, and semantic analysis. These analysis phases identify tokens, parse syntax trees, and determine program meaning. The document also discusses the synthesis phase where language processors generate target programs through memory allocation and code generation to bridge execution gaps.

Uploaded by

jaydip
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Language Processors

This document discusses language processors and their role in bridging semantic gaps between application domains and execution domains. It describes how language processors perform analysis on source programs through lexical analysis, syntax analysis, and semantic analysis. These analysis phases identify tokens, parse syntax trees, and determine program meaning. The document also discusses the synthesis phase where language processors generate target programs through memory allocation and code generation to bridge execution gaps.

Uploaded by

jaydip
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

LANGUAGE PROCESSORS

Presented By: Prof. Prakash Patel, IT Dept. GIT


Presented By: By:
Presented Prof. S.J.Prakash
Prof. Soni, SPCE
Patel, –ITVisnagar.
Dept. GIT
Introduction
 Language Processing activities arise due to the
differences between the manner in which a software
designer describes the ideas concerning the behavior
of a software and the manner in which these ideas
are implemented in a computer system.
 The designer expresses the ideas in terms related to
the application domain of the software. To implement
these ideas, their description has to be interpreted
in terms related to the execution domain.
Semantic Gap

Semantic Gap

Application Execution
Domain Domain

 Semantic Gap has many consequences


 Large development time
 Large development effort
 Poor quality of software
Specification and Execution Gaps

Specification Gap Execution Gap

Application PL Execution
Domain Domain Domain

 The software engineering steps aimed at the use of a


PL can be grouped into
 Specification, design and coding steps
 PL implementation steps
Specification and Execution Gaps
 Specification Gap
 Itis the semantic gap between two specifications of the
same task.
 Execution Gap
 Itis the gap between the semantics of programs (that
perform the same task) written in different
programming languages.
Language Processors
 “A language processor is a software which bridges a
specification or execution gap”.
 The program form input to a language processor as
the source program and to its output as the target
program.
 The languages in which these programs are written
are called source language and target language,
respectively.
Types of Language Processors
 A language translator bridges an execution gap to
the machine language (or assembly language) of a
computer system. E.g. Assembler, Compiler.
 A detranslator bridges the same execution gap as
the language translator, but in the reverse direction.
 A preprocessor is a language processor which
bridges an execution gap but is not a language
translator.
 A language migrator bridges the specification gap
between two PLs.
Language Processors - Examples
Errors

C++ Program
C++ C Program
preprocessor

Errors

C++ Program
C++ Machine Language
translator Program
Interpreters
 An interpreter is a language processor which bridges an
execution gap without generating a machine language program.
 An interpreter is a language translator according to classification.

Interpreter Domain

Application PL Execution
Domain Domain Domain
Language Processing Activities
 Program Generation Activities
 Program Execution Activities
Program Generation
Errors

Program Program Generator Program in


Specification target PL

Specification Gap

Application Program Target PL Execution


Domain Generator Domain Domain
Domain
Program Execution
 Two popular models for program execution are
translation and interpretation.
 Program translation
Errors Data

m/c
Source Translator language Target
Program Program
program
 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 Execution
 Program interpretation

Interpreter Memory CPU Memory

PC PC Machine
Source
Language
Program
Program
+
Errors +
Data
Data

Interpretation Program execution


Fundamentals of Language Processing

Language Processing = Analysis of SP + Synthesis of TP


Collection of LP components engaged in analysis a source
program as the analysis phase and components engaged in
synthesizing a target program constitute the synthesis phase.
Analysis Phase
 The specification consists of three components:
 Lexical rules which govern the formation of valid lexical units in the source language.
 Syntax rules which govern the formation of valid statements in the source language.
 Semantic rules which associate meaning with valid statements of the language.

 Consider the following example:


percent_profit = (profit * 100) / cost_price;
Lexical units identifies =, * and / operators, 100 as constant, and the remaining strings
as identifiers.
Syntax analysis identifies the statement as an assignment statement with percent_profit
as the left hand side and (profit * 100) / cost_price as the expression on the right
hand side.
Semantic analysis determines the meaning of the statement to be the assignment of
profit X 100 / cost_price to percent_profit.
Synthesis Phase
 The synthesis phase is concerned with the construction of target
language statements which have the same meaning as a source
statement.
 It performs two main activities:
 Creation of data structures in the target program (memory allocation)
 Generation of target code (code generation)
 Example
MOVER AREG, PROFIT
MULT AREG, 100
DIV AREG, COST_PRICE
MOVEM AREG, PERCENT_PROFIT

PERCENT_PROFIT DW 1
PROFIT DW 1
COST_PRICE DW 1
Phases and Passes of LP

Language Processor

Source
Analysis Synthesis Target
Program Phase Phase Program

Errors Errors

 Analysis of source statements can not be immediately followed


by synthesis of equivalent target statements due to following
reasons:
 Forward References
 Issues concerning memory requirements and organization of a LP
Lexical Analysis (Scanning)
 It identifies the lexical units in a source statements. It
then classifies the units into different lexical classes, e.g.
id‟s, constants, reserved id‟s, etc. and enters them into
different tables.
 It builds a descriptor, called token, for each lexical unit.
A token contains two fields – class code and number in
class.
 class code identifies the class to which a lexical unit
belongs. number in class is the entry number of the
lexical unit in the relevant table.
 We depict a token as Code # no, e.g. Id # 10
Lexical Analysis (Scanning) - Example

Symbol Type Length Address


1 i int
i : integer;
2 a real
a, b : real; 3 b real
a := b + i; 4 i* real
5 temp real
Note that int i first needed to be converted into real, that is why 4th entry is
added into the table.
Addition of entry 3 and 4, gives entry 5 (temp), which is value b + (i *).

The statement a := b+i; is represented as the string of tokens

Id#2 Op#5 Id#3 Op#3 Id#1 Op#10


Syntax Analysis (Parsing)
 It processes the string of tokens built by lexical analysis to
determine the statement class, e.g. assignment statement, if
statement etc.
 It then builds an IC which represents the structure of a
statement. The IC is passed to semantic analysis to
determine the meaning of the statement.

real :=

a b a +

a, b : real b i

a := b + i
Semantic Analysis
 It identifies the sequence of actions necessary to implement the
meaning of a source statement.
 It determines the meaning of a sub tree in the IC, it adds information
to a table or adds an action to the sequence of actions. The analysis
ends when the tree has been completely processed.

:= := :=

a, real + a, real + a, real temp, real

b, real i, int b, real i*, real


Analysis Phase (Front end)
Source Program

Lexical
Errors Scanning
Tokens
Symbol Table
Syntax Parsing
Errors
Constants Table
Trees Other tables
Semantic Semantic
Errors
Analysis

IC

IR
Synthesis Phase (Back end)
 It performs memory allocation and code generation.

 Memory Allocation
 The memory requirement of an identifier is computed from its type,
length and dimensionality and memory is allocated to it.
 The address of the memory area is entered in the symbol table.

Symbol Type Length Address


1 i int 2000
2 a real 2001
3 b Real 2002
Synthesis Phase (Back end)
 Code Generation
 It uses knowledge of the target architecture, viz. knowledge of
instructions and addressing modes in the target computer, to select the
appropriate instructions.
 The synthesis phase may decide to hold the values of i* and temp in
machine registers and may generate the assembly code.
 a := b + i;
CONV_R AREG, I
ADD_R AREG, B
MOVEM AREG, A
Synthesis Phase (Back end)
IR

IC Memory
Allocation
Symbol Table
Constants Table
Other tables
Code
Generation

Target
Program
Fundamentals of Language Specification

 PL Grammars
 The lexical and syntactic features of a programming
language are specified by its grammar.
 A language L can be considered to be a collection of
valid sentences.
 Each sentence can be looked upon as a sequence of
words, and each word as a sequence of letters or
graphic symbols acceptable in L.
 A language specified in this manner is known as a
formal language.
Alphabet
 The alphabet of L, denoted by the Greek symbol ∑
is the collection of symbols in its character set.
 We use lower case letters a, b, c, etc. to denote
symbols in ∑
 A symbol in the alphabet is known as a terminal
symbol (T) of L.
 The alphabet can be represented using mathematical
notation of a set, e.g.
∑ = { a, b, …, z, 0, 1, …, 9 }
where {, “,”, } are called meta symbols.
String
 A string is a finite sequence of symbols.
 We represent strings by Greek symbols α, β, γ, etc.
Thus α= axy is a string over ∑
 The length of a string is the number of symbols in it.
 Absence of any symbol is also a string, null string ε.
 Example
α= ab, β=axy
αβ = α.β = abaxy [concatenation]
Nonterminal symbols
 A Nonterminal symbol (NT) is the name of a syntax
category of a language, e.g. noun, verb, etc.
 An NT is written as a single capital letter, or as a
name enclosed between <…>, e.g. A or <Noun>.
 It is a set of symbols not in ∑ that represents
intermediate states in the generation process.
Productions
 A production, also called a rewriting rule, is a rule
of the grammar.
 It has the form
A nonterminal symbol ::= String of Ts and NTs

L.H.S. R.H.S.
e.g. <article> ::= a | an | the
<Noun> ::= boy | apple
<Noun Phrase> ::= <article> <Noun>
Derivation, Reduction and Parse Trees

 A grammar G is used for two purposes, to generate


valid strings of LG and to „recognize‟ valid strings of
LG.
 The derivation operation helps to generate valid
strings while the reduction operation helps to
recognize valid strings.
 A parse tree is used to depict the syntactic structure
of a valid string as it emerges during a sequence of
derivations or reductions.
Derivation
 Let production P1 of grammar G be of the form
P1 : A::= α
and let β be a string such that β = γAθ, then replacement of
A by α in string β constitutes a derivation according to
production P1.
 Example
<Sentence> ::= <Noun Phrase><Verb Phrase>
<Noun Phrase> ::= <Article> <Noun>
<Verb Phrase> ::= <Verb><Noun Phrase>
<Article> ::= a | an | the
<Noun> ::= boy | apple
<Verb> ::= ate
Derivation
 The following strings are sentential forms of LG.
<Noun Phrase> <Verb Phrase>
the boy <Verb Phrase> sentential
<Noun Phrase> ate <Noun Phrase> forms

the boy ate <Noun Phrase>

the boy ate an apple sentence


Reduction
Let production P1 of grammar G be of the form
P1 : A::= α
and let σ be a string such that σ = γAθ, then replacement of α by A
in string σ constitutes a reduction according to production P1.
Step String
0 the boy ate an apple
1 <Article> boy ate an apple
2 <Article> <Noun> ate an apple
3 <Article> <Noun> <Verb> an apple
4 <Article> <Noun> <Verb> <Article> apple
5 <Article> <Noun> <Verb> <Article> <Noun>
6 <Noun Phrase> <Verb> <Article> <Noun>
7 <Noun Phrase> <Verb> <Noun Phrase>
8 <Noun Phrase> <Verb Phrase>
9 <Sentence>
Parse Trees
 A sequence of derivations or reductions reveals the syntactic
structure of a string with respect to G, in the form of a parse tree.
<Sentence> 9

<Noun Phrase> 6 <Verb Phrase> 8

<Article> 1 <Noun> 2 <Verb> 3 <Noun Phrase> 7

<Article> 4 <Noun> 5

the boy ate an apple


Classification of Grammars
 Type-0 grammar (Phrase Structure Grammar)
α ::= β, where both can be strings of Ts and NTs.
But it is not relevant to specification of Prog. Lang.
 Type-1 grammar (Context Sensitive Grammar)
α A β ::= α π β,
But it is not relevant to specification of Prog. Lang.
 Type-2 grammar (Context Free Grammar)
A ::= π, which can be applied independent of its context.
CFGs are ideally suited for PL specifications.
 Type-3 grammar (Linear or Regular Grammar)
A ::= t B | t OR A ::= B t | t
Nesting of constructs or matching of parentheses cannot be
specified using such productions.
Ambiguity in Grammatic Specification
 It implies the possibility of different interpretation of a
source string.
 Existence of ambiguity at the level of the syntactic structure
of a string would mean that more than one parse tree can
be built for the string. So string can have more than one
meaning associated with it.

Ambiguous Grammar a+b*c a+b*c


+ *
E  id | E + E | E * E + c
a *
Id  a | b | c
b c a b
Assume source
string is a + b * c
Eliminating Ambiguity – An Example
Unambiguous Grammar a+b*c a+b*c
EE+T|T  id + id * id  id + id * id

TT*F|F  P + P * P  P + P * P

FF^P|P  F + P * P  F + F * P

P  id  T + F * F  T + T * F

 E + T * T  E + T
id  a | b | c
 E * T (?? Ambiguous)  E (Unambiguous)
E E
E E T
T T T T T

F F F F F F
P P P P P P

id + id * id id + id * id
GTU Examples
 List out the unambiguous production rules (grammar)
for arithmetic expression containing +, –, *, / and ^
(exponent).
EE+T|E–T|T
TT*F|T/F|F
FF^P|P
P  (E) | <id>

Derive string <id> – <id> * <id> ^ <id> + <id>


EE+T E Parse Tree
+
E–T+T
E T
T–T+T –
F–T+T E T F
P–T+T *
 <id> – T + T T T F
^
 <id> – T * F + T
F F F P P
 <id> – F * F + T
 <id> – P * F + T P P P
 <id> – <id> * F + T
id – id * id ^ id + id
 <id> – <id> * F ^ P + T
 <id> – <id> * P ^ P + T Abstract Syntax Tree
 <id> – <id> * <id> ^ P + T +
 <id> – <id> * <id> ^ <id> + T – id
 <id> – <id> * <id> ^ <id> + F
id *
 <id> – <id> * <id> ^ <id> + P
 <id> – <id> * <id> ^ <id> + <id> id ^

id id
Another Example
 Consider the following grammar:
SaSbS|bSaS|ε

Derive the string abab. Draw corresponding parse


tree. Are these rules ambiguous ? Justify.

You might also like