Semantic analysis Jakub Yaghob

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Error Handling A compiler should: detect errors locate errors recover from errors Errors may occur in any of the three main analysis phases of compiling:
Semantics Static semantics Dynamic semantics attribute grammars
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.
Lecture # 7 Chapter 4: Syntax Analysis. What is the job of Syntax Analysis? Syntax Analysis is also called Parsing or Hierarchical Analysis. A Parser.
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,
Context-sensitive Analysis. Beyond Syntax There is a level of correctness that is deeper than grammar fie(a,b,c,d) int a, b, c, d; { … } fee() { int f[3],g[0],
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
Syntax-Directed Translation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Syntax Directed Translation. Tokens Parser Semantic checking TAC Peephole, pipeline, …… TAC  assembly code/mc Cmm subexpression,……
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.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Overview of Previous Lesson(s) Over View  An ambiguous grammar which fails to be LR and thus is not in any of the classes of grammars i.e SLR, LALR.
Joey Paquet, 2000, Lecture 5 Error Recovery Techniques in Top-Down Predictive Syntactic Analysis.
Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are.
Scribe Sumbission Date: 28 th October, 2013 By M. Sudeep Kumar.
Semantic Analysis CPSC 388 Ellen Walker Hiram College.
Chapter 5. Syntax-Directed Translation. 2 Fig Syntax-directed definition of a simple desk calculator ProductionSemantic Rules L  E n print ( E.val.
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
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.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Syntax Directed Definition and Syntax directed Translation
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
Joey Paquet, 2000, 2002, 2008, 2012, Lecture 5 Error Recovery Techniques in Top-Down Predictive Syntactic Analysis.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
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.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 9 Ahmed Ezzat Semantic Analysis.
Lecture 9 Symbol Table and Attributed Grammars
Syntax-Directed Translation
Context-Sensitive Analysis
A Simple Syntax-Directed Translator
Constructing Precedence Table
Compiler Construction
Chapter 5 Syntax Directed Translation
Syntax Analysis Chapter 4.
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.
Syntax-Directed Translation Part I
Shift Reduce Parsing Unit -3
CS 3304 Comparative Languages
Syntax Analysis Sections :.
Chapter 5. Syntax-Directed Translation
Lecture 11: Context Sensitive Analysis
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
SYNTAX DIRECTED TRANSLATION
Context-sensitive Analysis
Ambiguity in Grammar, Error Recovery
COMPILER DESIGN 11CS30013 & 11CS30014 Group October 2013
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 4 Action Routines.
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Syntax-Directed Translation Part I
COP4020 Programming Languages
COP4020 Programming Languages
Chapter 5 Syntax Directed Translation
Presentation transcript:

Semantic analysis Jakub Yaghob Compiler principles Semantic analysis Jakub Yaghob

Syntax-directed definitions Each grammar symbol has an associated set of attributes Like a record Two kinds of attributes Synthesized Inherited Attributes can represent anything Attribute values defined by semantic rules assigned to grammar productions The order of evaluation of semantic rules is determined by the dependency graph Evaluation of semantic rules defines values of attributes

Kinds of attributes Each production A→α has associated with it a set of semantic rules of the form b=f(c1,…,ck), where f is a function, ci are grammar symbols attributes from the given production, and either b is a synthesized attribute of nonterminal A b is an inherited attribute of a grammar symbol on the right side of the production

Attribute grammar Syntax tree with attribute values is called annotated (colored) syntax tree Attributed grammar is a syntax directed definition, where functions (semantic rules) don’t have side effects

Attributed grammar for our grammar E → ER + T E → T T → TR * F T → F F → ( E ) F → uint E.val = ER.val + T.val E.val = T.val T.val = TR.val * F.val T.val = F.val F.val = E.val F.val = uint.lexval

Example of annotated tree 3+4*5 E.val=23 E.val=3 + T.val=20 T.val=3 T.val=4 F.val=5 * F.val=3 F.val=4 uint.lexval=5 uint.lexval=3 uint.lexval=4

Synthesized attributes An attribute of a nonterminal from the left side of a production is evaluated based on attributes of symbols from the right side of the production Extensively used in practice E.g. expression evaluation Attributed grammar, which uses only synthesized attributes, is called S-attributed grammar (purely synthesized attributed grammar) S-attributed grammar is easily used in bottom-up analysis Evaluation during reduction Attributes for grammar symbols lie on parser stack

Inherited attributes The value of an inherited attribute is evaluated based on parent and/or siblings attributes They are used for expressing dependency of a syntax construction on a context E.g. whether an identifier is on left or right side of an assignment We can always rewrite an attribute grammar to a S-attributed grammar

Example of inherited attributes D → T L ; T → int T → double L → LR , id L → id L.in = T.typ T.typ = int T.typ = double LR.in = L.in id.typ = L.in

Dependency graph Construction for a syntax tree Nodes are created for each attribute of each node of the syntax tree For each semantic rule b=f(c1,…,ck) construct directed edges from a node of the dependency graph representing ci to the node representing b

Evaluation order Any topological sort of a dependency graph gives a valid order in which the semantic rules associated with the nodes in a syntax tree can be evaluated If the dependency graph contains a circle, we are not able to determine any evaluation order

L-attributed grammar Czech alias “jednoduše zleva-doprava 1-průchodová gramatika” Attributed grammar is L-attributed, if each inherited attribute of a symbol Xj on the right side of A→X1…Xn depends only on The attributes of the symbols X1,…,Xj-1 (to the left of Xj) The inherited attributes of A Used for direct attribute evaluation in top-down analysis Every S-attributed grammar is L-attributed

Syntax tree traversal If the attribute grammar isn’t L-attributed grammar, it will not be possible to evaluate attributes directly during parsing We need to fully construct syntax tree. It will be traversed (possibly several times) during the semantic analysis. Several attributes, which we were unable to evaluate during syntax-directed translation, will be evaluated during the traversal

Static checking during translation Type checking Incorrect operand type of an operator Pointer multiplication Checking control flow If the change in control flow is legal Break statement in C, if it is not in switch or a loop Uniqueness checking Some objects can be defined only once Labels in a function, global objects identifiers Name checking Some constructions must have the same name at the start and at the end Assembler procedures

Symbolic tables Growing tables Stack tables Constants Identifier visibility in blocks Simple implementation Visibility linked list for a block (stack) Used implementation Identifiers „colored“ by a unique block number

Error handling The compiler must find all errors in the input word and must not show non-existent errors Error reporting Clearly and accurately Recover from an error quickly enough and continue in translation Do not significantly slow down the processing of a correct input Introduced errors Imprecise recovery from a previous error causes inception of non-existent errors

Types of errors Lexical errors Syntax errors Semantic errors Malformed lexical elements Unfinished string and the EOL Error recovery by ignoring the error Syntax errors The input word is not in an input language Unpaired parenthesis Semantic errors Static checks Undeclared variable, wrong number of parameters in a function call, wrong type used with an operator Logical errors Errors in programming Indefinite loop, uninitialized variable

Syntax errors recovery Panic mode A set of skeletal symbols When an error is encountered, skip all symbols until a symbol from the skeletal set is found Then the parser is put into a known state Productions modifications Insert, remove, replace a terminal in a production Intentional error production Grammar augmentation with usual errors with specific error message E.g. assignment in Pascal