Programing Language Concepts: Exam 1
Comments
(*******************) (* Comment *) (*******************)
Axiom for assignment statements
(x = E): {Qx->E} x = E {Q}
Assignment
+, -, DIV, MOD, *, /, <, =, >, not, and, or
The unambiguous grammar for IF-THEN-ELSE statements.
<stmt> -> <matched> | <unmatched> <matched> -> if (<logic_expr>) <stmt>| a non-if statement <unmatched> -> if (<logic_expr>) <stmt> | if (<logic_expr>) <matched> else <unmatched>
Components of a grammar
A finite non-empty set of rules
Precondition
An assertion before a statement, states the relationships and constraints among variables that are true at that point in execution
Postcondition
An assertion following a statement
What is the dangling else problem?
An optional else clause in an if-then(-else) statement results in nested conditionals being ambiguous if a then s if b then s1 else s2
Example language for Systems Programming
C
Example language for Business Applications
COBOL
Const statements
CONST DECKSIZE = 52; HALFDECK = 26;
Reliability
Conformance to specifications
End a program
End.
Example language for Scientific Applications
Fortran
Example language for Web Software
HTML
Feature Multiplicity
Having more than one way to perform a particular operation
What is an ambiguous grammar?
If and only if it generates a sentential form that has two or more distinct parse trees
Exception handling
Intercept run-time errors and take corrective measures
Weakest Precondition
Is the least restrictive precondition that will guarantee the postcondition
Example language for Artificial Intelligence
LISP
Variable statements
VAR currentNode : CardType; deck : NodePtr; gamesWon : Integer; oneGame : Boolean;
Operator Overloading
Where different operators have different implementations depending on their arguments
Case
case c of '0' .. '9' : s := 'digit (0-9)'; 'a' .. 'z' : s := 'lowercase letter (a-z)'; 'A' .. 'Z' : s := 'uppercase letter (A-Z)'; '+' , '-' : s := 'sign (+ or -)'; end;
For loop
for i := 1 to sizeOfCut do begin RemoveTop(deck ,tempCard); InsertTop(half_a, tempCard); end;
Function
function first(onTable : NodePtr) : Suits; var suit : Suits; begin suit := onTable^.card.suit; end;
If-then-else
if i<13 then node^.card.suit := Club else if (i>12) and (i<26) then node^.card.suit := Diamond else if (i>25) and (i<39) then node^.card.suit := Heart else node^.card.suit := Spade;
Token
is a category of lexemes (e.g., identifier <id>)
Lexeme
is the lowest level syntactic unit of a language (e.g., *, sum, begin)
Procedure
procedure WriteCards(anystring : string; pile : NodePtr); var current : NodePtr; i : Integer; begin i := 1; writeln(anystring); current := pile; while current <> nil do begin WriteCard(current^.card); current := current^.next; i := i + 1; if i = 52 then break; end; end;
Input/Output
read, readln, write, writeln, assign, reset, rewrite, close
What are three approaches to building a lexical analyzer?
• Write a formal description of the tokens and use a software tool that constructs a table-driven lexical analyzer from such a description • Design a state diagram that describes the tokens and write a program that implements the state diagram • Design a state diagram that describes the tokens and hand-construct a table-driven implementation of the state diagram
Lexical Analysis
• is a pattern matcher for character strings • is a "front-end" for the parser • Identifies substrings of the source program that belong together (lexemes)
What are the reasons for studying concepts in programming languages?
• Increased ability to express ideas • Improved background for choosing appropriate languages • Increased ability to learn new languages • Better understanding of significance of implementation • Better use of languages that are already known • Overall advancement of computing
Context Free Grammar
• Language generators, meant to describe the syntax of natural languages? • Cannot describe all of the syntax of programming languages
Functional languages
• Main means of making computations is by applying functions to given parameters • LISP, Scheme
Markup/programming hybrid languages
• Markup languages extended to support some programming • JSTL, XSLT
Pure Interpretation
• No translation • Easier implementation of programs (run-time errors can easily and immediately be displayed) Slower execution (10 to 100 times slower than compiled programs) • Often requires more space • Rare for traditional high-level languages but not for with some Web scripting languages (e.g., JavaScript, PHP)
What is the complexity of parsing?
• Parsers that work for any ambiguous grammar are complex and inefficient O(n3) • Compilers use parsers that only work for a subset of all unambiguous grammars O(n)
BNF to describe the syntax
• Provides a clear and concise syntax description • The parser can be based directly on the BNF • Parsers based on BNF are easy to maintain
What is the criteria for evaluating programming languages?
• Readability • Writability • Reliability • Cost
What are some of the major language design trade-offs?
• Reliability vs. cost of execution -Java checks all references to arrays • Readability vs. Writability -allowing complex calculation • Writability (flexibility) vs. Reliability -C pointers are powerful but unreliable
Logic languages
• Rule-based (rules are specified in no particular order) • Prolog
What are the various programming domains?
• Scientific applications • Business applications • Artificial intelligence • Systems programming • Web Software
What are the two broad categories of parsers?
• Top down - produce the parse tree, beginning at the root • Bottom up - produce the parse tree, beginning at the leaves
Compilation
• Translate high-level program (source language) into machine code (machine language) • Slow translation, fast execution • Compilation process has several phases: - lexical analysis - syntax analysis - Semantics analysis
Aliasing
Presence of two or more distinct referencing methods for the same memory location
What are axiomatic semantics?
Rather than directly specifying the meaning of a program, axiomatic semantics specifies what can be proven about the program.
Most common top down parsing algorithms.
Recursive descent, LL parsers
Top down parsers
• Given a sentential form, xAα, the parser must choose the correct A-rule to get the next sentential form in the leftmost derivation, using only the first token produced by A • Algorithms: Recursive descent, LL parsers (table)
What are the various language categories?
• Imperative • Functional • Logic • Markup/programming hybrid
Pascal Heading
PROGRAM solitare(input, output);
Type statements
TYPE NodePtr = ^NodeType; Suits = (Club, Diamond, Heart, Spade); CardType = Record suit : Suits; rank : 1..13; end;
Type checking
Testing for type errors
Support for Abstraction
The ability to define and use complex structures or operations in ways that allow details to be ignored
Writeability
The ease with which a language can be used to create programs
Readability
The ease with which programs can be read and understood
Assertions
The logic expressions
Repeat loop
repeat sum := sum + number; number := number - 2; until number = 0;
Syntax
the form or structure of the expressions, statements, and program units
Semantics
the meaning of the expressions, statements, and program units
While loop
while current <> nil do begin WriteCard(current^.card); current := current^.next; end;
Hybrid Implementation Systems
• 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 • ex: Perl
Overall Simplicity
• A manageable set of features and constructs • Minimal feature multiplicity • Minimal operator overloading
Orthogonality
• A relatively small set of primitive constructs can be combined in a relatively small number of ways • Every possible combination is legal
Expressivity
• A set of relatively convenient ways of specifying operations • Strength and number of operators and predefined functions
NFA vs DFA
• An NFA is a Nondeterministic Finite Automaton. Nondeterministic means it can transition to, and be in, multiple states at once (i.e. for some given input). • A DFA is a Deterministic Finite Automaton. Deterministic means that it can only be in, and transition to, one state at a time (i.e. for some given input).
What is an attribute grammar?
• An attribute grammar is a context-free grammar 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 nonterminals in the rule • Each rule has a (possibly empty) set of predicates to check for attribute consistency
Backus-Naur Form (BNF)
• BNF is equivalent to context-free grammars • abstractions are used to represent classes of syntactic structures--they act like syntactic variables?
Imperative languages
• Central features are variables, assignment statements, and iteration • Include languages that support object-oriented programming • Include scripting languages • Include the visual languages • C, Java
Three major programming language implementation methods.
• Compilation • Pure Interpretation • Hybrid Implementation Systems
What have been the major influences in programming language design?
• Computer Architecture (von Neumann architecture) • Program Design Methodologies (object-oriented development)
The von Neumann Architecture
• Data and programs stored in memory • Memory is separate from CPU • Instructions and data are piped from memory to CPU • Basis for imperative languages
What are operational semantics?
• Describe the meaning of a program by executing its statements on a machine, either simulated or actual. The change in the state of the machine (memory, registers, etc.) defines the meaning of the statement • To use operational semantics for a high-level language, a VM is needed
What is a derivation of a grammar?
• Every string of symbols in a 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