Faculty of Computer Science and Information System

Slides:



Advertisements
Similar presentations
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Advertisements

Context-Free Grammars Lecture 7
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
COP4020 Programming Languages
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
ISBN Chapter 3 Describing Syntax and Semantics.
CPS 506 Comparative Programming Languages Syntax Specification.
LESSON 04.
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Introduction to Parsing
CS 3304 Comparative Languages
5. Context-Free Grammars and Languages
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Compiler Design (40-414) Main Text Book:
LESSON 16.
Lexical and Syntax Analysis
A Simple Syntax-Directed Translator
CS510 Compiler Lecture 4.
Parsing and Parser Parsing methods: top-down & bottom-up
Introduction to Parsing (adapted from CS 164 at Berkeley)
Textbook:Modern Compiler Design
Chapter 3 – Describing Syntax
Overview of Compilation The Compiler Front End
Overview of Compilation The Compiler Front End
What does it mean? Notes from Robert Sebesta Programming Languages
Syntax Analysis Chapter 4.
Even-Even Devise a grammar that generates strings with even number of a’s and even number of b’s.
PROGRAMMING LANGUAGES
Compiler Construction
Syntax versus Semantics
Compiler Construction
CS416 Compiler Design lec00-outline September 19, 2018
CSE 3302 Programming Languages
Compiler Design 4. Language Grammars
Introduction CI612 Compiler Design CI612 Compiler Design.
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
5. Context-Free Grammars and Languages
Chapter 2: A Simple One Pass Compiler
Subject Name:Sysytem Software Subject Code: 10SCS52
R.Rajkumar Asst.Professor CSE
CS 3304 Comparative Languages
Lecture 4: Lexical Analysis & Chomsky Hierarchy
CS 3304 Comparative Languages
Programming Languages 2nd edition Tucker and Noonan
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CS416 Compiler Design lec00-outline February 23, 2019
Theory of Computation Lecture #
BNF 9-Apr-19.
High-Level Programming Language
Chapter 10: Compilers and Language Translation
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Programming Languages 2nd edition Tucker and Noonan
COMPILER CONSTRUCTION
Presentation transcript:

Faculty of Computer Science and Information System 2014-2015 1

A Simple Syntax-Directed Translator Chapter 1 A Simple Syntax-Directed Translator Aims: Front end of a compiler 1. Lexical analysis 2. Parsing 3. Intermediate code generation

Figure 2.1: A code fragment to be translated Output Input Figure 2.2: A Simplified intermediate code for the program fragment in Fig. 2.1 Figure 2.1: A code fragment to be translated

Lexical analysis Chapter 1 - “lexical” = “pertaining to words” = {variable names, numbers, keywords, etc. } tokens. - A lexer allows a translator to handle multicharacter constructs like identifiers, which are written as sequences of characters, but are treated as units called tokens during syntax analysis; - A lexer filters out whatever separates the tokens (the so-called white-space)  lay-out characters (spaces, newlines etc.) and comments. N.B: In theory, the work that is done during lexical analysis can be made an integral part of syntax analysis.

Chapter 1 Example

Chapter 1 7

Chapter 1

Chapter 1 - Lexers are normally constructed by lexer generators, which transform human-readable specifications of tokens and white-space into efficient programs. Regular expressions

Syntax analysis : Parser Chapter 1 Syntax analysis : Parser

Syntax analysis : Parser Chapter 1 Syntax analysis : Parser The parser produces a syntax tree, that is further translated into three-address code. N.B: Some compilers combine parsing and intermediate-code generation into one component

Intermediate code Chapter 1 Two forms of intermediate code are illustrated in Fig. 2 .4. One form, called abstract syntax trees or simply syntax trees, represents the hierarchical syntactic structure of the source program.

Chapter 1

Using “context-free grammar” in Syntax Definition Chapter 1 Using “context-free grammar” in Syntax Definition A set of terminal symbols, sometimes referred to as "tokens." The terminals are the elementary symbols of the language defined by the grammar. A set of nonterminals, sometimes called "syntactic variables." Each nonterminal represents a set of strings of terminals, in a manner we shall describe. A set of productions, where each production consists of a nonterminal, called the head or left side of the production, an arrow, and a sequence of terminals and/or nonterminals , called the body or right side of the production. A designation of one of the nonterminals as the start symbol.

Chapter 1 Example: stmt expr Keywords

Derivations 9-5+2 Parse Trees

Chapter 1

Chapter 1 Ambiguity in grammar Example

A grammar can have more than one parse tree generating a given string of terminals. Such a grammar is said to be ambiguous To show that a grammar is ambiguous, all we need to do is find a terminal string that is the yield of more than one parse tree. Since a string with more than one parse tree usually has more than one meaning, we need to design unambiguous grammars for compiling applications, or to use ambiguous grammars with additional rules to resolve the ambiguities.

Associativity of Operators By convention, 9+5+2 is equivalent to (9+5) +2 and 9-5-2 is equivalent to ( 9-5) -2. When an operand like 5 has operators to its left and right, conventions are needed for deciding which operator applies to that operand. We say that the operator + associates to the left, because an operand with plus signs on both sides of it belongs to the operator to its left. In most programming languages the four arithmetic operators, addition, subtraction, multiplication, and division are left-associative. Some common operators such as exponentiation are right-associative. As another example, the assignment operator = in C and its descendants is right associative; that is, the expression a=b=c is treated in the same way as the expression a= (b=c ) Strings like a=b=c with a right-associative operator are generated by the following 'grammar:

Example 2.6 (Construct grammar) -1 Step 1 We start with the four common arithmetic operators and a precedence table, showing the operators in order of increasing precedence. Operators may be left-associative (meaning the operations are grouped from the left), right-associative (meaning the operations are grouped from the right) or non-associative (meaning there is no defined grouping).

Example 2.6 (Construct grammar) -2 Step 2 We create two nonterminals expr and term for the two levels of precedence, and an extra nonterminal factor for generating basic units in expressions.

Example 2.6 (Construct grammar) -3 Step 3 Now consider the binary operators, * and /, that have the highest precedence. Since these operators associate to the left, the productions are similar to those for lists that associate to the left. Similarly, expr generates lists of terms separated by the additive operators

A grammar for arithmetic expressions

Example 2 . 7 (Homework)

If statement - java