Programming Language Concepts

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What is Unicode?

16 bit per character, include world's all language and mathematical symbols. Consistent with ASCII code.

How to draw a parse tree?

A parse tree is a hierarchical representation of a derivation

Sentence, language, lexeme, and token concepts

A sentence is a string of characters over some alphabet (e.g., def add1(n): return n + 1) A language is a set of sentences A lexeme is the lowest level syntactic unit of a language (e.g., *, add1, begin) A token is a category of lexemes (e.g., identifier)

What are the widely-used coding schemes?

ASCII and Unicode

What is the ASCII code?

American Standard Code for Information Interchange. upper/lower case letters, digits, and punctuation marks with an 7-bit number.

Attribute grammar: how to define it?

Attribute Grammars Attribute Grammars (AGs) were developed by Donald Knuth in ~1968 Def: An attribute grammar is a CFG G=(S,N,T,P) with the following additions: - For each grammar symbol x there is a set A(x) of attribute values - Each rule has a set of functions that define certain attributes of the non-terminals in the rule - Each rule has a (possibly empty) set of predicates to check for attribute consistency Motivation: *CFGs can't describe all of the syntax of programming languages *Additions to CFGs to annotate the parse tree with some "semantic" info Primary value of AGs: *Static semantics specification *Compiler design (static semantics checking)

Usage and meaning of BASIC (1964)?

Beginner's All purpose Symbolic Instruction Code. Designed by Kemeny & Kurtz at Dartmouth for the GE 225 with the goals: •Easy to learn and use for non-science students and as a path to Fortran and Algol •Must be "pleasant and friendly" •Fast turnaround for homework •Free and private access •User time is more important than computer time

What compose a PC?

CPU, main memory (RAM), secondary storage, bus, I/O devices.

What is the CPU and what does it do?

Central Processing Unit •The "brain" of the computer •Controls all other computer functions •In PCs also called the microprocessor or simply processor.

What is the Bus and what does it do?

Computer components are connected by a bus. A bus is a group of parallel wires that carry control signals and data between components.

Context-free grammar and BNF

Context-Free Grammars •Developed by Noam Chomsky in the mid-1950s •Language generators, meant to describe the syntax of natural languages •Define a class of languages called context-free languages Backus Normal/Naur Form (1959) •Invented by John Backus to describe Algol 58 and refined by Peter Naur for Algol 60. •BNF is equivalent to context-free grammars

What is the Java Development Kit (JDK)?

DK (formerly SDK) is the Java Development Kit. JDK = JRE + development tools

How to work out a leftmost derivation?

Every string of symbols in the derivation is a sentential form (e.g., the man eats <nounPhrase> .) A sentence is a sentential form that has only terminal symbols (e.g., the man eats the apple .) A leftmost derivation is one in which the left-most nonterminal in each sentential form is the one that is expanded in the next step. A derivation may be either leftmost or rightmost or something else

How are Files stored?

Files are stored in disk, grouped by directory. File system is hierarchical, directories can contain directories that can contain more directories etc .

How do normal HLL and Java run a program?

For normal HLL to run a program, they are first compiled to assembly language, then sent to an assembler into machine language, then loader, to finally computer. For Java to run a program, it is first complied to Java Bytecode, then loaded into and executed by the JVM, and finally onto a real machine.

Briefly describe four levels of Java access control (such as public) for methods and variables.

Four levels of Java access control for methods and variables are: Public, Protected, Default, and Private. Private - access within same class Default - access within same class and package Protected - access within same class, package, and subclass Public - access within same class, package, subclass, and is universal.

Abstract Classes and Methods in Java

If a class has an abstract method, then it must be declared abstract. Abstract methods have no implementation. Abstract classes can not be instantiated.

What is the Java Runtime Environment (JRE)?

It creates a virtual machine within your computer known as the JVM (Java Virtual Machine). JRE is specific to your platform and is the environment in which Java byte code is run.

Usage and meaning of LISP (1959)?

