Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic lexical analyzer for arithmetics in Java  RSS feed

 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Moores
Saloon Keeper
Posts: 3889
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice. When I have time, I'll definitely learn about lexers, parsers and so on. For the moment I'm sticking with something very basic, but still challenging for me
I'll try to do something and post something here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!