Presentation is loading. Please wait.

Presentation is loading. Please wait.

A basis for computer theory and A means of specifying languages

Similar presentations


Presentation on theme: "A basis for computer theory and A means of specifying languages"— Presentation transcript:

1 A basis for computer theory and A means of specifying languages
Grammars A basis for computer theory and A means of specifying languages

2 Languages defined by Grammars
<statement> A grammar has terminals non-terminals rules start-symbol <var> = <expression> A <var> + <var> B C

3 Example A grammar has terminals {A,B,C,==,+} non-terminals
{<statement>,<var>,<expression>} rules {<statement> -> <var> = <expression> <expression> -> <var>+<var> <var> -> { A | B | C } start-symbol <statement>

4 A sentence A derivation from a start symbol
Using the previous example: <statement> -> <var> = <expression> A = <var> + <var> A = B + C A valid sentence in this language is A = B + C Defining a language by listing all sentences is not usually practical

5 Derivations have classifications
Leftmost derivations replace one non-terminal at a time, always replacing the leftmost non-terminal <statement> -> <var> = <expression> A = <expression> A = <var> + <var> A = B + <var> A = B + C Rightmost derivations replace one non-terminal at a time, always replacing the rightmost non-terminal .. Surprised?

6 Recursion Non-terminals can appear on both the left and right side of a rule <A> -> a | a <A> <A> -> a or <A> -> a <A> a a <A> a a a One can represent the language with a regular expression: a+ + means one or more * means zero or more THIS IS AN EXAMPLE OF RIGHT RECURSION!

7 Regular grammars <A> = a | a <A> is an example of a regular grammar: RHS is a terminal a terminal followed by a non-terminal a non-terminal followed by a terminal Regular languages (defined by regular grammars) can typically be expressed by regular expressions

8 Context-free Grammar Single non-terminal on LHS
Most programming language constructs Most examples in the text <assign> -> <id> := <expr> <id> -> A | B | C <expr> -> <id> + <expr> | <id> * <expr> | ( <expr> ) | <id>

9 Parse Tree shows derivation
<assign> <id> := <expr> A <id> * <expr> B ( <expr> ) <id> + <expr> A <id> C A := B * ( A + C )

10 What is ambiguity in a grammar?
First observe the significance of the structure See Figure 3.2 Tree structure defines associativity/precedence Highest in the tree, last operation done Lowest in tree, earliest operation done SO ? … If the grammar is defined so that there are two different parse trees, it implies two different interpretations to the same statement

11 How do we avoid ambiguity?
Grammars are usually tricky Introduction of term and factor removes the ambiguity in algebraic expressions Another classical example is that of if-then-else How do you determine which if the else gets paired with? Use of braces, etc can eliminate the confusion.

12 BNF and extended BNF Just a syntax for writing grammar
BNF uses | for OR but that’s it EBNF has other shorthands { , <id> } represents an optional ,<id> repeated indefinitely or not at all (zero or more) [ , <id> ] represents zero or one ….. ( <a> | <b> ) occurs in middle of expression giving choice (ex 3.5)

13 Be sure to practice problems
Syntax Graphs Not a new idea Another means of representing a grammar Easier to read Fig. 3.6 Be sure to practice problems in the chapter!

14 Can you write grammars for:
a+b a b+ a*bc* Be sure your grammar generates ALL sentences in the language but not extra sentences which don’t belong Typical errors are too few or too many!


Download ppt "A basis for computer theory and A means of specifying languages"

Similar presentations


Ads by Google