COP 4020 meeting -*- Outline -*- * Overall Tips for Learning Programming Languages ** implementation ------------------------------------------ WHAT TO SEARCH FOR? - implementation (compiler, interpreter) - possibly a programming environment - documentation Minimal environment: - A text editor (Emacs) ------------------------------------------ Some students think getting too much help (eclipse, visual studio) hampers learning. ** documentation *** what to find ------------------------------------------ WHAT DOCUMENTATION? - Language Reference Manual - syntax - types and type checking - semantics - Tutorial with examples When you read: - think about (check) examples - run examples - try things out on your own - learn strategy + tactics ------------------------------------------ ... strategy is big picture (overall design principles) tactics are smaller ideas (e.g., design patterns, code fragments) *** syntax ------------------------------------------ HOW COMPILERS WORK character stream | |"(define id (lambda (x) x)) ..." _______v____________ | Lexical Analysis | |__________________| | "(" token stream "define" | "id" _______v___________ | Parser | |___________________| | abstract syntax | tree | | \ _______v___________ | Static Checker | | | |___________________| id ... | annotated AST | _______v___________ | Code Generation | |___________________| | v object code ------------------------------------------ ... each of these phases has a different grammar - lexer : regular - parser : context-free - static checker : attribute (or context-sensitive) **** lexical grammar You don't need to pay attention to the details of this example, but only to the way that syntax is described. This is almost the grammar for Scheme, based on R5RS, but slightly simplified ------------------------------------------ LEXICAL (REGULAR) GRAMMAR EXAMPLE ::= * ::= | ::= a | b | c | d | ... | z | A | B | C | D | ... | Z ::= ! | $ | % | & | * | / | : | < | = | > | ? | ^ | _ ::= | | ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ::= + | . + | + . * ------------------------------------------ In a lexical grammar, spaces matter "::=" means "produces" or "can become", written "->" in modern grammar books "|" means "or" separates alternatives is a non-terminal A, B, etc. are a terminals Q: What are examples of s? dog, i Q: What are examples of s? X, Var ------------------------------------------ CONVENTIONS AND EXTENSIONS (EBNF) { } ::= | { } * ::= | * + ::= | + [ ] ::= | [ ] ... ::= | [ ]... ::= ------------------------------------------ **** syntax and context-free grammars ------------------------------------------ BNF (CONTEXT-FREE GRAMMAR) EXAMPLE ::= | | | | ::= ::= | | ::= #t | #f ::= ' | ( quote ) ::= | | ::= ( * ) ::= ( + ) ::= ( lambda ( * ) ) ::= ( if ) ------------------------------------------ Q: What are examples of s? X, i, 4020, (f x), (lambda (x) x), (if (> 3 x) y z) The Scheme report : ::= that isn't also a > Examples of keywords include "lambda" and "if"