LISt Processing language (Designed at MIT by McCarthy). •AI research needed a language that: •Process data in lists (rather than arrays) •Symbolic computation (rather than numeric) •One universal, recursive data type: the s-expression •An s-expression is either an atom or a list of zero or more s-expressions• Syntax is based on the lambda calculus •Pioneered functional programming •No need for variables or assignment •Control via recursion and conditional expressions •Status •Still the dominant language for AI •COMMON LISP and Scheme are contemporary dialects •ML, Miranda, and Haskell are related languages

Usage and meaning of Ada (1979)?

Language DoD-1 was developed thru a series of competitive contracts, Renamed Ada in May 1979. •Ada was "mandated" for use in DoD work during late 80's and early 90's. Contributions: 1. Packages - support for data abstraction 2. Exception handling - elaborate 3. Generic program units 4. Concurrency - through the tasking model

What is the Main Memory and what does it do?

Main memory holds information such as computer programs, numeric data, or documents created by a word processor. •Main memory is made up of capacitors. •Main memory is volatile storage. That is, if power is lost, the information in main memory is lost. •Other computer components can -get the information held at a particular address in memory, known as a READ, -or store information at a particular address in memory, known as a WRITE. •Writing to a memory location alters its contents. •Reading from a memory location does not alter its contents. •All addresses in memory can be accessed in the same amount of time. •We do not have to start at address 0 and read everything until we get to the address we really want (sequential access). •We can go directly to the address we want and access the data (direct or random access). •That is why we call main memory RAM (Random Access Memory).

Parse tree V.S. abstract syntax tree

Parse trees follow a grammar and usually have many nodes that are artifacts of how the grammar was written. An abstract syntax tree (AST) eliminates useless structural nodes. Use nodes corresponding to constructs in the programming language, easing interpretation and compilation.

Meaning of program verification and why we need it

Program verification involves formally proving that the computer program does exactly what is stated in the program's specification Program verification can be done for simple programming languages and small or moderately sized programs. We need it to prove program correctness and avoid consequences such like the Ariane 5 and the Intel Pentium Bug incidents.

Repetition is done using recursion

Syntactic lists are described in BNF using recursion

Difference between syntax and semantics

Syntax - the form or structure of the expressions, statements, and program units. Semantics - the meaning of the expressions, statements, and program units.

Syntax Graphs

Syntax Graphs - Put the terminals in ellipses and put the nonterminal in rectangles; connect with lines with arrowheads

What are the BIG FOUR programming language paradigms? Please give a programming language example for each of them.

The Big Four programming language paradigms are Procedural (Fortran, C), Functional (LISP, Haskell, ML), Logic/Rule based (Prolog), and OOP (C++, Java).

What are some Various PL application domains?

The five main application domains of the programming languages are: Business (Example: COBOL) Scientific (Example: Fortran) AI (Example: Python) System programming (Example: C) Mobile (Example: Java)

What are the four major language evaluation criteria? Briefly describe them.

The four major language evaluation criteria are: Readability, writability, reliability, and cost. Readability: how easy it is for programmers to read and understand programs in that PL. Writability: how easy it is for programmers to write programs in that PL. Reliability: factors that are taken into account - type checking, exception handling, aliasing. Cost: how much it costs for program training, software creation, compiler cost, execution, and maintenance.

List different implementation methods for programming languages and explain how each of them works.

The implementation methods for programming languages are: Direct execution by hardware, Direct execution by software, Compilation to another language, Hybrid, and Just-in-time compilation. Direct execution by hardware - machine language Direct execution by software - Interpreters run through a program by each line and execute each command. (Python, LISP) Compilation to another language - programs compiled into machine code. Hybrid - compilation to another language, then interpreted by a virtual machine. (Java, Pearl) Just-in-time: dynamically compile bytecode to machine code. (JavaScript)

What are the major drawbacks for the machine code that was developed in 1940s?

The major drawbacks for the machine code that was developed in 1940s were: poor readability and modifiability, tedious, doesn't allow indexing or floating point numbers, and inherited hardware deficiencies.

