CMSC 331 Midterm 1 Review
APL
"A Programming Language"; algebra-like language with a special set of characters for writing complex programs as about formulas
Derivations in BNF
- A derivation applies rules repeatedly, starting from the start symbol and ending with terminal symbols. - It shows that the grammar generates the resulting sentence and belongs to the defined language.
C++
- A large and complex language, in part because it supports both procedural and OO programming - Rapidly grew in popularity, along with OOP
Backus Naur Form (BNF)
- Abstractions represent groups of syntactic structures.
Logic Languages
- Based on formal logic and rules. - Use facts and queries to derive conclusions. - Emphasize declarative programming. - Focus on what rather than how. Examples: Prolog, Jess.
Motivation for studying programming languages
- Better expression of programming concepts - Stronger foundation for selecting languages - Enhanced learning of new languages - Deeper understanding of implementation significance - Improved design skills for new languages - Mastery of diverse programming paradigms
Implementation Issue of Interpretation
- Complexity of compiler/interpreter - Translation speed - Code portability - Code compactness - Debugging ease
Dynamic Semantics
- Describes the behavior of a program during execution. - Focuses on how the program executes, including state changes and runtime evaluation. - Examples include how variables are assigned values and how functions are called.
Static Semantics:
- Describes the rules and constraints that can be checked at compile-time. - Focuses on the structure and correctness of the program without executing it. - Examples include type checking, variable declarations, and scope resolution.
C
- Designed for systems programming at Bell Labs by Dennis Richie and colleagues - Powerful set of operators, but poor type checking - came with a free compiler
Implementation Methods
- Direct execution by hardware - Compilation to another language - Interpretation: direct execution by software - Hybrid: compilation then interpretation - Just-in-time compilation
Procedural Languages
- Focus on a sequence of instructions. - Use procedures or functions. - Emphasize control flow (loops, conditionals). - Good for structured programming. Examples: C, Fortran.
Functional languages
- Focus on functions as first-class citizens. - Emphasize immutability and side-effect-free functions. - Support higher-order functions. - Use recursion over loops. Examples: Lisp, ML
Machine Code
- Initial computers were written in machine code
Context-Free Grammars
- Language generators, meant to describe the syntax of natural languages - Define a class of languages called context-free languages
Extended BNF
- Notation: A formal syntax notation for describing language grammar. - Enhancements: Extends BNF with additional syntax for clarity. - Operators: Includes operators like {} for repetition and [] for optionality. - Grouping: Uses () to group expressions for clearer structure. - Terminal Symbols: Distinguishes between terminal and non-terminal symbols.
Categories that affect Cost
- Programmer training - Software creation - Compilation - Execution - Compiler cost - Poor reliability - Maintenance
Evaluation Criteria
- Readability - Writability - Reliability - Cost
Factors that affect writability:
- Simplicity and orthogonality - Abstraction support - Expressivity - Fit for domain & problem
Factors that affect readability:
- Simplicity: too many features is bad - Orthogonality: few primitive constructs combinable in limited ways. i. Easy to learn and read. ii. Context-independent meaning - Control Statements - Data type and structures - Syntax considerations
Pascal
- Small, simple, efficient structures - For teaching program - Designed for teaching structured programming - Small, simple - Introduces some modest improvements, such as the case statement
Factors that affect reliability
- Type checking - Exception handling - Aliasing - Readability & writability
Object-oriented languages
- Use objects to represent data and behavior. - Key concepts: encapsulation, inheritance, polymorphism. - Promote code reuse and modularity. - Focus on real-world modeling. Examples: Java, C++, Smalltalk
Grammar
- a set of rules for strings in a formal language - rules describe how to form strings - grammar does not describe the meaning of the string: only form
Program Verification
- formally proving that the computer program does exactly what is stated in the program's specification Ex: ● Security and encryption ● Financial transactions
Noam Chomsky
- made fundamental contributions to mathematical linguistics and was motivated by the study of human languages - With Backus, independently came up with equivalent formalisms for specifying the syntax of a language
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 a preferred style
Ada
1. Packages - support for data abstraction 2. Exception handling - elaborate 3. Generic program units 4. Concurrency - through the tasking model 5. Included all that was then known about software engineering and language design
BNF to EBNF: <expr> -> <expr> + <term> | <expr> - <term> | <term>
<expr> -> <term> {(+ | -) <term>}
Aliasing
A circumstance where two or more variables refer to the same object.
Token
A classification for a common set of strings <identifier>, <number>, <operator>, <open paren>, etc.
Ambiguous Grammar
A grammar is ambiguous if and only if (iff) it generates a sentential form that has two or more distinct parse trees <e> -> <e> <op> <e> <e> -> 1 | 2 | 3 <op> -> + | - | * | / 1+2*3 can lead to two parse trees: 1+(2*3) and (1+2)*3
Parse Tree
A hierarchical representation of a derivation
Von Neumann architecture
A processor where data and instructions are stored in the same memory and accessed via buses.
Finite Languages
A simple language may have a finite number of sentences
First Programmer
Ada Lovelace
Basic parts and operators of a regular expression
Basic operations are (1) set union: one patter or another: letter | digit (2) concatenation: one pattern followed by another ( letter (letter | digit)* ) (3) Kleene closure: zero or more repetitions of a pattern (an asterisk) (4) parenthesis (5) naming patterns
Why are semantics important?
Capturing what a program in some programming language means is very difficult, so try your best; - To inform the programming language compiler/interpreter writer what it should do - To know that the compiler/interpreter did the right thing when it executed our code - To ensure the program satisfies its specification
Lexeme
Character sequence that matches pattern a token Ex. identifies (x, count, name, etc.), integers, and open parens ")"
Hybrid: compilation then interpretation
Compilation to another language (aka bytecode), then interpreted by a 'virtual machine', e.g., Java, Perl
Operational Semantics
Describing meaning of a PL construct using (1) simpler constructs or (2) an expression in another PL common; To use operational semantics for a high-level language, a virtual machine is needed
Recursion in BNF + example
Done through repetition <ident_list> ::= <ident> | <ident> , <ident_list>
Just-in-time compilation
Dynamically compile some bytecode to native code
Implementation Issue of Hybrid
Equally bad/good in these categories: - Complexity of compiler/interpreter - Translation speed - Execution Speed - Code portability - Code compactness - Debugging ease
Left associative vs right associative
Evaluated left to right vs right to left
Sentential Form
Every string of symbols in the derivation
Implementation Issue of Compilation
Execution speed
Readability
How easy is it to read and understand programs in the language?
Writability
How easy is it to write programs in the language?
Categories of programming languages
Imperative: - Procedural, - Object-oriented Declarative: - Functional - Logic
Imperative programming languages
Languages based on giving the computer commands or procedures to follow.
Declarative programming languages
Languages that declare or specify what properties a result should have, e.g. results will be based on functions.
Plankalkül
Meaning "plan calculus"; the first algorithmic programming language; Konrad Zuse developed Plankalkül to create theoretical foundations for general problem formulation.
What are non-terminals in a grammar?
Non-terminals are symbols in the left-hand side of a rule and represent abstractions in the language. They usually have angled brackets.
Other Evaluation Criteria
Portability, Generality, Well-definedness
Infinite Languages
Possessing the quality of an infinite number of sentences
A+B in postfix, prefix, and infix
Postfix: AB+ Prefix: +AB Infix: A+B
Attribute grammars (AGs)
Practical extension to BNF/CFGs allowing parse trees annotation with information needed for semantic processing Ex. BNF: <assign> -> <var> = <expr> <expr> -> <var> + <var> <var> -> id Attributes: actual_type - synthesized for <var> and <expr> expected_type - inherited for <expr>
Finite State Machine/Finite Automaton
Provides abstract way to define a process that ○ Has a finite set of states it can be in, with a special start state and a set of accepting states ○ Gets a sequence of inputs ○ Each input causes process to go from its current state to a new state (which might be the same!) ○ If after the input ends, we are in one of a set of accepting state, the input is accepted by the FA
Logical Implication
Relates truth between two statements.
Grammars: Four general types
Sentence - a string of characters over some alphabet Language - a set of sentences Lexeme - the lowest level syntactic unit of a language Token - a category of lexemes
What are terminals in a grammar?
Terminals are literal symbols that appear in a program and are not in any rule's left-hand side.
What does syntax refer to?
The form or structure of the expressions, statements, and program units
What does semantics refer to?
The meaning of the expressions, statements, and program units
Pattern
The rules which characterize the set of strings for a token ○ Typically defined via regular expressions
Operators
Things such as +, - , asterisk, etc.
Conditionals
constructs that allow for decision-making in programming languages; only run in certain contexts. ○ if x < 0 then x = -x ○ if x < 0 then x = -x else x = x+1
Regular expressions
define simple patterns: a sequence of characters that specifies a match pattern in text.; Simpler and less expressive than BNF
Java
designed for embedded systems, simplified C++, focused solely on object-oriented programming, replaced pointers with references for safety, and supported applets and concurrency, making it versatile for web and application development
Compilation to another language e.g.
e.g., C compiled to Intel Pentium 4 native machine language
Interpretation: direct execution by software e.g.
e.g., csh, Lisp, Python, JavaScript
Direct execution by hardware e.g.
e.g., native machine language
Axiomatic semantics
formal program verification; Define axioms and inference rules for each statement type in the language.
Von Neumann
led a team that built computers with stored programs and a central processor
Konrad Zuse
made the world's first binary digital computer, the Z1; completed first fully functional programming language in 1941
Leftmost Derivation in BNF
one in which the leftmost nonterminal in each sentential form is the one that is expanded in the next step
Rightmost Derivation in BNF
one in which the rightmost nonterminal in each sentential form is the one that is expanded in the next step
Algol
one of the most influential programming languages ever designed
preterminal symbol
one that appears as a LHS of rules, but in every case, the RHSs consist of single terminal symbol
Associativity
rules specify order to evaluate operators of same precedence level
Precedence
specify order in which operators of different precedence level are evaluated
COBOL
the first business-oriented language Contributions: - Hierarchical data structures (records) - Nested selection statements - Long names (up to 30 characters), with hyphens - Data Division - First language required by DoD; would have failed without DoD - Still the most widely used business applications language
Fortran
the first higher level programming language; for scientific, computational programming
LISP
the first language outside the von Neumann model; • Pioneered functional programming; One universal, recursive data type: the s-expression; used in AI research
weakest precondition
the least restrictive precondition that will guarantee the post-condition
Axiomatic semantics for sequence of statements
{P1}S{P2} {P2}S2{P3}
Axiomatic semantics for conditionals
{P} If x>0 then y=y-1 else y=y+1 {y>0} {B ∧ P} S1 {Q}, {¬B ∧ P} S2 {Q}/{P} if B then S1 else S2 {Q}
Axiomatic semantics for statements
{Qx->E} x := E {Q}