Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3: Describing Syntax and Semantics

Similar presentations


Presentation on theme: "Chapter 3: Describing Syntax and Semantics"— Presentation transcript:

1 Chapter 3: Describing Syntax and Semantics
Lectures # 7

2 Chapter 3 Topics Syntax Graphs Definitions Tokens and lexemes
Formal Definition of Languages Formal Methods of Describing Syntax Context Free Grammar (CFG) Backus-Naur Form (BNF) Derivation Parse Trees An Ambiguous Expression Grammar Presidency and associativity of grammars Syntax Graphs Chapter 3: Describing Syntax and Semantics 2

3 Derivation A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence. In each step of a derivation, exactly one nonterminal is expanded. Every string of symbols in a derivation is a sentential form. A sentential form may contain terminal and nonterminal symbols. A sentence is a sentential form that has only terminal symbols. Chapter 3: Describing Syntax and Semantics 3

4 Types of Derivations Leftmost derivation: a derivation in which the leftmost nonterminal in the sentential form is always the one that is expanded. Rightmost derivation: a derivation in which the rightmost nonterminal in the sentential form is always the one that is expanded. A derivation may be neither leftmost nor rightmost. Chapter 3: Describing Syntax and Semantics 4

5 An Example Grammar <program>  <stmts> <stmts>  <stmt> | <stmt> ; <stmts> <stmt>  <var> = <expr> <var>  a | b | c | d <expr>  <term> + <term> | <term> - <term> <term>  <var> | const Chapter 3: Describing Syntax and Semantics 5

6 An Example Derivation Using the above grammar derive the following sentence: a = b + const <program>  <stmts>  <stmt>  <var> = <expr>  a = <expr>  a = <term> + <term>  a = <var> + <term>  a = b + <term>  a = b + const Sentential Forms Sentence Chapter 3: Describing Syntax and Semantics 6

7 Parse Trees Parse tree is a well-defined representation of a syntactic structure of a sentence. A parse tree is a tree with the following properties: 1) Root is labeled with the starting symbol; 2) Each leaf is labeled with a terminal symbol (token); 3) Each internal node is labeled with a nonterminal symbol; Chapter 3: Describing Syntax and Semantics 7

8 Parse Tree vs Derivation
<program>  <stmts>  <stmt>  <var> = <expr>  a = <expr>  a = <term> + <term>  a = <var> + <term>  a = b + <term>  a = b + const Chapter 3: Describing Syntax and Semantics 8

9 Ambiguous Grammars A grammar is ambiguous if and only if it generates a sentential form that has 2 or more distinct parse trees. Example: Using the sentence const – const/const, prove that the following expression grammar is ambiguous: <expr>  <expr> <op> <expr> | const <op>  / | - Chapter 3: Describing Syntax and Semantics 9

10 An Ambiguous Expression Grammar
The sentence: const – const/const <expr>  <expr> <op> <expr> | const <op>  / | - Chapter 3: Describing Syntax and Semantics 10

11 Indicating Precedence
If we use the parse tree to indicate precedence levels of the operators, we can avoid ambiguity. <expr>  <expr> - <term> | <term> <term>  <term>/const | const Chapter 3: Describing Syntax and Semantics 11

12 Associativity of Operators
Operator associativity can also be indicated by a grammar. The following tree is left associative: <expr>  <expr> + <term> | <term> <term>  <term> * const | const Produce the expression: (3 + 4) + 5 Chapter 3: Describing Syntax and Semantics 12

13 Associativity of Operators (cont.)
Suppose we reverse the order of <expr> and <term> on the RHS of the first rule: <expr>  <term> + <expr> | <term> <term>  <term> * const | const The tree corresponds to: 3 + (4 + 5), meaning + is now right associative. Chapter 3: Describing Syntax and Semantics 13

14 Rule of thumb for associativity
A left recursive production results in left associativity E  E + T (+ is left associative) A right recursive production results in right associativity E  T + E (+ is right associative) Chapter 3: Describing Syntax and Semantics 14

15 Extended BNF (EBNF) Optional parts are placed in brackets [ ].
<proc_call>  ident [(<expr_list>)] BNF: <proc_call>  ident | ident(<expr_list>) Alternative parts of RHSs are placed inside parentheses and separated via vertical bars. <term>  <term> (+|-) const BNF: <term>  <term> + const | <term> - const Repetitions (0 or more) are placed inside braces { }. <ident>  letter {letter|digit} BNF: <ident>  <ident>(letter|digit) | letter Chapter 3: Describing Syntax and Semantics \ 15

16 BNF and EBNF BNF <expr>  <expr> + <term>
<term>  <term> * <factor> | <term> / <factor> | <factor> EBNF <expr>  <term>{(+|-)<term>} <term>  <factor>{(*|/)<factor>} Chapter 3: Describing Syntax and Semantics 16

17 Syntax Graphs Syntax graphs use directed graphs to represent syntax graphically. Terminals are placed in circles. Nonterminals are placed in rectangles. Circles and rectangles are connected with lines with arrowheads. Pascal type declarations Chapter 3: Describing Syntax and Semantics 17


Download ppt "Chapter 3: Describing Syntax and Semantics"

Similar presentations


Ads by Google