Prof. Fateman CS 164 Lecture 111 Syntax  Simple Semantics Lecture 11.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
1 Compiler Construction Intermediate Code Generation.
Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS AM MWF 10 Evans.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
Prof. Fateman CS 164 Lecture 151 Language definition by interpreter, translator, continued Lecture 15.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Prof. Fateman CS 164 Lecture 141 Language definition by interpreter Lecture 14.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
PSUCS322 HM 1 Languages and Compiler Design II Formal Semantics Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
CS784 (Prasad)L167AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
Static checking and symbol table Chapter 6, Chapter 7.6 and Chapter 8.2 Static checking: check whether the program follows both the syntactic and semantic.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Imperative Programming
Syntax Directed Definitions Synthesized Attributes
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
COP4020 Programming Languages
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Introduction to Parsing
CPS 506 Comparative Programming Languages Syntax Specification.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
. 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.
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
Overview of Previous Lesson(s) Over View  In syntax-directed translation 1 st we construct a parse tree or a syntax tree then compute the values of.
Syntax (2).
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
CPSC 388 – Compiler Design and Construction Parsers – Syntax Directed Translation.
CMSC 330: Organization of Programming Languages Operational Semantics.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Prof. Fateman CS 164 Lecture 181 Language definition by interpreter Lecture 18.
MiniJava Compiler A multi-back-end JIT compiler of Java.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
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.
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.
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.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lecture 9 Symbol Table and Attributed Grammars
Constructing Precedence Table
Chapter 3 Context-Free Grammar and Parsing
Syntax Simple Semantics
Corky Cartwright January 18, 2017
Syntax-Directed Translation Part I
Syntax-Directed Translation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Programming Languages 2nd edition Tucker and Noonan
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSE401 Introduction to Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
COMPILER CONSTRUCTION
Presentation transcript:

Prof. Fateman CS 164 Lecture 111 Syntax  Simple Semantics Lecture 11

Prof. Fateman CS 164 Lecture 112 What do we do while parsing What semantic actions are plausible? –Mere recognition –Immediate execution –Generate Abstract Parse Trees

Prof. Fateman CS 164 Lecture 113 What does a rule E ! E+T mean? (I) E 1 ! E 0 +T could mean “I will happily exchange my E, +, and T for an E, if it means I can determine if this input string is a sentence.” (E ! E + T #’(lambda(E P T) ‘t)) in this model the result of parsing 3+4 is “true”

Prof. Fateman CS 164 Lecture 114 What does a rule E ! E+T mean? (II) E 1 ! E 0 +T could mean “E 1 means: the result of taking whatever E 0 means, and whatever T means, and adding them together.” (E ! E + T #’(lambda(E P T)(+ E T))) In this model, the result of parsing is 7

Prof. Fateman CS 164 Lecture 115 What does a rule E ! E+T mean? (III) E 1 ! E 0 +T could mean “whatever E 0 means, and whatever T means, make a tree of them with + at the root.” (E ! E + T #’(lambda(E P T)(list ‘+ E T))) In this model, parsing produces (+ 3 4). [this happens also to be a lisp program, but you could imagine some other tree structure, e.g. Tree{root=“plus”, left=Tree{root= “3” left=nil…, right=Tree{ ….} } ]

Prof. Fateman CS 164 Lecture 116 What else might a rule mean? Arbitrary other semantics could be attached to a rule, to be applied when reducing E+T to E. For example –If E 0 and T are of type integer then E 1 must also be of type integer A rule typedeclare ! type id might set up some information in memory to help make sense of a program’s variables. In principle might not be in the tree.

Prof. Fateman CS 164 Lecture 117 What else might a rule mean? We could have a grammar with augments that formatted mathematical formulas... –Fraction ! Num / Denom #’(lambda (N s D) (makevertpile N (hline (max (width N)(width D)) D))

Prof. Fateman CS 164 Lecture 118 Building a parse tree S ! E $ E ! T EP EP ! ADDOP T EP EP ! ADDOP ! + | - T ! F TP TP ! MULOP F TP TP ! MULOP ! * | / F ! id | num | (E) Sample = id + id + num * ( num / num ) $

Prof. Fateman CS 164 Lecture 119 Building a parse tree ;; (id + id + num * |(| num / num |)| $ (S (E (T (F id) (TP)) (EP (addop +) (T (F id) (TP)) (EP (addop +) (T (F num) (TP (mulop *) (T (F |(| (E (T (F num) (TP (mulop /) (T (F num) (TP)) (TP))) (EP)) |)|) (TP)) (TP))) (EP)))) $)

Prof. Fateman CS 164 Lecture 1110 Building a more useful tree ;; (id + id + num * |(| num / num |)| $ (+ id id (* num (/ num num))) ; recdec315s … just the terminals!!! instead of the clumsy previous tree (S (E (T (F id) (TP)) (EP (addop +) (T (F id) (TP)) (EP (addop +) …. ;; all these pointless reductions in tree

Prof. Fateman CS 164 Lecture 1111 What kind of tree should we build for a MiniJava program? Cheap trick: a tree that is an equivalent Lisp program. Cheap trick: a tree that if printed in infix order is a C program that could be compiled and run. Really cheap trick: a Java program More typical treatment: –A symbol table structure listing all the types, variables, functions (in all their different scopes) –With all the function bodies or other executable components defined as trees –Use above data to do type checking.

Prof. Fateman CS 164 Lecture 1112 Abstract Syntax Tree Example. Here is a MJ program (BinarySearch.java) class BinarySearch{ public static void main(String[] a){ System.out.println(new BS().Start(20)); } // This class contains an array of integers and // methods to initialize, print and search the array // using Binary Search class BS{ int[] number ; int size ; // Invoke methods to initialize, print and search // for elements on the array public int Start(int sz){ int aux01 ; int aux02 ;

Prof. Fateman CS 164 Lecture 1113 Abstract Syntax Tree Example. Here is a MJ parser OUTPUT (BinarySearch.java) (Program ;; this has line numbers only, output of simple- ;;lex, simple-parse (MainClass (id BinarySearch 1) (id a 2) (Print (Call (NewObject (id BS 3)) (id Start 3) (ExpList (IntegerLiteral 20))))) (ClassDecls (ClassDecl (id BS 10) (extends nil) (VarDecls (VarDecl IntArrayType (id number 11)) (VarDecl IntType (id size 12))) (MethodDecls (MethodDecl IntType (id Start 16) (Formals (Formal IntType (id sz 16))) (VarDecls (VarDecl IntType (id aux01 17)) ……

Prof. Fateman CS 164 Lecture 1114 This is really rather clumsy to look at But at least we can look at it without writing another program (not like Java). We have removed line/column markers in many places, but left some in there so that we still can identify some errors by location. The next pass can be like our CS61a analyzing meta-circular evaluator (remember that ?)

Prof. Fateman CS 164 Lecture 1115 Just as a preview If we see a binary arithmetic operator, we can hope to look up the declared types of the arguments to it (if ids). Or the computed types of the arguments to it (if expressions). For more ambitious languages, a<b is legal when a comparison is valid (e.g. strings, floats, ints and floats, single and double). Or a+b makes sense when one or both are strings. The primary issues: –knowing types of objects (in scopes..) –Knowing what’s legal to do with the objects.