Presentation is loading. Please wait.

Presentation is loading. Please wait.

Languages and Grammars. A language is a set of strings. Example: The set of all valid C++ programs is a language. Each program consists of a string of.

Similar presentations


Presentation on theme: "Languages and Grammars. A language is a set of strings. Example: The set of all valid C++ programs is a language. Each program consists of a string of."— Presentation transcript:

1 Languages and Grammars

2 A language is a set of strings. Example: The set of all valid C++ programs is a language. Each program consists of a string of symbols – identifiers, keywords, numeric constants, operator symbols, and various punctuation marks, called tokens. Problem: Given a string, and a language, determine whether the string is recognized by the language. A mechanism to describe, or recognize, the strings that make up a language is a grammar.

3 A grammar consists of 1.A set of non-terminal symbols 2.A set of terminal symbols – strings in the language are formed by the terminal symbols. 3.A specified non-terminal symbol called the start symbol. 4.A set of rules, called productions, to reduce the start symbol to a string in the language.

4 A string of terminal symbols belongs to the language defined by the grammar if beginning with the start symbol, and repeatedly replacing a non-terminal symbol with the right hand side of a production for that non-terminal, we eventually obtain the specified string. This process is called a derivation.

5 Example: Consider the following grammar for arithmetic expressions involving additions and subtractions. The non-terminal symbols are:, The terminal symbols are: all integers, +, - The start symbol is: The rules are  +  -   any integer

6 Example of a string: 4 – 2 + 5  -  4 -  4 - +  4 – 2 +  4 – 2 +  4 – 2 + 5 This derivation verifies that the original string, or arithmetic expression, belongs to the language, but it doesn’t tell the whole story. We need to look at the parse tree for this derivation. The root of a parse tree is the start symbol, and the children of any node on the tree which contains a non-terminal, are the objects – terminals and non-terminals- on the right hand side of a production for that non- terminal.

7 - 4 + 2 5

8 - 4 + 2 5 In other words 4 – 2 + 5 = -3 What’s wrong with this? 4 – 2 + 5 = 7 in “conventional” arithmetic. 2+5=7 4-7=-3

9 The additions and subtractions in expressions recognized by this grammar are right associative, that is they are performed from right to left – the right-most operation is performed first, then the right-most operation of those remaining is performed next, etc. In ordinary arithmetic if several additions and subtractions appear in an expression, they are performed from left to right. The operations are said to be left associative. Example: Revise the productions in the previous example so the operations are left associative.  + | - |  any integer The symbol “|” represents “or”

10 + - 5 2 4

11 + - 5 2 4 In this grammar the associativity is consistent with ordinary arithmetic. 4-2=2 2+5=7

12 Example: The language L consists of ton-terminals: which is also the start symbol terminals: a, b, c Any string of a’s, b’s, and c’s are candidates to belong to this language. productions:  a b | c It’s not too hard to see that L = { a n c b n : n >= 0 }

13 Example: Does the string aacbb belong to the language L:  a b  aa bb  aacbb a b c

14 To write a program which will recognize strings in a particular language we rely heavily on the grammar for the language. There is a function for each non-terminal symbol.

15 Consider the previous example with productions  a b | c Write a function corresponding to the non-terminal symbol which takes note of the current character in the specified string, and performs the actions indicated by the right hand side of the productions for. The function bool S ( ) is called in main via input a string ch = getNextChar ( ) // the first character in the string if ( S ( ) ) display: the string is recognized by the grammar else display: the string is not recognized by the grammar

16 bool S ( ) { if (ch = ‘a’) {// applies the production  a b st.push (ch) if (EOS) return false ch = getNextChar ( ) if (!S ( ) ) return false if ( ch == ‘b’) { if (st.empty( ) ) return false st.pop ( ) if (st.empty ( ) && EOS ) return true if ( st.empty( ) xor EOS ) return false ch = getNextChar ( ) return true } }

17 else if ( ch == ‘c’ ) { // applies the production  c if ( st.empty ( ) && EOS ) return true if ( st.empty ( ) xor EOS ) return false ch = getNextChar ( ) return true } else// something bad happened return false } // the end of S

18 Example: In main: input string: aacbbch = ‘a’ Call to S: st: a ch = ‘a’ Call to Sst: a ach = ‘c’ Call to Sst: a ach = ‘b’ return true and

19 Example: In main: input string: aacbbch = ‘a’ Call to S: st: a ch = ‘a’ Call to Sst: a ach = ‘c’ch = ‘b’ = ‘b’ return true true Call to Sst: a ach = ‘b’ return true and

20 Example: In main: input string: aacbbch = ‘a’ Call to S: st: a ch = ‘a’ ‘b’true Call to Sst: a ach = ‘c’ch = ‘b’ = ‘b’ return true true Call to Sst: a ach = ‘b’ return true and

21 Example: In main: input string: aacbbch = ‘a’true Call to S: st: a ch = ‘a’ ‘b’ EOStrue Call to Sst: a ach = ‘c’ch = ‘b’ = ‘b’ return true true Call to Sst: a ach = ‘b’ return true and


Download ppt "Languages and Grammars. A language is a set of strings. Example: The set of all valid C++ programs is a language. Each program consists of a string of."

Similar presentations


Ads by Google