Question: Write the program in Ruby: Consider the following BNF for the Sim Programming Language (SimPL). ::= ::= ';' | ';' ::= identi er ':=' |

Write the program in Ruby:

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

::= ::= ';'

| ';' ::= 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.

The following code should be a syntacticly correct SimPL program.

prev := 0; curr := 1;

for iter from 0 to N1 do // iterative fibonacci tmp := prev + curr; prev := curr ;

curr := tmp; end

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

prev := 0 curr := 1;

/ not a comment for iter from 1 by 2 do

tmp := prev (+ curr); prev = curr ; curr := ;

end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!