Download presentation
Presentation is loading. Please wait.
1
Lecture 3: Introduction to Syntax (Cont’)
(Revised based on the Tucker’s slides) 11/13/2018 CS485, Lecture 3, Parse Tree
2
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
3
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
4
An Expression Grammar G1
Expr -> Expr Op Expr | Term | “(“ Expr “)” Op -> “+” | “-” | “*” | “/” Term -> 0 | 1 | 2 |…| 9 Write a parse tree for 5 – and 5 * respectively. 11/13/2018 CS485, Lecture 3, Parse Tree
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
2.1.4 Associativity Associativity specifies whether operators of equal precedence should be performed in left-to-right or right-to-left order. Solution: ( ) 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
14
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
15
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
16
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
17
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
18
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
19
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
20
Expressions with Addition
Syntax Diagram for Expressions with Addition Figure 2.6 11/13/2018 CS485, Lecture 3, Parse Tree
21
Exercise 1 Consider the following grammar E -> aEbE | bEaE | ε
Is this ambiguous grammar? 11/13/2018 CS485, Lecture 3, Parse Tree
22
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.