Presentation is loading. Please wait.

Presentation is loading. Please wait.

Grammars Examples and Issues. Examples from Last Lecture a + b a b + a*bc* First draw a state diagram Then create a rule for each transition.

Similar presentations


Presentation on theme: "Grammars Examples and Issues. Examples from Last Lecture a + b a b + a*bc* First draw a state diagram Then create a rule for each transition."— Presentation transcript:

1 Grammars Examples and Issues

2 Examples from Last Lecture a + b a b + a*bc* First draw a state diagram Then create a rule for each transition

3 a+ba+b 1 2 3 a a b -> a -> b ->  -> a -> a | b or

4 a b + 1 2 3 a b b -> a -> b ->  -> a -> b -> b |  or (  represents an empty string … done)

5 a*bc* 1 2 b c -> a -> b -> c ->  -> a | b -> b |  or (  represents an empty string … done) a

6 What does this have to do with programming languages? Text processing is common in applications Generalizing formats make processing more complex html and xml are examples Grammars and Programming Compilers need to process strings

7 State Diagram

8 Lexical Analysis (cont.) Implementation (assume initialization): int lex() { getChar(); switch (charClass) { case LETTER: addChar(); getChar(); while (charClass == LETTER || charClass == DIGIT) { addChar(); getChar(); } return lookup(lexeme); break; …

9 Summary Regular expressions, regular grammars, state machines are all related They represent tools to aid in the processing of text Coding structure (see the addChar/getChar calls in the previous example) follows the state machine structure.

10 Could you write a state machine to recognize html titles?

11 Could you write a program to recognize html titles?

12 Other issues? Compilers start with derived strings. Then try to “reverse engineer” the structure/derivation associated with the program. If the grammar is too simple, there are multiple interpretations …. –AMBIGUITY! First lets look at a Context Free Grammar

13 An Example Grammar   | ;  =  a | b | c | d  + | -  | const

14 An Example Derivation => => => = => a = => a = + => a = b + => a = b + const

15 Parse Tree A hierarchical representation of a derivation const a = b +

16 Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has two or more distinct parse trees

17 An Ambiguous Expression Grammar  | const  / | - const --//

18 An Unambiguous Expression Grammar If we use the parse tree to indicate precedence levels of the operators, we cannot have ambiguity  - |  / const| const const / -

19 Associativity of Operators Operator associativity can also be indicated by a grammar -> + | const (ambiguous) -> + const | const (unambiguous) const + +

20 Awkward Appearance of Grammars Not for you For compilers to have unambiguous interpretation As with string processing, provides the basis of program structure for writing compilers.


Download ppt "Grammars Examples and Issues. Examples from Last Lecture a + b a b + a*bc* First draw a state diagram Then create a rule for each transition."

Similar presentations


Ads by Google