Given the following English grammar rules, work out a leftmost derivation for the following sentence. The man throws a baseball.

The man throws a baseball. <sentence> ::= <nounPhrase><verbPhrase>. <nounPhrase> ::= <article><noun> <article> ::= a | an | the <noun> ::= man | orange | girl | baseball <verbPhrase> ::= <verb> | <verb><nounPhrase> <verb> ::= eats | throws | sees | is Your Answer: <sentence> ::= <nounPhrase><verbPhrase>. <article><noun><verbPhrase>. The<noun><verbPhrase>. The man <verbPhrase>. The man <verb><nounPhrase>. The man throws <nounPhrase>. The man throws <article> <noun>. The man throws a <noun>. The man throws a baseball.

What was the original design goal for Simula language? What were the primary contributions of Simula?

The original design goal for Simula language is for simulation of discrete events. The primary contributions of Simula were the introduction to basic ideas that lead to OOP, as well as coroutines, classes, and inheritance.

What are the three levels of computer languages? Please briefly describe them as well.

The three levels of computer languages are Machine (consists of 0's and 1's), Assembly (low level language, uses symbolic operation codes), and High level language (easier readability and writability for the users).

Some application domains that require program verification

There are applications where it is worth it to 1) use a simplified programming language 2) work out formal specs for a program 3) capture the semantics of the simplified PL and 4) do the hard work of putting it all together and proving program correctness. What are these applications? *Security and encryption *Financial transactions *Applications on which lives depend (e.g., healthcare, aviation) *Expensive, one-shot, un-repairable applications (e.g., Mars rover) *Hardware design (e.g. Pentium chip)

What are the data types in Java?

There are two types of data types in Java - primitives and references. •Primitives are data types that store data. •References store the address of an object, which is encapsulated data.

Von Neumann Architecture

modern computer structure consisting of four subsystems: memory, I/O, ALU, and control unit, and the stored program concept and sequential execution of instructions concept

Formal grammar and its role in PL

•A (formal) grammar is a set of rules for strings in a formal language •The rules describe how to form strings from the language's alphabet that are valid according to the language's syntax •A grammar does not describe the meaning of the strings or what can be done with them in whatever context — only their form •A grammar defines a (programming) language, at least w.r.t. its syntax •It can be used to -Generate sentences in the language -Test whether a string is in the language -Produce a structured representation of the string that can be used in subsequent processing

Derivations

•A derivation is a repeated application of rules, beginning with the start symbol and ending with a sequence of just terminal symbols •It demonstrates, or proves, that the derived sentence is "generated" by the grammar and is thus in the language that the grammar defines

Meaning of parsing

•A grammar describes the strings of tokens that are syntactically legal in a PL •A recognizer simply accepts or rejects strings. •A generator produces sentences in the language described by the grammar •A parser construct a derivation or parse tree for a sentence (if possible) •Two common types of parsers are: -bottom-up or data driven -top-down or hypothesis driven •A recursive descent parser is a way to implement a top-down parser that is particularly simple.

Non-terminal, terminal, and pre-terminal symbols

•A non-terminal symbol is any symbol in the LHS (or RHS) of a rule. They represent abstractions in the language, e.g. <if-then-else-statement> ::= if <test> then <statement> else <statement> •A terminal symbol (AKA lexemes) is a symbol that's NOT in any rule's LHS. They are literal symbols that will appear in a program (e.g., if, then, else in rule above) <if-then-else-statement> ::= if <test> then <statement> else <statement> •A pre-terminal symbol is one that appears as a LHS of rules, but in every case, the RHSs consist of single terminal symbol, e.g., <digit> in <digit> ::= 0 | 1 | 2 | 3 ... 7 | 8 | 9

Finite and infinite languages

•A simple language may have a finite number of sentences •The set of strings representing integers between -10**6 and +10**6 is a finite language •A finite language can be defined by enumerating the sentences, but using a grammar is usually easier (e.g., small integers) •Most interesting languages have an infinite number of sentences •Hint: When you see recursion in a BNF, the language is probably infinite -When might it not be? •The recursive rule might not be reachable. There might be epsilons.

