1 comp 144 programming language concepts felix hernandez-campos lecture 12: semantic analysis comp...

18
COMP 144 Programming Language Concepts Felix Hernandez-Campos 1 Lecture 12: Lecture 12: Semantic Analysis Semantic Analysis COMP 144 Programming Language COMP 144 Programming Language Concepts Concepts Spring 2002 Spring 2002 Felix Hernandez-Campos Felix Hernandez-Campos Feb 6 Feb 6 The University of North Carolina at The University of North Carolina at Chapel Hill Chapel Hill

Post on 19-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

11

Lecture 12: Lecture 12: Semantic AnalysisSemantic Analysis

COMP 144 Programming Language ConceptsCOMP 144 Programming Language Concepts

Spring 2002Spring 2002

Felix Hernandez-CamposFelix Hernandez-Campos

Feb 6Feb 6

The University of North Carolina at Chapel HillThe University of North Carolina at Chapel Hill

Page 2: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

22

Semantic AnalysisSemantic AnalysisFrom Code Form To Program MeaningFrom Code Form To Program Meaning

Compiler or InterpreterCompiler or Interpreter

Translation Translation ExecutionExecution

Source CodeSource Code

Target CodeTarget Code

Interpre-Interpre-tationtation

Page 3: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

33

Phases of CompilationPhases of Compilation

Page 4: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

44

Specification of Programming LanguagesSpecification of Programming Languages

• PLs require precise definitions (i.e. no PLs require precise definitions (i.e. no ambiguityambiguity))– LanguageLanguage form form (Syntax)(Syntax)– LanguageLanguage meaning meaning (Semantics) (Semantics)

• Consequently, PLs are specified using formal Consequently, PLs are specified using formal notation:notation:

– Formal syntaxFormal syntax» TokensTokens» GrammarGrammar

– Formal semanticsFormal semantics» Attribute GrammarsAttribute Grammars» Dynamic SemanticsDynamic Semantics

Page 5: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

55

Attribute GrammarsAttribute Grammars

• Context-Free Grammars (CFGs) are used to specify Context-Free Grammars (CFGs) are used to specify the syntax of programming languagesthe syntax of programming languages

– E.g.E.g. arithmetic expressions arithmetic expressions

• How do we tie these rules to How do we tie these rules to mathematical concepts?mathematical concepts?

• Attribute grammarsAttribute grammars are annotated are annotated CFGs in which CFGs in which annotationsannotations are are used to establish meaning used to establish meaning relationships among symbolsrelationships among symbols

– Annotations are also known as Annotations are also known as decorationsdecorations

Page 6: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

66

Attribute GrammarsAttribute GrammarsExampleExample

• Each grammar symbols Each grammar symbols has a set of has a set of attributesattributes

– E.g.E.g. the value of E the value of E11 is the is the attribute attribute EE11.val.val

• Each grammar rule has Each grammar rule has a set of rules over the a set of rules over the symbol attributessymbol attributes

– Copy rulesCopy rules– Semantic Function rulesSemantic Function rules

» E.g. E.g. sumsum, , quotientquotient

Page 7: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

77

Attribute FlowAttribute Flow

• Context-free grammars are not tied to an specific Context-free grammars are not tied to an specific parsing orderparsing order

– E.g.E.g. Recursive descent, LR parsing Recursive descent, LR parsing

• Attribute grammars are not tied to an specific Attribute grammars are not tied to an specific evaluation orderevaluation order

– This evaluation is known as the This evaluation is known as the annotationannotation or or decorationdecoration of the parse treeof the parse tree

Page 8: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

88

Attribute Flow Attribute Flow ExampleExample

• The figure shows the The figure shows the result of annotating the result of annotating the parse tree for parse tree for (1+3)*2(1+3)*2

• Each symbols has at most Each symbols has at most one attribute shown in the one attribute shown in the corresponding boxcorresponding box

– Numerical value in this Numerical value in this exampleexample

– Operator symbols have no Operator symbols have no valuevalue

• Arrows represent Arrows represent attribute attribute flowflow

Page 9: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

99

Attribute Flow Attribute Flow ExampleExample

Page 10: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1010

