Title: CSE 3341 Principles of Programming Languages
1CSE 3341Principles of Programming Languages
- Neelam Soundarajan
- Computer Sc. Eng.
- Dreese Labs 579
- e-mail neelam_at_cse
2Goals of the Course
- Main Goal
- Discuss key concepts underlying PLs
- Sub-Goals
- Alternative programming paradigms
- Implementation issues
- At end of course Given a feature, you should
be able to - Decide whether you like it or not, and why
- Have an idea how to implement it
- Decide what kinds of problems it is suited for.
3How do we study a language?
- Syntax What do legal programs in L look like?
- Semantics What do the various instructions of L
do (when exec.)? - Programming Methodology How are you supposed to
use the features of L? For solving what kinds of
problems?
4Compilers
5Compilers (contd.)
6Compilers Intermediate Langs.
- Identify a language I
- For each L, write
- For each M, write
- New machines are easy to handle
- New languages are easy to handle
- Common intermediate language C
7Interpreters
8Compilers Interpreters
- What is an assembler?A simulator?The JVM?
- JIT Just-in-time compilation
- Running theme for the course Runtime versus
compile-time - "Interpreter interprets each line into binary
code which can be run on different platforms"
Not true!
9Syntax
- BNF (Backus Normal Form) Notation for
describing syntax of languages precisely. - Example Set of all non-negative
integers ltnogt ltdigitgt ltdigitgt
ltnogt ltdigitgt 0 1 2 3 4 5 6 7
8 9 - Set of all non-neg. nos. not starting with
0ltnlznogt ltnlzdigitgt ltnlzdigitgt
ltnlznogtltnlzdigitgt 1 2 3 4 5 6 7
8 9 ?? - lt, gt, , are reserved (meta)
symbolsltdigitgt, ltnogt Non-terminals 0, 1, 2,
... Terminals - To define a BNF grammar Specify terminal and
non-termnial symbols Define the production for
each non-terminal.
10Derivation trees/Parse trees
How do you derive 655 from this grammar?
ltnogt
(There is a bug in this tree!)
The string derived (or parsed) by the tree
Append together the labels at the leaves in
left-to- right order.
11Example Grammar of expressions
ltexpgt ltnogt ltidgt ltexpgt ltexpgt ltexpgt
ltexpgt ltidgt X Y Z
Parse tree for X Y
12Grammar of expressions (contd.)
Parse tree for X Y Z
ltexpgt
13Grammar of expressions (contd.)
Another tree for X Y Z
Which is the right tree? The grammar is
ambiguous.
14Another grammar for expressions
- ltexpgt ltfacgt ltfacgt ltexpgtltfacgt ltnogt
ltidgt ltnogt ltfacgt ltidgt ltfacgt - This grammar is not ambiguous
- Reintroduce ambiguity among s and s (but
not between and ) - Parenthesized expressions?
15Grammars (contd.)
- Exercise Make precedence left to right or
right to left - Ambiguous grammar for numbers ltnogt ltdigitgt
ltnogt ltnogt - Even the following is not a good grammar ltnogt
ltdigitgt ltdigitgt ltnogtProblem Wrong
semantics.Q Can you fix it?
16Grammars (contd.)
- Syntax graphs Pictorial representation of BNF
grammars. - Extended BNF (see Ch. 2.1.2 of book) ...
optional item ... repetition (0 or more
times) repetition (0 or more times)
repetition (1 or more times) - ltifgt if ltcondgt then ltstmtgt ltelse-ifgt
else ltstmtsgt end-ifltelse-ifgt elseIf
ltcondgt then ltstmtsgt