TDDD55- Compilers and Interpreters Lesson 1 Zeinab Ganjei Department of Computer and Information Science Linköping University.

Slides:



Advertisements
Similar presentations
Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Advertisements

Intermediate Code Generation
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
Abstract Syntax Mooly Sagiv html:// 1.
1 Compiler Construction Intermediate Code Generation.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Context-Free Grammars Lecture 7
Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter 2.2 (Partial) Hashlama 11:00-14:00.
Chapter 2 A Simple Compiler
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
Compiler construction in4020 – lecture 3 Koen Langendoen Delft University of Technology The Netherlands.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Compiler Principles Winter Compiler Principles Exercises on scanning & top-down parsing Roman Manevich Ben-Gurion University.
Syntax Analysis – Part II Quick Look at Using Bison Top-Down Parsers EECS 483 – Lecture 5 University of Michigan Wednesday, September 20, 2006.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
LEX and YACC work as a team
1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your.
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.
Syntax-Directed Translation
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Review Joey Paquet,
1 Top Down Parsing. CS 412/413 Spring 2008Introduction to Compilers2 Outline Top-down parsing SLL(1) grammars Transforming a grammar into SLL(1) form.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Lesson 5 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
CPS 506 Comparative Programming Languages Syntax Specification.
Chapter 5. Syntax-Directed Translation. 2 Fig Syntax-directed definition of a simple desk calculator ProductionSemantic Rules L  E n print ( E.val.
COMPILERS AND INTERPRETERS Lesson 3 – TDDD16 TDDB44 Compiler Construction 2010 Kristian Stavåker Department.
1 Parsers and Grammar. 2 Categories of Grammar Rules  Declarations or definitions. AttributeDeclaration ::= [ final ] [ static ] [ access ] datatype.
1 Using Yacc. 2 Introduction Grammar –CFG –Recursive Rules Shift/Reduce Parsing –See Figure 3-2. –LALR(1) –What Yacc Cannot Parse It cannot deal with.
. 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.
Introduction to Compiling
YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another.
Compiler Principles Fall Compiler Principles Lecture 6: Parsing part 5 Roman Manevich Ben-Gurion University.
COMPILER CONSTRUCTION Lesson 1 – TDDD16 TDDB44 Compiler Construction 2010 Kristian Stavåker (Erik Hansson.
Chapter 1: Introduction 1 Compiler Designs and Constructions Chapter 1: Introduction Objectives: Course Objectives Introduction Dr. Mohsen Chitsaz.
Compiler Construction Composed By, Muhammad Bilal Qureshi.
Top-down Parsing Recursive Descent & LL(1) Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Course Overview for Compilers J. H. Wang Sep. 20, 2011.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
CS510 Compiler Lecture 1. Sources Lecture Notes Book 1 : “Compiler construction principles and practice”, Kenneth C. Louden. Book 2 : “Compilers Principles,
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
YACC (Yet Another Compiler-Compiler) Chung-Ju Wu
Chapter4 Syntax-Directed Translation Introduction : 1.In the lexical analysis step, each token has its attribute , e.g., the attribute of an id is a pointer.
Semantic analysis Jakub Yaghob
System Software Unit-1 (Language Processors) A TOY Compiler
Constructing Precedence Table
Programming Languages Translator
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
PROGRAMMING LANGUAGES
TDDD55- Compilers and Interpreters Lesson 2
TDDD55- Compilers and Interpreters Lesson 3
Chapter 5. Syntax-Directed Translation
SYNTAX DIRECTED TRANSLATION
Subject: Language Processor
High-Level Programming Language
Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing
Faculty of Computer Science and Information System
Presentation transcript:

TDDD55- Compilers and Interpreters Lesson 1 Zeinab Ganjei Department of Computer and Information Science Linköping University

Purpose of Lessons The purpose of the lessons is to practice some theory, introduce the laboratory assignments, and prepare for the final examination. Read the laboratory instructions, the course book, and the lecture notes. All the laboratory instructions and material available in the course directory, ~TDDD55/lab/. Most of the PDF’s also available from the course homepage.

Laboratory Assignments In the laboratory exercises you should get some practical experience in compiler construction. There are 4 separate assignments to complete in 4x2 laboratory hours. You will also (most likely) have to work during non-scheduled time. TDDD55 Compilers and Interpreters 2014

Lessons Schedule November7, : Formal languages and automata theory November 11, :Formal languages and automata theory, Flex November 21, : Bison and intermediate code generation December2, : Exam preparation

HANDING IN AND DEADLINE Demonstrate the working solutions to your lab assistant during scheduled time. Then send the modified files to the same assistant as well as answers to questions if any (put TDDD55 in the topic field). One per group. Deadline for all the assignments is: December Remember to register yourself in the webreg system,

Laboratory Assignments Lab 1 Attribute Grammars and Top-Down Parsing Lab 2 Scanner Specification Lab 3 Parser Generators Lab 4 Intermediate Code Generation

1. Attribute Grammars and Top-Down Parsing Some grammar rules are given Your task: Rewrite the grammar (eliminate left recursion, etc.) Add attributes and attribute rules to the grammar Implement your attribute grammar in a C++ class named Parser. The Parser class should contain a method named Parse that returns the value of a single statement in the language.

2. Scanner Specification Finish a scanner specification given in a scanner.l flex file, by adding rules for comments, identifiers, integers, and reals. More on flex in lesson 2.

3. Parser Generators Finish a parser specification given in a parser.y bison file, by adding rules for expressions, conditions and function definitions,.... You also need to augment the grammar with error productions. More on bison in lesson 3.

4. Intermediate Code Generation The purpose of this assignment to learn about how parse trees can be translated into intermediate code. You are to finish a generator for intermediate code by adding rules for some language statements. More in lesson 3

Hints for Laboratory Assignment 1

Grammar for simple mathematical expressions S -> E S Single expression | No more input E -> E + E Addition | E - E Subtraction | E * E Multiplication | E / E Division | E ^ E Exponentiation | - E Unary minus | ( E ) Grouping | id ( E ) Function call | id Symbolic constant | num Numeric value TDDD55 Compilers and Interpreters 2014

Not Suitable for a Top-Down Technique Left recursion Ambiguous No operator precedence – 5 ^2*1 No operator associativity *1-4^2^3 TDDD55 Compilers and Interpreters 2014

Rewriting the Grammar Use one non-terminal for each precedence level. E ::= E + E | E – E | T T ::= T * T | T / T (Left) Associativity: using (left-)recursive production E ::= E + E | E – E | T => E ::= E + T | E – T | T See for instance: html

Rewriting the Grammar (2) The grammar obtained so far has left recursion Not suitable for a predictive top-down parser Transform the grammar to right recursive form: A ::= A α | β (where β may not be preceded by A) is rewritten to A ::= β A’ A’ ::= α A’ | ε See Lecture 5 Syntax Analysis, Parsing More details:

Attribute Grammars Define attributes for the productions of a formal grammar Example: S ::= E { display( E.val ); } E ::= E 1 + T { E.val = E 1.val + T.val; } | T { E.val = T.val; } T ::= T 1 * F { T.val = T 1.val * F.val; } | F { T.val = F.val; } F ::= ( E ) { F.val = E.val; } | num { F.val = num.val; } See course book and Lecture 8 Semantic Analysis, Intermediate Representation, and Attribute Grammars. See also:

int main(void) { Parser parser; double val; while (1) { try { cout << "Expression: " << flush; val = parser.Parse(); cout << "Result: " << val << '\n' << flush; } catch (ScannerError& e) { cerr << e << '\n' << flush; parser.Recover(); } catch (ParserError) { parser.Recover(); } catch (ParserEndOfFile) { cerr << "End of file\n" << flush; exit(0); } } Implementation: main.cc You have been given a main function in main.cc.

Implementation: lex.cc, lex.hh These files implement the lexer You don’t need to change anything in lex.cc and lex.hh.

Implementation : lab1.cc, lab1.hh lab1.cc and lab1.hh for implementing Parser class. In the function Parse(), start the parsing. double Parser::Parse(void) { Trace x(“Parse”); double val; val= 0; crt_token = the_scanner.Scan(); switch (crt.token.type) { case kIdentifier: case kNumber: case kLeftParen: case kMinus: val = pExpression(); if (crt_token.type != kEndOfLine) throw ParserError(); return val; default: throw ParserError(); } return val; }

Implementation… Add one function for each non-terminal in the grammar to your Parser class. Also implement some simple error recovery in your Parser class. See Lecture 5 Syntax Analysis, Parsing double Parser::pExpression(void) { switch (crt_token.type) {... }

LABORATORY SKELETON ~ TDDD55 /lab /doc Documentation for the assignments. /lab1 Contains all the necessary files to complete the first assignment /lab2 Contains all the necessary files to complete the second assignment /lab3-4 Contains all the necessary files to complete assignment three and four

Installation Take the following steps in order to install the lab skeleton on your system: – Copy the source files from the course directory onto your local account: – You might also have to load some modules (more information in the laboratory instructions). mkdir TDDD55 cp -r ~TDDD55/lab TDDD55

Questions? TDDD55 Compilers and Interpreters 2014