21
G22.2110-001 G22.2110-001 1 Programming Languages Programming Languages G22.2110-001 G22.2110-001 Walter Williams Walter Williams

Programming Languages G22.2110-001 Walter Williams

Embed Size (px)

DESCRIPTION

Programming Languages G22.2110-001 Walter Williams. Administrative Stuff. Homework, Exams, etc. Weekly assignments Programming projects Mid-Term & Final Exams No cheating Join the mailing list: http://www.cs.nyu.edu/mailman/listinfo/g22_2110_001_su04 Recitation. - PowerPoint PPT Presentation

Citation preview

Page 1: Programming Languages G22.2110-001 Walter Williams

G22.2110-001G22.2110-001 11

Programming Programming LanguagesLanguages

G22.2110-001G22.2110-001

Walter WilliamsWalter Williams

Page 2: Programming Languages G22.2110-001 Walter Williams

G22.2110-001G22.2110-001 22

Administrative StuffAdministrative Stuff

Homework, Exams, etc.Homework, Exams, etc. Weekly assignmentsWeekly assignments Programming projectsProgramming projects Mid-Term & Final ExamsMid-Term & Final Exams No cheatingNo cheating

Join the mailing list:Join the mailing list:http://www.cs.nyu.edu/mailman/listinfo/http://www.cs.nyu.edu/mailman/listinfo/

g22_2110_001_su04g22_2110_001_su04

RecitationRecitation

Page 3: Programming Languages G22.2110-001 Walter Williams

G22.2110-001G22.2110-001 33

What’s covered in Lectures & What’s covered in Lectures & TextsTexts

Purpose of course is for you to understand:Purpose of course is for you to understand: The issues involved in programming language designThe issues involved in programming language design The various strategies for programming and how The various strategies for programming and how

languages support those strategieslanguages support those strategies Type systems, OO support, abstraction, concurrent & Type systems, OO support, abstraction, concurrent &

generic programming generic programming Not just learning to program in different languagesNot just learning to program in different languages TextBooks:TextBooks:

Scott – covers both compilers and programming Scott – covers both compilers and programming languages. You can skip the compiler stuff.languages. You can skip the compiler stuff.

Barnes – Ada language, used by Defense Dept and other Barnes – Ada language, used by Defense Dept and other critical applications.critical applications.

Paulson – ML language, widely used in AI, Language Paulson – ML language, widely used in AI, Language theory, etc.theory, etc.

Others:Others: Stanley Lippman, Stanley Lippman, The C++ Object ModelThe C++ Object Model; ; Bjarne Stroustrup, Bjarne Stroustrup, The Design and Evolution of C++The Design and Evolution of C++ The Little SchemerThe Little Schemer Java Language Specification Java Language Specification

Page 4: Programming Languages G22.2110-001 Walter Williams

G22.2110-001G22.2110-001 44

Language & CommunicationLanguage & Communication

Human (Natural) Language Human (Natural) Language Problem Domain LanguageProblem Domain Language Algorithmic LanguageAlgorithmic Language Documentation LanguageDocumentation Language Programming LanguageProgramming Language Language as a tool for thoughtLanguage as a tool for thought

Page 5: Programming Languages G22.2110-001 Walter Williams

G22.2110-001G22.2110-001 55

Programming Language Programming Language StakeholdersStakeholders

Software DevelopersSoftware Developers Specification & DesignSpecification & Design CodingCoding

Compiler WritersCompiler Writers Maintenance ProgrammersMaintenance Programmers Quality Control & SupportQuality Control & Support ManagementManagement

Page 6: Programming Languages G22.2110-001 Walter Williams

G22.2110-001G22.2110-001 66

Language AttributesLanguage Attributes ExpressivenessExpressiveness

APL for arrays, Lisp for lists, etc.APL for arrays, Lisp for lists, etc. All major languages are Turing completeAll major languages are Turing complete

EfficiencyEfficiency Of coding, compilation or execution Of coding, compilation or execution

ReadabilityReadability By programming experts, domain experts and non-expertsBy programming experts, domain experts and non-experts

ScalabilityScalability Communicating parallel programmersCommunicating parallel programmers Modules, separate compilation and information hidingModules, separate compilation and information hiding

Safety and SecuritySafety and Security Market AttributesMarket Attributes

Popularity => availability of programmers, tools, libraries, Popularity => availability of programmers, tools, libraries, etc.etc.

