0% found this document useful (0 votes)
70 views81 pages

PPL Unit - I

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views81 pages

PPL Unit - I

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 81

Principles of Programming Languages

Unit - I
Preliminary Concepts, Syntax and
Semantics
History of Programming
Languages
Reasons for Studying Concepts of Programming Languages

➔ Increased capacity to express ideas


➔ Improved background for
choosing appropriate languages
➔ Increased ability to learn new
languages
➔ Better understanding of
significance of implementation
➔ Overall advancement of computing
Programming Domains

Scientific applications – Large number of floating point


computations – Fortran
Business applications – Produce reports, use decimal
numbers and characters – COBOL
Artificial intelligence – Symbols rather than numbers
manipulated – LISP
Systems programming – Need efficiency because of
continuous use – C
Web Software – Eclectic collection of languages: markup (e.g.,
Language Evaluation Criteria
Language Evaluation Criteria

Readability : the ease with which programs can be read and


understood
Writability : the ease with which a language can be used to
create programs
Reliability : conformance to specifications (i.e.,
performs to its specifications)
Cost : the ultimate total cost
Influence on Language
Design
Computer Architecture

Programming Design Methodologies


Fetch-execute Cycle
Programming Design Methodologies
1960s and early 1970s brought an intense analysis
Shift in the major cost of computing from hardware to software, as
hardware costs decreased and programmer costs increased.
New software development methodology 1970s were called top-
down design Procedure-oriented program design methodologies
Data-oriented program design methodologies
1980s evolution of data-oriented software
development Object-oriented programming
Procedure-oriented programming - concurrency
Language Categories
Four Categories - imperative, functional, logic, and object oriented

Visual language - .NET

Scripting language - Perl, JavaScript, and

Ruby Logic programming language - prolog

Markup language - HTML, XML

Special purpose language - Report Program Generator (RPG),


Automatically Programmed Tools (APT), General Purpose Simulation
System (GPSS)
Language Design Trade-Offs

Reliability vs. cost of execution


Example: Java demands all references to array elements be checked for
proper indexing but that leads to increased execution costs

Readability vs. writability


Example: APL provides many powerful operators (and a large number of
new symbols), allowing complex computations to be written in a
compact program but at the cost of poor readability
Writability (flexibility) vs. reliability
Example: C++ pointers are powerful and very flexible but not reliably
used
Implementation Methods
Programming Languages can be implemented in three ways:
1. Pure Interpretation- Programs are interpreted by
another program known as an interpreter
2. Compilation- Programs are translated into machine
language
3. Hybrid Implementation System- A compromise
between compilers and pure interpreters
Pure Interpretation:
No translation
Easier implementation of programs (run-time errors can easily and
immediately displayed)
Slower execution (10 to 100 times slower than compiled programs)
Often requires more space
Becoming rare on high-level languages
Significantly comeback with some latest web scripting languages
(e.g., JavaScript)
Interpretation
Compilation :
Translate high-level program (source language) into machine code
(machine language)
 Slow translation, fast execution
Compilation process has several phases: –
Lexical analysis: converts characters in the source program into lexical
units
Syntax analysis: transforms lexical units into parse trees which represent
the syntactic structure of program
Semantics analysis: generate intermediate code
Code generation: machine code is generated
Additional Compilation Terminologies :
 Load module (executable image): the user and system code together
 Linking and loading: the process of collecting system program and
linking them to user program
Compilation
Hybrid Implementation:

A compromise between compilers and pure interpreters


 A high-level language program is translated to an intermediate
language that allows easy interpretation
 Faster than pure interpretation
Examples :
Perl programs are partially compiled to detect errors before
interpretation
 Initial implementations of Java were hybrid; the intermediate
form, byte code, provides portability to any machine that has a
byte code interpreter and a runtime system (together, these are
called Java Virtual Machine)
Hybrid Implementation
Just-in-Time Implementation Systems:
Initially translate programs to an intermediate language
Then compile intermediate language into machine code
Machine code version is kept for subsequent calls
JIT systems are widely used for Java programs
.NET languages are implemented with a JIT system
A Layered
Interface Of
Virtual
Computers
Provided by a
typical Computer
System
Programming Environments
Collection of tools used in the development of software
This collection may consist of only a file system, a text editor, a linker,
and a compiler
UNIX is an older programming environment
Borland JBuilder is a programming environment that provides an
integrated compiler, editor, debugger, and file system for Java
development
Microsoft Visual Studio .NET - collection of software development tool -
NetBeans - freeware
Syntax and Semantics
Topics to be Covered

Introduction
The General Problem of Describing Syntax
Formal Methods of Describing Syntax
Attribute Grammars
Describing the Meanings of Programs
What do you mean by
Syntax?
the form or structure of the
expressions, statements, and
program units
What do you mean by
Semantics?
the meaning of the
expressions, statements, and
program units
Example of an
Syntax
the syntax of a Java while
statement is

while (boolean_expr) statement


The General Problem of Describing Syntax

Terminologies
Sentence
Language
Lexeme
Token
Terminologies - Definition

A sentence is a string of characters over some


alphabet A language is a set of sentences
A lexeme is the lowest level syntactic unit of a
language (e.g., *, sum, begin)
A token is a category of lexemes (e.g., identifier)
Example for Lexemes &
Tokens

Consider the following Java


statement:
index = 2 * count + 17;
Lexemes Tokens
index identifier
=
equal_sign
*2
int_liter
al
mult_op
count
identifi
er
Language Recognizers

Languages can be formally defined in two distinct


ways: by
recognition and by generation
Language L that uses an alphabet Σ of characters
To define L formally using the recognition method, we
would need to construct a mechanism R, called a
recognition device, capable of reading strings of
characters from the alphabet
The syntax analysis part of a compiler is a recognizer
for the language the compiler translates
Language Generators

A language generator is a device that can be used to


generate the sentences of a language.
There is a close connection between formal generation
and recognition devices for the same language.
This was one of the seminal discoveries in computer
science, and it led to much of what is now known about
formal languages and compiler design theory.
Formal Methods of Describing Syntax

Backus-Naur Form and Context-Free Grammars


Most widely known method for describing
programming language syntax
Extended BNF
Improves readability and writability of BNF
Grammars and Recognizers
Context-Free Grammars

Developed by Noam Chomsky(linguist) in the mid-1950s


Described four classes of grammars
Two of these grammar classes, named context-free and regular,
turned out to be useful for describing the syntax of
programming languages.
Syntax of whole programming languages, with minor exceptions,
can be described by context-free grammars
No interest at the time in the artificial languages used to
communicate with computers
Backus-Naur Form

After Chomsky’s work ACM-GAMM group began designing


ALGOL 58.
A landmark paper describing ALGOL 58 was presented by
John Backus, a prominent member of the ACM-GAMM
group, at an international conference in 1959
This paper introduced a new formal notation for
specifying
programming language syntax.
Backus-Naur Form

Later modified slightly by Peter Naur for the description of


ALGOL 60
This revised method of syntax description became known as
Backus-Naur Form, or simply BNF.
BNF is a natural notation for describing syntax
BNF in the ALGOL 60 report was not immediately
accepted by computer users
It soon became and is still the most popular method of
concisely describing programming language syntax.
Fundamentals

A metalanguage is a language that is


used to describe another language.
BNF is a metalanguage for programming
languages. BNF uses abstractions for
syntactic structures.
Fundamentals

A simple Java assignment statement.

The text on the left side of the arrow, which is aptly called the
left-hand side (LHS), is the abstraction being defined

The text to the right of the arrow is the definition of the


LHS.
It is called the right-hand side (RHS) and consists of some
mixture of tokens, lexemes, and references to other
abstractions
Fundamentals
One example sentence whose syntactic structure is described by
the rule is

The abstractions in a BNF description, or grammar, are often called


nonterminal symbols, or simply nonterminals,

The lexemes and tokens of the rules are called terminal symbols, or
simply terminals.

A BNF description, or grammar, is a collection of rules.


Fundamentals
For example, a Java if statement can be described with
the rules

with the
rule

BNF is simple, it is sufficiently powerful to describe nearly all of


the syntax of programming languages
Gramm
ar
A grammar is a generative device for defining
languages. The sentences of the language are
generated through a
sequence of applications of the rules, beginning with
a special nonterminal of the grammar called the
start symbol.
This sequence of rule applications is called a
derivation.
In a grammar for a complete programming language,
Grammars and
Derivations
Derivation

Every string of symbols in the derivation is a sentential


form
A sentence is a sentential form that has only terminal
symbols
A leftmost derivation is one in which the leftmost
nonterminal in each sentential form is the one that is
expanded
A derivation may be neither leftmost nor rightmost
A Derivation of a program
Parse Trees

One of the most attractive features of grammars


is that they naturally describe the hierarchical
syntactic structure of the sentences of the
languages they define.
These hierarchical structures are called parse
trees.
Derivation of the sentence A = B + C * A
Backus-Naur Form
<> used to enclose an item

::= separates an item from its definition

| between items indicates a choice

; the end of a rule


Backus-Naur Form Example 1
<variable> ::= <letter>| <letter><digi>;

<letter> ::= A|B|C;

<digit> ::= 1|2|3;


Backus-Naur Form Example 2
<variable> ::= <letter>| <variable><letter>;

<letter> ::= A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|


W|X|Y|Z
Backus-Naur Form Example 3
<identifier> ::= <letter>|<identifier><letter>|<identifier><digit>

<digit> ::= 0|1|2|3|4|5|6|7|8|9

<letter> ::= <Ucaseletter>|<Lcaseletter>

<Ucaseletter> ::= A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z

<Lcaseletter> ::= a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z


Attribute
Grammar
An attribute grammar is a device used to describe more of the
structure of a programming language than can be described
with a context-free grammar.
An attribute grammar is an extension to a context-free grammar.
Attribute grammars allows certain language rules to be
conveniently described, such as type compatibility.
Attribute grammars have been used in a wide variety of
applications.
They have been used to provide complete descriptions of the
syntax and static semantics of programming languages
Attribute Grammar
Attribute grammar have been used as the formal definition of a
language that can be input to a compiler generation system
Attribute grammars have been used in natural-language processing
systems
Some characteristics of the structure of programming languages that
are difficult to describe with BNF and some are impossible.
As an example of a syntax rule that is difficult to specify with BNF,
consider type
compatibility rules
In Java, for example, a floating-point value cannot be assigned to an
integer type variable
This restrictions can be specified in BNF with additional Non terminal
Attribute Grammar

Example. Syntax rule that cannot be specified in BNF


Attribute grammars are a formal approach both to describing and
checking the correctness of the static semantics rules of a
program.
Although, they are not always used in a formal way in
compiler design, the basic concepts of attribute grammars
are at least informally used in every compiler.
Attribute grammars are context-free grammars to which
have been added attributes, attribute computation functions,
Attribute Grammar
Attributes, which are associated with grammar symbols (the terminal
and nonterminal symbols), are similar to variables in the sense that they
can have values assigned to them.

Attribute computation functions, sometimes called semantic functions,


are associated with grammar rules. They are used to specify how
attribute values are computed.

Predicate functions, which state the static semantic rules of the


language,
are associated with grammar rules.

Intrinsic attributes are synthesized attributes of leaf nodes whose values


are determined outside the parse tree.
Example of Attribute
Grammars
that the name on the end of an Ada procedure must match the
procedure’s name

This rule cannot be stated in BNF


A Larger example of an Attribute Grammar
An Attribute Grammar for Simple Assignment
Statements
An Attribute Grammar for Simple Assignment
Statements
A parse tree
for A = A
+B
The flow of
attributes in
the tree
Evaluation of the
attributes,
in an order in which it is possible to
compute them
A Fully Attributed Parse
Tree
Evaluation

Checking the static semantic rules of a language is an


essential part of all compilers.
Even if a compiler writer has never heard of an attribute
grammar, he or she would need to use their fundamental
ideas to design the checks of static semantics rules for his or
her compiler.
One of the main difficulties in using an attribute grammar to
describe all of the syntax and static semantics of a real
contemporary programming language is the size and complexity
of the attribute grammar.
Evaluation

The large number of attributes and semantic rules


required for a complete programming language make
such grammars difficult to write and read.
The attribute values on a large parse tree are costly to
evaluate.
On the other hand, less formal attribute grammars are a
powerful and commonly used tool for compiler writers,
who are more interested in the process of producing a
compiler than they are in formalism.
The
End

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy