Lecture 3: Introduction to Syntax (Cont’)

Slides:



Advertisements
Similar presentations
Exercise 1: Balanced Parentheses Show that the following balanced parentheses grammar is ambiguous (by finding two parse trees for some input sequence)
Advertisements

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Session 14 (DM62) / 15 (DM63) Recursive Descendent Parsing.
Programming Languages 2nd edition Tucker and Noonan
CS 536 Spring Ambiguity Lecture 8. CS 536 Spring Announcement Reading Assignment –“Context-Free Grammars” (Sections 4.1, 4.2) Programming.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Context-Free Grammars and Parsing 1.
8/19/2015© Hal Perkins & UW CSEC-1 CSE P 501 – Compilers Parsing & Context-Free Grammars Hal Perkins Winter 2008.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Compiler Principle and Technology Prof. Dongming LU Mar. 7th, 2014.
Syntax and Backus Naur Form
Context-Free Grammars
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.
PART I: overview material
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax (cont.) Lecture 4 – Syntax (Cont.), Spring CSE3302 Programming Languages,
C H A P T E R TWO Syntax and Semantic.
ISBN Chapter 3 Describing Syntax and Semantics.
Dr. Philip Cannata 1 Lexical and Syntactic Analysis Chomsky Grammar Hierarchy Lexical Analysis – Tokenizing Syntactic Analysis – Parsing Hmm Concrete Syntax.
1 COMP313A Programming Languages Syntax Analysis (2)
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
CPS 506 Comparative Programming Languages Syntax Specification.
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.
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.
Syntax Analyzer (Parser)
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.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
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.
Syntax analysis Jianguo Lu School of Computer Science University of Windsor.
CSE 3302 Programming Languages
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
lec02-parserCFG May 8, 2018 Syntax Analyzer
Parsing & Context-Free Grammars
CS510 Compiler Lecture 4.
Chapter 3 Context-Free Grammar and Parsing
Chapter 3 – Describing Syntax
Syntax (1).
Syntax Specification and Analysis
Syntax versus Semantics
Compiler Construction (CS-636)
CSE 3302 Programming Languages
Syntax Analysis Sections :.
Syntax One - Hybrid CMSC 331.
Parsing & Context-Free Grammars Hal Perkins Autumn 2011
Compiler Design 4. Language Grammars
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
Context-Free Grammars
Some CFL Practicalities
Programming Languages 2nd edition Tucker and Noonan
Programming Language Syntax 2
Lecture 7: Introduction to Parsing (Syntax Analysis)
CSC 4181Compiler Construction Context-Free Grammars
R.Rajkumar Asst.Professor CSE
CSC 4181 Compiler Construction Context-Free Grammars
Context-Free Grammars
BNF 9-Apr-19.
Parsing & Context-Free Grammars Hal Perkins Summer 2004
lec02-parserCFG May 27, 2019 Syntax Analyzer
Context-Free Grammars
Parsing & Context-Free Grammars Hal Perkins Autumn 2005
Programming Languages 2nd edition Tucker and Noonan
COMPILER CONSTRUCTION
Presentation transcript:

Lecture 3: Introduction to Syntax (Cont’) (Revised based on the Tucker’s slides) 11/13/2018 CS485, Lecture 3, Parse Tree

2.1.3 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. 11/13/2018 CS485, Lecture 3, Parse Tree

Back to Grammar: Integer  Digit | Integer Digit Digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9  3 5 2 2 Digit 5 3 11/13/2018 CS485, Lecture 3, Parse Tree

An Expression Grammar G1 Expr -> Expr Op Expr | Term | “(“ Expr “)” Op -> “+” | “-” | “*” | “/” Term -> 0 | 1 | 2 |…| 9 Write a parse tree for 5 – 4 + 3 and 5 * 4 + 3 respectively. 11/13/2018 CS485, Lecture 3, Parse Tree

Ambiguous Grammars A grammar is ambiguous if one of its strings has two or more different parse trees. Each parse tree actually gives one explanation of why a string belongs to the language defined by a grammar. 11/13/2018 CS485, Lecture 3, Parse Tree

Equivalent Grammars Two grammars are equivalent iff the languages defined by the two grammars are the same. To remove the ambiguity in Grammar G1, We refined the grammar G2 11/13/2018 CS485, Lecture 3, Parse Tree

Arithmetic Expression Grammar G2 The following grammar defines the language of arithmetic expressions with 1-digit integers, addition, and subtraction. Expr  Term | Expr “+” Term | Expr “–” Term Term  0 | ... | 9 | “(“ Expr “)” 11/13/2018 CS485, Lecture 3, Parse Tree

Ambiguous Grammars in PLs C, C++, and Java have a large number of operators and precedence levels 11/13/2018 CS485, Lecture 3, Parse Tree

