Assignment e Semantic Analysis by fjn47816

VIEWS: 14 PAGES: 18

									 Assignment e:
Semantic Analysis

Compiler Design 1 @ ETH Zurich
          April 9, 2009
        Stephanie Balzer
                Assignment 4
•   Develop a semantic analyzer

    • for the complete Javali language
    • according to the Javali language specification.
•   The semantic analyzer must gurantee

    • that a Javali program can be safely executed
    • according to the semantic rules of Javali.
•   Semantically incorrect Javali programs accepted by the
    parser must latest be rejected by the semantic analyzer.



                                                               2
                      Overview
•   I will discuss:

    • Symbols
    • Symbol table
    • Your tasks.
•   Your turn:

    •   Read language specification
    •   Read assignment sheet for a overview of the semantic
        checks
    •   Implement.

                                                               3
                 Semantic analysis
                                               semantic      semantic
         lexer                parser
                                               checker        failures


         lexical/syntax analysis              semantic analysis

•   Lexical analysis (linear analysis, scanning):

     •  reads stream of characters from left to right
     •  groups character stream into tokens
•   Syntax analysis (hierarchical analysis, parsing):

     •   groups tokens hierarchically
     •   nested collections with collective meaning
                                                                         4
                 Semantic analysis
                                          semantic       semantic
         lexer                parser
                                          checker         failures


         lexical/syntax analysis          semantic analysis

•   Semantic analysis:

     •   Checks that the syntactically well formed program
         components fit meaningfully together.
     •   Rejects any programs that are not “meaningful” by
         generating a semantic failure.
     •   The language specification defines what it means for a
         program to semantically correct.
                                                                     5
        Semantic checker’s tasks
•   Check class declarations, e.g.,:

    •   There exists a unique starting point (main method).
    •   No class is declared with the name Object.
    •   The superclass specified for each class exists.
    •   There are no two fields/methods with the same name
        in the same class.
    •   Overridden methods must have the same number
        and types of parameters as the corresponding
        superclass’ method.
    •   etc. (see assignment sheet)

                                                              6
        Semantic checker’s tasks
•   Check method bodies, e.g.,:

    •   The condition for an if or while statement must be of
        boolean type.
    •   Binary and unary arithmetic operators must take
        arguments of integer types.
    •   For assignments, the type of the RHS must be a
        subtype of the type of the LHS.
    •   A cast from type R to type C is only legal if R is a
        subtype of C or vice-versa.
    •   etc. (see assignment sheet)

                                                                7
                            Symbols
•   Analyzer relies on symbols to perform semantic checks.

•   A symbol:

    •   Represents a “program element” (i.e., declaration).
    •   Encodes all the information about the element that
        are needed by the semantic checker and later by the
        code generator:
        •   Kind of a variable (parameter, local, field)
        •   Type of a variable
        •   Superclass of a class
        •   etc.

•   AST nodes must link to corresponding symbols.
                                                              8
               Fragment symbols

                          Symbol        cd.ir.Symbol.java




  VariableSymbol      MethodSymbol       TypeSymbol




PrimitiveTypeSymbol   ArrayTypeSymbol    ClassSymbol


                                                            9
          Example information
•   Symbol

    • name
•   TypeSymbol

    • flag whether is reference type
•   VariableSymbol

    •   kind: PARAM, LOCAL, FIELD
    •   type: reference to a TypeSymbol
    •   ast: reference to Ast.VarDecl


                                          10
          Example information
•   ClassSymbol

    •   ast: reference to Ast.ClassDecl
    •   superClass: reference to ClassSymbol
    •   thisSymbol: reference to VariableSymbol
    •   fields: Map<String,VariableSymbol>
    •   methods: <String, MethodSymbol>




                                                  11
          Example information
•   MethodSymbol

    •   ast: reference to Ast.MethodDecl
    •   locals: Map<String,VariableSymbol>
    •   parameters: List<VariableSymbol>




                                             12
                  Symbol table
•   Symbol table:

    •   Look-up table: Name ➞ Symbol
    •   Data structure containing symbols
    •   Allows retrieval of a symbol by its name
    •   Provides lexical scoping information.




                                                   13
                Lexical scoping
•   Javali uses lexical scoping (see language spec).

•   A scope determines the visibility of a program element

•   Names of program elements within a scope must be
    unique.

•   Scopes can be nested. Declarations of inner scopes hide
    any equally named declarations of outer scopes.

•   Javali scopes are:

    •   Superclass (inherited members)
    •   Class (members)
    •   Method (parameters and local variables)               14
                       Fragment
•   We provide:

    •cd.semantic.SemanticAnalyzer.java: use as starting
     point
    •cd.ir.Symbol.java: represent symbols
    •cd.ir.Ast.java: update to provide links to symbols
    •cd.exceptions.SemanticFailur.java: throw to indicate
     semantic errors.
•   We do not provide:

    •   Symbol table

                                                            15
            Preliminary steps
•   Create symbols

    •For every class
    •For every array of each type
    •For every method and its parameters and locals
    •For every field
    •Don’t forget: integer, boolean, Object, null
•   Complement AST with links to symbols

•   Create symbol table (or later)


                                                      16
             Semantic checks
•   See assignment sheet.

•   On error detection, the semantic analyzer must throw
    an instance of SemanticFailure.

•   Indicate cause (see enumeration) as specified by the
    assignment sheet.

•   Testing framework: only examines cause but not
    attached error message.




                                                           17
                          Hints
•   No need for a single symbol table

    •  You may want to separate types from variables.
    •  You may want to link various symbol tables to reflect
       the lexical scoping.
•   You may find visitors (AST and expression) again useful,
    e.g., to:

    •   check the well-formedness of declarations (e.g.,
        correct method override)
    •   type expressions


                                                              18

								
To top