UMBC Introduction to Compilers CMSC 431 Shon Vick 01/28/02.

Slides:



Advertisements
Similar presentations
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Advertisements

Compilers and Language Translation
ISBN Chapter 3 Describing Syntax and Semantics.
Introduction to Compilers Professor Yihjia Tsai 2006 Spring Tamkang University.
ISBN Chapter 3 More Syntax –BNF –Derivations –Practice.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Fall 2007CS 2251 Miscellaneous Topics Deque Recursion and Grammars.
A basis for computer theory and A means of specifying languages
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Slide 1 Chapter 2-b Syntax, Semantics. Slide 2 Syntax, Semantics - Definition The syntax of a programming language is the form of its expressions, statements.
Slide 1 Chapter 3 Attribute Grammars. Slide 2 Attribute Grammars Certain language structures cannot be described using EBNF. Attribute grammars are extensions.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
Chapter 2 A Simple Compiler
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
Describing Syntax and Semantics
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
1 Chapter 2 A Simple Compiler. 2 Outlines 2.1 The Structure of a Micro Compiler 2.2 A Micro Scanner 2.3 The Syntax of Micro 2.4 Recursive Descent Parsing.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax.
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
Compiler Construction1 COMP Compiler Construction Lecturer: Dr. Arthur Cater Teaching Assistant:
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
C H A P T E R TWO Syntax and Semantic.
ISBN Chapter 3 Describing Syntax and Semantics.
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
Copyright © by Curt Hill Grammar Types The Chomsky Hierarchy BNF and Derivation Trees.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
CPS 506 Comparative Programming Languages Syntax Specification.
D Goforth COSC Translating High Level Languages.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Chapter 3 Describing Syntax and Semantics. Chapter 3: Describing Syntax and Semantics - Introduction - The General Problem of Describing Syntax - Formal.
Chapter 3 Describing Syntax and Semantics
Syntax The Structure of a Language. Lexical Structure The structure of the tokens of a programming language The scanner takes a sequence of characters.
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
ISBN Chapter 3 Describing Syntax and Semantics.
LESSON 04.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Syntax and Semantics Form and Meaning of Programming Languages Copyright © by Curt Hill.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Compilers Computer Symbol Table Output Scanner (lexical analysis)
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
C H A P T E R T W O Syntax and Semantic. 2 Introduction Who must use language definitions? Other language designers Implementors Programmers (the users.
1 CS Programming Languages Class 04 September 5, 2000.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
BNF A CFL Metalanguage Some Variations Particular View to SLK Copyright © 2015 – Curt Hill.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Describing Syntax and Semantics Chapter 3: Describing Syntax and Semantics Lectures # 6.
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Describing Syntax and Semantics
A Simple Syntax-Directed Translator
Chapter 3 – Describing Syntax
Syntax One - Hybrid CMSC 331.
Compiler Design 4. Language Grammars
R.Rajkumar Asst.Professor CSE
Chapter 3 Describing Syntax and Semantics.
Chapter 10: Compilers and Language Translation
Faculty of Computer Science and Information System
Presentation transcript:

UMBC Introduction to Compilers CMSC 431 Shon Vick 01/28/02

UMBC 2 What is a compiler? Translates source code to target code –Source code is typically a high level programming language (Java, C++, etc) but does not have to be –Target code is often a low level language like assembly or machine code but does not have to be Can you think of other compilers that you have used – according to this definition?

UMBC 3 Other Compilers Javadoc -> HTML SQL Query output -> Table Poscript -> PDF High level description of a circuit -> machine instructions to fabricate circuit

The Compilation Process

UMBC 5 The analysis Stage Broken up into four phases –Lexical Analysis (also called scanning or tokenization) –Parsing –Semantic Analysis –Intermediate Code Generation

UMBC 6 Lexing Example double d1; double d2; d2 = d1 * 2.0; doubleTOK_DOUBLE reserved word d1TOK_IDvariable name ;TOK_PUNCThas value of “;” double TOK_DOUBLE reserved word d2TOK_IDvariable name ;TOK_PUNCThas value of “;” d2TOK_IDvariable name =TOK_OPER has value of “=” d1 TOK_IDvariable name * TOK_OPER has value of “*” 2.0 TOK_FLOAT_CONST has value of 2.0 ; TOK_PUNCThas value of “;” lexemes

UMBC 7 Syntax and Semantics Syntax - the form or structure of the expressions – whether an expression is well formed Semantics – the meaning of an expression

UMBC 8 Syntactic Structure Syntax almost always expressed using some variant of a notation called a context-free grammar (CFG) or simply grammar –BNF –EBNF

UMBC 9 A CFG has 4 parts A set of tokens (lexemes), known as terminal symbols A set of non-terminals A set of rules (productions) where each production consists of a left-hand side (LHS) and a right-hand side (RHS) The LHS is a non-terminal and the RHS is a sequence of terminals and/or non-terminal symbols. A special non-terminal symbol designated as the start symbol

UMBC 10 An example of BNF syntax for real numbers ::=. ::= | ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7| 8 | 9 encloses non-terminal symbols ::= 'is' or 'is made up of ' or 'derives' (sometimes denoted with an arrow ->) | or

UMBC 11 Example On the example from the previous slide: –What are the tokens? –What are the lexemes? –What are the non terminals? –What are the productions?

UMBC 12 BNF Points A non terminal can have more than RHS or an OR can be used Lists or sequences are expressed via recursion A derivation is just a repeated set of production (rule) applications Examples

UMBC 13 Example Grammar -> -> | ; -> = -> a | b | c | d -> + | - -> | const

UMBC 14 Example Derivation => => => = => a = => a = + => a = b + => a = b + const

UMBC 15 Parse Trees Alternative representation for a derivation Example parse tree for the previous example var expr = term + var b const stmts stmt term a

UMBC 16 Another Example Expression -> Expression + Expression | Expression - Expression |... Variable | Constant |... Variable -> T_IDENTIFIER Constant -> T_INTCONSTANT | T_DOUBLECONSTANT

UMBC 17 The Parse Expression -> Expression + Expression -> Variable + Expression -> T_IDENTIFIER + Expression -> T_IDENTIFIER + Constant -> T_IDENTIFIER + T_INTCONSTANT a + 2

UMBC 18 Parse Trees PS -> P | P PS P ->  | '(' PS ')' | ' ' | '[' PS ']' What’s the parse tree for this statement ? ] >

