An Attribute Grammar for Tiny Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 18.

Slides:



Advertisements
Similar presentations
AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9.
Advertisements

1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Semantics Static semantics Dynamic semantics attribute grammars
Intermediate Code Generation
Chapter 8 ICS 412. Code Generation Final phase of a compiler construction. It generates executable code for a target machine. A compiler may instead generate.
8 Intermediate code generation
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
1 Compiler Construction Intermediate Code Generation.
1 Beyond syntax analysis An identifier named x has been recognized. Is x a scalar, array or function? How big is x? If x is a function, how many and what.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Environments and Evaluation
Compiler Summary Mooly Sagiv html://
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02.
Attribute Grammars Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 17.
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y and z here are the three operands and op is any logical.
1 Structure of a Compiler Front end of a compiler is efficient and can be automated Back end is generally hard to automate and finding the optimum solution.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
The TINY sample language and it’s compiler
Compiler Chapter# 5 Intermediate code generation.
1. Exam Topics Difference between computers and calculators John creates a new device. It will compute the orbit of all the planets in the solar system.
Parsing Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 3.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Chapter 7 Syntax-Directed Compilation (AST & Target Code) 1.
TDDD55- Compilers and Interpreters Lesson 1 Zeinab Ganjei Department of Computer and Information Science Linköping University.
. 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.
Compiler Principles Fall Compiler Principles Lecture 6: Parsing part 5 Roman Manevich Ben-Gurion University.
Week 6(10.7): The TINY sample language and it ’ s compiler The TINY + extension of TINY Week 7 and 8(10.14 and 10.21): The lexical of TINY + Implement.
COMPILER CONSTRUCTION Lesson 1 – TDDD16 TDDB44 Compiler Construction 2010 Kristian Stavåker (Erik Hansson.
Writing RPAL Programs Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 13.
Semantics (1).
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Code Generation CPSC 388 Ellen Walker Hiram College.
Intermediate code generation Simplifying initial assumptions a 3-address code will be used for instructions  choice of instruction mnemonics and layouts.
Code Generation How to produce intermediate or target code.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Programming Language Concepts
Building AST's for RPAL Programs
A Simple Syntax-Directed Translator
Constructing Precedence Table
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
6.001 SICP Compilation Context: special purpose vs. universal machines
Overview of Compilation The Compiler BACK End
An Attribute Grammar for Tiny
Programming Language Principles
Programming Language Concepts
Chapter 6 Intermediate-Code Generation
Overview of Compilation The Compiler BACK End
Programming Language Principles
Three-address code A more common representation is THREE-ADDRESS CODE . Three address code is close to assembly language, making machine code generation.
Compiler Design 21. Intermediate Code Generation
Building AST's for RPAL Programs
Programming Language Principles
Intermediate Code Generation
Optimizations for the CSE Machine
Compiler Design 21. Intermediate Code Generation
Programming Language Principles
Programming Language Concepts
Presentation transcript:

An Attribute Grammar for Tiny Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 18

An Attribute Grammar for Tiny Tiny: a very small Pascal-like language with the following: Every program is given a name. Variables of type integer. Expressions composed of: variables integers intrinsic function "read" boolean operator "not"

An Attribute Grammar for Tiny (cont’d) equality operator " = " binary operators " + " and " - " unary " - ", " not ". Assignment statements. Variables must be assigned a value before they are used. No declarations.

An Attribute Grammar for Tiny (cont’d) Statements: if-then-else while statement sequencing (" ; ") output statement.

Tiny’s Syntax Tiny → 'program' Name ':' S 'end' Name '.’ => 'program'; S → 'assign' Name ':=' Exp=> 'assign' → 'output' Exp => 'output' → 'if' Exp 'then' S 'else' S 'fi'=> 'if' → 'while' Exp 'do' S 'od'=> 'while' → S list ';' => ';' ; Exp → Term '=' Term=> '=' → Term; Term → Term '+' Factor=> '+' → Term '-' Factor=> '-' → Factor;

Tiny’s Syntax (cont’d) Factor → '-' Factor => '-' → 'not' Factor => 'not' → Name → 'read' => 'read' → ' ' → '(' Expression ')'; Name → ' ';

Sample Tiny Program Copies 10 integers from input to output: program copy: assign i := 1; while not (i=11) do output read; assign i := i + 1 od end copy.

AST Grammar for Tiny P → ' E ' '> E → ' E> → → ' ' → 'read'

Note AST grammar simplified. No distinction between expressions and statements. Our attribute grammar will specify the translation (compilation) of Tiny programs to assembly for an abstract machine.

Target Machine Instruction Set PC := 1; Next: case Code[PC] of save n:stack[n] := stack[top--] load n:stack[++top] := stack[n] negate:stack[top] := -stack[top] not: stack[top] := not stack[top] add: t := stack[top--]; stack[top] := stack[top] + t subtract:t := stack[top--]; stack[top] := stack[top] - t equal:t := stack[top--]; stack[top] := stack[top] = t

Target Machine Instruction Set (cont’d) read:stack[++top] := getinteger(input) print:putinteger(stack[top--]) lit n:stack[++top] := n goto n:PC := n; goto Next iffalse n:if stack[top--] = 0 then PC:=n; goto Next fi iftrue n:if stack[top--] = 1 then PC:=n; goto Next fi stop:halt end; ++PC; goto Next;

Target Code for our Sample Tiny Program 1: lit 1 # i := 1 2: load 1 # i = 11 3: lit 11 4: equal 5: not 6: iffalse 14 # while 7: read 8: print 9: load 1 # i := i : lit 1 11: add 12: save 1 13: goto 2 # od 14: stop # end program copy: assign i := 1; while not (i=11) do output read; assign i := i + 1 od end copy.

Semantic Errors Variables used before being initialized. Non-boolean expression in " while ", " not ", or " if ". Non-integer expression in " = ", " - ", or " + ". Program names do not match.

Data Structures Required Declaration table. Function enter( name,l ). Binds " name " with stack location " l ". Returns " l ". Function lookup( name ). Returns the location of " name ". Returns 0 if " name " is not found.

Data Structures Required (cont’d) Files. Function gen( file, arg 1,..., arg n ). Writes a new line to " file ". The line contains arg 1,..., arg n. Returns the new, modified file. function Open. Creates a new file. function Close. Closes a file.

Attributes code: File of code generated. next: Label of the next instruction on the code file. error: File of semantic errors. top: Current (predicted) size of run-time stack. type: Type of subtree. Used for type-checking.

Attributes (cont’d) ALL attributes are both synthesized and inherited. Convention: a  is the synthesized attribute a. a  is the inherited attribute a.

Synthesized and Inherited Attributes S(program) = { code , error  } I(program) = { } S(assign) = { code , next , error , top , type  } I(assign) = { code , next , error , top  } All other nodes: Same synthesized and inherited attributes as "assign".

Synthesized and Inherited Attributes Most nodes have: Four inherited attributes (all except type  ). All five synthesized attributes. One exception: "program" node: no inherited attributes. two synthesized attributes: code  and error .

Tree Walking Assumptions Top-down on the left, Bottom-up on the right. Defaults: If axiom is missing, assume no kids: a  (  ) = a  (  ) n kids: a  (1) = a  (  ) a  (i) = a  (i-1), for 1 < i <= n a  (  ) = a  (n)

Tiny's Attribute Grammar E → ' :n' code  (  ) = gen (code  (  ), "lit", "n") next  (  ) = next  (  ) + 1 top  (  ) = top  (  ) + 1 type  (  ) = "integer"

Tiny's Attribute Grammar (cont’d) E → ' ' code  (  ) = gen (code  (  ), "load", lookup("x")) next  (  ) = next  (  ) + 1 top  (  ) = top  (  ) + 1 type  (  ) = "integer" error  (  ) = if lookup("x") = 0 then gen (error  (  ), "identifier un-initialized") else error  (  )

Tiny's Attribute Grammar (cont’d) E → read code  (  ) = gen (code  (  ), "read") next  (  ) = next  (  ) + 1 top  (  ) = top  (  ) + 1 type  (  ) = "integer"

Tiny's Attribute Grammar (cont’d) E → code  (  ) = gen (code  (1), "negate") next  (  ) = next  (1) + 1 type  (  ) = "integer" error  (  ) = if type  (1) = "integer" then error  (1 ) else gen (error  (1), “Illegal type for minus"))

Tiny's Attribute Grammar (cont’d) E → code  (  ) = gen (code  (1), “not") next  (  ) = next  (1) + 1 type  (  ) = “boolean" error  (  ) = if type  (1) = “boolean" then error  (1) else gen (error  (1), "Illegal type for not")

Tiny's Attribute Grammar (cont’d) E → code  (  ) = gen (code  (2), "add") next  (  ) = next  (2) + 1 top  (  ) = top  (2) - 1 type  (  ) = "integer" error  (  ) = if type  (1)=type  (2)= "integer" then error  (2) else gen (error  (2), "Illegal type for plus")

Tiny's Attribute Grammar (cont’d) E → code  (  ) = gen (code  (2), “subtract") next  (  ) = next  (2) + 1 top  (  ) = top  (2) - 1 type  (  ) = "integer" error  (  ) = if type  (1)=type  (2)= "integer" then error  (2) else gen (error  (2), "Illegal type for minus")

Tiny's Attribute Grammar (cont’d) E → code  (  ) = gen (code  (2), “equal") next  (  ) = next  (2) + 1 type  (  ) = “boolean" top  (  ) = top  (2) - 1 error  (  ) = if type  (1) = type  (2) then error  (2) else gen (error  (2), "Type clash in equal comparison")

Tiny's Attribute Grammar (cont’d) E → code  (  ) = gen (code  (1), “print") next  (  ) = next  (1) + 1 top  (  ) = top  (1) - 1 type  (  ) = “statement" error  (  ) = if type  (1) = "integer" then error  (1) else gen (error  (1), "Illegal type for output")

Tiny's Attribute Grammar (cont’d) E → ' E > code  (  ) = if lookup("x") = 0 then enter("x",top  (2)); code  (2) else gen (code  (2), "save", lookup("x")) next  (  ) = if lookup("x") = 0 then next  (2) else next  (2) + 1

E → ' E > (cont’d) top  (  ) = if lookup ("x") = 0 then top  (2) else top  (2) - 1 error  (  ) = if type  (2) = "integer" then error  (2) else gen (error  (2), "Assignment type clash") type  (  ) = "statement"

Tiny's Attribute Grammar (cont’d) E -> Use Defaults !

Tiny's Attribute Grammar (cont’d) E → code  (2) = gen (code  (1),"iffalse", next  (2) + 1) next  (2) = next  (1) + 1 top  (2) = top  (1) - 1 code  (3) = gen (code  (2), "goto", next  (3)) next  (3) = next  (2) + 1 error  (2) = if type  (1) = "boolean" then error  (1) else gen (error  (1), "Illegal expression for if")

E → (cont’d) error  (3) = if type  (2) = "statement" then error  (2) else gen (error  (2), "Statement required for if") error  (  ) = if type  (3) = "statement" then error  (3) else gen (error  (3), "Statement required for if")

Tiny's Attribute Grammar (cont’d) E → code  (2) = gen (code  (1), "iffalse", next  (2) + 1) next  (2) = next  (1) + 1 top  (2) = top  (1) - 1 code  (  ) = gen (code  (2), "goto", next  (  )) next  (  ) = next  (2) + 1 type  (  ) = "statement"

E → (cont’d) error  (2) = if type  (1) = "boolean" then error  (1) else gen (error  (1), "Illegal expression in while") error  (  ) = if type  (2) = "statement" then error  (2) else gen (error  (2), "Statement required in while")

Tiny's Attribute Grammar (cont’d) Tiny → ' E ' ' > code  (2) = Open error  (2) = Open next  (2) = 1 top  (2) = 0 code  (  ) = close (gen (code  (2), "stop")) error  (  ) = close (if x = y then error  (2) else gen (error  (2), "program names don't match") )

An Attribute Grammar for Tiny Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 18