create a tokenizer in ruby your tokenizer will take two command line arguments the f 4949882
Create a tokenizer in Ruby. Your tokenizer will take two command line arguments: The first willbe a grammar specification and the second will be a file totokenize. Tokenize the file and print the tokens (symbol, lexeme,line) to the screen. If the file cannot be tokenized, print anerror 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 fileputs
“what is the file of grammar specification”grammar = gets.chompfile = File.open(grammar,
“r”)lines = Array.newFile.open(grammar).each { |
line| lines
line }contents = file.readputs contents
#
=> Lorem ipsum etc.grammarContents = file.readputs grammarContents
#
get
second file to tokenizeputs
“what is the tokenize file”tokenize = gets.chompfile = File.open(tokenize,
“r”)contents = file.readputs contents
#
=> Lorem ipsum etc.tokenizeContents = file.readputs tokenizeContents . . .