Presentation is loading. Please wait.

Presentation is loading. Please wait.

PROGRAMMING LANGUAGES

Similar presentations


Presentation on theme: "PROGRAMMING LANGUAGES"— Presentation transcript:

1 PROGRAMMING LANGUAGES
CS 222 LECTURE 05 PROGRAMMING LANGUAGES

2 Parse Tree The grammar is a set of rules that say how to build a tree—a parse tree You put <S> at the root of the tree The grammar’s rules say how children can be added at any point in the tree For instance, the rule <S> ::= <NP> <V> <NP> says you can add nodes <NP>, <V>, and <NP>, in that order, as children of <S>

3 Parse Tree <sentence>  <noun-phrase><verb-phrase> <noun-phrase>  <article><adjective><noun> <noun-phrase>  <article><noun> <verb-phrase>  <verb><adverb> | <verb> <article>  a | the <adjective>  large | hungry <noun>  rabbit | mathematician <verb>  eat | hops <adverb>  quickly | wildly

4 Parse Tree Example, the sentence is “the large rabbit hops quickly”
Derivation : <sentence>  <noun-phrase><verb-phrase> <article><adjective><noun><verb-phrase> the<adjective><noun><verb-phrase> the large<noun><verb-phrase> the large rabbit <verb-phrase> the large rabbit <verb><adverb> the large rabbit hops <adverb> the large rabbit hops quickly

5 We can draw a tree from the derivation which is call parse tree or derivation tree
Parse tree : we start with <sentence> is a root and <noun- phrase>, <verb-phrase> are child node of the parse tree.

6 Parse tree : On the left child node, we add <article>, <adjective>, <noun> to be <noun-pharse> ’s child node.

7 Continue adding child node from the leftmost derivation to the parse tree.

8

9 Now, let try the right most derivation :
<sentence>  <noun-phrase><verb-phrase> <noun-phrase><verb><adverb> <noun-phrase><verb>quickly <noun-phrase> hops quickly <article><adjective><noun> hops quickly <article><adjective> rabbit hops quickly <article> large rabbit hops quickly the large rabbit hops quickly

10 Parse tree : we start with <sentence> is a root and <noun- phrase>, <verb-phrase> are child node of the parse tree. Then add <verb>, <adverb> as the <verb-phrase>’s child node.

11 Continue adding child node from the rightmost derivation to the parse tree.

12

13 Some Pascal production rules
<expression>  <simple expression> <simple expression>  <term> | <sign><term> | <simple expression><adding operator><term> <adding operator>  + | - <multiplying operator>  * | / | div | mod <term>  <factor> | <term><multiplying operator><factor> <factor>  <identifier> | <unsigned constant> | (<expression>)

14 <unsigned constant>  <unsigned number> <unsigned number>  <unsigned integer> | <unsigned real> <unsigned integer>  <digit><unsigned integer> | <digit > <identifier>  <letter><identifier tail> <identifier tail>  <letter><identifier tail> | <digit><identifier tail> |  <sign>  + | - <digit> 0 | 1 | 2 |…| 9 <letter>  a | b | c | … | z

15 Example : Uses Pascal production rules to find the leftmost derivation that given the number Derivation is <expression>  <simple expression>  <sign><term>  - <term>  - <factor>  - <unsigned constant>  - <unsigned number>  - <unsigned integer>  - <digit><unsigned integer>  - 2 <unsigned integer>  - 2<digit>  - 25

16 Let’s draw a parse tree

17 Ambiguous A context free grammar G is ambiguous if there is a string w  L(G) that can be derived by two distinct leftmost derivations. A grammar that is not ambiguous is called unambiguous. Example : S  aS | Sa | a we need to generate string aa S  aS  aa (1) S  Sa  aa (2) String aa can have two leftmost derivation. This is an ambiguous.

18 Ambiguous A context free grammar G is ambiguous if there is a string w  L(G) that can be derived by two distinct derivation tree From the last example we can draw the parse tree from (1) and (2) :

19 Example Let G be the grammar S  aS | Sb | ab | SS
Construct two leftmost derivations of the string aabb. Show that G is ambiguous. Two leftmost derivations are S  aS  aSb  aabb S  Sb  aSb  aabb

20 Draw a parse tree, Parse tree 1:

21 Parse tree 2 : we have two different leftmost parse trees given the string aabb so that G is ambiguous.

