Chapter 4 Action Routines.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Attribute Grammars Prabhaker Mateti ACK: Assembled from many sources.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
Semantic Analysis Chapter 4. Role of Semantic Analysis Following parsing, the next two phases of the "typical" compiler are – semantic analysis – (intermediate)
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.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Topic 5 -Semantic Analysis Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
1 Lecture 11: Semantic Analysis (Section ) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
Copyright © 2005 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
Syntax-Directed Translation
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
CS 363 Comparative Programming Languages Semantics.
Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are.
ISBN Chapter 3 Describing Semantics.
Chapter 3 Part II Describing Syntax and Semantics.
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
Copyright © 2009 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03CX - Language semantics Programming Language Design.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
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.
Lecture 9 Symbol Table and Attributed Grammars
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Semantic analysis Jakub Yaghob
Describing Syntax and Semantics
Semantic Analysis Chapter 4.
Syntax-Directed Translation
Context-Sensitive Analysis
A Simple Syntax-Directed Translator
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Table-driven parsing Parsing performed by a finite state machine.
Compiler Construction
Chapter 5 Syntax Directed Translation
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Ch. 4 – Semantic Analysis Errors can arise in syntax, static semantics, dynamic semantics Some PL features are impossible or infeasible to specify in grammar.
Compiler Lecture 1 CS510.
Syntax-Directed Translation Part I
CS 3304 Comparative Languages
Syntax Questions 6. Define a left recursive grammar rule.
Chapter 5. Syntax-Directed Translation
Syntax-Directed Translation Part I
CPSC 388 – Compiler Design and Construction
SYNTAX DIRECTED TRANSLATION
CSE 3302 Programming Languages
Chapter 2: A Simple One Pass Compiler
CS 3304 Comparative Languages
Subject Name:Sysytem Software Subject Code: 10SCS52
CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Chapter 3 Describing Syntax and Semantics.
Recursive descent parsing
Syntax-Directed Translation Part I
COP4020 Programming Languages
COP4020 Programming Languages
Chapter 5 Syntax Directed Translation
Recursive descent parsing
Chapter 2 :: Programming Language Syntax
Presentation transcript:

Chapter 4 Action Routines

Syntax and Semantics In general: In programming languages: Syntax – form Semantics – meaning In programming languages: Syntax – part of the language definition that can be described via a context-free grammar Semantics – part of language definition that can’t.

Non-Syntax Items Can’t be enforced by a context-free grammar: Variables must be declared before use Variables must be assigned before use Type compatibility in expressions Number and type of arguments must match the number and type of formal parameters Procedure must contain a return statement for each execution path

Semantics Recall, semantics can be broken into: Static semantics – can be enforced by the compiler at compile time Dynamic semantics – enforced at run-time, probably via code generated at compile time by the compiler.

Evaluating Attribute Grammars Attribute grammar variables can be characterized as: Intrinsic – look-up value Synthesized – Variables on the left side of a production get attribute values calculated from values on the right side of the production. In other words, attribute values come from values lower in the parse tree. Inherited – Variables on the right side of a production get attribute values from items on the left side of the production or items to the left of the variable. In other words, values come from parents or siblings on the right in the parse tree.

Synthesized Attributes Simple attribute grammar for constant expressions using standard arithmetic operations Figure 4.1, page 181

Evaluating Attributes

Synthesized & Inherited Attributes Attribute grammar for constant expressions based on LL(1) context-free grammar. Several productions have two semantic rules Figure 4.3, page 186

Evaluating Attributes - Example

Attribute Grammars Semantic analysis and code generation can be described in terms of annotation, or “decoration” of a parse or syntax tree Attribute grammars provide a formal framework for decorating such a tree

Example Context-Free Grammar E → E + T E → E – T E → T T → T * F T → T / F T → F F → - F Says nothing about what the program means

Example Attribute Grammar E → E + T E1.val = E2.val + T.val E → E – T E1.val = E2.val - T.val E → T E.val = T.val T → T * F T1.val = T2.val * F.val T → T / F T1.val = T2.val / F.val T → F T.val = F.val F → - F F1.val = - F2.val

Assertions Some languages allow programmers to write logical assertions about values of program data Example: assert denominator != 0 C allows via library routine assert Ada and Java have built-in language support for assertions Euclid and Eiffel include explicit support for invariants, preconditions and post-conditions

Action Routines Action routine – a semantic function that a grammar writer can associate with a grammar for the compiler to execute at particular points during parsing Action routines are more flexible than attribute grammars.

Action Routines Action routines – routines attached to a grammar which is executed by the compiler as it parses code Often used in production compilers Can be used to build parse trees

Example for LL Parsing LL parsing – action routines can appear anywhere within a right-had side. Pointer to the action routine is pushed onto the stack and is executed when it is at the top of the stack. Thus: Action routine at the beginning is executed when the parser predicts the production Routine embedded in the middle of the right-hand side is executed when the parser “matches” the symbol to its left

Action Routine Example Consider the grammar: E → T TT TT → + T TT | - T TT | ε T → F FT FT → * F FT | / F FT | ε F → - F F → ( E ) F → const Figure 4.9 in text, page 195

Action Routine Example Action routines added: E → T {TT.st:=T.ptr} TT {E.ptr:=TT.ptr} TT1 → T {TT2.st:=make_bin_op(“+”,TT1.st,T.ptr)} TT2 {TT1.ptr:=TT2.ptr} TT1 → T {TT2.st:=make_bin_op(“-”,TT1.st,T.ptr)} TT2 {TT1.ptr:=TT2.ptr} TT → ε {TT.ptr:=TT.st} T → F {FT.st:=F.ptr} FT {T.ptr:=FT.ptr}

Action Routine Example - continued FT1 → F {FT2.st:=make_bin_op(“x”,FT1.st,F.ptr)} FT2 {FT1.ptr:=FT2.ptr} FT1 → F {FT2.st:=make_bin_op(“/”,FT1.st,F.ptr)} FT2 {FT1.ptr:=FT2.ptr} FT → ε {FT.ptr:=FT.st} F1 → F2 {F1.ptr:=make_un_op(“+/-”,FT2.ptr)} F → (E) {F.ptr:=E.ptr} F → const {F.ptr:=make_leaf(const.ptr)}

Build Tree Use the previous grammar with action routines to build a tree for the expression: 2 * 3 + 5

Build Tree Stack: F F → const {F.ptr:=make_leaf(const.ptr)} FT T TT E

Build Tree Stack: FT1→ * F {FT2.st:= make_bin_op(“x”,FT1.st, F.ptr)} FT2 {FT1.ptr:= FT2.ptr} * F FT TT

Simplified Action Routine Example Demonstrating action routines using a more simple (LR) grammar. E → E + T E → E – T E → T T → T * F T → T / F T → F F → - F F → (E) F → const

Simplified Action Routine Example More simplified example: E → E + T E1.ptr:= make_bin_op(“+”, E2.ptr, T.ptr) E → E - T E1.ptr:= make_bin_op(“-”, E2.ptr, T.ptr) E → T E.ptr:= T.ptr T → T * F T1.ptr:= make_bin_op(“x”, T2.ptr, F.ptr) T → T / F T1.ptr:= make_bin_op(“/”, T2.ptr, F.ptr) T → F T.ptr:= F.ptr F → - F F1.ptr:= make_un_op(“+/-”, F2.ptr) (+/- - indicates a change of sign, similar to on calculators) F → (E) F.ptr:=E.ptr F → const F.ptr:=make_leaf(const.val)