PL Revision overview Course topics Parsing G = (S, P, NT, T); (E)BNF; recursive descent predictive parser (RDPP) Lexical analysis; Syntax and semantic errors; type checking Programming language structure Types + data objects + code objects Flow of control (FOC): sequence, decision, repetition Expression evaluation; assignment; parameters: in, out, in out Parameter passing semantics: value; reference; value return Run time system Run time stack; activation records; function calls; memory maps Logic programming: Prolog Functional Programming: LISP 1PS - Revision Notes
PL Revision course goals To understand all aspects of programming languages To introduce parsing concepts & grammars; ((E)BNF); CFGs To deepen your awareness of imperative/oo languages Structure (types; data; code; foc); language design aspects The relationship between the program text and run time system Parameters and parameter handling at run time To introduce new programming paradigms Logic + functional: Prolog & LISP To enable you to learn new programming languages quickly To encourage reflection about programming and languages PLs as a means of describing and manipulating models To move your programming to a higher level of abstraction 2PS - Revision Notes
PL Revision Grammar CFG Context Free Grammar; G = (S, P, NT, T) (E)BNF notation (Extended) Backus Naur (Normal) Form T set of terminal symbols (program text) NT set of non terminal symbols (LHS of Ps) P set of production rules NT sentential form Sentential form a sequence of Ts and NTs (RHS of Ps) Sentence a sentential form containing only Ts (program) S the start symbol (S is an NT) The grammar describes the SYNTAX of the language L(G) Used to write a Recursive Descent Predictive Parser (RDPP) Used to write the grammar using YACC or DCG (Prolog) 3PS - Revision Notes
PL Revision Syntax & Semantics SYNTAX relates to the grammatical structure of the language as defined by the grammar (and nothing else!) SEMANTICS refers to the meaning of the constructs in the language (rules of meaning for the language) examples: Variables must be declared before use ( symbol table) Variables may not be declared twice ( symbol table) All types in an expression must be the same ( type checking) In assignment the type of the LHS must be the same as the RHS In order to support semantic checks, additional information must be stored e.g. Symbol table: Operator table: name, type, rôle, size, address (offset) e.g. +: int, int int 4PS - Revision Notes
PL Revision Lexical analysis The Lexical Analyser (lexer) reads the source code for the program (a string) and identifies lexemes (sub strings) which are then mapped to the corresponding tokens. lexer: lexeme token e.g. keywords, identifiers, numbers, symbols Identifiers and numbers have many lexemes per token Keywords have one lexeme per token ( program / program) Keywords tokens is via the keyword table (not keyword ID) Other tokens are managed via the token table The lexer & these tables use pattern matching where a pattern is A literal string (tables) e.g. program, :=, +, A regular expression (lexer) e.g. alphanumeric strings [A Z,a z][a Z,a z,0 9]* e.g. numbers (integers) [0 9][0 9]* 5PS - Revision Notes
PL Revision Syntax analysis The Syntax Analyser (parser) receives a stream of tokens from the lexer and analyses these according to the grammar rules. The result is binary: syntax OK or syntax error. parser: tokens T F The parser may also attempt to identify semantic errors On syntax or semantic errors the parser may Print an error message Attempt to recover from the error For syntax errors, recovery implies one of two assumptions 1. A missing token in the input stream do nothing 2. An extra token in the input stream skip the token A syntax error may give rise to consequential errors On modern computers detailed messages are not required since the first few errors may be corrected and the code re compiled (usually faster) 6PS - Revision Notes
PL Revision PL structure Types: simple (atomic) types; compound (constructed user) Operations: predefined (+,, *, /, library); user defined (f ns ) Data Objects: literals; constants; variables; parameters Code Objects: program; module; method (f n ); (parameters) Bindings: the association of an object and an attribute Attributes: type; name; address; value; parameters; code Expressions: operations on data objects Assignment: binding of a value to a data object Flow of Control: sequence; decision; repetition (recursion) Declaration part: data objects and code objects Manipulation part: assignment; expressions; method calls 7PS - Revision Notes
PL Revision Parameters Formal parameter: IDENTIFIER Actual parameter: EXPRESSION Parameter modes: in, out, in/out Parameter passing: value; reference; result; value/result Formal/actual parameter binding: Value: fp = value(ap) on entry to f n Reference: fp = address(ap) on entry to f n Result: ap= value(fp) on exit from f n Value/result: fp= value(ap) on entry to f n ap = value(fp) on exit from f n 8PS - Revision Notes
PL Revision Run time system Memory: static; stack (activation record); heap Static memory: module & local static data objects; code Stack: local data objects (methods/f ns ) Heap: dynamically (user) allocated data objects Environment : Scope: environments in which object is visible Local: local data objects (methods/f ns ) Non local: non local data objects (methods/f ns ) Semantics: static: lexically enclosing environment dynamic: environment of previous call Symbol table: describes (map of) the activation record Activation record: parameters; local data objects; return value; return address; static pointer; dynamic pointer 9PS - Revision Notes
PL Revision Logic Prolog Logic language: facts + rules + resolution Rule: head : body. (order is important) Fact: rule with a head only Application areas: AI; DB; problem solving; parsing Parameters: in, out; last parameter is often result prog(a,d) : phead(a,b), var(b,c), stat(c,d). Flow of control: sequence; decision; recursion (tail) Example: Replace E1 with E2 in list L1 giving list L2 NB: list [H T]; tail recursion rep(e1, E2, [], []). ; empty stop rep(e1, E2, [E1 R1], [E2 R2]) : rep(e1, E2, R1, R2). ; replace rep(e1, E2, [F R1], [F R2]) : rep(e1, E2, R1, R2). ; no replace PS - Revision Notes 10
PL Revision Reading Prolog 1. Identify the predicates and whether these are built in or user defined predicates (facts or rules) 2. For each predicate identify whether the parameter is an input parameter or an output parameter. Often the output from one predicate is the input to the next predicate 3. NOTE: in a rule, the last parameter on the RHS often passes back the result to the last parameter on the LHS of the rules these parameters must have the same name 4. Look for cases of tail recursion 5. Look for cases where alternatives are presented (if thenelse; if then elseif else) these conditions often represent filters 6. Look for lists and their construction/deconstruction [H T] PS - Revision Notes 11
PL Revision functional LISP Functional language: syntax (+ 2 2) (f n arg 1 arg 2 ) Functions often nested: f n (f n 1 ( f 2 (f 1 ()))) ;;read inside out Higher order functions: f ns parameters / return values Flow of control: sequence; decision; recursion Lisp expression: Evaluated: e.g. (+ 2 2) 4 default Quoted (unevaluated): e.g. (+ 2 2) (+ 2 2) Evaluation order: NOT defined (i.e. parallel) Referential transparency: same args always same result Mapping functions: (mapcar # parser (file 1 file 2 )) Filters: count /find /remove if( not) Data objects: constructor; reader; writer PS - Revision Notes 12
PL Revision Reading Lisp Read Lisp programs inside out f n (f n 1 ( f 2 (f 1 ()))) f k : x y Recognising functions: defun, function (# ), funcall Functions taking functions as arguments mapcar, every, some optional parameters e.g. :test # < Recognising tail recursion in function bodies Recognising parameters + &rest, &key, &aux (let) Data structures: list, association list, property list, structure Constructor (make), readers, writers, predicates (xxx p) Predicates: listp, atom, null, endp, stringp, characterp, Filters: remove if( not), find if( not), count if (not) Recognising lists arguments to list functions [list, cons, append], [first, rest, second, last], length, member, PS - Revision Notes 13
PL Revision Exam technique 5 hours 300 minutes; 40 points 7.5 minutes/point 5p question 37,5 minutes ( reading/checking) 30 mins Read all the questions first! Order according to difficulty do the easiest first etc. Keep to time (discipline!) Prepare model answers in advance (read previous exams) Sketch your answer first (mentally or on paper) Answer questions in detail!!! (½ page is NOT enough) Answer the question asked!!! Use pictures and diagrams AND textual explanation PS - Revision Notes 14