LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

Slides:



Advertisements
Similar presentations
COMPILER CONSTRUCTION WEEK-2: LANGUAGE DESCRIPTION- SYNTACTIC STRUCTURE:
Advertisements

Programming Language Concepts
Grammars, Languages and Parse Trees. Language Let V be an alphabet or vocabulary V* is set of all strings over V A language L is a subset of V*, i.e.,
Chapter Chapter Summary Languages and Grammars Finite-State Machines with Output Finite-State Machines with No Output Language Recognition Turing.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax Lecture 2 - Syntax, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai.
Discussion #31/20 Discussion #3 Grammar Formalization & Parse-Tree Construction.
Slide 1 Chapter 2-b Syntax, Semantics. Slide 2 Syntax, Semantics - Definition The syntax of a programming language is the form of its expressions, statements.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Chapter 3: Formal Translation Models
Specifying Languages CS 480/680 – Comparative Languages.
COP4020 Programming Languages
Winter 2003/4Pls – syntax – Catriel Beeri1 SYNTAX Syntax: form, structure The syntax of a pl: The set of its well-formed programs The rules that define.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Language Translation Principles Part 1: Language Specification.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Syntactic Analysis Dr. Hyunyoung Lee.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
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.
Compiler1 Chapter V: Compiler Overview: r To study the design and operation of compiler for high-level programming languages. r Contents m Basic compiler.
CSI 3120, Grammars, page 1 Language description methods Major topics in this part of the course: –Syntax and semantics –Grammars –Axiomatic semantics (next.
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
CS 331, Principles of Programming Languages Chapter 2.
Context-Free Grammars
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Grammars CPSC 5135.
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
C H A P T E R TWO Syntax and Semantic.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
11 Chapter 4 Grammars and Parsing Grammar Grammars, or more precisely, context-free grammars, are the formalism for describing the structure of.
CMSC 330: Organization of Programming Languages Context-Free Grammars.
Bernd Fischer RW713: Compiler and Software Language Engineering.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
CPS 506 Comparative Programming Languages Syntax Specification.
Chapter 3 Describing Syntax and Semantics
ISBN Chapter 3 Describing Syntax and Semantics.
LESSON 04.
CS 331, Principles of Programming Languages Chapter 2.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
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.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
C H A P T E R T W O Syntax and Semantic. 2 Introduction Who must use language definitions? Other language designers Implementors Programmers (the users.
©SoftMoore ConsultingSlide 1 Context-Free Grammars.
Compiler Chapter 5. Context-free Grammar Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Syntax(1). 2 Syntax  The syntax of a programming language is a precise description of all its grammatically correct programs.  Levels of syntax Lexical.
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
A Simple Syntax-Directed Translator
CS510 Compiler Lecture 4.
Chapter 3 Context-Free Grammar and Parsing
Syntax (1).
Compiler Construction
Compiler Construction (CS-636)
Context-Free Grammars
Chapter 2: A Simple One Pass Compiler
CSC 4181Compiler Construction Context-Free Grammars
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CSC 4181 Compiler Construction Context-Free Grammars
Context-Free Grammars
High-Level Programming Language
Context-Free Grammars
Context Free Grammars-II
Programming Languages 2nd edition Tucker and Noonan
Faculty of Computer Science and Information System
Presentation transcript:

LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

Description The state of art can be summarized by saying that a language is described by a combination of formal syntax and informal semantics. Syntax: How programs in the language are built up, grammar. Semantics: what programs mean, meaning.

EXAMPLE Suppose that dates are built up from digits represented by D and the symbol /, as follows: D D / D D / D D D D 01/02/2001 is a date, not identified by syntax January 2, 2001 or 1st February, 2001

Organization of Language Descriptions Several Style of language description Tutorials : It provides impressions of what the main constructs of the language are and how they are meant to be used Reference Manuals : describing the syntax and semantics of a language is traditionally organized around the syntax of the language Formal Definitions : a precise description of the syntax and semantics of a language; it is aimed at specialists.

Over View EXPRESSION NOTATIONS ABSTRACT SYNTAX TREES LEXICAL SYNTAX CONTEXT-FREE GRAMMARS GRAMMARS FOR EXPRESSIONS VARIANTS OF GRAMMARS

Expression Notations It serves 2 purposes Expressions Introducing notations for expressions Providing examples for the syntax description methods Expressions Notation:how to combine operators and operands Prefix, Postfix, Infix, Mixfix write as expression: (-b+sqrt(b*b-4.0*a*c)/(2.0*a)

Operator Expression1 Expression2 Prefix Notation Operator Expression1 Expression2 + 3 5 is meaning summation of 3 and 5 Example 2.1 * + 20 30 60 = * 50 60 = 3000 * 20 + 30 60 = * 20 90 = 1800 Try to do this * 2 - 4 + 5 7 = ?

Expression1 Expression2 Operator Postfix Notation Expression1 Expression2 Operator 3 5 + is meaning summation of 3 and 5 Example 2.2 20 30 + 60 * = 50 60 * = 3000 20 30 60 + * = 20 90 * = 1800 Try to do this 45 37 - 69 * = ?

Infix Notation Precedence and Associativity Expression1 Operator Expression2 3 + 5 is meaning summation of 3 and 5 Example 4 – 2 – 1 = ( 4 – 2 ) – 1 = 2 – 1 = 1 Or = 4 – ( 2 – 1 ) = 4 – 1 = 3 Try to do this b*b – 4*a*c = ?

Mixfix Notation Combination of symbols do not fit neatly into the prefix, infix, postfix classification. Example the keywords if, then and else if a>b then a else b

ABSTRACT SYNTAX TREES Identify the meaningful components of each construct in the language. Prefix : + a b Postfix : a b + Infix : a + b Corresponding tree representation is + a b

Tree Representation of Expression A tree consists of nodes, k>0 A node with no children is called a leaf A node with no parent is called root A diagram for the tree is Op denoted operator Ek denoted operand Op E1 E2

Example A tree of expression b*b – 4*a*c (b*b)-(4*a*c)  E1 – E2 1. b*b  E1 * E2 2. (4*a)*c  E1 * E2 * b 4 a c - Prefix : - * b b * * 4 a c Infix: b * b – 4 * a * c Postfix: b b * 4 a * c * -

LEXICAL SYNTAX A lexical syntax for a language specified the correspondence between the written representation of the language and the tokens or terminals in a grammar for the language Alphabetic sequences as units in a language, called keywords Occurrences of a token; using token name for names and token number for integers White space: blank not effect meaning

CONTEXT-FREE GRAMMARS The concrete syntax of a language describes its written representation. Context-free grammars are a notation for specifying concrete syntax. BNF from: Backus-Naur Form A way of writing grammars.

Grammars Parse tree 3 . 1 4 Real-number Integer-part fraction digit

Context-free grammar Terminals are the atomic symbols Non-terminals are the variables Production: A set of rules for identifying the components of a construct A starting non-terminal represent in Main construct

BNF:Backus-Naur Form Terminals Non-terminals enclosed between < and > Production in form non-terminal ::= terminal | <non-terminal> ::= “can be” | “or” A starting non-terminal

Example of BNF A BNF grammar for real numbers Terminals : 0,1,2,3,4,5,6,7 Non-terminals : <real-number>,<integer-part>,<fraction>, and <digit> Productions <real-number> ::=<integer-part>.<fraction> <integer-part> ::=<digit>|<integer-part><digit> <fraction> ::=<digit>|<digit><fraction> <digit> ::=0|1|2|3|4|5|6|7|8|9

Parse Trees Depict Concrete Syntax Each leaf is labeled with a terminal or <empty> Each non-leaf node is labeled with a non-terminal The root is labeled with the starting non-terminal Children of node, from left to right of right side

Example Real-number Integer-part fraction . Integer-part digit 1 2 3 7 8 9

Ambiguity Syntactic Ambiguity Dangling-Else Ambiguity