1 / 190

Specification of Syntax

6/10/2012. CSE 655 Su'07. 2. Specification of Syntax. PL/0How the nesting of expression, term and factor in PL/0 work together and generate codeHow the nesting of recognition routines has the effect of static scopingProject questions and answersUML Use Case ModelingGeneral Problem of Describing SyntaxRecursive Descent ParsingAttribute GrammarsDescribing the Meanings of Programs: Dynamic Semantics.

hayden
Download Presentation

Specification of Syntax

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. 6/10/2012 CSE 655 Su'07 1 PL/0 and the 655 Project

    2. 6/10/2012 CSE 655 Su'07 2 Specification of Syntax PL/0 How the nesting of expression, term and factor in PL/0 work together and generate code How the nesting of recognition routines has the effect of static scoping Project questions and answers UML Use Case Modeling General Problem of Describing Syntax Recursive Descent Parsing Attribute Grammars Describing the Meanings of Programs: Dynamic Semantics

    3. 6/10/2012 CSE 655 Su'07 3 Simple Syntax & Processing Prerequisites 321, 560, 625 cover basics of processing simple programs 655 to advance and unify that understanding Wirth approach to describing syntax graphs as flow charts for programming a parser: recursive descent Textbook – Chapters Following slides should be a review

    4. 6/10/2012 CSE 655 Su'07 4 Syntax, semantics, language Syntax - the form or structure of the expressions, statements, and program units Semantics - the meaning of the expressions, statements, and program units Sentence - string of characters over some alphabet (maybe what are usually words) Language - set of sentences Lexeme - lowest level syntactic unit of a language (e.g., *, sum, begin) Token - category of lexemes (e.g., identifier)

    5. 6/10/2012 CSE 655 Su'07 5 Language (following Wirth) L = L ( T, N, P, S ) Vocabulary T of terminal symbols Set N of non-terminal symbols (grammatical categories) Set P of productions (syntactical rules) Symbol S (from N) called the start symbol Language is set of sequences of terminal symbols that can be generated (directly or indirectly (that’s his points 3 and 4)

    6. 6/10/2012 CSE 655 Su'07 6 Backus Normal Form (1959) Invented by John Backus to describe Algol 58 BNF is equivalent to context-free grammars A metalanguage is a language used to describe another language. In BNF, abstractions are used to represent classes of syntactic structures--they act like syntactic variables (also called nonterminal symbols) e.g. <while_stmt> -> while <logic_expr> do <stmt> This is a rule; it describes the structure of a while statement

    7. 6/10/2012 CSE 655 Su'07 7 Syntax rules A rule has a left-hand side (LHS) and a right-hand side (RHS), and consists of terminal and non-terminal symbols A grammar is a finite nonempty set of rules An abstraction (or non-terminal symbol) can have more than one RHS <stmt> -> <single_stmt> | begin <stmt_list> end Syntactic lists are described in BNF using recursion <ident_list> -> ident | ident, <ident_list> A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (all terminal symbols)

    8. 6/10/2012 CSE 655 Su'07 8 An example grammar <program> -> <stmts> <stmts> -> <stmt> | <stmt> ; <stmts> <stmt> -> <var> = <expr> <var> -> a | b | c | d <expr> -> <term> + <term> | <term> - <term> <term> -> <var> | const

    9. 6/10/2012 CSE 655 Su'07 9 An example derivation: <program> => <stmts> => <stmt> => <var> = <expr> => a = <expr> => a = <term> + <term> => a = <var> + <term> => a = b + <term> => a = b + const

    10. 6/10/2012 CSE 655 Su'07 10 Derivation explanation Every string of symbols in the derivation is a sentential form A sentence is a sentential form that has only terminal symbols A leftmost derivation is one in which the leftmost non-terminal in each sentential form is the one that is expanded A derivation may be neither leftmost nor rightmost Parse tree is a hierarchical representation of a derivation

    11. 6/10/2012 CSE 655 Su'07 11 Parsing – another view

    12. 6/10/2012 CSE 655 Su'07 12 Static Semantics Other information about the language not specified with the BNF Identifier length Maximum integer value Other restrictions on your compiler Symbol table size Code array size Specify these in your description of your language processor Recognize the restrictions you’ve implied

    13. 6/10/2012 CSE 655 Su'07 13 Unstated Assumptions Input program read top-to-bottom, left-to-right, with no backtracking Things declared before they are used No redefining at same level Inner declarations hidden by nesting Inner can locally hide outer declarations

    14. 6/10/2012 CSE 655 Su'07 14 Ambiguity & Right Recursive A grammar is ambiguous iff [if and only if] it generates a sentential form that has two or more distinct parse trees If we use the parse tree to indicate precedence levels of the operators, we cannot have ambiguity Operator associativity can also be indicated by a grammar <expr> -> <expr> + <expr> | const (ambiguous) <expr> -> <expr> + const | const (unambiguous) Left recursive (left associative) (recursive descent will require right recursive)

    15. 6/10/2012 CSE 655 Su'07 15 Extended BNF (abbreviations) Optional parts are placed in brackets ([]) <proc_call> -> ident [ ( <expr_list>)] Put alternative parts of RHSs in parentheses and separate them with vertical bars <term> -> <term> (+ | -) const Put repetitions (0 or more) in braces ({}) <ident> -> letter {letter | digit}

    16. 6/10/2012 CSE 655 Su'07 16 BNF / EBNF BNF: <expr> -> <expr> + <term> | <expr> - <term> | <term> <term> -> <term> * <factor> | <term> / <factor> | <factor> EBNF: <expr> -> <term> {(+ | -) <term>} <term> -> <factor> {(* | /) <factor>}

    17. 6/10/2012 CSE 655 Su'07 17 Syntax Graphs Put the terminals in circles or ellipses and put the non-terminals in rectangles; Connect with lines with arrowheads e.g., Pascal type declarations

    18. 6/10/2012 CSE 655 Su'07 18 Wirth’s Rules B1: Reduce system of syntax graphs to a few of reasonable size B2: Translate each graph to a procedure according to subsequent rules B3: Sequence of elements translates to begin T(S1); T(S2); … ; T(Sn) end or { T(S1); T(S2); … ; T(Sn) } procedure TSx(); begin TS1(); getsym(); TS2(); getsym(); end;

    19. 6/10/2012 CSE 655 Su'07 19 <term> -> <factor> {(* | /) <factor>} { Pascal comment} begin factor; while sym in [times, slash] do begin mulop := sym; getsym; factor; gen_proper_op; end; end;

    20. 6/10/2012 CSE 655 Su'07 20 <term> -> <factor> {(* | /) <factor>} void term() { factor(); /* parse the first factor*/ while (next_token == aster_code || next_token == slash_code) { lexical(); /* get next token */ factor(); /* parse the next factor */ } }

    21. 6/10/2012 CSE 655 Su'07 21 Recursive Descent Parsing Parsing - constructing a parse / derivation tree for a given input string Lexical analyzer is called by the parser A recursive descent parser traces out a parse tree in top-down order; it is a top-down parser Each non-terminal in the grammar has a subprogram associated with it; the subprogram parses all sentential forms that the nonterminal can generate The recursive descent parsing subprograms are built directly from the grammar rules Recursive descent parsers cannot be built from left-recursive grammars

    22. 6/10/2012 CSE 655 Su'07 22 PL/0 Program Structure Initialize keyword arrays, operator symbols, mnemonics, and so forth Initialize variables controlling scanning (getting the individual characters), lexical analysis (forming tokens), and parsing Call the <block> recognizing routine Note that block ends with a call to listcode Call the virtual machine interpreter Machine code kept in an array between phases Need to add to the output capabilities of PL/0

    23. 6/10/2012 CSE 655 Su'07 23 Blocks and Static Scoping Blocks are different than sequences of statements or compound statements Blocks can include declarations Sort of like a single use subprogram used and defined here Where can blocks appear? Ada: almost anywhere a statement could be Pascal: only as bodies of procedures Java: inner classes

    24. 6/10/2012 CSE 655 Su'07 24 Data Specific to a Procedure To be able to return from call Program address of its call (return address) Address of data segment of caller Keep in data segment of procedure as: RA (return address) & DL (dynamic link) Location of variables Relative address only (since memory dynamic) Displacement off base address of appropriate data segment (locally B register or by descending chain of static links) What does static scoping mean here?

    25. 6/10/2012 CSE 655 Su'07 25 Example of Static Scoping void a { local variable one void b { | local variable two | void c { | | local variable three | | // beginning of code for c | | reference one, two | | call b | } // end of c | // beginning of code for b | reference one, two | call c } // end of b // beginning of code for a call b } // end of a a ? b ? c ? b

    26. 6/10/2012 CSE 655 Su'07 26 Example of Static Scoping In a, one is local In b, two is local In b, one is a single static level out In c, three is local In c, two is a single static level out In c, one is double static levels out Then c calls b In b, one is still a single static level out

    27. 6/10/2012 CSE 655 Su'07 27 Block Recognition & Processing Block(level, symbolTableStartingIndex) Page 13, left <block> ::= <const_decl> <var_decl> <proc_decl> <statement_body> <proc_decl> ::= procedure <name> ; <block> ; Recognize inner block Block(currentLevel+1, currentSymbolTableIndex) Jump around decalrations tx0 := tx; table[tx0].adr:=cx; gen(jmp,0,0); ... code[table[tx0].adr].a:=cx; table[tx0].adr:=cx; statement(); gen(opr,0,0); {return}

    28. 6/10/2012 CSE 655 Su'07 28 Symbol Table and Static Scope Variable declaration – storage allocated by incrementing DX (data index) by 1 Initially DX is 3 to allocate space for the “block mark” (RA, DL, and SL) Symbol table (table) enter – “enter object into table” Nested in block which determines static scoping Recursive calls make table act like a stack position - “find identifier id in table” Linear search backward

    29. 6/10/2012 CSE 655 Su'07 29 Blocks and Scoping Nesting blocks does scope Restoring symbol table pointers makes symbol table work like stack Inner definitions lost to outer contexts Idea: make symbol table work like a tree (one branch along a tree looks like a stack)

    30. 6/10/2012 CSE 655 Su'07 30 PL/0 Virtual Machine Section 5.10 (page 6 of handout) Stack machine – primary data store is stack push, pop, insert or retrieve from within Operations on top of stack (add, test, etc.) Program store – array named code Unchanged during interpretation I: instruction register P: program address register Data store – array named S – stack

    31. 6/10/2012 CSE 655 Su'07 31 Example of Static Scoping (Repeat) void a { local variable one void b { | local variable two | void c { | | local variable three | | // beginning of code for c | | reference one, two | | call b | } // end of c | // beginning of code for b | reference one, two | call c } // end of b // beginning of code for a call b } // end of a a ? b ? c ? b

    32. 6/10/2012 CSE 655 Su'07 32 Example of Static Scoping (Repeat) In a, one is local In b, two is local In b, one is a single static level out In c, three is local In c, two is a single static level out In c, one is double static levels out Then c calls b In b, one is still a single static level out

    33. 6/10/2012 CSE 655 Su'07 33 Stack of PL/0 Machine (Fig. 5.7)

    34. 6/10/2012 CSE 655 Su'07 34 Data Specific to a Procedure (again) To be able to return from call Program address of its call (return address) Address of data segment of caller Keep in data segment of procedure as: RA (return address) & DL (dynamic link) Location of variables Relative address only (since memory dynamic) Displacement off base address of appropriate data segment (locally B register or by descending chain of static links) What does static scoping mean here?

    35. 6/10/2012 CSE 655 Su'07 35 PL/0 Code Generation (page 7) Addresses are generated as pairs of numbers indicating the static level difference and the relative displacement within a data segment. But how does the compiler figure this out? PL/0 code Other question: how does PL/0 handle forward references?

    36. 6/10/2012 CSE 655 Su'07 36 PL/0 Machine Commands LIT – load numbers (literals) onto the stack LOD – fetch variable values to top of stack STO – store values at variable locations CAL – call a subprogram INT – allocate storage by incrementing stack pointer (T) JMP - transfer of control (new program address - P) JPC - conditional transfer of control OPR - arithmetic and relational operators

    37. 6/10/2012 CSE 655 Su'07 37 More on PL/0 Code Generation fct = (lit, opr, lod, sto, cal, int, jmp, jpc); instruction = packed record f: fct; {function code} l: 0 .. levmax; {level} a: 0 .. amax {displacement address} end; procedure gen (x: fct; y, z: integer); begin code[cx].f := x; code[cx].l := y; code[cx].a := z; cx := cx + 1; end; procedure listcode; var i: integer; begin {list code generated for this bock} for i := cx0 to cx-1 do writeln(i, mnemonic[code[i].f]: 5, code[i].l : 3, code[i].a : 5) end;

    38. 6/10/2012 CSE 655 Su'07 38 PL/0 Interpreter t:=0; b:=1; p:=0; {initialize registers} S[1]:=0; s[2]:=0; s[3]:=0; (initialize memory} repeat {instruction fetch loop} i:=code[p]; p:=p+1; With i do case f of {decode instruction} lit: begin t:=t+1; s[t]:=a; end; opr: case a of 1: s[t]:= -s[t]; 2: begin t:=t-1; s[t]:= s[t] + s[t+1]; end; end; jmp: p:=a; sto: begin s[base(l)+a]:=s[t]; writeln(s[t]); t:=t-1; end; cal: begin {generate new block mark} s[t+1]:=base(l); s[t+2]:=b; s[t+3]:=p; b:=t+1; p:=a; end; end; until p=0; {not a good way to end}

    39. 6/10/2012 CSE 655 Su'07 39 Project Virtual Machine Can use the design of the PL/0 one Operations in PL/0 are integer oriented; you probably want to add to this Can also use other machine designs Hybrid approach – compiles to intermediate form, then interprets that Direct interpretation possible if clearly proposed Idea: add output whenever computation done Idea: build some messages in that could be output with new opr instructions

    40. 6/10/2012 CSE 655 Su'07 40 Programs Writing Programs Compilers basically take programs in one language and write programs in another Compiler-compilers take grammars and write compilers (second level program writers) Translation of ENBF straightforward: terminal ? specific recognizer non-terminal ? call to recognizing routine alternatives ? explicit initial characters loops ? graph following algorithms LEXX, YACC, other compiler tools Add in other stuff to the language & processor

    41. 6/10/2012 CSE 655 Su'07 41 Adding – Predefined (Variable) Names procedure “block” has 2 parameters: lev (the nesting level for the block) tx (starting index for the symbol table) The nested procedure “enter” is what puts symbols (variable names) into the symbol table Right-side page 14 of handout Initialize symbol table Make initial call of “block” non-zero table index Can initialize or do other things not normal in the user visible input language

    42. 6/10/2012 CSE 655 Su'07 42 Adding – New Operator Add to “getsym” to recognize new symbol Look in condition, expression, term, factor Is new operator parallel to one of those operators? Basically another option in code generation If not like existing operators, add new syntactic construct. New action – add to PL/0 machine Generate new instruction: gen(opr,0,14) (“square”) Implement new instruction functionality (page 14, left-side) 14: begin s[t] := s[t]*s[t]; end; Add it into list of mnemonics

    43. 6/10/2012 CSE 655 Su'07 43 Adding – Built-in Function Design new indicator for symbol table Put function name in symbol table Parser will recognize as defined name (there will be no way for user to put in) In “term” “if sym=ident then” i:=position(id); case kind of constant: variable: procedure: built-in: begin getsym {left paren}; expression; getsym {right paren}; gen (opr, 0, new-thing) end

    44. 6/10/2012 CSE 655 Su'07 44 Adding – Pre-defined Function Another approach Put entry into symbol table Make it a regular procedure Initialize the code array to represent the code that might have been generated Adding - New statement type Add new syntax into body of statement (page 12) Look at “call” as an example “Syntactic sugar”

    45. 6/10/2012 CSE 655 Su'07 45 How To Start on the Project Get your tokenizer working This is the getsym procedure of the Pascal version of PL/0 distributed in class Can also be done with classes in C++ and Java Read in sample programs in the language you’re trying to compile and output the tokens (with some other information) Benefits Written some programs in your language Can leave the output statements for debugging

    46. 6/10/2012 CSE 655 Su'07 46 UML – Use Case Modeling program actions from the user viewpoint e.g., directions for the grader of how to execute your program begin developing different aspects of the program and planning its eventual actions as soon as possible

    47. 6/10/2012 CSE 655 Su'07 47

    48. 6/10/2012 CSE 655 Su'07 48

    49. 6/10/2012 CSE 655 Su'07 49 655 Project Unified Software Development Process (Rational) Unified Modeling Language (UML) Only to begin understanding, not required to use Unified Process Inception Phase begin understanding the problem and what you might do Spiral Approach try to have some partial version at each stage Project Report - proposal - introductory part Risk analysis – small steps rather than being overwhelmed, some small test programs

    50. 6/10/2012 CSE 655 Su'07 50 Project Activities Language and program processing Input (lexical) scanning Grammars and recursive descent parsing Compiling to a virtual machine Virtual machine interpreter Some other approaches to compiling

    51. 6/10/2012 CSE 655 Su'07 51 Project Options Proposals required as a first step; you may want an alternative language or alternative techniques. Individual proposal or work as a group of two. Many resources on the Internet; if you want to use them, propose to do something that goes beyond them in a significant way.

    52. 6/10/2012 CSE 655 Su'07 52 Project Stages Proposal (preliminary write-up) for project By e-mail to grader Simple parser for simple imperative language To exercise submit process Simple interpreter (step that doesn't have to be turned in) Final complete project significant write-up & electronic submission No Other program in Lisp/Scheme

    53. 6/10/2012 CSE 655 Su'07 53 Test Input Your language, your implementation, you know the features and restrictions, therefore You supply the test input Tell the grader what she should expect when running the tests and why you chose what you did (show off this or that feature, exercise an error message, clever program in your language)

    54. 6/10/2012 CSE 655 Su'07 54 655 Project Options Encourage you to make this into something you’ll enjoy and be proud of Flexibility probably unusual Available resources (books, Internet, etc.): Acknowledge their use Do significant work of your own Many different backgrounds and interests My experience has been at detail levels after others have started and thought they were close to finished (the very large “Last 10%”)

    55. 6/10/2012 CSE 655 Su'07 55 PL/0 Virtual Machine Section 5.10 (page 6 of handout) Stack machine – primary data store is stack push, pop, insert or retrieve from within Operations on top of stack (add, test, etc.) Program store – array named code Unchanged during interpretation I: instruction register P: program address register Data store – array named S – stack

    56. 6/10/2012 CSE 655 Su'07 56 Data specific to a procedure To be able to return from call Program address of its call (return address) Address of data segment of caller Keep in data segment of procedure as: RA (return address) & DL (dynamic link) Location of variables Relative address only (since memory dynamic) Displacement off base address of appropriate data segment (locally B register or by descending chain of static links) What does static scoping mean here?

    57. 6/10/2012 CSE 655 Su'07 57 Example of Static Scoping void a { local variable one void b { | local variable two | void c { | | local variable three | | // beginning of code for c | | reference one, two | | call b | } // end of c | // beginning of code for b | reference one, two | call c } // end of b // beginning of code for a call b } // end of a a ? b ? c ? b

    58. 6/10/2012 CSE 655 Su'07 58 Example of static scoping In a, one is local In b, two is local In b, one is a single static level out In c, three is local In c, two is a single static level out In c, one is double static levels out Then c calls b In b, one is still a single static level out

    59. 6/10/2012 CSE 655 Su'07 59 Stack of PL/0 machine (Fig. 5.7)

    60. 6/10/2012 CSE 655 Su'07 60 Data specific to a procedure (again) To be able to return from call Program address of its call (return address) Address of data segment of caller Keep in data segment of procedure as: RA (return address) & DL (dynamic link) Location of variables Relative address only (since memory dynamic) Displacement off base address of appropriate data segment (locally B register or by descending chain of static links) What does static scoping mean here?

    61. 6/10/2012 CSE 655 Su'07 61 PL/0 code generation (page 7) Addresses are generated as pairs of numbers indicating the static level difference and the relative displacement within a data segment. But how does the compiler figure this out? PL/0 code

    62. 6/10/2012 CSE 655 Su'07 62 PL/0 machine commands LIT – load numbers (literals) onto the stack LOD – fetch variable values to top of stack STO – store values at variable locations CAL – call a subprogram INT – allocate storage by incrementing stack pointer (T) JMP - transfer of control (new program address - P) JPC - conditional transfer of control OPR - arithmetic and relational operators

    63. 6/10/2012 CSE 655 Su'07 63 Symbol table and static scope Variable declaration – storage allocated by incrementing DX (data index) by 1 Initially DX is 3 to allocate space for the “block mark” (RA, DL, and SL) Symbol table (table) enter – “enter object into table” Nested in block which determines static scoping Recursive calls make table act like a stack position - “find identifier id in table” Linear search backward

    64. 6/10/2012 CSE 655 Su'07 64 More on PL/0 code generation fct = (lit, opr, lod, sto, cal, int, jmp, jpc); instruction = packed record f: fct; {function code} l: 0 .. levmax; {level} a: 0 .. amax {displacement address} end; procedure gen (x: fct; y, z: integer); begin code[cx].f := x; code[cx].l := y; code[cx].a := z; cx := cx + 1; end; procedure listcode; var i: integer; begin {list code generated for this bock} for i := cx0 to cx-1 do writeln(i, mnemonic[code[i]].f: 5, code[i].l : 3, code[i].a : 5) end;

    65. 6/10/2012 CSE 655 Su'07 65 Programs writing programs Compilers basically take programs in one language and write programs in another Compiler-compilers take grammars and write compilers (second level program writers) Translation of ENBF straightforward: terminal ? specific recognizer non-terminal ? call to recognizing routine alternatives ? explicit initial characters loops ? graph following algorithms LEXX, YACC, other compiler tools

    66. 6/10/2012 CSE 655 Su'07 66 655 Project Options Encourage you to make this into something you’ll enjoy and be proud of Flexibility probably unusual Available resources (books, Internet, etc.): Acknowledge their use Do significant work of your own Many different backgrounds and interests My experience has been at detail levels after others have started and thought they were close to finished (the very large “Last 10%”)

    67. 6/10/2012 CSE 655 Su'07 67 CIS 655 Project & PL/0 Niklaus Wirth, Algorithms + Data Structures = Programs, 1976, Prentice-Hall (ISBN 0-13-022418-9) PL/0 subset of Pascal Illustrated the way the Pascal P-code compiler built http://www.cs.rochester.edu/u/www/courses/ 254/PLzero/guide/guide.html 655 Project (100 pts, 40% of total grade) Project proposal (10 pts for turning in, revisable) Parser Intermediate step (non-graded) (but 10 pts for turning in) Input in syntax of programming language you’re building a compiler/interpreter for Some kind of output, maybe with XML markup Develop your own test cases Freedom to make the project into something you’ll enjoy and be proud of

    68. 6/10/2012 CSE 655 Su'07 68 655 Project Unified Software Development Process (Rational) Unified Modeling Language (UML) Only to begin understanding, not required to use Unified Process Inception Phase begin understanding the problem and what you might do Spiral Approach try to have some partial version at each stage Project Report - proposal - introductory part Risk analysis – small steps rather than being overwhelmed, some small test programs

    69. 6/10/2012 CSE 655 Su'07 69 Project Activities Language and program processing Input (lexical) scanning Grammars and recursive descent parsing Compiling to a virtual machine Virtual machine interpreter Some other approaches to compiling

    70. 6/10/2012 CSE 655 Su'07 70 Project Options Proposals required as a first step; you may want an alternative language or alternative techniques. Individual proposal or work as a group of two. Many resources on the Internet; if you want to use them, propose to do something that goes beyond them in a significant way.

    71. 6/10/2012 CSE 655 Su'07 71 Project Stages Proposal (preliminary write-up) for project By e-mail to grader Simple parser for simple imperative language To exercise submit process Simple interpreter (step that doesn't have to be turned in) Final complete project significant write-up & electronic submission No Other program in Lisp/Scheme

    72. 6/10/2012 CSE 655 Su'07 72 655 Project Options Encourage you to make this into something you’ll enjoy and be proud of Flexibility probably unusual Available resources (books, Internet, etc.): Acknowledge their use Do significant work of your own Many different backgrounds and interests My experience has been at detail levels after others have started and thought they were close to finished (the very large “Last 10%”)

    73. 6/10/2012 CSE 655 Su'07 73 Other Project Questions An answer is probably in the PL/0 handout if you only knew where to look and what you were looking for OK to use LEX and YACC if you build a significant project on top of their use JLex: http://www.cs.princeton.edu/~appel /modern/java/JLex/ More Class Questions?

    74. 6/10/2012 CSE 655 Su'07 74 How to start on the project Get your tokenizer working This is the getsym procedure of the Pascal version of PL/0 distributed in class Can also be done with classes in C++ and Java Read in sample programs in the language you’re trying to compile and output the tokens (with some other information) Benefits Written some programs in your language Can leave the output statements for debugging

    75. 6/10/2012 CSE 655 Su'07 75 655 Su’03 Project Grading Pick between possible project options Write proposals Revisable First part of final report Work singly or in pairs Communicate with grader Written reports required Demonstration (after grader has time to read report) Submit code to be run directly by grader Project approximately 40% of overall grade

    76. 6/10/2012 CSE 655 Su'07 76 PL/0 program structure Initialize keyword arrays, operator symbols, mnemonics, and so forth Initialize variables controlling scanning (getting the individual characters), lexical analysis (forming tokens), and parsing Call the <block> recognizing routine Note that block ends with a call to listcode Call the virtual machine interpreter Machine code kept in an array between phases Need to add to the output capabilities of PL/0

    77. 6/10/2012 CSE 655 Su'07 77 Specification of Syntax PL/0 How the nesting of expression, term and factor in PL/0 work together and generate code How the nesting of recognition routines has the effect of static scoping Project questions and answers UML Use Case Modeling General Problem of Describing Syntax Recursive Descent Parsing Attribute Grammars Describing the Meanings of Programs: Dynamic Semantics

    78. 6/10/2012 CSE 655 Su'07 78 Syntax, semantics, language Syntax - the form or structure of the expressions, statements, and program units Semantics - the meaning of the expressions, statements, and program units Sentence - string of characters over some alphabet (maybe what are usually words) Language - set of sentences Lexeme - lowest level syntactic unit of a language (e.g., *, sum, begin) Token - category of lexemes (e.g., identifier)

    79. 6/10/2012 CSE 655 Su'07 79 Language (following Wirth) L = L ( T, N, P, S ) Vocabulary T of terminal symbols Set N of non-terminal symbols (grammatical categories) Set P of productions (syntactical rules) Symbol S (from N) called the start symbol Language is set of sequences of terminal symbols that can be generated (directly or indirectly (that’s his points 3 and 4)

    80. 6/10/2012 CSE 655 Su'07 80 Language definitions Who must use language definitions? Other language designers Implementors Programmers (the users of the language) Formal approaches to describing syntax: Recognizers - used in compilers Generators - approach we'll study

    81. 6/10/2012 CSE 655 Su'07 81 Backus Normal Form (1959) Invented by John Backus to describe Algol 58 BNF is equivalent to context-free grammars A metalanguage is a language used to describe another language. In BNF, abstractions are used to represent classes of syntactic structures--they act like syntactic variables (also called nonterminal symbols) e.g. <while_stmt> -> while <logic_expr> do <stmt> This is a rule; it describes the structure of a while statement

    82. 6/10/2012 CSE 655 Su'07 82 Syntax rules A rule has a left-hand side (LHS) and a right-hand side (RHS), and consists of terminal and non-terminal symbols A grammar is a finite nonempty set of rules An abstraction (or non-terminal symbol) can have more than one RHS <stmt> -> <single_stmt> | begin <stmt_list> end Syntactic lists are described in BNF using recursion <ident_list> -> ident | ident, <ident_list> A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (all terminal symbols)

    83. 6/10/2012 CSE 655 Su'07 83 An example grammar <program> -> <stmts> <stmts> -> <stmt> | <stmt> ; <stmts> <stmt> -> <var> = <expr> <var> -> a | b | c | d <expr> -> <term> + <term> | <term> - <term> <term> -> <var> | const

    84. 6/10/2012 CSE 655 Su'07 84 An example derivation: <program> => <stmts> => <stmt> => <var> = <expr> => a = <expr> => a = <term> + <term> => a = <var> + <term> => a = b + <term> => a = b + const

    85. 6/10/2012 CSE 655 Su'07 85 Derivation explanation Every string of symbols in the derivation is a sentential form A sentence is a sentential form that has only terminal symbols A leftmost derivation is one in which the leftmost non-terminal in each sentential form is the one that is expanded A derivation may be neither leftmost nor rightmost Parse tree is a hierarchical representation of a derivation

    86. 6/10/2012 CSE 655 Su'07 86 Ambiguity & Right Recursive A grammar is ambiguous iff [if and only if] it generates a sentential form that has two or more distinct parse trees If we use the parse tree to indicate precedence levels of the operators, we cannot have ambiguity Operator associativity can also be indicated by a grammar <expr> -> <expr> + <expr> | const (ambiguous) <expr> -> <expr> + const | const (unambiguous) Left recursive (left associative) (recursive descent will require right recursive)

    87. 6/10/2012 CSE 655 Su'07 87 Extended BNF (abbreviations) Optional parts are placed in brackets ([]) <proc_call> -> ident [ ( <expr_list>)] Put alternative parts of RHSs in parentheses and separate them with vertical bars <term> -> <term> (+ | -) const Put repetitions (0 or more) in braces ({}) <ident> -> letter {letter | digit}

    88. 6/10/2012 CSE 655 Su'07 88 BNF / EBNF BNF: <expr> -> <expr> + <term> | <expr> - <term> | <term> <term> -> <term> * <factor> | <term> / <factor> | <factor> EBNF: <expr> -> <term> {(+ | -) <term>} <term> -> <factor> {(* | /) <factor>}

    89. 6/10/2012 CSE 655 Su'07 89 Recursive Descent Parsing Parsing - constructing a parse / derivation tree for a given input string Lexical analyzer is called by the parser A recursive descent parser traces out a parse tree in top-down order; it is a top-down parser Each non-terminal in the grammar has a subprogram associated with it; the subprogram parses all sentential forms that the nonterminal can generate The recursive descent parsing subprograms are built directly from the grammar rules Recursive descent parsers cannot be built from left-recursive grammars

    90. 6/10/2012 CSE 655 Su'07 90 <term> -> <factor> {(* | /) <factor>} void term() { factor(); /* parse the first factor*/ while (next_token == ast_code || next_token == slash_code) { lexical(); /* get next token */ factor(); /* parse the next factor */ } }

    91. 6/10/2012 CSE 655 Su'07 91 Wirth’s Rules B1: Reduce system of syntax graphs to a few of reasonable size B2: Translate each graph to a procedure according to subsequent rules B3: Sequence of elements translates to begin T(S1); T(S2); … ; T(Sn) end or { T(S1); T(S2); … ; T(Sn) } procedure TSx(); begin TS1(); getsym(); TS2(); getsym(); end;

    92. 6/10/2012 CSE 655 Su'07 92 <term> -> <factor> {(* | /) <factor>} { Pascal comment} begin factor; while sym in [times, slash] do begin mulop := sym; getsym; factor; gen_proper_op; end; end;

    93. 6/10/2012 CSE 655 Su'07 93 Blocks and static scoping Blocks are different than sequences of statements or compound statements Blocks can include declarations Sort of like a single use subprogram used and defined here Where can blocks appear? Ada: almost anywhere a statement could be Pascal: only as bodies of procedures Java: inner classes

    94. 6/10/2012 CSE 655 Su'07 94 Data specific to a procedure To be able to return from call Program address of its call (return address) Address of data segment of caller Keep in data segment of procedure as: RA (return address) & DL (dynamic link) Location of variables Relative address only (since memory dynamic) Displacement off base address of appropriate data segment (locally B register or by descending chain of static links) What does static scoping mean here?

    95. 6/10/2012 CSE 655 Su'07 95 Example of Static Scoping void a { local variable one void b { | local variable two | void c { | | local variable three | | // beginning of code for c | | reference one, two | | call b | } // end of c | // beginning of code for b | reference one, two | call c } // end of b // beginning of code for a call b } // end of a a ? b ? c ? b

    96. 6/10/2012 CSE 655 Su'07 96 Example of static scoping In a, one is local In b, two is local In b, one is a single static level out In c, three is local In c, two is a single static level out In c, one is double static levels out Then c calls b In b, one is still a single static level out

    97. 6/10/2012 CSE 655 Su'07 97 Block recognition & processing Block(level, symbolTableStartingIndex) Page 13, left <block> ::= <const_decl> <var_decl> <proc_decl> <statement_body> <proc_decl> ::= procedure <name> ; <block> ; Recognize inner block Block(currentLevel+1, currentSymbolTableIndex) Jump around decalrations tx0 := tx; table[tx0].adr:=cx; gen(jmp,0,0); ... code[table[tx0].adr].a:=cx; table[tx0].adr:=cx; statement(); gen(opr,0,0); {return}

    98. 6/10/2012 CSE 655 Su'07 98 Symbol table and static scope Variable declaration – storage allocated by incrementing DX (data index) by 1 Initially DX is 3 to allocate space for the “block mark” (RA, DL, and SL) Symbol table (table) enter – “enter object into table” Nested in block which determines static scoping Recursive calls make table act like a stack position - “find identifier id in table” Linear search backward

    99. 6/10/2012 CSE 655 Su'07 99 Blocks and Scoping Nesting blocks does scope Restoring symbol table pointers makes symbol table work like stack Inner definitions lost to outer contexts Idea: make symbol table work like a tree (one branch along a tree looks like a stack)

    100. 6/10/2012 CSE 655 Su'07 100 PL/0 Virtual Machine Section 5.10 (page 6 of handout) Stack machine – primary data store is stack push, pop, insert or retrieve from within Operations on top of stack (add, test, etc.) Program store – array named code Unchanged during interpretation I: instruction register P: program address register Data store – array named S – stack

    101. 6/10/2012 CSE 655 Su'07 101 Stack of PL/0 machine (Fig. 5.7)

    102. 6/10/2012 CSE 655 Su'07 102 Data specific to a procedure (again) To be able to return from call Program address of its call (return address) Address of data segment of caller Keep in data segment of procedure as: RA (return address) & DL (dynamic link) Location of variables Relative address only (since memory dynamic) Displacement off base address of appropriate data segment (locally B register or by descending chain of static links) What does static scoping mean here?

    103. 6/10/2012 CSE 655 Su'07 103 PL/0 code generation (page 7) Addresses are generated as pairs of numbers indicating the static level difference and the relative displacement within a data segment. But how does the compiler figure this out? PL/0 code

    104. 6/10/2012 CSE 655 Su'07 104 PL/0 machine commands LIT – load numbers (literals) onto the stack LOD – fetch variable values to top of stack STO – store values at variable locations CAL – call a subprogram INT – allocate storage by incrementing stack pointer (T) JMP - transfer of control (new program address - P) JPC - conditional transfer of control OPR - arithmetic and relational operators

    105. 6/10/2012 CSE 655 Su'07 105 More on PL/0 code generation fct = (lit, opr, lod, sto, cal, int, jmp, jpc); instruction = packed record f: fct; {function code} l: 0 .. levmax; {level} a: 0 .. amax {displacement address} end; procedure gen (x: fct; y, z: integer); begin code[cx].f := x; code[cx].l := y; code[cx].a := z; cx := cx + 1; end; procedure listcode; var i: integer; begin {list code generated for this bock} for i := cx0 to cx-1 do writeln(i, mnemonic[code[i].f]: 5, code[i].l : 3, code[i].a : 5) end;

    106. 6/10/2012 CSE 655 Su'07 106 PL/0 Interpreter t:=0; b:=1; p:=0; {initialize registers} S[1]:=0; s[2]:=0; s[3]:=0; (initialize memory} repeat {instruction fetch loop} i:=code[p]; p:=p+1; With i do case f of {decode instruction} lit: begin t:=t+1; s[t]:=a; end; opr: case a of 1: s[t]:= -s[t]; 2: begin t:=t-1; s[t]:= s[t] + s[t+1]; end; end; jmp: p:=a; sto: begin s[base(l)+a]:=s[t]; writeln(s[t]); t:=t-1; end; cal: begin {generate new block mark} s[t+1]:=base(l); s[t+2]:=b; s[t+3]:=p; b:=t+1; p:=a; end; end; until p=0; {not a good way to end}

    107. 6/10/2012 CSE 655 Su'07 107 Project Virtual Machine Can use the design of the PL/0 one Operations in PL/0 are integer oriented; you probably want to add to this Can also use other machine designs Hybrid approach – compiles to intermediate form, then interprets that Direct interpretation possible if clearly proposed Idea: add output whenever computation done Idea: build some messages in that could be output with new opr instructions

    108. 6/10/2012 CSE 655 Su'07 108 Programs writing programs Compilers basically take programs in one language and write programs in another Compiler-compilers take grammars and write compilers (second level program writers) Translation of ENBF straightforward: terminal ? specific recognizer non-terminal ? call to recognizing routine alternatives ? explicit initial characters loops ? graph following algorithms LEXX, YACC, other compiler tools

    109. 6/10/2012 CSE 655 Su'07 109 Static Semantics Other information about the language not specified with the BNF Identifier length Maximum integer value Other restrictions on your compiler Symbol table size Code array size Specify these in your description of your language processor Recognize the restrictions you’ve implied

    110. 6/10/2012 CSE 655 Su'07 110 Test input Your language, your implementation, you know the features and restrictions, therefore You supply the test input Tell the grader what she should expect when running the tests and why you chose what you did (show off this or that feature, exercise an error message, clever program in your language)

    111. 6/10/2012 CSE 655 Su'07 111 “Submit” assignment goals keeping you on track checking out the submit process This is not a separately graded assignment, but do do it within the next week or so. The second part of the assignment is about checking out the submit process. I have set things up like I've done before, so I expect the following will work for most of you very easily; but there will be a few problems, so get them out of the way as soon as possible.

    112. 6/10/2012 CSE 655 Su'07 112 Adding to PL/0 Predefined variable names New operator Built-in function Pre-defined function New statement type

    113. 6/10/2012 CSE 655 Su'07 113 Unstated Assumptions Input program read top-to-bottom, left-to-right, with no backtracking Things declared before they are used No redefining at same level Inner declarations hidden by nesting Inner can locally hide outer declarations

    114. 6/10/2012 CSE 655 Su'07 114 Predefined (Variable) Names procedure “block” has 2 parameters: lev (the nesting level for the block) tx (starting index for the symbol table) The nested procedure “enter” is what puts symbols (variable names) into the symbol table Right-side page 14 of handout Initialize symbol table Make initial call of “block” non-zero table index Can initialize or do other things not normal in the user visible input language

    115. 6/10/2012 CSE 655 Su'07 115 New Operator Add to “getsym” to recognize new symbol Look in condition, expression, term, factor Is new operator parallel to one of those operators? Basically another option in code generation If not like existing operators, add new syntactic construct. New action – add to PL/0 machine Generate new instruction: gen(opr,0,14) (“square”) Implement new instruction functionality (page 14, left-side) 14: begin s[t] := s[t]*s[t]; end; Add it into list of mnemonics

    116. 6/10/2012 CSE 655 Su'07 116 Built-in Function Design new indicator for symbol table Put function name in symbol table Parser will recognize as defined name (there will be no way for user to put in) In “term” “if sym=ident then” i:=position(id); case kind of constant: variable: procedure: built-in: begin getsym {left paren}; expression; getsym {right paren}; gen (opr, 0, new-thing) end

    117. 6/10/2012 CSE 655 Su'07 117 Pre-defined function Another approach Put entry into symbol table Make it a regular procedure Initialize the code array to represent the code that might have been generated

    118. 6/10/2012 CSE 655 Su'07 118 New statement type Add new syntax into body of statement (page 12) Look at “call” as an example

    119. 6/10/2012 CSE 655 Su'07 119

    120. 6/10/2012 CSE 655 Su'07 120

    121. 6/10/2012 CSE 655 Su'07 121 What is Computer Science? Lots of parts and specialties Core of computer science: How programs developed How programs execute Programming – software engineering 655 (programming languages) is central Programming How would you illustrate basic programming? Really, how would you illustrate basic programming? HTML for formatting text JavaScript for beginning programming

    122. 6/10/2012 CSE 655 Su'07 122 What is 655 Prog. Languages About? Compare programming languages, but how? Vertical: Fortran, Cobol, Lisp, C, etc. Horizontal: loops, selection, subprograms Topics: object-oriented, event handling, concurrency Processing: syntax, semantics, compiler basics Note: textbook and course have some of all of these Fundamental programming & language concepts (my idea) Divide a program into pieces (e.g., subprograms, types, threads, tasks, classes, packages, modules, components) Controlled modification of the pieces (e.g., compilers, templates) Have pieces communicate (during development, building, and execution; e.g., names, linkages, parameters) Combine pieces into program (e.g., loaders, building tools) Know the pieces will work together (e.g., design, reviews, proofs) Decisions, decisions, decisions, . . . How to use the languages we have in appropriate ways How to use the languages we have in appropriate ways

    123. 6/10/2012 CSE 655 Su'07 123 Goal of CIS 655 New insight into the programming process and the languages and concepts we use. Knowledge of where we are and why. Anticipate and prepare for future programming.

    124. 6/10/2012 CSE 655 Su'07 124 CIS 655 Warning Adult Content Warning: F-word of programming languages: FORTRAN A-word: ALGOL LISP: Lots of Irritating Silly Parentheses Sesame Street’s Cookie Monster: C is for programming, that’s good enough for me. COBOL: Committee Originated Boloney America Demanded Ada Java: Just Another Version of Ada C#: (C++)++

    125. 6/10/2012 CSE 655 Su'07 125 Traditional Content of 655 Languages you didn’t see elsewhere (1 hr courses) Compiler basics (used to be separate course) History of computing (still some of that) Classic languages Lisp (Scheme), Algol (Pascal, Ada), Snobol, etc. Classic computing approaches Recursion Concurrency New topics Programming with interfaces Distributed computing, web applications, Web Services

    126. 6/10/2012 CSE 655 Su'07 126 Now – 2005 Programming languages have changed mainly because of capabilities in execution environment Graphical User Interfaces Networking inside the execution environment Operating system concurrency in prog. language Classes (objects) as an extension of types Exception handling Security (probably coming soon) Some changes for program structuring Packages Generics and templates Proof conditions Components ?

    127. 6/10/2012 CSE 655 Su'07 127 Programming Languages Teaching at Old Dominion University Usual course: Lisp Snobol APL Algol / Pascal Summer 1979 LISP MUMPS JOVIAL CMS-2 Ada How I got started with Ada (then for 16 years)

    128. 6/10/2012 CSE 655 Su'07 128 655 Summary – Su’05 PL/0 compiler for traditional languages Lisp and its interpreter – similarities XML/XSLT – string matching (declarative) Patterns in programming High-level: constructor, factory, façade, singleton Low-level: loop, selection, exit, exception Parameterized types – generics, templates Event-oriented programming, concurrency, distributed Environments – Eclipse and its extensions “Next generation” programs (Oracle HTML DB)

    129. 6/10/2012 CSE 655 Su'07 129 Plan and Expectations for Su’05 Classical languages and topics History, comparisons, Algol (and its influence) Basic control structures, data structures, types Binding, parameter passing Lisp and functional programming Compilation basics Instead of a separate course Object-oriented programming, UML Java, C#, J2EE, .NET Concurrency, event driven programming Distributed Computing RMI, CORBA, RPC, SOAP (XML Protocol) XML, XSLT, XML Schemas, etc. SOAP: Simple Object Access Protocol (SOAP) is a specification for an XML-based messaging service. XML-based messaging is fast becoming the industry-standard communication mechanism to support B2B integration. SOAP has been donated to the W3C as a Note and is being considered for the basis of XMLP. (2001-06-01) SOAP: Simple Object Access Protocol (SOAP) is a specification for an XML-based messaging service. XML-based messaging is fast becoming the industry-standard communication mechanism to support B2B integration. SOAP has been donated to the W3C as a Note and is being considered for the basis of XMLP. (2001-06-01)

    130. 6/10/2012 CSE 655 Su'07 130 Summer Quarter 2005 Plan Trying new things in a different order Implementation of programming languages Syntax of programming languages – prerequisites Possible Project - PL/0 handout (understanding large program) New style project assignment (presentations) – start early Traditional topics – middle part of quarter New stuff, OO, concurrency, event driven, distributed computing platforms, Java, C# Course Expectations Broad perspective on computing How programs are developed & executed Knowledge of alternatives in programming Information about traditional language processing concepts Some specifics about programming languages considered in the common knowledge domain of computer scientists Prepare for 50 more years of programming

    131. 6/10/2012 CSE 655 Su'07 131 Traditional 655 Programming Project(s) Hybrid compiler / interpreter for small language with C/Java-style syntax Transform a high-level language to low-level form Reasonable use of tools and software encouraged Primarily individual, some 2-person groups Build on what you already know (e.g., 560) Project done in stages Proposals in class, demonstrations, documentation Develop your own appropriate tests Software to CIS computers using “submit” command Possible alternatives may be proposed No Perl implementations (use C++ or Java)

    132. 6/10/2012 CSE 655 Su'07 132 How to Use the Textbook The textbook and web resources are very informative and cost effective Read, question, summarize Learn from reading Work on it in this class Bring book to class on days suggested Relate the author’s comments to class Formulate your own conclusions Test against discussions, experience, and experiments Learning from reading is essential to being a successful computer scientist and leader

    133. 6/10/2012 CSE 655 Su'07 133 How “655 with Mathis” Class Works Mathis usually talks a lot This term I want more student interaction Student should learn a lot I’ve tried to structure topics of interest Students investigate and share with class Goal: enable students to learn without the teacher I want to help you in this direction, not force it on you by default There will be a lot of change in the next 40 years Information on web site is frequently updated (primary method of class communication)

    134. 6/10/2012 CSE 655 Su'07 134 Do I have to come to class? I don’t usually take attendance, but . . . I notice who is here. Class attendance more important than students seem to realize. I think the topics I talk about are important. Lots of new material not covered in text. Class attendance is the most efficient way to learn the information in the text & course – your opportunity to ask questions. The projects and tests are discussed in class. Office hours and e-mail important, but not a substitute for class attendance. I’ll miss you.

    135. 6/10/2012 CSE 655 Su'07 135 Relationship of 655 to Other Courses 560 (prerequisite) Classical system software and group projects 625 (prerequisite) 660 Operating Systems (when I have taught it) 3 credit hours Homework problems Little programming 655 Programming Languages 4 credit hours No specific homework (but text questions on tests) Lots of reading Lots of programming Not as easy as the instructor makes it seem

    136. 6/10/2012 CSE 655 Su'07 136 What 655 Student’s Should Think About Mathis has ideas React to them Expand on them What other example could be used to illustrate a similar point? What other points might be illustrated with this example? Mathis probably hasn’t seen all there is in an example – how could you expand it to make it richer / clearer 655 students should investigate on their own and contribute in class

    137. 6/10/2012 CSE 655 Su'07 137 Students in 655 Question what’s going on in course Instructor doesn’t know everything Students coming with very different backgrounds Develop a questioning, analytic attitude Goal – be able to get along (learn) without the teacher How to learn something by yourself from “books” Learning / problem solving strategies Techniques Divide and conquer – composition Outlines, spider diagrams UML modeling – classes, interfaces, state diagrams, …

    138. 6/10/2012 CSE 655 Su'07 138 What You Should Have Learned CIS 221, 222, 321 sequence System thinking Mathematical modeling Compositional reasoning Single-point control over change Making programs work, debugging, design Examining and learning from your own work Professors tend to think of class programming problems as very easy – not necessarily hard enough to illustrate the techniques necessary when doing large projects – think beyond Self examination and learning from your own mistakes is very important. Learning from others’ mistakes is even better. Self examination and learning from your own mistakes is very important. Learning from others’ mistakes is even better.

    139. 6/10/2012 CSE 655 Su'07 139 What You Should Learn Language influences thought process Possible to think beyond the language Use the language the way others do Use the language differently than others do Fortran & C programming styles will always be with us Object-oriented approaches originated in earlier langs. Patterns show language weaknesses and future directions for language enhancements Programming is about communicating with other humans – machines can figure it out.

    140. 6/10/2012 CSE 655 Su'07 140 What 655 Student’s Should Think About Mathis has ideas React to them Expand on them What other example could be used to illustrate a similar point? What other points might be illustrated with this example? Mathis probably hasn’t seen all there is in an example – how could you expand it to make it richer / clearer 655 students should investigate on their own and contribute in class

    141. 6/10/2012 CSE 655 Su'07 141 Students in 655 Question what’s going on in course Instructor doesn’t know everything Students coming with very different backgrounds Develop a questioning, analytic attitude Goal – be able to get along (learn) without the teacher How to learn something by yourself from “books” Learning / problem solving strategies Techniques Divide and conquer – composition Outlines, spider diagrams UML modeling – classes, interfaces, state diagrams, …

    142. 6/10/2012 CSE 655 Su'07 142 Informal 655 Pretest a) Give a BNF grammar for arithmetic expressions on identifiers (including binary operators +, -, *, / with the normal precedence and associativity relations and parenthesized sub expressions). b) Show a parse tree for the expression (A + B - (C * D)). Give three advantages that data type declarations offer when included in a programming language. The program SORT sorts an array X into ascending order. The number of elements in X is N. Using the first order predicate calculus, give a predicate that describes X after SORT is done. How do the uses of a symbol table in an assembler, a compiler, and a linking loader differ? Give the HTML code to put a simple button on an input form. [HTML, forms, JavaScript, ASP, JSP, CGI, etc.]

    143. 6/10/2012 CSE 655 Su'07 143 Revised Content of 655 My version for summer 2004 See schedule and descriptions Multi-lingual web application HTML, JavaScript, XML, XSLT, Java, SQL; J2EE framework (.NET has similarities) Interfaces, contexts, and environments Open source examples and tools What the languages are today Where the ideas came from Background context of co-workers Where things should be going in programming

    144. 6/10/2012 CSE 655 Su'07 144 Software Development Steps Narrative requirements from the users Requirements analysis to be sure needs are well understood Use cases – user perspective Use case analysis from the development perspective Use case analysis from a testing perspective Identification of boundary classes in the design Determination of business rules and logic Design of supporting classes rest of the design and development process

    145. 6/10/2012 CSE 655 Su'07 145 History of Programming Languages Pyramids – priests kept the secrets of geometry Plato’s Academy – no royal road to geometry Mathematics and computation not easy – (think Roman numerals and an abacus) Computational Tableau – Exchequer – Treasury (break computation into steps others could do) (spreadsheets are what we would call them now) Ways of describing the steps – languages With automated processing of languages, what else could they do? higher-level? closer to the problem? provably correct? automatically generated?

    146. 6/10/2012 CSE 655 Su'07 146 Starting With a Programming Language How to get a simple program to execute “Hello World” Basic control flow: iteration, selection, subprograms, exceptions Input / Output / Files / Database usually left out of discussions and comparisons Naming: variables, visibility, scope, lifetime, grouping, name spaces, protection, checking, interfaces Extensions to the language new operators, new statements, structures, patterns in language or development environment? Other control flow Searching, sorting, concurrency, distributed execution

    147. 6/10/2012 CSE 655 Su'07 147 Understanding a Language More than just the basics of vocabulary How people use the language polite, idioms, slang (221 Resolve v. hacking) in Paris: “Je pense” Some rhetorical styles since before Aristotle (remember Socrates from Bill & Ted) (Socrates, Plato, Aristotle, Alexander, Ptolemy) Common stories and examples part of a culture World view: sunrise, future is behind us How a language works Make your own language (or make a language your own) Every program is an interpreter

    148. 6/10/2012 CSE 655 Su'07 148 Understanding a Language Visit to the British Museum Family (daughters) for birthday trip to London Magna Carta – 1215, King John (Robin Hood’s Prince) Handle’s Messiah, song by John Lennon Rosetta Stone – 3 languages, Egyptian-Greek (Ptolemy V) Extensive collection of mummies (including Ramses II) What to learn from this Start with a few specific things to look for Look around for what else is interesting There is something to be learned everywhere There is always a point and something to be learned from the stories in this class

    149. 6/10/2012 CSE 655 Su'07 149 Languages and Processors Programming Languages View of Programming Every Computer Program is an interpreter of a special custom language How to handle input according to rules How to decide on special actions Chaining things together How to compose programs and ideas

    150. 6/10/2012 CSE 655 Su'07 150 Fallacy of Language Learning If you know one (programming) language, it is easy to learn another one. Hardly; think of your experience with French or Spanish or Latin or German or Chinese or Japanese or Hebrew or Arabic Even plumbing with water and gas is different. Certainly there are similarities, but also significant differences: in the languages and the problems to which they are applied

    151. 6/10/2012 CSE 655 Su'07 151 What To Do Lots of opportunities RESOLVE work at Ohio State, for example How to impact software development Elementary view of “advanced” topics Some of what I’m trying to do for courses Advanced views of elementary topics The kind of examples we typically use Create some: Books, articles, teaching strategies resource materials, distributable software

    152. 6/10/2012 CSE 655 Su'07 152 Programming Languages Big issue: Describe problems Describe constructive solutions How to solve problems? Reuse previous solutions? New environments bring ? new problems and (hopefully) ? new solutions If description was good enough, could a solution be automatic?

    153. 6/10/2012 CSE 655 Su'07 153 Revised Content of 655 My version for summer 2003 See schedule and descriptions Multi-lingual web application HTML, JavaScript, XML, XSLT, Java, SQL; J2EE framework (.NET has similarities) Interfaces, contexts, and environments Open source examples and tools What the languages are today Where the ideas came from Background context of co-workers Where things should be going in programming

    154. 6/10/2012 CSE 655 Su'07 154 Summary of Day 1 Course administration (web site, attend class) What is computer science Development & execution of programs Central role of programming languages (and 655) Traditional content of CIS 655 History of programming languages Features / concepts of different languages Past is prologue, but where is future going? Learn by reading / individual study Carpe Mańana Carpe Download (seize the web)

    155. 6/10/2012 CSE 655 Su'07 155 Describing the Programming Process Traditional Input or edit the program text with a text editor Prepare or modify the program inputs Cause the program to execute View the results Repeat or modify steps above to obtain different or additional results How Else Might You Program? Select from Components Graphically or even in text style: Imperative (step by step directions) Declarative (describe what you want) Functional (like mathematical functions)

    156. 6/10/2012 CSE 655 Su'07 156 What is 655 About? Programming (broadly and positively) How people have written programs How they would like to write programs How we can support the program writing and execution process Reusable programs Object-oriented programming Design languages Operating systems (execution environments) Development environments

    157. 6/10/2012 CSE 655 Su'07 157 CIS 655 - First Day Discussion What programming language would you suggest for a high school student who wanted to learn something about programming? And why? Things to think about Resources to write & run programs Ability to do something useful and interesting Easy to understand concepts HTML and JavaScript is Mathis suggestion, 2002.12.12 HTML and JavaScript is Mathis suggestion, 2002.12.12

    158. 6/10/2012 CSE 655 Su'07 158 CIS 655 - Discussion Which is the best analogy to software and programming? House, apartment building Carpenter, plumber, electrician, painter Do-it-yourself person Real-estate agent Real-estate developer Architect Construction manager A program is like a ______ and the work of developing a program is like the work of a ______ and software in general is ______

    159. 6/10/2012 CSE 655 Su'07 159 Traditional Content of 655 Languages you didn’t see elsewhere (1 hr courses) Compiler basics (used to be separate course) History of computing (still some of that) Classic languages Lisp (Scheme), Algol (Pascal, Ada), Snobol, etc. Classic computing approaches Recursion Concurrency New topics Programming with interfaces Distributed computing, web applications, Web Services

    160. 6/10/2012 CSE 655 Su'07 160 Now – 2004 Programming languages have changed mainly because of capabilities in execution environment Graphical User Interfaces Networking inside the execution environment Operating system concurrency in prog. language Classes (objects) as an extension of types Exception handling Security (probably coming soon) Some changes for program structuring Packages Generics and templates Proof conditions Components ?

    161. 6/10/2012 CSE 655 Su'07 161 What is 655 Prog. Languages About? Compare programming languages, but how? Vertical: Fortran, Cobol, Lisp, C, etc. Horizontal: loops, selection, subprograms Topics: object-oriented, event handling, concurrency Processing: syntax, semantics, compiler basics Note: textbook and course have some of all of these Fundamental programming & language concepts (my idea) Divide a program into pieces (e.g., subprograms, types, threads, tasks, classes, packages, modules, components) Controlled modification of the pieces (e.g., compilers, templates) Have pieces communicate (during development, building, and execution; e.g., names, linkages, parameters) Combine pieces into program (e.g., loaders, building tools) Know the pieces will work together (e.g., design, reviews, proofs) Decisions, decisions, decisions, . . . How to use the languages we have in appropriate ways How to use the languages we have in appropriate ways

    162. 6/10/2012 CSE 655 Su'07 162 “Your Moment of Zen” A computer program is at the same time like a recipe and the food itself. Or the menu item and the recipe and the food itself are all the same and none of them are at all like computer programs. With apologies to the Daily Show `You are sad,' the Knight said in an anxious tone:  `let me sing you a song to comfort you.'   `You are sad,' the Knight said in an anxious tone:  `let me   `Is it very long?' Alice asked, for she had heard a good deal of poetry that day.   `It's  long,' said the Knight, `but very, VERY beautiful. Everybody that hears me sing it -- either it brings the TEARS into their eyes, or else -- '   `Or else what?' said Alice, for the Knight had made a sudden pause.   `Or else it doesn't, you know.  The name of the song is called "HADDOCKS' EYES."'   `Oh, that's the name of the song, is it?' Alice said, trying to feel interested.   `No, you don't understand,' the Knight said, looking a little vexed.  `That's what the name is CALLED.  The name really IS "THE AGED AGED MAN."'   `Then I ought to have said "That's what the SONG is called"?' Alice corrected herself.   `No, you oughtn't:  that's quite another thing!  The SONG is called "WAYS AND MEANS":  but that's only what it's CALLED, you know!'   `Well, what IS the song, then?' said Alice, who was by this time completely bewildered.   `I was coming to that,' the Knight said.  `The song really IS "A-SITTING ON A GATE":  and the tune's my own invention.' `You are sad,' the Knight said in an anxious tone:  `let me sing you a song to comfort you.'   `You are sad,' the Knight said in an anxious tone:  `let me   `Is it very long?' Alice asked, for she had heard a good deal of poetry that day.   `It's  long,' said the Knight, `but very, VERY beautiful. Everybody that hears me sing it -- either it brings the TEARS into their eyes, or else -- '   `Or else what?' said Alice, for the Knight had made a sudden pause.   `Or else it doesn't, you know.  The name of the song is called "HADDOCKS' EYES."'   `Oh, that's the name of the song, is it?' Alice said, trying to feel interested.   `No, you don't understand,' the Knight said, looking a little vexed.  `That's what the name is CALLED.  The name really IS "THE AGED AGED MAN."'   `Then I ought to have said "That's what the SONG is called"?' Alice corrected herself.   `No, you oughtn't:  that's quite another thing!  The SONG is called "WAYS AND MEANS":  but that's only what it's CALLED, you know!'   `Well, what IS the song, then?' said Alice, who was by this time completely bewildered.   `I was coming to that,' the Knight said.  `The song really IS "A-SITTING ON A GATE":  and the tune's my own invention.'

    163. 6/10/2012 CSE 655 Su'07 163 PL/0 Related Project Language of your own design Approximately same complexity of PL/0 Use the design of the PL/0 virtual machine Declarations, scope, etc. Know your design and document it Progress – current status Aug 6 – something substantial working Aug 20 - finished

    164. 6/10/2012 CSE 655 Su'07 164 XML/XSLT Related Project Write an event-oriented parser and use Use a parser to do XSLT action

    165. 6/10/2012 CSE 655 Su'07 165 Java StringTokenizer Example import java.io.*; import java.util.*; public class ExampleTokenizer { public static void main(String args[]) { try { BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream(args[0]))); String line = null; StringTokenizer st; while ((line=br.readLine()) != null ) { st = new StringTokenizer(line); while (st.hasMoreTokens()) System.out.println(st.nextToken()); } } catch (Exception e) { e.printStackTrace(); } } }

    166. 6/10/2012 CSE 655 Su'07 166 Software Development Steps Narrative requirements from the users Requirements analysis to be sure needs are well understood Use cases – user perspective Use case analysis from the development perspective Use case analysis from a testing perspective Identification of boundary classes in the design Determination of business rules and logic Design of supporting classes rest of the design and development process

    167. 6/10/2012 CSE 655 Su'07 167 UML Version

    168. 6/10/2012 CSE 655 Su'07 168 Java Code Version public interface Alt { public String alt(String string); } public interface Service { public String service(String string); } public class ExistingCode { public static String entryOne(String string) { return null; } // or something more useful public String entryTwo(String string) { return null; } // or something more useful } public class NewClass implements Alt, Service { public static void main(String[] args) { } // for unit testing public String alt(String string) { return ExistingCode.entryOne(string); } public String service(String string) { ExistingCode exCode = new ExistingCode(); return exCode.entryTwo(string); } }

    169. 6/10/2012 CSE 655 Su'07 169 Why Study PL/0 Need to look at large programs PL/0 is a classic Understand how Pascal (and Algol and Ada) works Local variables Recursion How recursive descent parsing works How typical language features added Code generation Working of a computer interpreter/emulator See how everything is brought together

    170. 6/10/2012 CSE 655 Su'07 170 Traditional 655 Programming Project(s) Hybrid compiler / interpreter for small language with C/Java-style syntax Transform a high-level language to low-level form Reasonable use of tools and software encouraged Primarily individual, some 2-person groups Build on what you already know (e.g., 560) Project done in stages Proposals in class, demonstrations, documentation Develop your own appropriate tests Software to CIS computers using “submit” command Possible alternatives may be proposed No Perl implementations (use C++ or Java)

    171. 6/10/2012 CSE 655 Su'07 171 Comparing Languages C Pascal No typing Strong typing { } grouping { } comments = assignment = comparison == comparison := assignment subprograms subprograms same level nested

    172. 6/10/2012 CSE 655 Su'07 172 Motivating Problems Business data processing (e.g., medical insurance) Sort by patient to produce individual bill Sort by insurance company for submission Sort by procedure for evaluation Sort by provider for payment Scientific / engineering calculation Evaluate an approximating polynomial for various values of the arguments Organizing and optimizing steps of computation Cryptographic analysis Apply transformation to input, search for patterns Play games with complicated rules Lots of possibilities and alternate strategies

    173. 6/10/2012 CSE 655 Su'07 173 New Problems and Environments Business record keeping on punched cards: Cobol Mathematical algorithms, repetitive calculations: Fortran (Algol, Pascal, Ada, C) Time sharing and teletype terminals: Basic Hope for artificial intelligence: Lisp, Prolog Database systems: SQL Large systems: Ada, C++ Graphical displays: Visual Basic Distributed networks: Java, C# What’s in the future? Widely available wireless Tablet computers

    174. 6/10/2012 CSE 655 Su'07 174 Role Of Programming Languages

    175. 6/10/2012 CSE 655 Su'07 175 Change of Paradigm Thomas Kuhn, Structure of Scientific Revolutions New problems/situations for which previous solutions inadequate People are very slow to change; We still talk about the sun rising and setting (Apollo driving his chariot across the sky) Newtonian mechanics good enough except for nuclear physics, black holes, and getting on news Atomic theory of chemistry depends on knowing the difference between a compound and a mixture

    176. 6/10/2012 CSE 655 Su'07 176 Languages, Mathematics, Programming Abstraction – where and how we reason, then try to apply to “real” world problem “S” no longer “snake,” it represents the sound 5 can mean apples or oranges even though they are so different – incomparable When these all say basically the same thing: 3+7; 37+; +37; add(3,7); i=add_to_3(7); i=3, i+=7 Objects and functions: x.f(a) or f(x,a) Sameness in two different versions of a program Associativity, communativity, refactoring, reorganizing, moving things, interchanging – what can you do and what not?

    177. 6/10/2012 CSE 655 Su'07 177 How Programs Written Programming paradigms Early computational models Manually calculated spreadsheet Describing a pattern of computation How you organize a complicated process Plan and then delegate the work Make a table of a function – FORTRAN Report writing program - COBOL Loop over relevant stuff in DB or file Do computations along the way Write a calculated line at a time At the end of the loop, write out summaries

    178. 6/10/2012 CSE 655 Su'07 178 Naming and Dependencies Rather than refer to specific memory locations give them names and let the computer system assign them and keep track of them But this may introduce dependencies A program that might work for different types gets all tied up with them during compilation Various approaches for more flexibility during the program construction process (generics in Ada, templates in C++, parameterized types) Approaches for flexibility during execution factories and abstract factories. Naming Symbol tables, Binding, Scope, Naming, JNDI, Registry

    179. 6/10/2012 CSE 655 Su'07 179 Composition as a Programming Style Construction with components Levels of Separation for Flexibility Constructors, Factories, Abstract factories Containers to Hold Components In an operating system, task control block is the external connection for the container OS had allocated some memory space, loader puts the code there, TCB is interface between the OS and the program JavaBeans, J2EE – Enterprise Java Beans Primitive Operations Why should methods directly manipulate the internal representation versus calling sister methods to do it?

    180. 6/10/2012 CSE 655 Su'07 180 Programming With Components Software component is much more than just the source code Documentation Design information (JavaBean attributes) Deployment information (for Enterprise Java Beans) Object at execution time Object at program design / construction time

    181. 6/10/2012 CSE 655 Su'07 181 655 Project Ideas Small language like C or Pascal or Basic Mathis has used since 1971 in various forms Lisp / Scheme interpreter Very similar to other sections of 655 XML parser, XSLT processor Experimental but of current interest Project Tests Responsible for writing test cases Grader will review, not do Illustrate all the features you’re claiming Illustrate all your error checking Project you can describe with pride

    182. 6/10/2012 CSE 655 Su'07 182 About Project Options Target machine – PL/0 machine (easy to expand); Java Virtual Machine (JVM) has too many checks Parse 2 XML files treating one as data and the other as XSLT to manipulate first, output the result (slide formatter with links to next, previous, and so forth) Evolving write-up and software together PL/0 machine, Java events, UML & JVision

    183. 6/10/2012 CSE 655 Su'07 183 Java StringTokenizer Example import java.io.*; import java.util.*; public class ExampleTokenizer { public static void main(String args[]) { try { BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream(args[0]))); String line = null; StringTokenizer st; while ((line=br.readLine()) != null ) { st = new StringTokenizer(line); while (st.hasMoreTokens()) System.out.println(st.nextToken()); } } catch (Exception e) { e.printStackTrace(); } } }

    184. 6/10/2012 CSE 655 Su'07 184 Practical Philosophy Philosophy (love of knowledge) and knowledge is what a university education is about Topics in this course related to lots of other things – I’ll talk about a lot of different things, you may not understand everything, but my goal is to connect this course to other things you know so you’ll remember the important things Greek presentation style – logical, chronological Hebrew – historical reference Arabic (1000+1 Nights) – nested explanation (story) Mathis – hyperlinked connections University’s statement about a general / liberal / liberating education (next slide)

    185. 6/10/2012 CSE 655 Su'07 185 655 Project Ideas Small language like C or Pascal or Basic Mathis has used since 1971 in various forms Lisp / Scheme interpreter Very similar to other sections of 655 XML parser, XSLT processor Experimental but of current interest Project Tests Responsible for writing test cases Grader will review, not do Illustrate all the features you’re claiming Illustrate all your error checking Project you can describe with pride

    186. 6/10/2012 CSE 655 Su'07 186 PL/0 Related Project Language of your own design Approximately same complexity of PL/0 Use the design of the PL/0 virtual machine Declarations, scope, etc. Know your design and document it Progress – current status Aug 6 – something substantial working Aug 20 - finished

    187. 6/10/2012 CSE 655 Su'07 187 History of Programming Languages “The past is prologue.” – US National Archives History important Future is even more important How can we presume to teach you to program in a language that hasn’t even been thought of yet? Future will be built on current technology Transition described in terms currently known For example, C++ in terms of C Java in terms of C++ But that’s misleading Mix of syntax and concepts from various languages

    188. 6/10/2012 CSE 655 Su'07 188 Programming Paradigms Already Discussed Imperative Pattern Matching Functional Today Event-Driven Object-Oriented Coming Graphical Logic Declarative Concurrent Distributed

    189. 6/10/2012 CSE 655 Su'07 189 655 Project Ideas Small language like C or Pascal or Basic Mathis has used since 1971 in various forms Lisp / Scheme interpreter Very similar to other sections of 655 XML parser, XSLT processor Experimental but of current interest Project Tests Responsible for writing test cases Grader will review, not do Illustrate all the features you’re claiming Illustrate all your error checking Project you can describe with pride

    190. 6/10/2012 CSE 655 Su'07 190 About Project Options Target machine – PL/0 machine (easy to expand); Java Virtual Machine (JVM) has too many checks Parse 2 XML files treating one as data and the other as XSLT to manipulate first, output the result (slide formatter with links to next, previous, and so forth) Evolving write-up and software together PL/0 machine, Java events, UML & JVision

More Related