With which ‘if’ does the following ‘else’ associate Example With which ‘if’ does the following ‘else’ associate if (x < 0) if (y < 0) y = y - 1; else y = 0; 11/13/2018 CS485, Lecture 3, Parse Tree

Associativity and Precedence in Grammar A grammar can be used to define associativity and precedence among the operators in an expression. E.g., + and - are left-associative operators in mathematics; * and / have higher precedence than + and - . Expr -> Expr + Expr | Expr – Expr | Expr * Expr | Integer Integer -> 0 | 1 | … | 9 11/13/2018 CS485, Lecture 3, Parse Tree

How to Remove Ambiguity Rewrite an unambiguous grammar Make sure the unambiguous grammar can accept the same language as the previous ambiguous grammar can. Some tools provide some built-in features to allow ambiguous grammar. 11/13/2018 CS485, Lecture 3, Parse Tree

2.1.4 Precedence An operator has higher precedence than another operator if the former should be evaluated sooner in all parenthesis-free expressions involving only the two operators. Solution: 5 + 4 * 3 ( ) Grammar 1 Grammar 2 Expr  Expr + Term | Term; Term  Term * Factor | Factor ; Factor  0 | 1 | … | 9; Expr  Expr * Term | Term; Term  Term + Factor | Factor ; Factor  0 | 1 | … | 9; 11/13/2018 CS485, Lecture 3, Parse Tree

2.1.4 Associativity Associativity specifies whether operators of equal precedence should be performed in left-to-right or right-to-left order. Solution: ( ) 5 + 4 + 3 Grammar 1 Grammar 2 Expr  Expr + Term | Term; Term  0 | 1 | … | 9; Expr  Term + Expr | Term; Term  0 | 1 | … | 9; 11/13/2018 CS485, Lecture 3, Parse Tree

Another Famous Ambiguous G To make the dangling else clear, we consider the following grammar: stmt -> IF exp THEN stmt | IF exp THEN stmt ELSE stmt | OTHERS // can be assignment, while etc. ; exp -> E As a rule in all PLs, match each ELSE with the closest previous unmatched THEN. When we rewrite a grammar to remove the dangling else ambiguity, we make sure we will not change the language accepted by the two grammars!!! 11/13/2018 CS485, Lecture 3, Parse Tree

Parse Trees Consider the following string: IF E THEN OTHERS ELSE IF E THEN OTHERS ELSE OTHERS 11/13/2018 CS485, Lecture 3, Parse Tree

No Dangling Else Grammar Here is the solution: stmt -> matched_stmt | unmatched_stmt; matched_stmt -> IF exp THEN matched_stmt ELSE matched_stmt | OTHERS Unmatched_stmt -> IF exp THEN stmt | IF exp THEN matched_stmt ELSE unmatched_stmt 11/13/2018 CS485, Lecture 3, Parse Tree

Extended BNF (EBNF) BNF: EBNF: additional metacharacters recursion for iteration nonterminals for grouping EBNF: additional metacharacters { } for a series of zero or more ( ) for a list, must pick one [ ] for an optional list; pick none or one 11/13/2018 CS485, Lecture 3, Parse Tree

EBNF Examples Expression is a list of one or more Terms separated by operators + and - Expression -> Term { ( + | - ) Term } IfStatement -> if ‘(‘ Expression ‘)’ Statement [ else Statement ] C-style EBNF lists alternatives vertically and uses opt to signify optional parts. E.g., IfStatement: if ‘(‘ Expression ) Statement ElsePartopt ElsePart: else Statement 11/13/2018 CS485, Lecture 3, Parse Tree

We can always rewrite an EBNF grammar as a BNF grammar. E.g., EBNF to BNF We can always rewrite an EBNF grammar as a BNF grammar. E.g., A -> x { y } z can be rewritten: A -> x A' z A' -> | y A' (Rewriting EBNF rules with ( ), [ ] is left as an exercise.) While EBNF is no more powerful than BNF, its rules are often simpler and clearer. 11/13/2018 CS485, Lecture 3, Parse Tree

Expressions with Addition Syntax Diagram for Expressions with Addition Figure 2.6 11/13/2018 CS485, Lecture 3, Parse Tree

Exercise 1 Consider the following grammar E -> aEbE | bEaE | ε Is this ambiguous grammar? 11/13/2018 CS485, Lecture 3, Parse Tree

Exercise 2 Show the following grammar is still ambiguous: stmt -> matched_stmt | unmatched_stmt; matched_stmt -> IF exp THEN matched_stmt ELSE matched_stmt | OTHERS Unmatched_stmt -> IF exp THEN stmt | IF exp THEN matched_stmt ELSE unmatched_stmt Exp -> E stmt -> IF exp THEN stmt | matched_stmt matched_stmt -> IF exp THEN matched_stmt ELSE stmt exp -> E 11/13/2018 CS485, Lecture 3, Parse Tree