Page 7: Programming Languages G22.2110-001 Walter Williams

G22.2110-001G22.2110-001 77

Models (Styles) of Models (Styles) of ComputationComputation

Imperative (Procedural)Imperative (Procedural) Mutable storage – modified by assignmentMutable storage – modified by assignment Fortran, Algol, C++, JavaFortran, Algol, C++, Java

Functional (Applicative)Functional (Applicative) Pure mathematical functions – no side effectsPure mathematical functions – no side effects ML, Haskell, SmalltalkML, Haskell, Smalltalk

DeclarativeDeclarative Programs are sets of (logical) assertionsPrograms are sets of (logical) assertions Prolog, SQL Prolog, SQL

Object Oriented Object Oriented Orthogonal to the three models aboveOrthogonal to the three models above Inheritance, Polymorphism, Encapsulation Inheritance, Polymorphism, Encapsulation

Page 8: Programming Languages G22.2110-001 Walter Williams

G22.2110-001G22.2110-001 88

Compilers & InterpretersCompilers & Interpreters

Compiling vs. InterpretingCompiling vs. Interpreting Compilers translate at compile time, onceCompilers translate at compile time, once Interpreters translate at runtime, every timeInterpreters translate at runtime, every time

Front EndFront End Syntactic Analysis: Lexical Analysis & ParsingSyntactic Analysis: Lexical Analysis & Parsing Semantic Analysis & Error CheckingSemantic Analysis & Error Checking Generates Intermediate CodeGenerates Intermediate Code

Back EndBack End Most optimizationsMost optimizations Turns Intermediate Code into ExecutableTurns Intermediate Code into Executable

Page 9: Programming Languages G22.2110-001 Walter Williams

G22.2110-001G22.2110-001 99

Programming EnvironmentsProgramming Environments

Development EnvironmentDevelopment Environment Interactive Development EnvironmentsInteractive Development Environments

Smalltalk browser environmentSmalltalk browser environment Microsoft IDEMicrosoft IDE

Development FrameworksDevelopment Frameworks Swing, MFCSwing, MFC

Language aware EditorsLanguage aware Editors LibrariesLibraries

Java Swing classesJava Swing classes C++ Standard Template Library (STL)C++ Standard Template Library (STL)

Libraries change much more quickly than the languageLibraries change much more quickly than the language Libraries usually very different for different languagesLibraries usually very different for different languages

Page 10: Programming Languages G22.2110-001 Walter Williams

G22.2110-001G22.2110-001 1010

Lexical IssuesLexical Issues Lexical Elements are TokensLexical Elements are Tokens

Keywords, operators, punctuation, names, numbers, etc.Keywords, operators, punctuation, names, numbers, etc.

Tokens are described by regular expressions Tokens are described by regular expressions (Type 3 grammars)(Type 3 grammars)

ExamplesExamples Identifiers: letter (letter or digit)*Identifiers: letter (letter or digit)* Integer: digit digit* Integer: digit digit*

Terminal symbols of lexical grammar are usually Terminal symbols of lexical grammar are usually characterscharacters ASCII, Unicode, etc.ASCII, Unicode, etc. Escape sequences and tri-gramsEscape sequences and tri-grams

Page 11: Programming Languages G22.2110-001 Walter Williams

G22.2110-001G22.2110-001 1111

Syntax & SemanticsSyntax & Semantics

SyntaxSyntax Deals with FormDeals with Form Gives structure to a stream of lexical elementsGives structure to a stream of lexical elements

SemanticsSemantics Deals with meaningDeals with meaning Meaning often depends on contextMeaning often depends on context

Both syntax and semantics can be represented by Both syntax and semantics can be represented by grammars – grammars – attribute grammarsattribute grammars are used for are used for semantics.semantics.

Distinction is somewhat artificialDistinction is somewhat artificial Syntax is that which can be conveniently expressed Syntax is that which can be conveniently expressed

using a context free grammarusing a context free grammar Semantics is everything elseSemantics is everything else

Page 12: Programming Languages G22.2110-001 Walter Williams

G22.2110-001G22.2110-001 1212

Language and GrammarLanguage and Grammar

An Alphabet An Alphabet ΣΣ is a finite set of lexical symbolsis a finite set of lexical symbols Formal languages use letters of the alphabet as lexical Formal languages use letters of the alphabet as lexical

