CSE 3341 Principles of Programming Languages Neelam Soundarajan

  • Slides: 17
Download presentation
CSE 3341 Principles of Programming Languages Neelam Soundarajan Computer Sc. & Eng. Dreese Labs

CSE 3341 Principles of Programming Languages Neelam Soundarajan Computer Sc. & Eng. Dreese Labs 579 e-mail: neelam@cse

Goals of the Course n Main Goal: n n Sub-Goals: n n n Discuss

Goals of the Course n Main Goal: n n Sub-Goals: n n n Discuss key concepts underlying PLs Alternative programming paradigms Implementation issues At end of course: Given a feature, you should be able to: n n n 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. CSE 3341/655; Part 1 2

Important Points n Text: The Scott book is very good but is also hard

Important Points n Text: The Scott book is very good but is also hard to read and contains *far* too much material. So: n n n Notes: n n n Use it as a reference Do NOT miss classes *You* are responsible for creating your own class notes The slides simply (usually) just list the bullet points to guide the class discussion. But you have to create your own notes Repeat: Slides are NOT class “notes” What we say in class overrides what you may find online; so when you do your homework assignments and answer questions in the exam etc. , they should be based on the class discussion and the text, not what you may have seen elsewhere Do NOT miss classes CSE 3341/655; Part 1 3

How do we study a language? n n n Syntax: What do legal programs

How do we study a language? n n n 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? CSE 3341/655; Part 1 4

Compilers Compiler M CL→M L P (in L) PM PM Output Input For P

Compilers Compiler M CL→M L P (in L) PM PM Output Input For P M CL→M : Compiler, in M, for translating from L to M CSE 3341/655; Part 1 5

Compilers (contd. ) M CL→M M CL’→M L CL’→M M CL’→L L CL’→L M

Compilers (contd. ) M CL→M M CL’→M L CL’→M M CL’→L L CL’→L M CL’→L P PL L’ M CL→M P PM L C E. g. : From C Eiffel→C to M C Eiffel→C CSE 3341/655; Part 1 6

Compilers: Intermediate Langs. n n n Identify a language I For each L, write

Compilers: Intermediate Langs. n n n Identify a language I For each L, write For each M, write I CL→I M CI→M New machines are easy to handle; New languages are easy to handle; Common intermediate language: C CSE 3341/655; Part 1 7

Interpreters Output From P M PL IL Data For P PL’ Data For P

Interpreters Output From P M PL IL Data For P PL’ Data For P I M L : Interpreter for L (written in M) L I L’ M IL CSE 3341/655; Part 1 Output From P 8

Compilers & Interpreters n n What is an assembler? A simulator? The JVM? JIT:

Compilers & Interpreters n n 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! CSE 3341/655; Part 1 9

Syntax n n n BNF (“Backus Normal Form”): Notation for describing syntax of languages

Syntax n n n 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. CSE 3341/655; Part 1 10

Derivation trees/Parse trees How do you derive “ 655” from this grammar? <no> <digit>

Derivation trees/Parse trees How do you derive “ 655” from this grammar? <no> <digit> 6 <digit> <no> 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-toright order. CSE 3341/655; Part 1 11

Example: Grammar of expressions <exp>: : = <no> | <id> | <exp> + <exp>

Example: Grammar of expressions <exp>: : = <no> | <id> | <exp> + <exp> | <exp> * <exp> <id> : : = X | Y | Z Parse tree for X + Y : <exp> + <exp> <id> X Y CSE 3341/655; Part 1 12

Grammar of expressions (contd. ) Parse tree for X + Y * Z: <exp>

Grammar of expressions (contd. ) Parse tree for X + Y * Z: <exp> + <id> <exp> X * <exp> <id> Y Z CSE 3341/655; Part 1 13

Grammar of expressions (contd. ) Another tree for X + Y * Z: <exp>

Grammar of expressions (contd. ) Another tree for X + Y * Z: <exp> * <id> <exp> + <exp> <id> X Y Z Which is the right tree? The grammar is ambiguous. CSE 3341/655; Part 1 14

Another grammar for expressions n n <exp> : : = <fac> | <fac> +

Another grammar for expressions n n <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? CSE 3341/655; Part 1 15

Grammars (contd. ) n n n Exercise: Make precedence left to right or right

Grammars (contd. ) n n n Exercise: Make precedence left to right or right to left Ambiguous grammar for numbers: <no> : : = <digit> | <no> Even the following is not a good grammar: <no> : : = <digit> | <digit> <no> Problem: Wrong semantics. Q: Can you fix it? CSE 3341/655; Part 1 16

Regular Expressions n CSE 3341/655; Part 1 17

Regular Expressions n CSE 3341/655; Part 1 17