CS485 Programming Languages - Western Michigan University

Download Report

Transcript CS485 Programming Languages - Western Michigan University

4/26/2020

Lecture 3: Introduction to Syntax (Cont’)

(Revised based on the Tucker’s slides) CS485, Lecture 3, Parse Tree 1

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.

4/26/2020 CS485, Lecture 3, Parse Tree 2

Back to Grammar: Integer Digit   Digit

|

Integer Digit

0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Integer Integer Integer Digit Digit 2 Digit 5 3 Integer  Integer Digit  Integer

2

 Integer Digit    Integer Digit

3 5 2 5 2 5 2 2

4/26/2020 CS485, Lecture 3, Parse Tree 3

An Expression Grammar

G

1

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.

4/26/2020 CS485, Lecture 3, Parse Tree 4

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.

5 4/26/2020 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

G

1 , We refined the grammar

G

2 6 4/26/2020 CS485, Lecture 3, Parse Tree

Arithmetic Expression Grammar

G

2 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 “

) ” 4/26/2020 CS485, Lecture 3, Parse Tree 7

Ambiguous Grammars in PLs

• C, C++, and Java have a large number of – operators and – precedence levels 4/26/2020 CS485, Lecture 3, Parse Tree 8

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

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 4/26/2020 CS485, Lecture 3, Parse Tree 10

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 4/26/2020 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: Grammar 1 Expr 

Term

Factor

Expr + Term | Term; Term * Factor | Factor ; 0 | 1 | … | 9;

Grammar 2 Expr 

Term

Factor

Expr * Term | Term; Term + Factor | Factor ; 0 | 1 | … | 9;

4/26/2020 CS485, Lecture 3, Parse Tree 12

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 Expr 

Term

Expr + Term | Term; 0 | 1 | … | 9;

Grammar 2 Expr 

Term

Term + Expr | Term; 0 | 1 | … | 9;

4/26/2020 CS485, Lecture 3, Parse Tree 13

Another Famous Ambiguous G

1. 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!!!

4/26/2020 CS485, Lecture 3, Parse Tree 14

Parse Trees

• Consider the following string: IF E THEN OTHERS ELSE IF E THEN OTHERS ELSE OTHERS 4/26/2020 CS485, Lecture 3, Parse Tree 15

No Dangling Else Grammar

• Here is the solution: stmt -> matched_stmt | unmatched_stmt; matched_stmt -> IF exp THEN matched_stmt | OTHERS ELSE matched_stmt Unmatched_stmt -> IF exp THEN stmt | IF exp THEN matched_stmt ELSE unmatched_stmt 4/26/2020 CS485, Lecture 3, Parse Tree 16

Extended BNF (EBNF)

• BNF: – 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

4/26/2020 CS485, Lecture 3, Parse Tree 17

EBNF Examples

Expression

is a list of one or more separated by operators

+

and

Terms

Expression IfStatement Statement

->

Term

-> if

]

‘(‘ { ( + | - )

Term Expression

‘)’

}

Statement

[ else

C-style EBNF lists alternatives vertically and uses opt to signify optional parts. E.g., IfStatement: if

‘(‘

Expression

)

Statement ElsePart opt ElsePart: else Statement

4/26/2020 CS485, Lecture 3, Parse Tree 18

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.

4/26/2020 CS485, Lecture 3, Parse Tree 19

Syntax Diagram for Expressions with Addition Figure 2.6

4/26/2020 CS485, Lecture 3, Parse Tree 20

Exercise 1

• Consider the following grammar E -> aEbE | bEaE | ε

Is this ambiguous grammar?

4/26/2020 CS485, Lecture 3, Parse Tree 21

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 | OTHERS exp -> E 4/26/2020 CS485, Lecture 3, Parse Tree 22