Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning VB 2008 Databases

Authors: Vidya Vrat Agarwal, James Huddleston

1st Edition

1590599470, 978-1590599471

More Books

Students also viewed these Databases questions

Question

How does this compare with the Fog Index for your written message?

Answered: 1 week ago