220 likes | 328 Views
Chapter 3: Describing Syntax and Semantics. Lectures # 7. Chapter 3 Topics. 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
E N D
Chapter 3:Describing Syntax and Semantics Lectures # 7
Chapter 3 Topics • 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
Derivation • Aderivation 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 aderivation is a sentential form. • A sentential form may contain terminal and nonterminal symbols. • A sentence is asentential form that has only terminal symbols. Chapter 3: Describing Syntax and Semantics 3
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. • Example:aasbb S -> AsB A -> aA | a B -> bB | b Chapter 3: Describing Syntax and Semantics 4
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
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
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) Rootis labeled with the starting symbol; 2) Each leafis labeled with a terminal symbol (token); 3) Each internal node is labeled with a nonterminal symbol; Chapter 3: Describing Syntax and Semantics 7
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
ExampleP = { S bA, S aA, A aA, A b } Derivation: S bA baA baaA baab Parse Tree:
<expr> ::= <factor><opr><expr> | <factor><factor> ::= X | Y | Z | <parexp><parexp> ::= (<expr>)<opr> ::= +|- Derivation: <expr> => <factor><opr><expr> =>X <opr><expr> =>X + <expr> =>X + <factor> =>X + <parexp> =>X + ( <expr> ) =>X + ( <factor><opr><expr> ) =>X + ( Z <opr><expr> ) =>X + ( Z - <expr> ) =>X + ( Z - <factor> ) =>X + ( Z - Y ) Sentence X + ( Z – Y ) Parse Tree:
Assignment2 • ملاحظات: • لايمكن استبدال اكتر من non-terminal في نفس الخطوه. • كل non-terminal يتم استبدالة في خطوه منفصلة. اي عدم دمج اكثر من خطوه. • استخدام طريقة واحده من البداية الى النهاية: • LMD • RMD
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 12
An Ambiguous Expression Grammar The sentence: const – const/const <expr> <expr> <op> <expr> | const <op> / | - Chapter 3: Describing Syntax and Semantics 14
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 15
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 16
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 17
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 18
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 \ 19
BNF and EBNF • BNF <expr> <expr> + <term> | <expr> - <term> | <term> <term> <term> * <factor> | <term> / <factor> | <factor> • EBNF <expr> <term>{(+|-)<term>} <term> <factor>{(*|/)<factor>} Chapter 3: Describing Syntax and Semantics 20
Pascal type declarations 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. Chapter 3: Describing Syntax and Semantics 22