GithubHelp home page GithubHelp logo

cs3342_09's Introduction

Grammars Assignment (50 points)

This file contains two questions. The first has 2 parts, the second has 7.

For all but questions 2.3 and 2.5 you'll answer questions by editing this file. Your answers will appear in the Ax.y section after each question, replacing the placeholders.

For question 2.3, you'll need to add an image file to the repository (.png or .pdf) and put its file name as the answer.

For question 2.5, you'll write one or more source code files and add them to the repository. You will then show the command used to run the files in the answer. Do not upload any binary files for this section.

Please do not edit anything outside the answers sections.

Q1: Mr Fussy Builds a Path

Q1

Mr Fussy wants to build a path using a row of large square pavers. His path is one paver wide and an odd number of pavers long. Pavers come in three colors: red, green, blue.

Mr Fussy has a rule: the sequence of colors must be symmetrical across the length of the path: the last paver must be the same color as the first, the second to last the same color as the second, and so on.

This is a valid path: rgbgr So is this: rrr

This is not: rgb This is not: rggr (it must be an odd length)

Q1.1 (5 points)

Write a Chomsky type 2 grammar that describes any valid path containing at least one paver. Use S as the start state, and r, g, b as terminals that represent the tiles.

A1.1

S -> r S r | r  
S -> g S g | g  
S -> b S b | b  

Q1.2 (1 point for the O() answer, 2 for the sentence)

Using big-O notation, what is the likely memory requirement for a parser that can validate a particular path configuration, where n is the number of tiles? In one sentence, explain why.

A1.2

The memory requirement would be O(n) because the stack for the parser will only contain at a maximum n/2 tiles before popping, so the memory usage will never surpass n + n/2.

Q2

A simple sentence structure might be

"the" (zero or more adjectives) noun verb (optional adverb)

Adjectives are "lazy" or "smelly"

Nouns are "dog" or "cat"

Verbs are "ate" and "ran"

Adverbs are "slowly" and "noisily"

The following are examples of valid sentences:

  • The smelly dog ran.
  • The smelly dog ran slowly.
  • The cat ate noisily.

Q2.1 (7 points)

Write the BNF (not EBNF) description for this language.

A2.1

<sentence>    ::= "The" <adjectives> <noun> <verb> <adverb> "."  
  
<adjectives>  ::= <adjective> <adjective>  
<adjective>   ::= <empty> | "lazy" | "smelly"  
<noun>        ::= "dog" | "cat"  
<verb>        ::= "ate" | "ran"  
<adverb>      ::= <empty> | "slowly" | "noisily"  

Q2.2 (5 points)

Write this grammar using EBNF with common extensions

A2.2

<sentence>  ::= "The" {<adjective>} <noun> <verb> [<adverb>] "."  
  
<adjective> ::= "lazy" | "smelly"  
<noun>      ::= "dog" | "cat"  
<verb>      ::= "ate" | "ran"  
<adverb>    ::= "slowly" | "noisily"  

Q2.3 (6 points)

Draw a diagram for an FSM which recognizes these sentences. Use EOI as the event that occurs at the end-of-input. Start with a state named S0. Receiving the word "the" in that state will transition to state S1. Name the final state END.

You can hand draw it and snap a picture, or use a drawing tool. Either way, upload the image, and put the file name in the answer below.

(Hint: my answer has seven states including the start and end states)

A2.3

FSMDiagram.png

Q2.4 (6 points)

Convert this diagram into a table of the form:

Current state Next word Next state
S0        |    the    |     S1
S1        |   . . .   |   . . .

(hint: my version has 13 entries. Yours might be different)

A2.4

Current state Next word Next state
S0        |    the    |     S1
S1        |   smelly  |     S1
S1        |    lazy   |     S1
S1        |    dog    |     S2
S1        |    cat    |     S2
S2        |    ate    |     S3
S2        |    ran    |     S3
S3        |    EOI    |     END
S3        |  noisily  |     S4
S3        |  slowly   |     S4
S4        |    EOI    |     END

Q2.5 (12 points)

Translate this table into a programming language of your choice. Then write a function that takes a list of words (ending "EOI") and runs the list through the state machine. If the state machine cannot find a transition for a word when in a given state, return false. If the state machine runs out of words and the state is not "END" then return false. Otherwise return true.

Then write some unit tests that exercise your function, making sure that it recognizes valid sentences and rejects invalid ones.

The answer should appear in one or more source files in the same directory as this file. If I need to do anything more that type a single command to run your code, include a script or makefile that will do the job.

A2.5

script.py

Q2.6 (3 points)

How many valid sentences are there in this language?

A2.6

60

Q2.7 (1 point for the level, 2 for the sentence)

Which is the simplest Chomsky grammar level for this language? In one sentence, explain why.

A2.7

Type-3 Chomsky Grammar is the simplest grammar level for this language. This
grammar is representable with a finite state machine, as are type-3 grammars,
so this grammar is a type-3, the simplest type.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.