Algorithm to Check if a String is in a Context-Free Grammar, Exercises of Algorithms and Programming

A problem in compiler theory, natural language processing, and theoretical computer science: determining if a string belongs to a context-free grammar. An example of a context-free grammar in chomsky normal form and explains how to generate strings from it. The main focus is on designing an algorithm to check if a given string is in the grammar, with a time complexity of o(n^3), where n is the number of characters in the string.

Typology: Exercises

2011/2012

Uploaded on 07/16/2012

santhanakrishnan
santhanakrishnan ๐Ÿ‡ฎ๐Ÿ‡ณ

5

(3)

47 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COS 423 Theory of Algorithms Spring 2005
Precept 4
1. Problem 6.5.
2. A fundamental problem in compilers, natural language processing, and theoretical computer science
is determining whether or not a string is in a cont ex t-free gra mmar. A context-free grammar is a
compact notation for expressing a set of strings. It consists of a set of terminals (lowercase letters), a
set of nonterminals (uppercase letters) and a set of productions (replacement rules for nonterminals).
A grammar in Chomsky Normal Form always has production rules with one nonterminal on the left
hand side and either (i) one nonterminal on the right hand side or (ii) two nonterminals on the right
hand side.
terminals a, b
nonterminals S, T, A, B , X
start S
Rules
Sโ†’๎˜
Sโ†’AB
Sโ†’AX
Xโ†’TB
Tโ†’AB
Tโ†’AX
Aโ†’a
Bโ†’b
For example, aabb is a string in the above context free grammar. To see why, we can apply the following
sequence of transformations Sโ†’AX โ†’AT B โ†’AABB โ†’aABB โ†’aaBB โ†’aabB โ†’aabb.In
fact, the grammar generates precisely those strings with kaโ€™s followed by kbโ€™s.
Given a context-free grammar in Chomsky Normal Form, design an algorithm to determine whether a
string is in the grammar. Your algorithm should run in O(n3) time where nis the number of characters
in the string. You may assume the number of production rules is a small constant.
1
docsity.com

Partial preview of the text

Download Algorithm to Check if a String is in a Context-Free Grammar and more Exercises Algorithms and Programming in PDF only on Docsity!

COS 423 Theory of Algorithms Spring 2005

Precept 4

  1. Problem 6.5.
  2. A fundamental problem in compilers, natural language processing, and theoretical computer science is determining whether or not a string is in a context-free grammar. A context-free grammar is a compact notation for expressing a set of strings. It consists of a set of terminals (lowercase letters), a set of nonterminals (uppercase letters) and a set of productions (replacement rules for nonterminals). A grammar in Chomsky Normal Form always has production rules with one nonterminal on the left hand side and either (i) one nonterminal on the right hand side or (ii) two nonterminals on the right hand side.

terminals a, b nonterminals S, T, A, B, X start S

Rules S โ†’  S โ†’ AB S โ†’ AX X โ†’ T B T โ†’ AB T โ†’ AX A โ†’ a B โ†’ b

For example, aabb is a string in the above context free grammar. To see why, we can apply the following sequence of transformations S โ†’ AX โ†’ AT B โ†’ AABB โ†’ aABB โ†’ aaBB โ†’ aabB โ†’ aabb. In fact, the grammar generates precisely those strings with k aโ€™s followed by k bโ€™s. Given a context-free grammar in Chomsky Normal Form, design an algorithm to determine whether a string is in the grammar. Your algorithm should run in O(n^3 ) time where n is the number of characters in the string. You may assume the number of production rules is a small constant.

docsity.com