Syntax (1).

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Advertisements

Chapter 2 Syntax. Syntax The syntax of a programming language specifies the structure of the language The lexical structure specifies how words can be.
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Programming Languages 2nd edition Tucker and Noonan
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
S YNTAX. Outline Programming Language Specification Lexical Structure of PLs Syntactic Structure of PLs Context-Free Grammar / BNF Parse Trees Abstract.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
Describing Syntax and Semantics
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
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.
Compiler Principle and Technology Prof. Dongming LU Mar. 7th, 2014.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax.
Context-Free Grammars
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Context-Free Grammars and Parsing
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.
C H A P T E R TWO Syntax and Semantic.
ISBN Chapter 3 Describing Syntax and Semantics.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
Dr. Philip Cannata 1 Lexical and Syntactic Analysis Chomsky Grammar Hierarchy Lexical Analysis – Tokenizing Syntactic Analysis – Parsing Hmm Concrete Syntax.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
CPS 506 Comparative Programming Languages Syntax Specification.
D Goforth COSC Translating High Level Languages.
Syntax and Semantics Structure of programming languages.
Copyright © 2006 Addison-Wesley. All rights reserved. Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has.
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
LESSON 04.
Chapter 3 Context-Free Grammars Dr. Frank Lee. 3.1 CFG Definition The next phase of compilation after lexical analysis is syntax analysis. This phase.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
9/15/2010CS485, Lecture 2, Fall Lecture 2: Introduction to Syntax (Revised based on the Tucker’s slides)
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.
Structure of programming languages
BNF A CFL Metalanguage Some Variations Particular View to SLK Copyright © 2015 – Curt Hill.
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
PROGRAMMING LANGUAGES
CS510 Compiler Lecture 4.
Chapter 3 Context-Free Grammar and Parsing
Programming Languages
Chapter 3 – Describing Syntax
What does it mean? Notes from Robert Sebesta Programming Languages
Context-Free Grammars
Syntax versus Semantics
Compiler Construction (CS-636)
CSE 3302 Programming Languages
Syntax One - Hybrid CMSC 331.
Lecture 3: Introduction to Syntax (Cont’)
Compiler Design 4. Language Grammars
Context-Free Grammars
Programming Languages 2nd edition Tucker and Noonan
Context-Free Grammars
Programming Language Syntax 2
CSC 4181Compiler Construction Context-Free Grammars
R.Rajkumar Asst.Professor CSE
C H A P T E R T W O Syntax.
BNF 23-Feb-19.
What are the names of the Meta Languages you have used?
CSC 4181 Compiler Construction Context-Free Grammars
Context-Free Grammars
Syntax vs Semantics Backus-Naur Form Extended BNF Derivations
Chapter 3 Describing Syntax and Semantics.
BNF 9-Apr-19.
Context-Free Grammars
Programming Languages 2nd edition Tucker and Noonan
COMPILER CONSTRUCTION
Presentation transcript:

Syntax (1)

Syntax The syntax of a programming language is a precise description of all its grammatically correct programs. Levels of syntax Lexical syntax Concrete syntax Abstract syntax

Levels of Syntax Lexical syntax Concrete syntax Abstract syntax all the basic symbols of the language (names, values, operators, etc.) Concrete syntax rules for writing expressions, statements and programs. Abstract syntax internal representation of the program, favouring content over form.

Grammars A metalanguage is a language used to define other languages. A grammar is a metalanguage used to define the syntax of a language. Backus-Naur Form (BNF) version of a context-free grammar

BNF Grammar Set of terminal symbols: T Set of nonterminal symbols: N Set of productions: P Start symbol: S N

Example: Binary Digits Consider the grammar: binaryDigit  0 binaryDigit  1 or equivalently: binaryDigit  0 | 1 Here, | is a metacharacter that separates alternatives.

Derivations A grammar for integers: Integer  Digit | Integer Digit Digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 How we can derive any unsigned integer, like 352, from this grammar?

Driving the Integer “352” using Integers Grammar Integer  Integer Digit  Integer 2  Integer Digit 2  Integer 5 2  Digit 5 2  3 5 2 Integer  Integer Digit  Integer Digit Digit  Digit Digit Digit  3 Digit Digit  3 5 Digit  3 5 2 rightmost derivation leftmost derivation

Parse Trees A parse tree is a graphical representation of a derivation. Each internal node of the tree corresponds to a step in the derivation. Each child of a node represents a right-hand side of a production. Each leaf node represents a symbol of the derived string, reading from left to right.

Example: The step Integer  Integer Digit appears in the parse tree as: Integer Digit

Example: Parse Tree for “352” as an Integer Integer  Digit | Integer Digit Digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Arithmetic Expression Grammar The following grammar defines the language of arithmetic expressions with 1-digit integers, addition, and subtraction. Example : Parse of the String 5-4+3 Expr  Expr + Term | Expr – Term | Term Term  0 | ... | 9 | ( Expr )

Example: Parse of the String 5-4+3 Expr  Expr + Term | Expr – Term | Term Term  0 | ... | 9 | ( Expr )

Precedence A grammar can be used to define and precedence among the operators in an expression. * and / have higher precedence than + and -. Example: expr → expr + term | expr – term | term term → term * factor | term \ factor | term % factor |factor factor → digit | (expr) digit → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Associativity A grammar can be used to define associativity among the operators in an expression. + and - are left-associative operators Example: Parse of 4**2**3+5*6+7 Expr → Expr + Term | Expr – Term | Term Term → Term * Factor | Term / Factor |Term % Factor | Factor Factor → Primary ** Factor | Primary Primary → 0 | ... | 9 | ( Expr )

Example Parse of 4**2**3+5*6+7 Expr → Expr + Term | Expr – Term | Term Term → Term * Factor | Term / Factor | Term % Factor | Factor Factor → Primary ** Factor | Primary Primary → 0 | ... | 9 | ( Expr )

Ambiguous Grammars A grammar can have more than one parse tree. Consider next grammar Example: Draw all a parse tree of (9-5+2) using previous grammar string → string + string | string – string |0|1|2|3|4|5|6|7|8|9

( 9 – 5 ) + 2 9 - ( 5 + 2 ) string 9 5 2 - + string 2 5 9 + -

Another Example: The Dangling Else With which ‘if’ does the following ‘else’ associate if (x < 0) if (y < 0) y = y - 1; else y = 0;

If statement Grammar IfStatement → if ( Expression ) Statement | if ( Expression ) Statement else Statement Statement → Assignment | IfStatement | Block Block → { Statements } Statements → Statements Statement | Statement

The Dangling Else Ambiguity

C and C++ Solution to dangling else ambiguity Associate each else with closest if Use { } to override

Extended BNF (EBNF) Additional metacharacters Braces { } zero or more occurrences of the symbols that are enclosed within braces. Parentheses ( ) Enclose a series of alternatives from which one must be chosen Brackets [ ] for an optional list; pick none or one

EBNF Example BNE EBNE A → x { y } z A → x A' z A' → | y A' Expr → Expr + Term | Expr – Term| Term EBNE Expr → Term {(+ | -) Term} A → x { y } z A → x A' z A' → | y A'

Syntax Diagram A version of EBNE Example: Expr → Term {(+ | -) Term}