semantic analysis (section 4.1- 4.4)

23
1 Semantic Analysis Semantic Analysis (Section 4.1- 4.4) (Section 4.1- 4.4) CSCI 431 Programming Languages CSCI 431 Programming Languages Fall 2003 Fall 2003 A modification of slides A modification of slides developed by Felix Hernandez- developed by Felix Hernandez- Campos at UNC Chapel Hill Campos at UNC Chapel Hill

Upload: rooney-chaney

Post on 03-Jan-2016

39 views

Category:

Documents


1 download

DESCRIPTION

Semantic Analysis (Section 4.1- 4.4). CSCI 431 Programming Languages Fall 2003. A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill. Source Code. Interpre- tation. Target Code. Semantic Analysis From Code Form To Program Meaning. Compiler or Interpreter - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Semantic Analysis (Section 4.1- 4.4)

11

Semantic AnalysisSemantic Analysis(Section 4.1- 4.4)(Section 4.1- 4.4)

CSCI 431 Programming LanguagesCSCI 431 Programming Languages

Fall 2003Fall 2003

A modification of slides developed by Felix A modification of slides developed by Felix Hernandez-Campos at UNC Chapel HillHernandez-Campos at UNC Chapel Hill

Page 2: Semantic Analysis (Section 4.1- 4.4)

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: Semantic Analysis (Section 4.1- 4.4)

33

Phases of CompilationPhases of Compilation

Page 4: Semantic Analysis (Section 4.1- 4.4)

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 Grammars (static semantics)Attribute Grammars (static semantics)» Dynamic SemanticsDynamic Semantics

Page 5: Semantic Analysis (Section 4.1- 4.4)

55

The Semantic AnalyzerThe Semantic Analyzer

• The principal job of the semantic analyzer is to The principal job of the semantic analyzer is to enforce static semantic rules. enforce static semantic rules.

• In general, anything that requires the requires the In general, anything that requires the requires the compiler to compare things that are separate by a compiler to compare things that are separate by a long distance or to count things ends up being a long distance or to count things ends up being a matter of matter of semantics.semantics.

• The semantic analyzer also commonly constructs a The semantic analyzer also commonly constructs a syntax tree (usually first), and much of the syntax tree (usually first), and much of the information it gathers is needed by the code information it gathers is needed by the code generator.generator.

Page 6: Semantic Analysis (Section 4.1- 4.4)

66

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 7: Semantic Analysis (Section 4.1- 4.4)

77

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 8: Semantic Analysis (Section 4.1- 4.4)

88

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 9: Semantic Analysis (Section 4.1- 4.4)

99

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 10: Semantic Analysis (Section 4.1- 4.4)

1010

Attribute Flow Attribute Flow ExampleExample

Page 11: Semantic Analysis (Section 4.1- 4.4)

1111

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 12: Semantic Analysis (Section 4.1- 4.4)

1212

Attribute FlowAttribute FlowInherited AttributesInherited Attributes

• L-attributedL-attributed grammars, such as the one on the next slide, can grammars, such as the one on the next slide, can still be evaluated in a single left-to-right pass over the input.still be evaluated in a single left-to-right pass over the input.

• Each synthetic attribute of a LHS symbol (by definition of Each synthetic attribute of a LHS symbol (by definition of syntheticsynthetic)depends only on attributes of its RHS symbols.)depends only on attributes of its RHS symbols.

• Each inherited attribute of a RHS symbol (by definition of Each inherited attribute of a RHS symbol (by definition of L-L-attributed) attributed) depends only on inherited attributes of the LHS depends only on inherited attributes of the LHS symbol or on synthetic or inherited attributes of symbols to its symbol or on synthetic or inherited attributes of symbols to its left in the RHS.left in the RHS.

• Top-down grammars generally require non-S-attributed flowsTop-down grammars generally require non-S-attributed flows– The previous annotated grammar was an S-attributed LR(1)The previous annotated grammar was an S-attributed LR(1)– L-attributed grammars are the most general class of attribute L-attributed grammars are the most general class of attribute

grammars that can be evaluated during an LL parse.grammars that can be evaluated during an LL parse.

Page 13: Semantic Analysis (Section 4.1- 4.4)

1313

LL GrammarLL Grammar

Page 14: Semantic Analysis (Section 4.1- 4.4)

