1 / 16

CSE 3341 Principles of Programming Languages

CSE 3341 Principles of Programming Languages. Neelam Soundarajan Computer Sc. & Eng. Dreese Labs 579 e-mail: neelam@cse. Goals of the Course. Main Goal: Discuss key concepts underlying PLs Sub-Goals: Alternative programming paradigms Implementation issues

freira
Download Presentation

CSE 3341 Principles of Programming Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. How do we study a language? • Syntax: What do legal programs in Llook like? • Semantics:What do the various instructions of Ldo (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. PL (in L) PM Compiler M M C C L→M L→M PM Output Input For P : Compiler, in M, for translating from L to M Compilers CSE 3341/655; Part 1

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

  6. I M C C L→I I→M Compilers: 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 CSE 3341/655; Part 1

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

  8. Compilers & 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! CSE 3341/655; Part 1

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

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

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

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

  14. Another grammar for expressions • <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.) • 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? CSE 3341/655; Part 1

  16. Grammars (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) • <if> ::= if <cond> then <stmt> {<else-if>} [else <stmts>] end-if;<else-if> ::= elseIf <cond> then <stmts> CSE 3341/655; Part 1

More Related