Meaning and usage of Epsilons

•An epsilon (ε) symbol expands to 'nothing' •It is used as a compact way to make something optional •You can always rewrite a grammar with ε symbols to an equivalent one without ε

Interfaces in Java

•An interface is like class without the implementation. It contains only -public, static and final fields, and -public and abstract method headers (no body). •A public interface, like a public class, must be in a file of the same name. •The methods and fields are implicitly public and abstract by virtue of being declared in an interface. •Many classes may implement the same interface. The classes may be in completely different inheritance hierarchies. •A class may implement several interfaces.

Hierarchy of linear parsers

•Basic containment relationship -All CFGs can be recognized by LR parser -Only a subset of all the CFGs can be recognized by LL parsers

Usage and meaning of COBOL?

•COmmon Business Oriented Language •CODASYL committee (Conference on Data Systems Languages) developed a programming language by the name of COBOL •Design goals: •Must look like simple English •Must be easy to use, even if that means it will be less powerful •Must broaden the base of computer users •Must not be biased by current compiler problems Contributions: - First macro facility in a high -level language -Hierarchical data structures (records) -Nested selection statements -Long names (up to 30 characters), with hyphens -Data Division

Usage and meaning of Simula (1962-67)?

•Designed and built by Ole-Johan Dahl and Kristen Nygaard at the Norwegian Computing Centre (NCC) in Oslo between 1962 and 1967 •Originally designed and implemented for discrete event simulation Primary Contributions: •Coroutines - a kind of subprogram •Classes (data plus methods) and objects •Inheritance •Dynamic binding => Introduced the basic ideas that developed into object-oriented programming.

Usage and meaning of Pascal (1971)?

•Designed by Wirth, who quit the ALGOL 68 committee because he didn't like the direction of that work •Designed for teaching structured programming •Small, simple•Introduces some modest improvements, such as the case statement •Was widely used for teaching programming in the 1980s

Usage and meaning of C (1972)?

•Designed for systems programming at Bell Labs by Dennis Richie and colleagues. •Evolved primarily from B, but also ALGOL 68 •Powerful set of operators, but poor type checking •Initially spread through UNIX and the availability of high quality, free compilers.

Usage and meaning of C++ (1985)?

•Developed at Bell Labs by Stroustrup •Evolved from C and SIMULA 67 •Facilities for object-oriented programming, taken partially from SIMULA 67, added to C •Also has exception handling •A large and complex language, in part because it supports both procedural and OO programming •Rapidly grew in popularity, along with OOP •ANSI standard approved in November, 1997

Usage and meaning of Java?

•Developed at Sun in the early 1990s with original goal of a language for embedded computers •Principals: Bill Joy, James Gosling, Mike Sheradin, Patrick Naughton •Original name, Oak, changed for copyright reasons •Based on C++ but significantly simplified •Supports only OOP •Has references, but not pointers •Includes support for applets and a form of concurrency

Usage and meaning of Prolog?

•Developed at the University of Aix Marseille, by Comerauer and Roussel, with some help from Kowalski at the University of Edinburgh •Based on formal logic •Non-procedural •Can be summarized as being an intelligent database system that uses an inferencing process to infer the truth of given queries

What are some Secondary Storage Media and what do they do?

•Disks -- floppy, hard, removable (random access) •Tapes (sequential access) •CDs (random access)•DVDs (random access) •Secondary storage media store files that contain-computer programs-data-other types of information •This type of storage is called persistent (permanent) storage because it is non-volatile.

Who developed Fortran and what does it stand for?

•FORmula TRANslator •Developed at IBM under the guidance of John Backus primarily for scientific, computational programming

What are some examples of High Level Languages (HLL)?

•Fortran: IBM during 50's. Scientific/engineering computation. •COBOL (Common Business Oriented Language), Late 50's. Database •C, 70's at Bell Lab. System language. Many modern OSs are written using C. •C++, 80's •Java: Early 90's SUN Microsystems. Written using C++.

