PROGRAMMING LANGUAGE
PROGRAMMING LANGUAGE
1. FORTRAN is originally developed by IBM in the 1950s for scientific and engineering applications.
True
COMPILER
3. In C programming language , help you understand unions, arrays & pointers, separate compilation, var args, catch and
throw.
True
4. An interpreter, like a compiler, translates high-level language into low-level machine language
True
5. A preprocessor is generally considered as a part of compiler, is a tool that produces input for compilers.
True
7. A programming language that is wide dissemination at minimal cost like Pascal and Java.
True
8. An Interpreter reads the whole source code at once, creates tokens, checks semantics, generates intermediate code,
executes the whole program and may involve many passes.
False (COMPILER)
False
10. The recursive rules known as parsing define the ways in which these constituents combine.
11. The main C language compiler produces .NET Common Intermediate Language (CIL), which is then translated into
machine code immediately prior to execution.
True
False (COMPILER)
13. The parsing organizes tokens into a parse tree that represents higher-level constructs in terms of their constituents.
True
LINEAR
15. Compilation is translation from one language into another, with full analysis of the meaning of the input. TRUE
FA 2 PROGRAMMING LANG – 15/15
FA 3 PROGRAMMING LANGUAGE
PROGRAMMING
LANGUAGES
Module 1
INTRODUCTION TO
LANGUAGES
What makes a language successful?
• easy to learn (BASIC, Pascal, LOGO, Scheme)
• easy to express things, easy use once fluent, "powerful” (C,
Common Lisp, APL, Algol-68, Perl)
• easy to implement (BASIC, Forth)
• possible to compile to very good (fast/small) code (Fortran)
• backing of a powerful sponsor (COBOL, PL/1, Ada, Visual
Basic)
• wide dissemination at minimal cost (Pascal, Turing, Java)
Why do we have programming languages? What is a
language for?
• way of thinking -- way of expressing algorithms
• languages from the user's point of view
• abstraction of virtual machine -- way of specifying
what you want
• the hardware to do without getting down into the bits
• languages from the implementor's point of view
Help you choose a language.
Compilation
• Better performance
Most language implementations include a mixture of
both compilation and interpretation
Note that compilation does NOT have to produce machine
language for some sort of hardware
Compilation is translation from one language into another,
with full analysis of the meaning of the input
Compilation entails semantic understanding of what is being
processed;
A pre-processor will often let errors through. A compiler
hides further steps; a pre-processor does not
Implementation strategies:
• Preprocessor
• Looks for compiler directives (e.g., #include )
• Removes comments and white space
• Groups characters into tokens (keywords, identifiers,
numbers, symbols)
• Identifies higher-level syntactic structures (loops,
subroutines)
Implementation strategies:
INTERMEDIATE
CODE
LEXICAL GENERATION
ANALYSIS
CODE
OPTIMIZATION
SYNTAX
ANALYSIS
TARGET CODE
GENERATION
SEMANTIC
ANALYSIS
Overview:
*
Scanning:
• divides the program into "tokens", which are the smallest meaningful
units; this saves time, since character-by-character processing is slow
• we can tune the scanner better if its job is simple; it also saves
complexity (lots of it) for later stages
• you can design a parser to take characters instead of tokens as input,
but it isn't pretty
• scanning is recognition of a regular language, e.g., via DFA
Parsing is recognition of a context-free language, e.g.,
via PDA
• Parsing discovers the "context free" structure of the
program
• Informally, it finds the structure you can describe with
syntax diagrams (the "circles and arrows" in a Pascal
manual)
Lexical Analyzer or Linear Analyzer breaks the sentence into
tokens. For Example following assignment statement :-
position = initial + rate * 60
Would be grouped into the following tokens:
1. The identifier position.
2. The assignment symbol =.
3. The identifier initial.
4. The plus sign.
5. The identifier rate.
6. The multiplication sign.
7. The number 60
SYMBOL TABLE: POSITION Id1 & attributes
https://www.computerhope.com/jargon/p/programming-language.htm
https://www.tutorialspoint.com/compiler_design/compiler_design_syntax_analysis.htm
https://www.cs.iusb.edu/~dvrajito/teach/c311/c311_3_scope.html
https://www.tutorialspoint.com/compiler_design/compiler_design_semantic_analysis.htm
https://cs.lmu.edu/~ray/notes/controlflow/
https://teachcomputerscience.com/programming-data-types/
https://algs4.cs.princeton.edu/12oop/
PROGRAMMING
LANGUAGES
Module 2
LEXICAL AND SYNTAX
ANALYSIS
•The word “lexical” in the traditional sense means “pertaining to
words”. In terms of programming languages, words are objects like
variable names, numbers, keywords etc.
Source
token
program
Lexical parser
analyzer
Nexttoken()
symbol
table
– Token: a group of characters having a collective
meaning.
– A lexeme is a particular instant of a token.
• E.g. token: identifier, lexeme: pi, etc.
– pattern: the rule describing how a token can be
formed.
• E.g: identifier: ([a-z]|[A-Z]) ([a-z]|[A-Z]|[0-9])*
• Lexical analyzer does not have to be an individual
phase. But having a separate phase simplifies the
design and improves the efficiency and portability.
– How to specify tokens (patterns)?
– How to recognize the tokens giving a token specification (how to
implement the nexttoken() routine)?
• How to specify tokens:
– all the basic elements in a language must be
tokens so that they can be recognized.
• Token types: constant, identifier, reserved word,
operator and misc. symbol.
says you can add nodes <NP>, <V>, and <NP>, in that
order, as children of <S>
ParseTree:
<S>
((a+b)*c)
<exp> * <exp>
( <exp> ) c
<exp> + <exp>
a b
Start symbol
<S> ::= <NP> <V> <NP>
tokens
Syntax - It is defined as the grammar in each
programming language and it is set of valid grammar
in each language.
A syntax analyzer or parser takes the input from a
lexical analyzer in the form of token streams.
The parser analyzes the source code (token stream)
against the production rules to detect any errors in
the code. The output of this phase is a parse tree.
A syntax analyzer or parser is a program that groups
sequences of tokens from the lexical analysis phase
into phrases each with an associated phrase type.
A phrase is a logical unit with respect to the rules of
the source language.
For example, consider:
a := x * y + z
After lexical analysis, this statement has the
structure:
This way, the parser accomplishes two tasks, i.e., parsing the code, looking for errors, and
generating a parse tree as the output of the phase.
Parsers are expected to parse the whole code even if some errors exist in the program.
Parsers use error recovering strategies.
• A parser should be able to detect and report any error in the
program.
• Mostly it is expected from the parser to check for errors but errors
may be encountered at various stages of the compilation process.
A program may have the following kinds of errors at various
stages:
Lexical : name of some identifier typed incorrectly
https://www.computerhope.com/jargon/p/programming-language.htm
https://www.tutorialspoint.com/compiler_design/compiler_design_syntax_analysis.htm
https://www.cs.iusb.edu/~dvrajito/teach/c311/c311_3_scope.html
https://www.tutorialspoint.com/compiler_design/compiler_design_semantic_analysis.htm
https://cs.lmu.edu/~ray/notes/controlflow/
https://teachcomputerscience.com/programming-data-types/
https://algs4.cs.princeton.edu/12oop/
PROGRAMMING
LANGUAGES
Module 3
NAME, SCOPE AND
BINDING
A name is a mnemonic character string representing
something else
Name: Identifiers that allow us to refer to variables,
constants, functions, types, operations, and so on
x, sin, f, prog1, null? are names
• Most names are identifiers
Names enable programmers to refer
to variables, constants, operations, and types using
identifier names rather than low-level hardware components
A binding is an association between two things, such as a
name and the thing it names
• e.g. Name of an object and the object
Many properties of a programming language are defined
during its creation.
For instance:
• the meaning of key words such as while or for in C,
• the size of the integer data type in Java, are properties
defined at language design time.
Binding:
-run time
• the time during which a program executes (runs)
Binding Time Sample:
Language feature Binding time
Syntax, e.g. if (a>0) b:=a; in C or
Language design
if a>0 then b:=a end if in Ada
Keywords, e.g. class in C++ and Java Language design
Reserved words, e.g. main in C and
Language design
writeln in Pascal
Meaning of operators, e.g. + (add) Language design
Primitive types, e.g. float
Language design
and struct in C
Internal representation of literals,
Language implementation
e.g. 3.1 and "foo bar"
The specific type of a variable in a
Compile time
C or Pascal declaration
Binding Time Sample:
Language design,
Storage allocation method for a variable language implementation,
and/or compile time
Linking calls to static library routines,
Linker
e.g. printf in C
Merging multiple object codes
Linker
into one executable
Loading executable in memory
Loader (OS)
and adjusting absolute addresses
Nonstatic allocation of space for variable Run time
The terms STATIC and DYNAMIC are generally used to refer
to things bound before run time and at run time,
respectively
• “static” is a coarse term; so is "dynamic“
•Object lifetime - (or life cycle) of an object is the period between the
object creation and destruction.
•Binding lifetime - the period between the creation and destruction
of the binding.
•Usually the binding lifetime is a subset of the object lifetime.
• In many object-oriented languages (OOLs), particularly those
that use garbage collection (GC) – objects are allocated on
the heap.
Why a stack?
• allocate space for recursive routines
(not necessary in FORTRAN – no recursion)
• reuse space
(in all programming languages)
Central stack for
• parameters
• local variables
Why a stack?
• allocate space for recursive routines
(not necessary in FORTRAN – no recursion)
• reuse space
(in all programming languages)
Stack
•very fast access
•don't have to explicitly de-allocate variables
•space is managed efficiently by CPU, memory will not become
fragmented
•local variables only
•limit on stack size (OS-dependent)
•variables cannot be resized
Heap
•variables can be accessed globally
•no limit on memory size
•(relatively) slower access
•no guaranteed efficient use of space, memory may become
fragmented over time as blocks of memory are allocated,
then freed
•you must manage memory (you're in charge of allocating and
freeing variables)
•variables can be resized using realloc()
Local Variables: Stack Allocation
• When we have a declaration of the form “int a;”:
– a variable with identifier “a” and some memory allocated to it
is created in the stack. The attributes of “a” are:
• Name: a
• Data type: int
• Scope: visible only inside the function it is defined,
disappears once we exit the function
• Address: address of the memory location reserved for it.
Note: Memory is allocated in the stack for a even before it
is initialized.
Local Variables: Stack Allocation
• Size: typically 4 bytes
• Value: Will be set once the variable is initialized
• overloaded functions - two different things with the same name; in C++
• overload norm
int norm (int a){return a>0 ? a : -a;)
complex norm (complex c ) { // ...
• polymorphic functions -- one thing that works in more then one way
• in Modula-2: function min (A : array of integer); …
• in Smalltalk
It's worth distinguishing between some closely related
concepts (2)
• generic functions (modules, etc.) - a syntactic template that
can be instantiated in more than one way at compile time
• via macro processors in C++
• built-in in C++
• in Clu
• in Ada
A language that is easy to compile often leads to
• a language that is easy to understand
• more good compilers on more machines (compare Pascal and Ada!)
• better (faster) code
• fewer compiler bugs
• smaller, cheaper, faster compilers
• better diagnostics
McGrath, Mike (2017). C++ Programming. In Easy Steps Limited.
Perkins, Benjamin (2016). Beginning Visual C# 2015 programming.
WroxSteve, Tale (2016). C++.
Chopra, R. (2015). Principles of Programming Languages. New Delhi: I.K. International
Publishing.
Kumar, Sachin (2015).Principles of programming Languages. S. K. Kataria& Sons.
https://www.computerhope.com/jargon/p/programming-language.htm
https://www.tutorialspoint.com/compiler_design/compiler_design_syntax_analysis.htm
https://www.cs.iusb.edu/~dvrajito/teach/c311/c311_3_scope.html
https://www.tutorialspoint.com/compiler_design/compiler_design_semantic_analysis.htm
https://cs.lmu.edu/~ray/notes/controlflow/
https://teachcomputerscience.com/programming-data-types/
https://algs4.cs.princeton.edu/12oop/