Question: Create a tokenizer in Ruby. Your tokenizer will take two command line arguments: The first will be a grammar specification and the second will be

Create a tokenizer in Ruby. Your tokenizer will take two command line arguments: The first will be a grammar specification and the second will be a file to tokenize. Tokenize the file and print the tokens (symbol, lexeme, line) to the screen. If the file cannot be tokenized, print an error message identifying the line with the error.

grammar specification file:

NUM -> \d+ ADDOP -> [-+] MULOP -> [*/] LP -> \( RP -> \) EQ -> = ID -> [A-Z]\w* comment -> \{[^}]*\}

S -> ID EQ expr expr -> expr ADDOP term | term term -> term MULOP factor | factor factor -> ID | NUM | LP expr RP

eample file 1 to tokenize:

4+2 { this is a comment } + 6

eample file 2 to tokenize:

1

+

2 *

3

Code so far:

# get grammar specification file puts "what is the file of grammar specification" grammar = gets.chomp file = File.open(grammar, "r") lines = Array.new File.open(grammar).each { |line| lines << line } contents = file.read puts contents #=> Lorem ipsum etc.  grammarContents = file.read puts grammarContents #get second file to tokenize puts "what is the tokenize file" tokenize = gets.chomp file = File.open(tokenize, "r") contents = file.read puts contents #=> Lorem ipsum etc.  tokenizeContents = file.read puts tokenizeContents 

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!