程序代写代做代考 Ruby Project

Ruby Project

401-Programming Languages

1 Ruby Installation

• Windows: Go to http://rubyinstaller.org/, and follow the instructions. For a quick and easy in-
stall, click the button at the top of the page, and choose desired version under

• OS X: preinstalled

• Linux/Unix

� Install from package manager

� Go to www.ruby-lang.org/en/downloads/, and follow the instructions.

• CIS: installed on vulcans

2 Assignment

Consider the following BNF for the Sim Programming Language (SimPL).

http://rubyinstaller.org/
www.ruby-lang.org/en/downloads/

::=
::= ‘;’

| ‘;’
::= identi�er ‘:=’

| ‘if’ ‘then’ ‘end’
| ‘if’ ‘then’ ‘else’ ‘end’
| ‘for’ identi�er ‘from’ ‘to’ ‘do’ ‘end’
| ‘for’ identi�er ‘from’ ‘to’ ‘by’ ‘do’ ‘end’

::= ‘+’
| ‘-‘
|

::= ‘*’
| ‘/’
|

::= integer
| identi�er
| ‘(‘ ‘)’

::= ‘and’
|

::= ‘not’
|

::= ‘true’
| ‘false’
|

::= ‘<='
| ‘<'
| ‘=’

• Write a lexer for the program language de�nition. The lexer should have a getTokenKind() function
that, when called, returns the symbol for the next sequential token (this function will be utilized in
the next section). A symbol is a numeric value indicating the kind of token that was parsed. (e.g.,
one value for each keyword, operator, one value for all identi�ers, one value for all integers, and one
value for EOF.) In addition, the lexer should have a function getTokenText that returns the textual
representation of the token. nextToken consumes the current token. The Lexer also needs to tack an
`EOF’ (end of �le) token to the end of the list of tokens (this will also be used later).

� separating by whitespace alone will not su�ce as there may be instances such as �x:=5�, which
consists of three tokens, but will only be counted as one.

� A SimPL identi�er consists of letters (only alphabetic characters), digits, and underscores (_)
with the restrictions that it must begin with a letter. SimPL comments are indicated by being
preceded by (//) and terminated by the end of the line.

• Create a recursive-descent syntax analyzer for the above language. Use the getTokenKind() and
getTokenText() methods from the previous section to retrieve the tokens at each step. If the `EOF’
token is reach with no issue, the program should report that the �le is syntactically correct. Otherwise,
if an error is found, an appropriate error message should be displayed.

Assignment report: Turn in the source code of your project together with an assignment report. The
assignment report should contain: (1) names of two team members including a short statement of work, (2)
a description of your design, (3) what was di�cult, (4) what did you like about Ruby, (5) what did you
dislike about Ruby.

2

The following code should be a syntacticly correct SimPL program.

prev := 0 ;
c u r r := 1 ;

f o r i t e r from 0 to N−1 do // i t e r a t i v e f i b o n a c c i
tmp := prev + cu r r ;
p r ev := cu r r ;
c u r r := tmp ;

end

;

The following is not a valid SimPL program. It contains a syntax error in every line.

prev := 0
c u r r := −1;
/ not a comment
f o r i t e r from 1 by 2 do

tmp := prev (+ cu r r ) ;
p r ev = cu r r ;
c u r r := ;

end

3

Ruby Installation
Assignment