Attribute FlowAttribute FlowSynthetic and Inherited AttributesSynthetic and Inherited Attributes

• In the previous example, semantic information is In the previous example, semantic information is pass up the parse treepass up the parse tree

– We call this type of attributes are called We call this type of attributes are called synthetic synthetic attributesattributes

– Attribute grammar with synthetic attributes only are said Attribute grammar with synthetic attributes only are said to be to be S-attributedS-attributed

• Semantic information can also be passed down the Semantic information can also be passed down the parse treeparse tree

– Using Using inherited attributesinherited attributes– Attribute grammar with inherited attributes only are said Attribute grammar with inherited attributes only are said

to be to be non-S-attributednon-S-attributed

Page 11: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1111

Attribute FlowAttribute FlowInherited AttributesInherited Attributes

• Semantic information can also be passed down the Semantic information can also be passed down the parse treeparse tree

– Using Using inherited attributesinherited attributes– Attribute grammar with inherited attributes only are said Attribute grammar with inherited attributes only are said

to be to be non-S-attributednon-S-attributed

• Top-down grammars generally require non-S-Top-down grammars generally require non-S-attributed flowsattributed flows

– The previous annotated grammar was an S-attributed The previous annotated grammar was an S-attributed LR(1)LR(1)

Page 12: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1212

Non-S-Attributed GrammarsNon-S-Attributed GrammarsExampleExample

Page 13: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1313

Action RoutinesAction Routines

• Automatic tools can construct a parser for a given Automatic tools can construct a parser for a given context-free grammarcontext-free grammar

– E.g. E.g. yaccyacc

• Automatic tools can construct a semantic analyzer Automatic tools can construct a semantic analyzer for an attribute grammarfor an attribute grammar

– An ad hoc techniques is to annotate the grammar with An ad hoc techniques is to annotate the grammar with executable rulesexecutable rules

– These rules are known as These rules are known as action routinesaction routines

Page 14: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1414

Action RoutinesAction RoutinesExampleExample

Page 15: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1515

Static and Dynamic SemanticsStatic and Dynamic Semantics

• Attribute grammars add basic semantic rules to the Attribute grammars add basic semantic rules to the specification of a languagespecification of a language

– They specify They specify static semanticsstatic semantics

• But they are limited to the semantic form that can be But they are limited to the semantic form that can be checked at compile timechecked at compile time

• Other semantic properties cannot be checked at Other semantic properties cannot be checked at compile timecompile time

– They are described using They are described using dynamic semanticsdynamic semantics

Page 16: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1616

Dynamic SemanticsDynamic Semantics

• Use to formally specify the behavior of a Use to formally specify the behavior of a programming languageprogramming language

– Semantic-based error detectionSemantic-based error detection– Correctness proofsCorrectness proofs

• There is not a universally accepted notationThere is not a universally accepted notation– Operational semanticsOperational semantics

» Executing statements that represent changes in the state of a real Executing statements that represent changes in the state of a real or simulated machineor simulated machine

– Axiomatic semanticsAxiomatic semantics» Using predicate calculus (pre and post-conditions)Using predicate calculus (pre and post-conditions)

– Denotational semanticsDenotational semantics» Using recursive function theoryUsing recursive function theory

Page 17: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1717

Semantic SpecificationSemantic Specification

• The most common way of The most common way of specifyingspecifying the semantics the semantics of a language is plain englishof a language is plain english

– http://www.python.org/doc/current/ref/binary.htmlhttp://www.python.org/doc/current/ref/binary.html– http://www.python.org/doc/current/ref/while.htmlhttp://www.python.org/doc/current/ref/while.html– http://java.sun.com/docs/books/jls/first_edition/html/14.dohttp://java.sun.com/docs/books/jls/first_edition/html/14.do

c.html#24588c.html#24588

• There is a lack of formal rigor in the semantic There is a lack of formal rigor in the semantic specification of programming languagesspecification of programming languages

– Guess whyGuess why

Page 18: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1818

Reading AssignmentReading Assignment

• Scott’s chapter 4Scott’s chapter 4– Section 4.1Section 4.1– Section 4.2Section 4.2– Section 4.3Section 4.3– Section 4.4Section 4.4