More CFGs, Parse Trees, Ambiguity




CS152

Chris Pollett

Feb. 16, 2009

Outline

More about CFGs

Recall we are learning about how to specify the syntax of a programming language. On Wednesday, we were learning about context-free grammars and gave an example grammar for a very tiny fragment of English:

sentence → noun-phrase verb-phrase .
noun-phrase → article noun
article → a | the
noun → girl | dog
verb-phrase → verb noun-phrase
verb → sees | pets

Remarks

What is a context free grammar?

Why are CFGs called context-free?

You could imagine productions where you have more than one thing on the left hand side:

<sentence> ::= <start-of-sentence> <noun-phrase> <verb-phrase>
<start-of-sentence> <article> ::= The | A
<start-of-sentence> ::= empty-string
...

This would allow you to capitalize the start of sentence, but the rule:

<start-of-sentence> <article> ::= The | A

uses the context in which the <article> appears.

Quiz

Which string does not match the regular expression: [a-z]+[0-9]*
?

  1. emptystring
  2. 9876
  3. aaaa78

More Example Grammars

Parse Trees and Abstract Syntax Trees

Example Parse Tree

Image with Parse tree of: the girl sees a dog.

Remarks

More remarks

Ambiguity