UMBC 19 EBNF - Extended BNF Like BNF except that Non-terminals start w/ uppercase Parens are used for grouping terminals Braces {} represent zero or more occurrences (iteration ) Brackets [] represent an optional construct, that is a construct that appears either once or not at all.

UMBC 20 EBNF example Exp -> Term { ('+' | '-') Term } Term -> Factor { ('*' | '/') Factor } Factor -> '(' Exp ')' | variable | constant

UMBC 21 EBNF/BNF EBNF and BNF are equivalent How can {} be expressed in BNF? How can ( ) be expressed? How can [ ] be expressed?

UMBC 22 Semantic Analysis The syntactically correct parse tree (or derivation) is checked for semantic errors Check for constructs that while valid syntax do not obey the semantic rules of the source language. Examples: –Use of an undeclared/un-initialized variable –Function called with improper arguments –Incompatible operands and type mismatches,

UMBC 23 Examples int i; int j; i = i + 2; int arr[2], c; c = arr * 10; Most semantic analysis pertains to the checking of types. void fun1(int i); double d; d = fun1(2.1);

UMBC 24 Intermediate Code Generation Where the intermediate representation of the source program is created. The representation can have a variety of forms, but a common one is called three- address code (TAC) Like assembly – the TAC is a sequence of simple instructions, each of which can have at most three operands.

UMBC 25 Example _t1 = b * c _t2 = b * d _t3 = _t1 + _t2 a = _t3 a = b * c + b * d Note temps

UMBC 26 Another Example _t1 = a > b if _t1 goto L0 _t2 = a - c a = _t2 L0: t3 = b * c c = _t3 if (a <= b) a = a - c; c = b * c; Note Temps Symbolic addresses

UMBC 27 Next Time Finish introduction to compilation stages Read Aho/Sethi/Ullman Chapter 1

UMBC 28 Selected References Compilers Principles, Techniques and Tools, Aho, Sethi, and Ullman /