Docsity

Grammars - Programming Languages and Translators - Notes | CS 440, Papers of Programming Languages

Toggle sidebar
bg1
CS 440 — Grammars February 13, 2009
1 Introduction and Objectives
Your goal for this lecture is to learn how to do the following things:
•Identify and explain the parts of a grammar.
•Define terminal,nonterminal,production,sentence,parse tree,left-recursive,ambiguous.
•Use a grammar to draw the parse tree of a sentence.
•Identify a grammar that is left-recursive.
•Know about ambiguous grammars:
–Be able to identify, demonstrate, and eliminate ambiguity.
•Define First Set and Follow Set.
•Compute the First Set from a grammar.
•Compute the Follow Set from a grammar.
1.1 Remember the problem
The conversion from strings to trees is accomplished in two steps.
•First, convert the stream of characters into a stream of tokens.
–This is called lexing or scanning.
–Turns characters into words and categorizes them.
–We did this in the last two lectures!
•Second, convert the stream of tokens into an abstract syntax tree.
–This is called parsing.
–Turns words into sentences.
2 What is a Grammar?
2.1 What is in a sentence?
When we specify a sentence, we talk about two things that could be in them.
1. Terminals: tokens that are atomic — they have no smaller parts (e.g., “nouns”, “verbs”,
“articles”)
2. Non-terminals: clauses that are not atomic — they are broken into smaller parts (e.g. “prepo-
sitional phrase”, “independent clause”, “predicate”)
Examples: (identify the terminals and the non-terminals)
Mattox Beckman Page 1 Illinois Institute of Technology
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Grammars - Programming Languages and Translators - Notes | CS 440 and more Papers Programming Languages in PDF only on Docsity!

1 Introduction and Objectives

Your goal for this lecture is to learn how to do the following things:

  • Identify and explain the parts of a grammar.
  • Define terminal, nonterminal, production, sentence, parse tree, left-recursive, ambiguous.
  • Use a grammar to draw the parse tree of a sentence.
  • Identify a grammar that is left-recursive.
  • Know about ambiguous grammars:
    • Be able to identify, demonstrate, and eliminate ambiguity.
  • Define First Set and Follow Set.
  • Compute the First Set from a grammar.
  • Compute the Follow Set from a grammar.

1.1 Remember the problem

The conversion from strings to trees is accomplished in two steps.

  • First, convert the stream of characters into a stream of tokens.
    • This is called lexing or scanning.
    • Turns characters into words and categorizes them.
    • We did this in the last two lectures!
  • Second, convert the stream of tokens into an abstract syntax tree.
    • This is called parsing.
    • Turns words into sentences.

2 What is a Grammar?

2.1 What is in a sentence?

When we specify a sentence, we talk about two things that could be in them.

  1. Terminals: tokens that are atomic — they have no smaller parts (e.g., “nouns”, “verbs”, “articles”)
  2. Non-terminals: clauses that are not atomic — they are broken into smaller parts (e.g. “prepo- sitional phrase”, “independent clause”, “predicate”)

Examples: (identify the terminals and the non-terminals)

  • A sentence is a noun phrase, a verb, and a prepositional phrase
  • A noun phrase is a determinant, and a noun
  • A prepositional phrase is a preposition and a noun phrase.

2.2 Notation

S → N verb P N→ det noun P→ prep N

  • Each of the above lines is called a production. The symbol on the left hand side can be produced by collecting the symbols on the right hand side.
  • The capital identifiers are non-terminal symbols.
  • The lower case identifiers are terminal symbols.
  • Because the left hand side is only a single non-terminal, the rules are context free. (Contrast: x S → NP verb PP)

2.3 Grammars specify trees...

“The dog runs under a chair.”

S → NP verb PP NP→ det noun PP→ prep NP S

NP

the dog

runs (^) PP

under NP

a (^) chair

2.4 Another Example...

E → E + E

→ v → E > E → if E then E else E

if x > y then a + b else y

  • Example: regular expression (10)*23 describes same language as this grammar: A 0 → 1 A 1 — 2A 2 A 1 → 0 A 0 A 2 → 3 A 3 A 3 → 

3.4 Left-Recursive

  • A grammar is recursive if the symbol being produced (the one on the left-hand side) also appears in the right hand side. Example: “E → if E then E else E”
  • A grammar is left-recursive if the production symbol appears as the first symbol on the right- hand-side. Example: “E → E + F”
  • ... or if is produced by a chain of left recursions ...

Example: A→ Bx B→ Ay

3.5 Ambiguous Grammars

  • A grammar is ambiguous if it can produce more than one parse tree for a single sentence.
  • There are two common forms of ambiguity:
    • The “dangling else” form: E→ if E then E else E E→ if E then E E→ whatever Example: if a then if x then y else z ... to which if does the else belong?
    • The “double-ended recursion” form:

E→ E + E

E→ E * E

Example “3 + 4 * 5” ... is it “(3 + 4) * 5” or “3 + (4 * 5)”?

3.6 Fixing Ambiguity

  • Ambiguity can often be eliminated by thinking more carefully about what you are trying to express with your grammar.
  • “Dangling else” usually matches with the nearest if. This can be encoded in the grammar.
  • The “double-ended recursion” form usually reveals a lack of precedence and associativity information. A technique called stratification often fixes this. - Left-recursive means “associates to the left”, similarly right-recursive. - Higher precedence rules occur lower in the grammar.

E→ F + E

E→ F

F→ T * E

F→ T

T→ ( E )

T→ integer

4 Activity!

4.1 Try the following problems

  1. Draw two separate parse trees for the sentence 2 + 3 * 5 given the grammar E→ E + E E→ E * E E→ integer
  2. Now draw a tree for the same sentence using the grammar E→ F + E E→ F F→ T * E F→ T T→ ( E ) T→ integer
  1. if X is a terminal, then F irst(X) = {X}
  2. if there is a production X → , then add  to F irst(X).
  3. if there is a production X → Y 1 Y 2 · · · Yn, then add F irst(Y 1 Y 2 · · · Yn) to F irst(X):
    • If F irst(Y 1 ) does not contain , then F irst(Y 1 Y 2 · · · Yn) = F irst(Y 1 ).
    • Otherwise, F irst(Y 1 Y 2 · · · Yn) = F irst(Y 1 )/ ∪ F irst(Y 2 · · · Yn)
    • If all of Y 1 , Y 2 ,... Yn have  then add  to F irst(X).

5.4 Follow Sets

  • Given a non-terminal symbol S, what terminal symbols could come after strings that are derived from S?

The Algorithm:

  1. Put $ in F OLLOW (S), where S is the start symbol. $ represents the “end of input.”
  2. If there is a production X → αY β, then add F IRST (β) (but not ) to F OLLOW (Y ).
  3. If there is a production X → αY , or if there is a production X → αY β, where  ∈ F IRST (β) then add F OLLOW (X) to F OLLOW (Y ).

6 Example Grammars

6.1 Example 1

S → x

6.2 Example 2

S → x S → y S → 

6.3 Example 3

S → x y S → z

6.4 Example 4

S → E y S → z E → q E → r

6.7 Example 7

S → E F

S → z E →  E → r F → s F → 

6.8 Example 8

S → A x S → B y S → z A → 1 C B A → 2 B B → 3 B B → C C → 4 C →