22 Example Let G be the grammar S → AB | C A → aAb | ab B → cBd | cd C → aCd | aDd D → bDc | bc Construct two leftmost derivations of the string aabbccdd. Show that G is ambiguous. Two leftmost derivations are : 1. S  AB  aAbB  aabbB aabbcBd  aabbccdd 2. S  C  aCd  aaDdd  aabDcdd  aabbccdd

23 Two leftmost parse tree of string aabbccdd :

24 Try This CFG G have production rules : S  AS |  A  A1 | 0A1 | 01
find two distinct leftmost derivation and parse tree to generate string 00111 S  S+S | S-S | S*S | S/S | a find two distinct leftmost derivation and parse tree to generate string a + a + a S  SbS | ScS | SdS |a | e find two distinct leftmost derivation and parse tree to generate string abedecaba

25 Backus Naur Form Backus Naur Form (BNF): a standard notation for expressing syntax as a set of grammar rules. BNF was developed by Noam Chomsky, John Backus, and Peter Naur. First used to describe Algol. BNF can describe any context-free grammar. Fortunately, computer languages are mostly context- free.

26 Backus Naur Form Grammar Rules or Productions: define symbols. assignment_stmt ::= id = expression ; The nonterminal symbol being defined. The definition (production) Nonterminal Symbols: anything that is defined on the left-side of some production. Terminal Symbols: things that are not defined by productions. They can be literals, symbols, and other lexemes of the language defined by lexical rules.

27 Backus Naur Form (2) Different notations (same meaning):
assignment_stmt ::= id = expression + term <assignment-stmt> ::= <id> = <expr> + <term> <assignment-stmt> => <id> = <expr> + <term> AssignmentStmt  id = expression + term ::=, =>,  mean "consists of" or "defined as” Null symbol : e Alternatives ( " | " ): <expression> ::= <expression> + <term> | <expression> - <term> | <term>

28 Terminology (review) Grammar rules are called productions ... since they "produce" the language. Left-hand sides of productions are non-terminal symbols (nonterminals) or structure names. Tokens (which are not defined by syntax rules) are terminal symbols. Metasymbols of BNF are ::= (or => or ), One nonterminal is designated as the start symbol. Usually the rule for producing the start symbol is written first.

29 Problems with BNF Notation
BNF notation is too long. Must use recursion to specify repeated occurrences Must use separate an alternative for every option

30 Extended BNF Notation EBNF adds notation for repetition and optional elements. {…} means the contents can occur 0 or more times: <expr> ::= <expr> + <term> | <term> becomes <expr> ::= <term> { + <term> } […] encloses an optional part: <if-stmt> ::= if ( <expr> ) <stmt> | if ( <expr> ) <stmt> else <stmt> becomes <if-stmt> ::= if ( <expr> ) <stmt> [else <stmt>]

31 Extended BNF Notation, continued
( a | b | ... ) is a list of choices. Choose exactly one. <expr> ::= <expr> + <term> | <expr> - <term> | <term> becomes <expr> ::= <term> { (+|-) <term> } Another example: <term> ::= <factor> { (*|/|%)<factor> }

32 EBNF compared to BNF | <expression> - <term>
<expression> ::= <expression> + <term> | <expression> - <term> | <term> <term> ::= <term> * <factor> | <term> / <factor> | <factor> <factor> ::= ( <expression> ) | <id> | <number> EBNF: <expression> ::= <term> { (+|-) <term> } <term> ::= <factor> { (*|/) <factor> } factor  '(' <expression> ')' | <id> | <number>

33 Notes on use of EBNF Do not start a rule with {…}:
Right: <expr> ::= <term> { + <term> } Wrong: <expr> ::= {<term> + } <term> For right recursive rules use [ ... ] instead: <expr> ::= <term> + <expr> | <term> EBNF: <expr> ::= <term> [ + <expr> ] Square brackets can be used anywhere: <expr> ::= <expr> + <term>|<term>|- <term> EBNF: <expr> ::= [ - ] <term> { + <term> }

34 Try this Rewrite this grammar using Extended BNF.
<sentence> ::= <noun-phrase> <verb-phrase> . <noun-phrase> ::= <article><noun> | <noun> <article> ::= a | the <noun> ::= boy | girl | cat | dog <verb-phrase> ::= <verb><noun-phrase> | <verb> <verb> ::= sees | pets | bites


Download ppt "PROGRAMMING LANGUAGES"

Similar presentations


Ads by Google