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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started