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

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Compilers and Language Translation
The Functions and Purposes of Translators Code Generation (Intermediate Code, Optimisation, Final Code), Linkers & Loaders.
 Lex helps to specify lexical analyzers by specifying regular expression  i/p notation for lex tool is lex language and the tool itself is refered to.
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 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
CPSC Compiler Tutorial 9 Review of Compiler.
Chapter3: Language Translation issues
Chapter 9 Compilers and Language Translation. The Compilation Process Phase I: Lexical analysis Phase I: Lexical analysis Phase II: Parsing Phase II:
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
1 Problem 2 A Scanner / Parser for Simple C. 2 Outline l Language syntax for SC l Requirements for the scanner l Requirement for the parser l companion.
Compiled by Benjamin Muganzi 3.2 Functions and Purposes of Translators Computing 9691 Paper 3 1.
Invitation to Computer Science 5th Edition
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CIS Computer Programming Logic
INTRODUCTION TO COMPUTING CHAPTER NO. 06. Compilers and Language Translation Introduction The Compilation Process Phase 1 – Lexical Analysis Phase 2 –
COP4020 Programming Languages
Parser-Driven Games Tool programming © Allan C. Milne Abertay University v
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
Chapter 6 Programming Languages (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
© 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Stewart Venit ~ Elizabeth Drake Developing a Program.
D. M. Akbar Hussain: Department of Software & Media Technology 1 Compiler is tool: which translate notations from one system to another, usually from source.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Lexical and Syntax Analysis
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Prog. techniques. Standard prog. techniques Complex programs can be broken down into simpler parts called “Modules” These come in 2 types,“Procedures”
CPS 506 Comparative Programming Languages Syntax Specification.
What on Earth? LEXEMETOKENPATTERN print p,r,i,n,t (leftpar( 4number4 *arith* 5number5 )rightpar) userAnswerID Letter followed by letters and digits “Game.
D Goforth COSC Translating High Level Languages.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
IN LINE FUNCTION AND MACRO Macro is processed at precompilation time. An Inline function is processed at compilation time. Example : let us consider this.
1 Program Planning and Design Important stages before actual program is written.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
ERRORS. Types of errors: Syntax errors Logical errors.
What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
 Fall Chart 2  Translators and Compilers  Textbook o Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000,
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
Parser: CFG, BNF Backus-Naur Form is notational variant of Context Free Grammar. Invented to specify syntax of ALGOL in late 1950’s Uses ::= to indicate.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
FG Group -Afrilia BP -Liana F.B.I -Maulidatun Nisa -Riza Amini F.
Component 1.6.
Compiler Design (40-414) Main Text Book:
Chapter 10 Programming Fundamentals with JavaScript
Component 1.6.
Project 1: Part b SPECIFICATION
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Lexical and Syntax Analysis
System Software Unit-1 (Language Processors) A TOY Compiler
A Simple Syntax-Directed Translator
Revision Lecture
Formal Language Theory
Chapter 10 Programming Fundamentals with JavaScript
CSE 3302 Programming Languages
Compilers B V Sai Aravind (11CS10008).
R.Rajkumar Asst.Professor CSE
CMPE 152: Compiler Design August 21/23 Lab
Software Development Process
High-Level Programming Language
Chapter 10: Compilers and Language Translation
WJEC GCSE Computer Science
Presentation transcript:

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: Check the meaning of each sequence of tokens (each sentence). Check the meaning of each sequence of tokens (each sentence). A Parser is the component of a compiler that carries out this task. A Parser is the component of a compiler that carries out this task. The grammar of programming languages is defined by means of BNF notation or syntax diagrams (see later). The grammar of programming languages is defined by means of BNF notation or syntax diagrams (see later).

Example – Grammar Rule should be: <expression> should be:

Example – Grammar Check -Valid sum = sum + number converted to tokens by lexical analysis becomes: which becomes and then which is valid.

Example – Grammar Check - Invalid sum = sum + + number converted to tokens by lexical analysis becomes: This does not represent a valid statement hence an error message will be returned. Another example could be an invalid number of brackets.

If the high-level language does 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).

If the high-level language does not force 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 but its lack of sense would be noticed at this stage.

Syntax (& Semantic) Analysis Label checks Checking to see if a procedure or function which is being called or used actually exists (only relevant if the language does not force the programmer to declare variables & write procedures before using them). Checking to see if a procedure or function which is being called or used actually exists (only relevant if the language does not force the programmer to declare variables & write procedures before using them). Flow of control checks Certain control constructs can only be placed in certain parts of a program. Certain control constructs can only be placed in certain parts of a program. E.g. In Visual Basic you can only Exit Sub inside a procedure, Exit Function inside a Function or matching End Ifs with corresponding Ifs etc… Declaration checks. Check that variables have not been assigned illegal values. Check that variables have not been assigned illegal values. Contents of variables must be of a specific type so an error will be created by the attempted use of anything else. E.g. letters to an integer. E.g. letters to an integer.

Syntax (& Semantic) Analysis Also: Determines priorities of arithmetic operators in an expression. Determines priorities of arithmetic operators in an expression.

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

The variable identifier. The kind of variable. E.g. Simple variable, structured variable such as an array, procedure, keyword etc … E.g. Simple variable, structured variable such as an array, procedure, keyword etc … Type of variable. E.g. Integer, decimal, string etc … E.g. Integer, decimal, string etc … Other Information. E.g. Bounds of array E.g. Bounds of array The block in which a variable is valid (local / global). Symbol Table Syntax (& Semantic Analysis Lexical Analysis

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

Plenary (Tokens) are analysed to check for grammatical correctness (form valid sentences) (Code)/reserved word checked against rules Invalid number of brackets found Determine priorities of arithmetic operators in an expression Produce intermediate code Diagnostic error messages are reported Label checks Flow of control checks Declaration checks