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

Slides:



Advertisements
Similar presentations
Simplifications of Context-Free Grammars
Advertisements

JavaCUP JavaCUP (Construct Useful Parser) is a parser generator
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
1 Mariano Ceccato FBK Fondazione Bruno Kessler The TXL Programming Language (2)
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
Addition Facts
Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00.
Programming Language Concepts
1 2.Lexical Analysis 2.1Tasks of a Scanner 2.2Regular Grammars and Finite Automata 2.3Scanner Implementation.
Chapter 2-2 A Simple One-Pass Compiler
1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Chapter 5 Test Review Sections 5-1 through 5-4.
Addition 1’s to 20.
25 seconds left…...
Week 1.
1 Week 9 Questions / Concerns Hand back Test#2 What’s due: Final Project due next Thursday June 5. Final Project check-off on Friday June 6 in class. Next.
1 Unit 1 Kinematics Chapter 1 Day
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Chapter 2 Chang Chi-Chung Lexical Analyzer The tasks of the lexical analyzer:  Remove white space and comments  Encode constants as tokens.
Environments and Evaluation
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
CH2.1 CSE4100 Chapter 2: A Simple One Pass Compiler Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371.
Chapter 2 Chang Chi-Chung rev.1. A Simple Syntax-Directed Translator This chapter contains introductory material to Chapters 3 to 8  To create.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Syntax Directed Definitions Synthesized Attributes
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.
Lexical Analysis - An Introduction. The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lesson 7 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.
D. M. Akbar Hussain: Department of Software & Media Technology 1 Compiler is tool: which translate notations from one system to another, usually from source.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Bernd Fischer RW713: Compiler and Software Language Engineering.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 1, 08/28/03 Prof. Roy Levow.
CPS 506 Comparative Programming Languages Syntax Specification.
Scribe Sumbission Date: 28 th October, 2013 By M. Sudeep Kumar.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Syntax-Directed Definitions and Attribute Evaluation Compiler Design Lecture (02/18/98) Computer Science Rensselaer Polytechnic.
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03BX –Recursive descent parsing Programming Language.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Chapter 2: A Simple One Pass Compiler
C HAPTER 2. A S IMPLE S YNTAX -D IRECTED T RANSLATOR DR. NIDJO SANDJOJO, M.Sc.
Lecture 9 Symbol Table and Attributed Grammars
Chapter 3 – Describing Syntax
A Simple Syntax-Directed Translator
Constructing Precedence Table
Compiler Construction
Basic Program Analysis: AST
CPSC 388 – Compiler Design and Construction
Syntax-Directed Definition
Chapter 2: A Simple One Pass Compiler
Syntax-Directed Translation
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Designing a Predictive Parser
Compiler design.
Lexical Analysis - An Introduction
Recursive descent parsing
Recursive descent parsing
Presentation transcript:

Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

2 Outline Code generation using syntax-directed translation Lexical analysis

CODE GENERATION USING SYNTAX-DIRECTED TRANSLATION 3

Syntax-directed translation Add attributes to the grammar symbols Add semantic actions to the grammar –Syntax-directed translation scheme Inject code into the parser 4

SDT example (Section 2.3 in the book) Expression grammar: expr expr + num | expr – num | num Infix to postfix notation 5

SDT example (Section 2.3 in the book) 6 Infix expressionPostfix expression (1 + 2) – – 1 + (2 – 3)1 2 3 – +

SDT example (Section 2.3 in the book) Formal definition: – POSTFIX (num) = num – POSTFIX ( (E) ) = POSTFIX (E) – POSTFIX (E 1 op E 2 ) = POSTFIX (E 1 ) POSTFIX (E 2 ) op 7

Exercise (1) Translate the following infix expressions into postfix notation: a)78 b)3 – 2 – 1 c)( * 3) d)3 * (17) / (92 + 8) Assume conventional operator precedence and associativity. 8

SDT example (Section 2.3 in the book) Translation scheme: expr expr + num{ print(num.value); print('+') } | expr – num{ print(num.value); print('–') } | num{ print(num.value) } 9

SDT example (Section 2.3 in the book) Extended parse tree for – 3: 10 expr num (1) – +num (2) num (3) { print(num.value); print('-') } { print(num.value); print('+') } { print(num.value) }

Exercise (2) Traverse the following extended parse tree in a depth-first, left-right order and execute the semantic actions: 11 expr num (1) – +num (2) num (3) { print(num.value); print('-') } { print(num.value); print('+') } { print(num.value) }

Left recursion elimination expr num { print(num.value) } rest rest + num { print(num.value); print('+') } rest rest - num { print(num.value); print('-') } rest rest ε 12

Exercise (3) Draw the parse tree for – 3 (i.e. num + num – num) with the new grammar. Include the semantic actions as leaf nodes. Then traverse it and execute the semantic actions. 13

Syntax-directed definitions Similar to translation schemes More abstract or declarative 14 ProductionSemantic rules expr expr 1 + numexpr.t = expr 1.t || num.value || '+' | expr 1 – numexpr.t = expr 1.t || num.value || '-' | numexpr.t = num.value

LEXICAL ANALYSIS 15

Lexical analysis Lexical analyzer/ scanner/tokenizer Simplifies the parser: –Removes white spaces –Removes comments –Identifies lexemes and returns tokens 16

Tokens Name + attribute Attributes: –Line and column number –Identifier name/symbol table index –Numerical value –… Lexemes 17

Differing requirements Allow spaces in identifiers? –Example: Fortran 90 Allow keywords as identifiers? –Example: PL/1 Language support for configuring the lexical analysis? –Example: TeX 18

Implementing lexical analysis Finite state machine? Hard-coded? Use a generator tool? 19

Input buffering 20

21 int lineno = 1, attribute = NONE; int GetNextToken(void) { char t; for (t = ReadChar(); t != 0; t = ReadChar()) { if (t == ' ' || t == '\t') /* Skip white spaces */ else if (t == '\n') lineno++; else if ('0' <= t && t <= '9') { attribute = GetNum(t); return NUM; } else { /* Error handling */ attribute = NONE; return UNKNOWN_TOKEN; } return EOF;/* End of file token */ }

22 int GetNum(char t) { int num = 0; for (; '0' <= t && t <= '9'; t = ReadChar()) { num *= 10; num += t – '0'; } // Put back the char that caused the loop to exit PutBack(t); return num; }

DFA-based scanner 23

DFA-based scanner 24 Symbol State <148 >638 =527 other48

Differentiating between keywords and identifiers Two strategies: –Keyword table –Test for keywords before identifiers 25

Error recovery Often hard to detect –Misspelled keywords = valid identifiers –Misspelled identifiers hard to detect Recovery strategies: –Panic mode –Try to fix the input 26

Conclusion Code generation using syntax-directed translation Lexical analysis 27

Next time Stack machine code Generating stack machine code using SDT 28