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 04 PROGRAMMING LANGUAGES

2 Grammar It is often convenient to specify languages in terms of grammars. The advantage in doing so arises mainly from the usage of a small number of rules for describing a language with a large number of sentences. For instance, the possibility that an English sentence consists of a subject phrase followed by a predicate phrase can be expressed by a grammatical rule of the form

3 Grammar <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 Grammar From these rules we can form valid sentences using a series of replacements until no more rules can be used. Example, the sentence is “the large rabbit hops quickly” we start with the rule : <sentence>  <noun-phrase><verb-phrase>  <article><adjective><noun> ><verb-phrase>  the<adjective><noun> ><verb-phrase>

5 Grammar 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 Let try this sentence : “a hungry mathematician eats wildly”

6 Grammar Let move to the formal definition of grammar Definition : A grammar < ,N,P,S> consists of four parts: 1. A finite set  of terminal symbols , the alphabet of the language, that areassembled to make up the sentences in the language. 2. A finite set N of nonterminal symbols , each of which represents some collection of subphrases of sentences. 3. A finite set P of productions or rules that describe how each nonterminal is defined in terms of terminal symbols and nonterminals. The choice of nonterminals determines the phrases of the language to which we ascribe meaning. 4. A distinguished nonterminal S, the start symbol , that specifies the principal category being defined—for example, sentence or program.

7 Classification of grammar
Chomsky classified grammars according to the structure of their productions,suggesting four forms of particular usefulness, calling them type 0 through type 3 Type 0 :The most general grammars, the unrestricted grammars , require only that at least one nonterminal occur on the left side of a rule where α and β are strings of symbols in and α is not the empty string, and is a specially designated start symbol.

8 Type 0 : Example Let  = { a, b }, N = { A, B, S } , S = { S } P = S  ABa , A  BB , B  ab , AB  b

9 Classification of grammar
Type 1 : When we add the restriction that the right side contains no fewer symbols than the left side, we get the context-sensitive grammars—for example, a rule of the form αβ → αγβ where α,β ∈ (N U Σ)+ (i.e., α and β are strings of nonterminals and terminals) and γ ∈ (N U Σ)+ (i.e., γ is a nonempty string of nonterminals and terminals).

10 Type 1 : Example P = S  aSbc S  aBC aB  ab bC  bc cb  bc
 = { a,b,c } , N ={ S,B,C } P = S  aSbc S  aBC aB  ab bC  bc cb  bc This type 1 grammar is generate string anbncn where n  1

11 Classification of grammar
Type 2 : The context-free grammars prescribe that the left side be a single nonterminal producing rules of the form A → w where A ∈ N (i.e., A is a single nonterminal), w ∈ (N U Σ)* (i.e., w are strings of nonterminals and terminals)

12 Type 2 : Example  = { a,b } , N ={ S,A} P = S  bSbb S  A A  aA
This type 2 grammar is generate string

13 Classification of grammar
Type 3 : The most restrictive grammars, the regular grammars , allow only a terminal or a terminal followed by one nonterminal on the right side—that is, rules of the form A → a - where A is a non-terminal in N and a is a terminal in Σ A → aB - where A and B are in N and a is in Σ A →  - where A is in N and  denotes the empty string, i.e. the string of length 0.

14 Type 3 : Example P = S → aS S → bA A →  A → cA
 = { a,b,c } , N ={ S,A } P = S → aS S → bA A →  A → cA This type 3 grammar is generate string a*bc*

15 Derivations The cfg G that generates the language consisting of strings over ={a,b} with a even number of a G = (,N,P,S) , N={S,A} , ={a,b} P : S AA A  AAA | bA | Ab | a we want to generate string ababaa, we will write its derivation. So, we have two types of derivation which are leftmost and rightmost derivation.

16 Derivations S=> AA => aA => aAAA => abAAA => abaAA
=> ababAA => ababaA => ababaa => AAAA => Aa => AAAa => AAbAa => AAbaa => AbAbaa => Ababaa Leftmost Left most Rightmost

17 CFG Example The cfg G that generates the language consisting of strings over ={0,1} G = (,N,P,S) , N={A,B} , ={0,1} , S={A} P : A B |  B  0A1 we want to generate string , write its leftmost or rightmost derivation.

18 Derivations Leftmost derivation A => B => 0A1 => 0B1
=>

19 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>)

20 <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

21 Example : Uses Pascal production rules to find the leftmost derivation that given the constant 65. Derivation is <expression>  <simple expression>  <term>  <factor>  <unsigned constant>  <unsigned number>  <unsigned integer>  <digit><unsigned integer>  6 <unsigned integer>  6<digit>  65

22 Try this Uses Pascal production rules to find the leftmost derivation that given -250. Uses Pascal production rules to find the leftmost derivation that given 5 * ( x + 15 ) Uses Pascal production rules to find the leftmost derivation that given ( (x – 10) div 2 )


Download ppt "PROGRAMMING LANGUAGES"

Similar presentations


Ads by Google