Title: Programming Language Concepts CIS 280
1Programming Language Concepts (CIS 280)
- Elsa L Gunter
- NJIT
- Fall 2001
2Bindings of Program Elements
- The binding time is when that selection is made
3Binding Times
- Language definition time language syntax and
semantics - Language implementation time interpreter versus
compiler, aspects left flexible in definition,
set of available libraries
4Binding Times
- A binding of a program element is the selection
of a particular value for it from a set of
possible values - Compile time data layout, internal data
structures - Link time (load time) binding of values to
identifiers across program modules - Run time actual values assigned to non-constant
identifiers
5To class
- In expression x y 5 what bindings can you
identify, and when are they most likely made?
6Program Aspects
- Syntax what valid programs look like
- Semantics what valid programs mean what they
should compute - Compiler must contain both information
7 Major Phases of a Compiler
- Lex
- Break the source into separate tokens
- Parse
- Analyze phrase structure and apply semantic
actions, usually to build an abstract syntax tree - Semantic analysis
- Determine what each phrase means, connect
variable name to definition (typically with
symbol tables), check types
8Major Phases of a Compiler
- Translate to intermediate representation
- Instruction selection
- Optimize
- Emit final machine code
9Major Phases of a Compiler
Source Program
Lex
Relocatable Object Code
Instruction Selection
Tokens
Linker
Parse
Unoptimized Machine-Specific Assembly Language
Abstract Syntax
Machine Code
Semantic Analysis
Optimize
Optimized Machine-Specific Assembly Language
Symbol Table
Translate
Emit code
Intermediate Representation
Assembly Langague
Assembler
Modified from Modern Compiler Implementation in
ML, by Andrew Appel
10Example of Optimization
- Program code X Y Z W
- Load reg1 with Y
- Load reg2 with Z
- Add reg1 and reg2, saving to reg1
- Store reg1 to tmp
- Load reg1 with tmp
- Load reg2 with W
- Add reg1 and reg2, saving to reg1
- Store reg1 to X
- Eliminate two steps marked
11Language Syntax
- Syntax is the description of which strings of
symbols are meaningful expressions in a language - It takes more than syntax to understand a
language need meaning (semantics) too - Syntax is the entry point
12Features of a Good Syntax
- Readable
- Writeable
- Lack of ambiguity
- Suggestive of correct meaning
- Ease of translation
13Elements of Syntax
- Character set previously typically ASCII, now
often 64 character sets (UNICODE) - Keywords usually reserved
- Special constants cannot be assigned to
- Identifiers can be assigned to
- Operator symbols
- Delimiters (parenthesis, braces,brackets,)
- Blanks (aka white space)
14Elements of Syntax
- Expressions
- Type expressions
- Declarations
- Statements (in imperative languages)
- Subprograms (subroutines)
15Elements of Syntax
- Modules
- Interfaces
- Classes (for object-oriented languages)
- Libraries
16Formal Language Descriptions
- Regular expressions, regular grammars
- Context-free grammars, BNF grammars, syntax
diagrams - Finite state automata
- Whole family more of grammars and automata
covered in automata theory
17Grammars
- Grammars are formal descriptions of which strings
over a given character set are in a particular
language - Language designers write grammar
- Language implementers use grammar to know what
programs to accept - Language users use grammar to know how to write
legitimate programs
18Regular Expressions
- Start with a given character set a,
b, c - Build bigger regular expressions from smaller
ones - Each character is a regular expression
- It represents the set of one string containing
just that character
19Regular Expressions
- If x and y are regular expressions, then xy is a
regular expression - It represents the set of all strings made from
first a string in x then a string in y - If x and y are regular expressions, then x?y is a
regular expression - It represents the set of strings in either x or y
20Regular Expressions
- If x is a regular expression, then so is (x)
- It represents the same thing as x
- If x is a regular expression, then so is x
- It represents strings made from concatenating
zero or more strings from x - ?
- It represents the empty set
21Example Regular Expressions
- (0?1)1
- The set of all strings of 0s and 1s ending in
1, 1, 01, 11, - ab(a)
- The set of all strings of as and bs with
exactly one b - ((01) ?(10))
- You tell me
- Regular expressions (equivalently, regular
grammars) important for lexing, breaking strings
into recognized words
22Example Lexing
- Regular expressions good for describing lexemes
(words) in a programming language - Identifier (a ? b ? ? z ? A ? B ? ? Z) (a ?
b ? ? z ? A ? B ? ? Z ? 0 ? 1 ? ? 9 ? _ ?
) - Digit (0 ? 1 ? ? 9)
- Number (1 ? ? 9)(0 ? ? 9) ? (1 ? ? 9)(0
? ? 9) - Keywords if if, while while,
23Implementing Regular Expressions
- Regular expressions, regular grammars reasonable
way to generates strings in language - Not so good for recognizing when a string is in
language - Regular expressions which option to choose, how
many repetitions to make - Answer finite state automata