Grammars - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Grammars

Description:

Defining a language by listing all sentences is not usually practical ... Leftmost derivations replace one non-terminal at a time, always replacing the ... – PowerPoint PPT presentation

Number of Views:9
Avg rating:3.0/5.0
Slides: 15
Provided by: davi643
Category:

less

Transcript and Presenter's Notes

Title: Grammars


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

2
Languages defined by Grammars
ltstatementgt
  • A grammar has
  • terminals
  • non-terminals
  • rules
  • start-symbol

ltvargt ltexpressiongt
A ltvargt ltvargt
B C
3
Example
  • A grammar has
  • terminals
  • A,B,C,,
  • non-terminals
  • ltstatementgt,ltvargt,ltexpressiongt
  • rules
  • ltstatementgt -gt ltvargt ltexpressiongt
  • ltexpressiongt -gt ltvargtltvargt
  • ltvargt -gt A B C
  • start-symbol
  • ltstatementgt

4
A sentence
  • A derivation from a start symbol
  • Using the previous example

ltstatementgt -gt ltvargt ltexpressiongt
A ltvargt ltvargt 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

ltstatementgt -gt ltvargt ltexpressiongt
A ltexpressiongt
A ltvargt ltvargt A B
ltvargt 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

ltAgt -gt a a ltAgt ltAgt -gt a or ltAgt -gt a ltAgt
a a ltAgt 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
  • ltAgt a a ltAgt 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

ltassigngt -gt ltidgt ltexprgt ltidgt -gt A B
C ltexprgt -gt ltidgt ltexprgt ltidgt
ltexprgt ( ltexprgt )
ltidgt
9
Parse Tree shows derivation
ltassigngt ltidgt ltexprgt A ltidgt
ltexprgt B ( ltexprgt )
ltidgt ltexprgt
A ltidgt 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 thats it
  • EBNF has other shorthands
  • , ltidgt
  • represents an optional ,ltidgt
  • repeated indefinitely or not at all (zero or
    more)
  • , ltidgt
  • represents zero or one
  • .. ( ltagt ltbgt )
  • occurs in middle of expression giving choice (ex
    3.5)

13
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
  • ab
  • a b
  • abc
  • Be sure your grammar generates ALL sentences in
    the language but not extra sentences which dont
    belong
  • Typical errors are too few or too many!
Write a Comment
User Comments (0)
About PowerShow.com