Why shall we study PL concepts?

•Increased capacity to express programming concepts •Improved background for choosing appropriate languages •Enhanced ability to learn new languages •Improved understanding of the significance of implementation •Increased ability to design new languages •Mastering different programming paradigms

What are I/O (Input / Output) Devices?

•Information input and output is handled by I/O (input/output) devices. •More generally, these devices are known as peripheral devices. •Examples: -monitor-keyboard-mouse-disk drive (floppy, hard, removable)-CD or DVD drive-printer-scanner

Inheritance in Java

•Inheritance is implemented using the keyword extends. •A class may only inherit from only one superclass. •If a class is not derived from a super class then it is derived from java.lang.Object.

Usage and meaning of C# (C Sharp)?

•Microsoft and Sun were bitter rivals in the 90s •C# is Microsoft's answer to Java •C# is very similar to Java with (maybe) some minor improvements •If you know Java, learning C# should be easy •However: both languages have extensive libraries, and mastering them is a big part of mastering the language.

Complexity of regular parser, LL and LR parsers

•Parsing an arbitrary context free grammar is O(n3), e.g., it can take time proportional the cube of the number of symbols in the input. This is bad! •If we constrain the grammar somewhat, we can always parse in linear time. This is good! LL(n) : Left to right, Leftmost derivation, look ahead at most n symbols. •Linear-time parsing »Recognize LL grammar »Use a top-down strategy •LR(n) : Left to right, Right derivation, look ahead at most n symbols. •Linear-time parsing »Recognize LR grammar »Use a bottom-up strategy

Polymorphism in Java

•Polymorphism requires general class on left of assignment operator, and specialized class on right. •Casting allows you to make such an assignment provided you are confident that it is ok.

Precedence and associativity of Operators

•Precedence and associativity deal with the evaluation order within expressions •Precedence rules specify order in which operators of different precedence level are evaluated, e.g.: "*" Has a higher precedence that "+", so "*" groups more tightly than "+" •A language's precedence hierarchy should match our conventions and intuitions, but the result's not always perfect, as in this Pascal example: If A > 0 and A < 10 then A := 0 ; •Pascal's relational operators (e.g., <) have lowest precedence! •Associativity rules specify order to evaluate operators of same precedence level •Operators are typically either left associative or right associative. •Left associativity is typical for +, - , * and / •For + and * it doesn't matter in theory (though it can in practice) but for - and / it matters in theory, too •Most languages use a similar associativity rules for the most common operators •Precedence refers to the order in which operations are evaluated •Usual convention: exponents > mult, div > add, sub •Deal with operations in categories: exponents, mulops, addops. •Associativity refers to the order in which two of the same operation should be computed •We'll see that conditionals right associate but with a wrinkle: an else clause associates with closest unmatched if

Scripting Languages

•Scripting languages like Perl, Ruby, Javascript and PHP have become important •They shine at connecting diverse pre-existing components to accomplish new tasks •Cf. shell languages in Unix •Typical properties include - privileging rapid development over execution efficiency -implemented with interpreters rather than compilers -strong at communication with program components in other languages

Generics in Java

•Since JDK 1.5 (Java 5), the Collections framework has been parameterized. •A class that is defined with a parameter for a type is called a generic or a parameterized class. •If you compare the Collection interface in the API for 1.4.2 to the one in version 1.5.0, you will see the interface is now called Collection<E>.

Meaning of Syntactic sugar

•Syntactic sugar: syntactic features designed to make code easier to read or write while alternatives exist •Makes a language sweeter for humans to use: things can be expressed more clearly, concisely or in an preferred style •Syntactic sugar can be removed from language without effecting what can be done •All applications of the construct can be systematically replaced with equivalents that don't use it

Two phases: scanning and parsing

•The scanning phase (lexical analyzer) collects characters into tokens •Parsing phase (syntactic analyzer) determines syntactic structure


संबंधित स्टडी सेट्स

Working Capital Management Review (Problems)

View Set

Ch.6 & Ch.15 Final Exam Multiple Choice

View Set