1414

Non-S-Attributed GrammarsNon-S-Attributed GrammarsExampleExample

Page 15: Semantic Analysis (Section 4.1- 4.4)

1515

Syntax TreeSyntax Tree

• There is considerable variety in the extent to which There is considerable variety in the extent to which parsing, semantic analysis, and intermediate code parsing, semantic analysis, and intermediate code generation are interleaved. generation are interleaved.

• A A one-passone-pass compiler interleaves scanning, parsing, compiler interleaves scanning, parsing, semantic analysis, and code generation in a single semantic analysis, and code generation in a single traversal of the input.traversal of the input.

• A common approach interleaves construction of a A common approach interleaves construction of a syntax tree with parsing (eliminating the need to syntax tree with parsing (eliminating the need to build an explicit parse tree), then follows with build an explicit parse tree), then follows with separate, sequential phases for semantic analysis and separate, sequential phases for semantic analysis and code generation.code generation.

Page 16: Semantic Analysis (Section 4.1- 4.4)

1616

Bottom-up Attribute Grammar to Bottom-up Attribute Grammar to Construct a Syntax TreeConstruct a Syntax Tree

Page 17: Semantic Analysis (Section 4.1- 4.4)

1717

Construction of the Syntax TreeConstruction of the Syntax Tree

Page 18: Semantic Analysis (Section 4.1- 4.4)

1818

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 19: Semantic Analysis (Section 4.1- 4.4)

1919

Action Rules for the Previous Action Rules for the Previous LL(1) attribute grammarLL(1) attribute grammar

E => T { TT.st := T.v } TT { E.v := TT.v }E => T { TT.st := T.v } TT { E.v := TT.v }

TT => + T { TT2.st := TT1.st + T.v } TT { TT1.v := TT2.v }TT => + T { TT2.st := TT1.st + T.v } TT { TT1.v := TT2.v }

TT => - T { TT2.st := TT1.st - T.v } TT { TT1.v := TT2.v }TT => - T { TT2.st := TT1.st - T.v } TT { TT1.v := TT2.v }

TT => { TT.v := TT.st }TT => { TT.v := TT.st }

T => F { FT.st := F.v } FT { T.v := FT.v }T => F { FT.st := F.v } FT { T.v := FT.v }

FT => * F { FT2.st := FT1.st * F.v } FT { FT1.v := FT2.v }FT => * F { FT2.st := FT1.st * F.v } FT { FT1.v := FT2.v }

FT => / F { FT2.st := FT1.st / F.v } FT { FT1.v := FT2.v }FT => / F { FT2.st := FT1.st / F.v } FT { FT1.v := FT2.v }

FT => { FT.v := FT.st }FT => { FT.v := FT.st }

F => - F { F1.v := - F2.v }F => - F { F1.v := - F2.v }

F => ( E ) { F.v := E.v }F => ( E ) { F.v := E.v }

F => const { F.v := C.v }F => const { F.v := C.v }

Page 20: Semantic Analysis (Section 4.1- 4.4)

2020

Action RulesAction Rules

• The ease with which rules were incorporated in the The ease with which rules were incorporated in the grammar is due to the fact that the attribute grammar grammar is due to the fact that the attribute grammar is is L-attributedL-attributed..

• The action rules forThe action rules for L-attributed L-attributed grammars, in which grammars, in which the attribute flow is depth-first left-to-right, can be the attribute flow is depth-first left-to-right, can be evaluated in the order of the parse tree prediction for evaluated in the order of the parse tree prediction for LL grammars.LL grammars.

• Action rules for Action rules for S-attributedS-attributed grammars can be grammars can be incorporated at the end of the right-hand sides of LR incorporated at the end of the right-hand sides of LR grammars. But, if action rules are responsible for a grammars. But, if action rules are responsible for a significant part of the semantic analysis, they will significant part of the semantic analysis, they will need more contextual information to do their job.need more contextual information to do their job.

Page 21: Semantic Analysis (Section 4.1- 4.4)

2121

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 22: Semantic Analysis (Section 4.1- 4.4)

2222

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 23: Semantic Analysis (Section 4.1- 4.4)

2323

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://java.sun.com/docs/books/jls/first_edition/html/14.dohttp://java.sun.com/docs/books/jls/first_edition/html/14.doc.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