PL Revision overview

Similar documents
CPS 506 Comparative Programming Languages. Syntax Specification

Lisp: Lab Information. Donald F. Ross

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

CS 415 Midterm Exam Spring 2002

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

CS 415 Midterm Exam Spring SOLUTION

Functional Programming. Big Picture. Design of Programming Languages

Compilers - Chapter 2: An introduction to syntax analysis (and a complete toy compiler)

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

A Pascal program. Input from the file is read to a buffer program buffer. program xyz(input, output) --- begin A := B + C * 2 end.

Syntax. In Text: Chapter 3

Programming Language Syntax and Analysis

Time : 1 Hour Max Marks : 30

Introduction to Scheme

Chapter 3. Describing Syntax and Semantics ISBN

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

CST-402(T): Language Processors

Describing Syntax and Semantics

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

CMSC 330: Organization of Programming Languages. Context Free Grammars

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

Syntax Intro and Overview. Syntax

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

Functional Programming. Pure Functional Programming

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Part 5 Program Analysis Principles and Techniques

CSCI 3155: Principles of Programming Languages Exam preparation #1 2007

1 Lexical Considerations

Context-free grammars (CFG s)

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Introduction to Parsing

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

COP4020 Programming Assignment 1 - Spring 2011

Introduction to Lexical Analysis

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

Functional Programming. Pure Functional Languages

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

Functional Programming. Pure Functional Languages

MIDTERM EXAM (Solutions)

The role of semantic analysis in a compiler

A programming language requires two major definitions A simple one pass compiler

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

Programming Languages Third Edition. Chapter 7 Basic Semantics

A simple syntax-directed

Programming Languages & Translators PARSING. Baishakhi Ray. Fall These slides are motivated from Prof. Alex Aiken: Compilers (Stanford)

Chapter 3. Describing Syntax and Semantics

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

CS 6353 Compiler Construction Project Assignments

Appendix A The DL Language

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

UMBC CMSC 331 Final Exam

Building Compilers with Phoenix

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CMPS Programming Languages. Dr. Chengwei Lei CEECS California State University, Bakersfield

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

Parser Tools: lex and yacc-style Parsing

CSE3322 Programming Languages and Implementation

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Decaf Language Reference Manual

CS 314 Principles of Programming Languages

Chapter 4. Lexical and Syntax Analysis

UNIT-4 (COMPILER DESIGN)

The PCAT Programming Language Reference Manual

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

Compiler Theory. (Semantic Analysis and Run-Time Environments)

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Today. Assignments. Lecture Notes CPSC 326 (Spring 2019) Quiz 5. Exam 1 overview. Type checking basics. HW4 due. HW5 out, due in 2 Tuesdays

Parser Tools: lex and yacc-style Parsing

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

COP 3402 Systems Software Syntax Analysis (Parser)

Languages and Compilers

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

Lisp. Versions of LISP

COSE312: Compilers. Lecture 1 Overview of Compilers

Lexical and Syntax Analysis. Top-Down Parsing

CS 6353 Compiler Construction Project Assignments

Programming Language Concepts, cs2104 Lecture 04 ( )

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Course Overview. Introduction (Chapter 1) Compiler Frontend: Today. Compiler Backend:

Functional programming with Common Lisp

Syntax-Directed Translation. Lecture 14

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Semantic Analysis. Lecture 9. February 7, 2018

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

CA Compiler Construction

Chapter 4. Syntax - the form or structure of the expressions, statements, and program units

The SPL Programming Language Reference Manual

Introduction to Parsing Ambiguity and Syntax Errors

ECE251 Midterm practice questions, Fall 2010

Syntax. A. Bellaachia Page: 1

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

Introduction to Parsing Ambiguity and Syntax Errors

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Transcription:

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