the functions and purposes of translators

15
The Functions and The Functions and Purposes of Purposes of Translators Translators Syntax (& Semantic) Syntax (& Semantic) Analysis Analysis

Upload: jayme-bowen

Post on 30-Dec-2015

29 views

Category:

Documents


0 download

DESCRIPTION

The Functions and Purposes of Translators. Syntax (& Semantic) Analysis. Learning Objectives. Describe what happens during syntax analysis, explaining how errors are handled. Translation Process. Syntax (& Semantic) Analysis. Semantic analysis: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: The Functions and Purposes of Translators

The Functions and The Functions and Purposes of TranslatorsPurposes of Translators

Syntax (& Semantic) AnalysisSyntax (& Semantic) Analysis

Page 2: The Functions and Purposes of Translators

Learning ObjectivesLearning Objectives

Describe what happens during syntax analysis, explaining how errors are handled.

Page 3: The Functions and Purposes of Translators

Translation ProcessTranslation Process

Page 4: The Functions and Purposes of Translators

Syntax Syntax (& Semantic)(& Semantic) Analysis Analysis

Semantic analysis:Semantic analysis: Check the meaning of each sequence of Check the meaning of each sequence of

tokens (each sentence).tokens (each sentence). A A ParserParser is the component of a compiler that is the component of a compiler that

carries out this task.carries out this task. The grammar of programming languages is The grammar of programming languages is

defined by means of BNF notation or syntax defined by means of BNF notation or syntax diagrams (see later).diagrams (see later).

Page 5: The Functions and Purposes of Translators

Example – Grammar RuleExample – Grammar Rule

<assignment statement> <assignment statement>

should be:should be:

<variable> <assignment_operator> <expression><variable> <assignment_operator> <expression>

<expression><expression>

should be:should be:

<variable> <arithmetic_operator> <variable><variable> <arithmetic_operator> <variable>

Page 6: The Functions and Purposes of Translators

Example – Grammar Check -ValidExample – Grammar Check -Validsum = sum + numbersum = sum + number

converted to tokens by lexical analysis becomes:converted to tokens by lexical analysis becomes:

<variable> <assignment_operator> <variable> <variable> <assignment_operator> <variable> <arithmetic_operator> <variable><arithmetic_operator> <variable>

which becomeswhich becomes

<variable> <assignment_operator> <expression><variable> <assignment_operator> <expression>

and thenand then

<assignment statement><assignment statement>

which is valid.which is valid.

Page 7: The Functions and Purposes of Translators

Example – Grammar Check - InvalidExample – Grammar Check - Invalid

sum = sum + + numbersum = sum + + number

converted to tokens by lexical analysis becomes:converted to tokens by lexical analysis becomes:

<variable> <assignment_operator> <variable> <variable> <assignment_operator> <variable> <arithmetic_operator> <arithmetic_operator> <variable><arithmetic_operator> <arithmetic_operator> <variable>

This does not represent a valid statement hence an error This does not represent a valid statement hence an error message will be returned.message will be returned.

Another example could be an invalid number of brackets.Another example could be an invalid number of brackets.

Page 8: The Functions and Purposes of Translators

If the high-level language If the high-level language doesdoes require variables to be declared.require variables to be declared.

Lexical (& Syntax) Analysis

- Including syntax analysis e.g. misspelling of reserved words – PRT instead of PRINT

Semantic Analysis

- Check the meaning of each sequence of tokens (each sentence).

Page 9: The Functions and Purposes of Translators

If the high-level language If the high-level language does does notnot forceforce declaration of variables. declaration of variables.

Lexical Analysis

- Does not include syntax analysis as all words which are not recognised would be assumed to be variables. e.g. PRT would be assumed to be a variable.

Syntax (& Semantic) Analysis

-Check the meaning of each sequence of tokens (each sentence).- Incorrectly spelt keywords would be picked up here as the tokens would not fit the rules. e.g. PRT X would have tokenised to <Variable><Variable> but its lack of sense would be noticed at this stage.

Page 10: The Functions and Purposes of Translators

Syntax Syntax (& Semantic)(& Semantic) Analysis AnalysisLabel checksLabel checks Checking to see if a procedure or function which is being Checking to see if a procedure or function which is being

called or used actually exists called or used actually exists (only relevant if the language (only relevant if the language does not force the programmer to declare variables & write does not force the programmer to declare variables & write procedures before using them)procedures before using them)..

Flow of control checksFlow of control checks Certain control constructs can only be placed in certain Certain control constructs can only be placed in certain

parts of a program. parts of a program. E.g. In Visual Basic you can only Exit Sub inside a procedure, E.g. In Visual Basic you can only Exit Sub inside a procedure, Exit Function inside a Function or matching End Ifs with Exit Function inside a Function or matching End Ifs with corresponding Ifs etc…corresponding Ifs etc…

Declaration checks. Declaration checks. Check that variables have not been assigned illegal Check that variables have not been assigned illegal

values.values.Contents of variables must be of a specific type so an error will Contents of variables must be of a specific type so an error will be created by the attempted use of anything else.be created by the attempted use of anything else.

E.g. letters to an integer.E.g. letters to an integer.

Page 11: The Functions and Purposes of Translators

Syntax Syntax (& Semantic)(& Semantic) Analysis Analysis

Also:Also: Determines priorities of arithmetic operators in Determines priorities of arithmetic operators in

an expression.an expression.

Page 12: The Functions and Purposes of Translators

Diagnostic Error MessagesDiagnostic Error Messages

During both lexical and Syntax (& During both lexical and Syntax (& Semantic) analysis if any errors are Semantic) analysis if any errors are detected then diagnostic error messages detected then diagnostic error messages will be reported.will be reported.

Page 13: The Functions and Purposes of Translators

The variable identifier.The variable identifier.The kind of variable.The kind of variable. E.g. Simple variable, structured variable such E.g. Simple variable, structured variable such

as an array, procedure, keyword etc …as an array, procedure, keyword etc …

Type of variable.Type of variable. E.g. Integer, decimal, string etc …E.g. Integer, decimal, string etc …

Other Information.Other Information. E.g. Bounds of arrayE.g. Bounds of array

The block in which a variable is valid The block in which a variable is valid ((local local / / globalglobal).).

Symbol TableSymbol Table

Syntax (& Semantic Analysis

Lexical Analysis

Page 14: The Functions and Purposes of Translators

PlenaryPlenary

Describe what happens during syntax Describe what happens during syntax analysis, explaining how errors are analysis, explaining how errors are handled.handled.

Page 15: The Functions and Purposes of Translators

PlenaryPlenary

(Tokens) are analysed to check for grammatical (Tokens) are analysed to check for grammatical correctness (form valid sentences)correctness (form valid sentences)(Code)/reserved word checked against rules(Code)/reserved word checked against rulesInvalid number of brackets foundInvalid number of brackets foundDetermine priorities of arithmetic operators in an Determine priorities of arithmetic operators in an expressionexpressionProduce intermediate codeProduce intermediate codeDiagnostic error messages are reportedDiagnostic error messages are reportedLabel checksLabel checksFlow of control checksFlow of control checksDeclaration checksDeclaration checks