symbolssymbols Programming languages use TokensProgramming languages use Tokens L systems use lines to draw realistic images of trees and L systems use lines to draw realistic images of trees and

flowersflowers Language Language LL is a subset of strings in is a subset of strings in ΣΣ** A grammar A grammar GG defines the subset of defines the subset of ΣΣ* that * that

belongs to L, belongs to L, andand excludes the subset that does excludes the subset that does not belong to L.not belong to L.

A grammar can be used to generate new strings A grammar can be used to generate new strings in L in L oror to accept (or reject) strings in (or not it) L. to accept (or reject) strings in (or not it) L.

Page 13: Programming Languages G22.2110-001 Walter Williams

G22.2110-001G22.2110-001 1313

CFG ExampleCFG Example

Block:Block:{{ BlockStatements BlockStatementsopt opt }}

BlockStatements:BlockStatements:BlockStatementBlockStatementBlockStatements BlockStatementBlockStatements BlockStatement

BlockStatement:BlockStatement:LocalVariableDeclarationStatementLocalVariableDeclarationStatementStatementStatement

LocalVariableDeclarationStatement:LocalVariableDeclarationStatement:LocalVariableDeclaration LocalVariableDeclaration ;;

LocalVariableDeclaration:LocalVariableDeclaration:TypeName VariableDeclaratorIdTypeName VariableDeclaratorId

Statement:Statement:while ( while ( exprexpr ) ) BlockStatement ; BlockStatement ;

Page 14: Programming Languages G22.2110-001 Walter Williams

G22.2110-001G22.2110-001 1414

Context Free GrammarsContext Free Grammars Substitution Rules of the form: Substitution Rules of the form: AA ::= ::= ωω

where where AA is a Non-Terminal symbol and is a Non-Terminal symbol and ωω is a string of is a string of terminal and non-terminal symbolsterminal and non-terminal symbols

A Simple CFG for a language EA Simple CFG for a language E1.1. S ::= EXPR S ::= EXPR 2.2. S ::= EXPR SS ::= EXPR S3.3. EXPR ::= EXPR ‘+’ EXPREXPR ::= EXPR ‘+’ EXPR4.4. EXPR ::= EXPR ‘–’ EXPEXPR ::= EXPR ‘–’ EXP5.5. EXPR ::= ‘(‘ EXPR ‘)’ EXPR ::= ‘(‘ EXPR ‘)’ 6.6. EXPR ::= digit EXPR ::= digit

At least one rule must have only terminal At least one rule must have only terminal symbols on RHSsymbols on RHS

Every rule must have exactly one non-terminal Every rule must have exactly one non-terminal on LHSon LHS

Terminal Symbols: Terminal Symbols: digit digit + + –– ( ( ) ) Non-Terminal Symbols: Non-Terminal Symbols: EXPR EXPR SS Examples of statements in E: Examples of statements in E:

1 1 1+1 1+1 (1+1) - 1(1+1) - 1

Page 15: Programming Languages G22.2110-001 Walter Williams

G22.2110-001G22.2110-001 1515

Formal CFGFormal CFG A CFG, G, is a 4-tuple G = (A CFG, G, is a 4-tuple G = (ΣΣ, N, S, , N, S, δδ))

ΣΣ is an alphabet of terminal symbols is an alphabet of terminal symbols N is a set of non-terminal symbolsN is a set of non-terminal symbols S is a distinguished element of N, called S is a distinguished element of N, called

the start symbol, which represents all the start symbol, which represents all strings in the language.strings in the language.

δδ is a set of rules of the form is a set of rules of the formA ::= A ::= ωω | A | A N, N, ωω ( (ΣΣ, N), N)++

Page 16: Programming Languages G22.2110-001 Walter Williams

G22.2110-001G22.2110-001 1616

CFG IdiomsCFG Idioms

L ::= a L | L ::= a L | a a makes a list of one or more ‘a’smakes a list of one or more ‘a’s

L ::= a , L | a L ::= a , L | a makes a comma separated list makes a comma separated list of ‘a’sof ‘a’s

L ::= a L | L ::= a L | λλ makes a list of zero or more ‘a’s makes a list of zero or more ‘a’s λλ is a null symbol is a null symbol

L :: L L | a | L :: L L | a | λλ another way to make a listanother way to make a list

