CSE 3341 Principles of Programming Languages Neelam Soundarajan Computer Sc. & Eng. Dreese Labs 579...
-
Upload
clifton-kennedy -
Category
Documents
-
view
236 -
download
3
Transcript of CSE 3341 Principles of Programming Languages Neelam Soundarajan Computer Sc. & Eng. Dreese Labs 579...
![Page 1: CSE 3341 Principles of Programming Languages Neelam Soundarajan Computer Sc. & Eng. Dreese Labs 579 e-mail: neelam@cse.](https://reader036.fdocuments.us/reader036/viewer/2022081506/56649f205503460f94c3900f/html5/thumbnails/1.jpg)
CSE 3341Principles of Programming
Languages
Neelam Soundarajan Computer Sc. & Eng.
Dreese Labs 579e-mail: neelam@cse
![Page 2: CSE 3341 Principles of Programming Languages Neelam Soundarajan Computer Sc. & Eng. Dreese Labs 579 e-mail: neelam@cse.](https://reader036.fdocuments.us/reader036/viewer/2022081506/56649f205503460f94c3900f/html5/thumbnails/2.jpg)
CSE 3341/655; Part 1 2
Goals 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.
![Page 3: CSE 3341 Principles of Programming Languages Neelam Soundarajan Computer Sc. & Eng. Dreese Labs 579 e-mail: neelam@cse.](https://reader036.fdocuments.us/reader036/viewer/2022081506/56649f205503460f94c3900f/html5/thumbnails/3.jpg)
CSE 3341/655; Part 1 3
How 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?
![Page 4: CSE 3341 Principles of Programming Languages Neelam Soundarajan Computer Sc. & Eng. Dreese Labs 579 e-mail: neelam@cse.](https://reader036.fdocuments.us/reader036/viewer/2022081506/56649f205503460f94c3900f/html5/thumbnails/4.jpg)
CSE 3341/655; Part 1 4
Compilers
Compiler
M
L→MC
PL
(in L)PM
InputFor P
PM Output
M
L→MC : Compiler, in M, for translating from L to M
![Page 5: CSE 3341 Principles of Programming Languages Neelam Soundarajan Computer Sc. & Eng. Dreese Labs 579 e-mail: neelam@cse.](https://reader036.fdocuments.us/reader036/viewer/2022081506/56649f205503460f94c3900f/html5/thumbnails/5.jpg)
CSE 3341/655; Part 1 5
Compilers (contd.)M
L→MC
M
L’→MCL
L’→MC
M
L→MC
M
L’→LCL
L’→LC
M
L’→LC
PL’ PL
M
L→MC
PL PM
C
Eiffel→CC
M
Eiffel→CCE.g.: From to
![Page 6: CSE 3341 Principles of Programming Languages Neelam Soundarajan Computer Sc. & Eng. Dreese Labs 579 e-mail: neelam@cse.](https://reader036.fdocuments.us/reader036/viewer/2022081506/56649f205503460f94c3900f/html5/thumbnails/6.jpg)
CSE 3341/655; Part 1 6
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
Compilers: Intermediate Langs.
I
L→IC
M
I→MC
![Page 7: CSE 3341 Principles of Programming Languages Neelam Soundarajan Computer Sc. & Eng. Dreese Labs 579 e-mail: neelam@cse.](https://reader036.fdocuments.us/reader036/viewer/2022081506/56649f205503460f94c3900f/html5/thumbnails/7.jpg)
CSE 3341/655; Part 1 7
Interpreters
M
LI
L
L’IPL’
Data
For P
Output
From P
PL M
LI
Data
For P
Output
From P
LM
I : Interpreter for L (written in M)
![Page 8: CSE 3341 Principles of Programming Languages Neelam Soundarajan Computer Sc. & Eng. Dreese Labs 579 e-mail: neelam@cse.](https://reader036.fdocuments.us/reader036/viewer/2022081506/56649f205503460f94c3900f/html5/thumbnails/8.jpg)
CSE 3341/655; Part 1 8
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!
Compilers & Interpreters
![Page 9: CSE 3341 Principles of Programming Languages Neelam Soundarajan Computer Sc. & Eng. Dreese Labs 579 e-mail: neelam@cse.](https://reader036.fdocuments.us/reader036/viewer/2022081506/56649f205503460f94c3900f/html5/thumbnails/9.jpg)
CSE 3341/655; Part 1 9
BNF (“Backus Normal Form”): Notation for describing syntax of languages precisely.
Example: Set of all non-negative integers:<no> ::= <digit> | <digit> <no><digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 |7 | 8 | 9
Set of all non-neg. nos. not starting with 0:<nlzno> ::= <nlzdigit> | <nlzdigit> <nlzno><nlzdigit>::= 1 | 2 | 3 | 4 | 5 | 6 |7 | 8 | 9 ??
<, >, ::=, | are reserved (“meta”) symbols;<digit>, <no> : Non-terminals; 0, 1, 2, ...: Terminals
To define a BNF grammar: Specify terminal and non-termnial symbols;Define the production for each non-terminal.
Syntax
![Page 10: CSE 3341 Principles of Programming Languages Neelam Soundarajan Computer Sc. & Eng. Dreese Labs 579 e-mail: neelam@cse.](https://reader036.fdocuments.us/reader036/viewer/2022081506/56649f205503460f94c3900f/html5/thumbnails/10.jpg)
CSE 3341/655; Part 1 10
Derivation trees/Parse trees
How do you derive “655” from this grammar?
<no>
<no><digit>
<digit> <no>6
5 5
(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.
![Page 11: CSE 3341 Principles of Programming Languages Neelam Soundarajan Computer Sc. & Eng. Dreese Labs 579 e-mail: neelam@cse.](https://reader036.fdocuments.us/reader036/viewer/2022081506/56649f205503460f94c3900f/html5/thumbnails/11.jpg)
CSE 3341/655; Part 1 11
Example: Grammar of expressions
<exp>::= <no> | <id> | <exp> + <exp> | <exp> * <exp>
<id> ::= X | Y | Z
Parse tree for X + Y :
<exp>
<exp><exp>
<id><id>
X Y
+
![Page 12: CSE 3341 Principles of Programming Languages Neelam Soundarajan Computer Sc. & Eng. Dreese Labs 579 e-mail: neelam@cse.](https://reader036.fdocuments.us/reader036/viewer/2022081506/56649f205503460f94c3900f/html5/thumbnails/12.jpg)
CSE 3341/655; Part 1 12
Grammar of expressions (contd.)
Parse tree for X + Y * Z:
<exp>
<id>
X
<exp><exp> +
* <exp><exp>
<id>
Y
<id>
Z
![Page 13: CSE 3341 Principles of Programming Languages Neelam Soundarajan Computer Sc. & Eng. Dreese Labs 579 e-mail: neelam@cse.](https://reader036.fdocuments.us/reader036/viewer/2022081506/56649f205503460f94c3900f/html5/thumbnails/13.jpg)
CSE 3341/655; Part 1 13
Grammar of expressions (contd.)
Another tree for X + Y * Z:
<exp>
<id>
Z
<exp><exp> *
<exp><exp> +
<id>
X
<id>
Y
Which is the right tree?The grammar is ambiguous.
![Page 14: CSE 3341 Principles of Programming Languages Neelam Soundarajan Computer Sc. & Eng. Dreese Labs 579 e-mail: neelam@cse.](https://reader036.fdocuments.us/reader036/viewer/2022081506/56649f205503460f94c3900f/html5/thumbnails/14.jpg)
CSE 3341/655; Part 1 14
<exp> ::= <fac> | <fac> + <exp><fac> ::= <no> | <id>
| <no> * <fac> | <id> * <fac>
This grammar is not ambiguous
Reintroduce ambiguity among +’s and *’s (but not between + and *)
Parenthesized expressions?
Another grammar for expressions
![Page 15: CSE 3341 Principles of Programming Languages Neelam Soundarajan Computer Sc. & Eng. Dreese Labs 579 e-mail: neelam@cse.](https://reader036.fdocuments.us/reader036/viewer/2022081506/56649f205503460f94c3900f/html5/thumbnails/15.jpg)
CSE 3341/655; Part 1 15
Exercise: Make precedence left to right or right to left
Ambiguous grammar for numbers:<no> ::= <digit> | <no> <no>
Even the following is not a good grammar: <no> ::= <digit> | <digit> <no>
Problem: Wrong semantics.Q: Can you fix it?
Grammars (contd.)
![Page 16: CSE 3341 Principles of Programming Languages Neelam Soundarajan Computer Sc. & Eng. Dreese Labs 579 e-mail: neelam@cse.](https://reader036.fdocuments.us/reader036/viewer/2022081506/56649f205503460f94c3900f/html5/thumbnails/16.jpg)
CSE 3341/655; Part 1 16
**Replace this page** with a discussion of regular expressions
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)
<if> ::= if <cond> then <stmt>{<else-if>} [else <stmts>] end-if;
<else-if> ::= elseIf <cond> then <stmts>
Grammars (contd.)