7 Replies - 1166 Views - Last Post: 16 September 2010 - 04:04 PM Rate Topic: -----

#1 hardwired002   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 05-February 10

What Are The Ways To Implement This In Java?

Posted 10 September 2010 - 02:26 AM

id like to know the approaches on how to implement grammars in Java
are there specific data structures or classes used to implement this?

for example i have:

<var dec> ::= <var name> | <var name> , <var name> : <var type>;
<var type> ::= int | char | float
<var name> are just strings

which should be like

a, b : int;

thanks!
Is This A Good Question/Topic? 0
  • +

Replies To: What Are The Ways To Implement This In Java?

#2 Luckless   User is offline

  • </luck>
  • member icon

Reputation: 294
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: What Are The Ways To Implement This In Java?

Posted 10 September 2010 - 04:24 AM

I'm a little confused by your wording, but you can declare variable like this:

dataType varName
String a;
int b = 20,c; //initialized as 0
char d = 'd';
//and so on


Was This Post Helpful? 0
  • +
  • -

#3 Nasm   User is offline

  • New D.I.C Head

Reputation: 27
  • View blog
  • Posts: 47
  • Joined: 10-June 10

Re: What Are The Ways To Implement This In Java?

Posted 10 September 2010 - 04:54 AM

Well you simply have to process the text and search for operators. I'm not exactly sure what your trying to do here but you could take a look at parse trees which is used in compilers/interpretors for just this reason. Depending on how simply or not your system is two stacks where you push back the operators & operands and then match them could also work.

View PostLuckless, on 10 September 2010 - 03:24 AM, said:

int b = 20,c; //initialized as 0


This is true only for class members. Otherwise it's uninitialised.

This post has been edited by Nasm: 10 September 2010 - 04:56 AM

Was This Post Helpful? 0
  • +
  • -

#4 guido-granobles   User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: What Are The Ways To Implement This In Java?

Posted 10 September 2010 - 07:18 AM

I think @Nasm is right you should implement parse trees here is a good document which explain it.
Was This Post Helpful? 1
  • +
  • -

#5 hardwired002   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 05-February 10

Re: What Are The Ways To Implement This In Java?

Posted 16 September 2010 - 06:42 AM

<var declaration> ::= <identifier list> : <var type> ;

<identifier list> ::= <var name> | <var name> , <identifier list>

<var type> ::= integer | character | float

<var name> standard naming conventions



this is for my compiler, (hypothetical high-lvl PL only)

im thinking of state diagrams but i dont know how to implement those too, still reading about it,
are there some other ways? thanks
Was This Post Helpful? 0
  • +
  • -

#6 javadork   User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 135
  • Joined: 21-August 10

Re: What Are The Ways To Implement This In Java?

Posted 16 September 2010 - 07:07 AM

A state machine is one way to do it, and is likely to be the most fun and challenging route to take for working with BNF grammars. It's a ton of work, although you'll learn a lot.

But I think it's also worth while looking at software solutions like ANTLR, it would certainly be quicker to do it that way.

Depends on your requirements I guess.
Was This Post Helpful? 0
  • +
  • -

#7 guido-granobles   User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: What Are The Ways To Implement This In Java?

Posted 16 September 2010 - 10:30 AM

I think that you might want look at the The Java Parser Generator. It allows you write
grammar specification such as you wrote above and then it generates a Java program.
Was This Post Helpful? 1
  • +
  • -

#8 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2972
  • View blog
  • Posts: 11,223
  • Joined: 15-July 08

Re: What Are The Ways To Implement This In Java?

Posted 16 September 2010 - 04:04 PM

Duplicate topics merged. You had a lot of good advice in the first post and this one! Good luck!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1