P ::= (P)P ::= (P) makes P’s within nested parenthesis makes P’s within nested parenthesis

of arbitrary depth. of arbitrary depth.

Page 17: Programming Languages G22.2110-001 Walter Williams

G22.2110-001G22.2110-001 1717

Backus-Naur Form (BNF)Backus-Naur Form (BNF) non-terminal symbols are non-terminal symbols are

identified by angle bracketsidentified by angle bracketse.g. <stmt>e.g. <stmt>

Terminal Symbols are token Terminal Symbols are token names or literal symbolsnames or literal symbols

““::=“ is definitional ::=“ is definitional equivalenceequivalence

‘‘|’ indicates “or”|’ indicates “or”

Many variationsMany variations [ ] for optional elements[ ] for optional elements Parentheses for groupingParentheses for grouping + and * (kleene star)+ and * (kleene star) Superscripts for Superscripts for nn occurances occurances Subscripts, Subscripts, optopt in Java in Java Italics or lowercase for Non-Italics or lowercase for Non-

terminal symbolsterminal symbols

<stmt> ::= while (<exp>) <stmt> ::= while (<exp>) <stmt> <stmt>

| if (<exp> ) <stmt> | if (<exp> ) <stmt> [else <stmt>][else <stmt>]

| id = EXP| id = EXP| <stmt_list> | <stmt_list> ;;

<stmt_list> ::= <stmt><stmt_list> ::= <stmt>| <stmt_list> <stmt> ;| <stmt_list> <stmt> ;

<exp> ::= <exp> <op> <exp> ::= <exp> <op> <exp><exp>

| ID | NUMBER;| ID | NUMBER;<op> ::= + | - | * | / ;<op> ::= + | - | * | / ;

Most language specifications Most language specifications use some variation of BNF use some variation of BNF

Page 18: Programming Languages G22.2110-001 Walter Williams

G22.2110-001G22.2110-001 1818

Derivation & Parse TreeDerivation & Parse Tree Parse tree represents structure of Parse tree represents structure of

parseparse Leaf nodes are terminal symbolsLeaf nodes are terminal symbols Intermediate nodes are non-terminal symbolsIntermediate nodes are non-terminal symbols Root node is start symbol of grammarRoot node is start symbol of grammar

Derivation tree also records which Derivation tree also records which rules were used to build treerules were used to build tree

Each node represents a specific productionEach node represents a specific production

ExampleExample (1 + 2 + 3 ) - 2(1 + 2 + 3 ) - 2

Page 19: Programming Languages G22.2110-001 Walter Williams

G22.2110-001G22.2110-001 1919

Grammars – Chomsky Grammars – Chomsky HierarchyHierarchy

Type 0 – Unrestricted Type 0 – Unrestricted Can express anything that can be computedCan express anything that can be computed Impossible to parseImpossible to parse

Type 1 – Context SensitiveType 1 – Context Sensitive Difficult to parseDifficult to parse Attribute Grammars used for programming language Attribute Grammars used for programming language

semanticssemantics Type 2 – Context FreeType 2 – Context Free

CFGs used for describing programming language syntax CFGs used for describing programming language syntax Type 3 – RegularType 3 – Regular

Used to describe lexical elements of programming Used to describe lexical elements of programming languageslanguages

Page 20: Programming Languages G22.2110-001 Walter Williams

G22.2110-001G22.2110-001 2020

Grammatical ProblemsGrammatical Problems Programming languages use restricted Programming languages use restricted

grammars, such as LL or LR, which are not grammars, such as LL or LR, which are not as powerful as general CFGsas powerful as general CFGs

Dangling Else – Not LR Dangling Else – Not LR shift reduce conflictshift reduce conflict S ::= if E then SS ::= if E then S S ::= if E then S else SS ::= if E then S else S

Solutions: Solutions: Always choose shiftAlways choose shift Specify endmarker e.g., Specify endmarker e.g., endifendif

Left Recursion – Not LL Left Recursion – Not LL AmbiguityAmbiguity

Foo(A)Foo(A) (in C) declaration or use of function Foo? (in C) declaration or use of function Foo? Requires lookahead in parser or more complex Requires lookahead in parser or more complex

grammargrammar

Page 21: Programming Languages G22.2110-001 Walter Williams

G22.2110-001G22.2110-001 2121

Programming Language Programming Language HistoryHistory

diagram-light.pdf