posted 1 year ago
Hello, I would like to make a very simple lexical analyzer but I don't really know where to start.
Let's say I have only two arithmetic operations (addition and multiplication) and two integers. An addition or a multiplication is made with the two operands (the order does not matter since it's an addition or multiplication). For example as input "ADD 5 3", is expected 5+3.
My idea is the following :
first tokenize the expression (String type) with the split function (to make it simple for the moment, between each item a space " " is expected. I'll deal with two spaces and stuff later)
create two different array, one for the operation (ADD, MUL) and one for the integers
iterate over the tokenized expression and compare them with the two arrays looking for "matches".
and and...that's all for the moment, that's where I'm stuck.
I always provide some code but I think it was not relevant here.
How should I proceed ? Thanks for your insights.
Let's say I have only two arithmetic operations (addition and multiplication) and two integers. An addition or a multiplication is made with the two operands (the order does not matter since it's an addition or multiplication). For example as input "ADD 5 3", is expected 5+3.
My idea is the following :
first tokenize the expression (String type) with the split function (to make it simple for the moment, between each item a space " " is expected. I'll deal with two spaces and stuff later)
create two different array, one for the operation (ADD, MUL) and one for the integers
iterate over the tokenized expression and compare them with the two arrays looking for "matches".
and and...that's all for the moment, that's where I'm stuck.
I always provide some code but I think it was not relevant here.
How should I proceed ? Thanks for your insights.
posted 1 year ago
I think we need to know a little bit more about this new language. For instance, how would you deal with
ADD MUL 1 2 3
?
Some other notes: when splitting, you can deal with any kind and any number of whitespace with "\\s+". This says "one or more whitespace characters."
Making two arrays, one for the operators and one for the digits, is the right way to go, except that you will find stacks work better.
ADD MUL 1 2 3
?
Some other notes: when splitting, you can deal with any kind and any number of whitespace with "\\s+". This says "one or more whitespace characters."
Making two arrays, one for the operators and one for the digits, is the right way to go, except that you will find stacks work better.
All things are lawful, but not all things are profitable.
posted 1 year ago
I recommend that you take the time to learn how to use a lexer, such as JFlex. Grammars defined in lexer syntax are generally easier to maintain and extend than code you wrote manually. Plus, once you learn the basic lexer syntax, you'll find it much easier to write grammars for other file formats (a task that is more common than one might assume).
Danger, 10,000 volts, very electic .... tiny ad:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
