RUBY GRAMMAR

P

puellula

Hi!
Can you help me? I have to write a Ruby Editor + Parser with Java
Language.
I use ANTLR Parser Generator.
The grammar (a little part of Ruby grammar) I use is my hands made. I
had use ANTLR and I had write a main in Java Project. Now the problem
is: if I write a correct ruby code or not correct ruby code the result
is the same. Therefore I had think the problem is on the grammar.
Please, can you validate my Grammar. For me it's correct, but I think
the problem is this for my project.
This is the file.g how I had write my Grammar for ANTLR.

class P extends Parser;
options {
k = 2; // pone a 2 il lookahead di token
exportVocab=Ruby; // chiama il suo vocabolario "Ruby"
defaultErrorHandler = false; // non genera gestori di errori di
parser
buildAST=true; // il parser costruisce un AST (Abstract Syntax
Tree)
}

// l'intera classe
program : (CLASS IDENTIFIER body_class)*;

// il corpo della classe
body_class : (DEF (IDENTIFIER)* body)*;

// il corpo
body : (statement)+;

// istruzioni che si possono trovare nel corpo del programma
statement : (IF | WHILE) condition
| LOOP_DO
| YIELD LPAREN IDENTIFIER RPAREN
| RETURN ret
| END
| IDENTIFIER ASSIGN (LPAREN)+ (IDENTIFIER | NUMBER | STRING)
(RPAREN)+
| IDENTIFIER ASSIGN (LPAREN)+ IDENTIFIER (RPAREN)+
| IDENTIFIER ASSIGN (LPAREN)+ instruction (RPAREN)+;

// condizioni dei costrutti
condition : (LPAREN)+ IDENTIFIER booleano IDENTIFIER (RPAREN)+
| (LPAREN)+ IDENTIFIER booleano NUMBER (RPAREN)+
| (LPAREN)+ NUMBER booleano IDENTIFIER (RPAREN)+
| (LPAREN)+ NUMBER booleano NUMBER (RPAREN)+
| (LPAREN)+ instruction (RPAREN)+
| IDENTIFIER booleano (LPAREN)+ instruction (RPAREN)+;

instruction : NUMBER operator NUMBER
| IDENTIFIER operator NUMBER
| NUMBER operator IDENTIFIER;

// operatori che si possono trovare nelle condizioni
booleano : LT
| LE
| GE
| GT
| EGUAL
| MOD
| AND
| OR
| LPAREN
| RPAREN
| DIV;

// ciò che può essere scritto dopo il "return"
ret : (LPAREN)+ IDENTIFIER (RPAREN)+
| (LPAREN)+ NUMBER (RPAREN)+
| (LPAREN)+ TRUE (RPAREN)+
| (LPAREN)+ FALSE (RPAREN)+;

operator : DIV
| MUL
| PLUS
| SUB
| MOD;


//------------------------------------------------------------------------------
// LEXER
//------------------------------------------------------------------------------
class RubyLexer extends Lexer;

options {
charVocabulary = '\0'..'\377';
exportVocab=Ruby; // chiama il suo vocabolario "Ruby"
testLiterals = false; // don't automatically test for literals
k = 4; // four characters of lookahead
caseSensitive = true;
caseSensitiveLiterals = false;
filter = true;
}

tokens {
CLASS = "class";
DEF = "def";
IF = "if";
RETURN = "return";
END = "end";
LOOP_DO = "loop do";
YIELD = "yield";
DO = "do";
FALSE = "false";
TRUE = "true";
}

// Operatori
LT : '<';
LE : "<=";
GE : ">=";
GT : '>';
EGUAL : "==";
DIV : '/';
MUL : '*';
ASSIGN : '=';
LPAREN : '(';
RPAREN : ')';
PLUS : '+';
POINT : '.';
AT : '@';
OR : '|'; // questo simbolo non ha solo la funzionalità
dell'OR!
AND : '&';
SUB : '-';
MOD : '%';

NUMBER : ('0'..'9')+;

// Identificatori
IDENTIFIER : ('a'..'z'|'A'..'Z')+ (NUMBER)?;

// Stringhe
STRING : '"' (('a'..'z'|'A'..'Z')+)* '"'
| '\'' (('a'..'z'|'A'..'Z')+)* '\'';

// regola per il ritorno a capo
NEWLINE : ( "\r\n" // DOS
| '\r' // MAC
| '\n' // Unix
);




If you can help me for me it's very important.
Thank you very much!
bye,
puellula
 
E

Eric Mahurin

------=_Part_31706_30342465.1131550512345
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hi!
Can you help me? I have to write a Ruby Editor + Parser with Java
Language.
I use ANTLR Parser Generator.
The grammar (a little part of Ruby grammar) I use is my hands made. I
had use ANTLR and I had write a main in Java Project. Now the problem
is: if I write a correct ruby code or not correct ruby code the result
is the same. Therefore I had think the problem is on the grammar.
Please, can you validate my Grammar. For me it's correct, but I think
the problem is this for my project.
This is the file.g how I had write my Grammar for ANTLR.

class P extends Parser;
options {
k =3D 2; // pone a 2 il lookahead di token
exportVocab=3DRuby; // chiama il suo vocabolario "Ruby"
defaultErrorHandler =3D false; // non genera gestori di errori di
parser
buildAST=3Dtrue; // il parser costruisce un AST (Abstract Syntax
Tree)
}

// l'intera classe
program : (CLASS IDENTIFIER body_class)*;

// il corpo della classe
body_class : (DEF (IDENTIFIER)* body)*;

// il corpo
body : (statement)+;

// istruzioni che si possono trovare nel corpo del programma
statement : (IF | WHILE) condition
| LOOP_DO
| YIELD LPAREN IDENTIFIER RPAREN
| RETURN ret
| END
| IDENTIFIER ASSIGN (LPAREN)+ (IDENTIFIER | NUMBER | STRING)
(RPAREN)+
| IDENTIFIER ASSIGN (LPAREN)+ IDENTIFIER (RPAREN)+
| IDENTIFIER ASSIGN (LPAREN)+ instruction (RPAREN)+;

// condizioni dei costrutti
condition : (LPAREN)+ IDENTIFIER booleano IDENTIFIER (RPAREN)+
| (LPAREN)+ IDENTIFIER booleano NUMBER (RPAREN)+
| (LPAREN)+ NUMBER booleano IDENTIFIER (RPAREN)+
| (LPAREN)+ NUMBER booleano NUMBER (RPAREN)+
| (LPAREN)+ instruction (RPAREN)+
| IDENTIFIER booleano (LPAREN)+ instruction (RPAREN)+;

instruction : NUMBER operator NUMBER
| IDENTIFIER operator NUMBER
| NUMBER operator IDENTIFIER;

// operatori che si possono trovare nelle condizioni
booleano : LT
| LE
| GE
| GT
| EGUAL
| MOD
| AND
| OR
| LPAREN
| RPAREN
| DIV;

// ci=F2 che pu=F2 essere scritto dopo il "return"
ret : (LPAREN)+ IDENTIFIER (RPAREN)+
| (LPAREN)+ NUMBER (RPAREN)+
| (LPAREN)+ TRUE (RPAREN)+
| (LPAREN)+ FALSE (RPAREN)+;

operator : DIV
| MUL
| PLUS
| SUB
| MOD;



//-----------------------------------------------------------------------= -------
// LEXER

//-----------------------------------------------------------------------= -------
class RubyLexer extends Lexer;

options {
charVocabulary =3D '\0'..'\377';
exportVocab=3DRuby; // chiama il suo vocabolario "Ruby"
testLiterals =3D false; // don't automatically test for literals
k =3D 4; // four characters of lookahead
caseSensitive =3D true;
caseSensitiveLiterals =3D false;
filter =3D true;
}

tokens {
CLASS =3D "class";
DEF =3D "def";
IF =3D "if";
RETURN =3D "return";
END =3D "end";
LOOP_DO =3D "loop do";
YIELD =3D "yield";
DO =3D "do";
FALSE =3D "false";
TRUE =3D "true";
}

// Operatori
LT : '<';
LE : "<=3D";
GE : ">=3D";
GT : '>';
EGUAL : "=3D=3D";
DIV : '/';
MUL : '*';
ASSIGN : '=3D';
LPAREN : '(';
RPAREN : ')';
PLUS : '+';
POINT : '.';
AT : '@';
OR : '|'; // questo simbolo non ha solo la funzionalit=E0
dell'OR!
AND : '&';
SUB : '-';
MOD : '%';

NUMBER : ('0'..'9')+;

// Identificatori
IDENTIFIER : ('a'..'z'|'A'..'Z')+ (NUMBER)?;

// Stringhe
STRING : '"' (('a'..'z'|'A'..'Z')+)* '"'
| '\'' (('a'..'z'|'A'..'Z')+)* '\'';

// regola per il ritorno a capo
NEWLINE : ( "\r\n" // DOS
| '\r' // MAC
| '\n' // Unix
);




If you can help me for me it's very important.
Thank you very much!
bye,
puellula
Why are you posting this to ruby-talk? Wouldn't antlr-interest be better?
And I think you need to do a little more debugging and narrow your question=
 
H

Hugh Sasse

---559023410-641078858-1131550976=:13445
Content-Type: MULTIPART/MIXED; BOUNDARY="-559023410-641078858-1131550976=:13445"

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

---559023410-641078858-1131550976=:13445
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi!
Can you help me? I have to write a Ruby Editor + Parser with Java
Language.
I use ANTLR Parser Generator.
The grammar (a little part of Ruby grammar) I use is my hands made. I
had use ANTLR and I had write a main in Java Project. Now the problem
is: if I write a correct ruby code or not correct ruby code the result
is the same. Therefore I had think the problem is on the grammar.
Please, can you validate my Grammar. For me it's correct, but I think
the problem is this for my project.
This is the file.g how I had write my Grammar for ANTLR.
=20

I don't know what language you are using, so guessing from the latin
based languages I know I think the comments mean something like
(with my additional remarks as //HGS):
class P extends Parser;
options {
=09k =3D 2; // pone a 2 il lookahead di token

// Can look ahead 2 tokens
=09exportVocab=3DRuby; // chiama il suo vocabolario "Ruby"

// Now it uses the Ruby
// vocabulary
=09defaultErrorHandler =3D false; // non genera gestori di errori di
parser
// don't do anything with
// grammatical errors=20
//HGS I think part of the problem is turning this off, but
//HGS still.....
=09buildAST=3Dtrue; // il parser costruisce un AST (Abstract Syntax
Tree)
// The parser constructs an AST
}
=20
// l'intera classe // Internal? class
program : (CLASS IDENTIFIER body_class)*;
=20
// il corpo della classe // body class
body_class : (DEF (IDENTIFIER)* body)*;
=20
// il corpo // the body
body : (statement)+;
=20
// istruzioni che si possono trovare nel corpo del programma
// instructins here find the body of the program
statement : (IF | WHILE) condition
=09 | LOOP_DO
=09 | YIELD LPAREN IDENTIFIER RPAREN
//HGS you don't need the parens in ruby...
=09 | RETURN ret //HGS ret would be optional.
=09 | END //HGS (
=09 | IDENTIFIER ASSIGN (LPAREN)+ (IDENTIFIER | NUMBER | STRING)
(RPAREN)+
=09 | IDENTIFIER ASSIGN (LPAREN)+ IDENTIFIER (RPAREN)+
=09 | IDENTIFIER ASSIGN (LPAREN)+ instruction (RPAREN)+;
//HGS ) look like three tokens to be read before they
//HGS differ : how does the parser choose the correct
//HGS 'branch'?
=20
// condizioni dei costrutti
// conditional constructors
//HGS again 3 tokens identical (
condition : (LPAREN)+ IDENTIFIER booleano IDENTIFIER (RPAREN)+
| (LPAREN)+ IDENTIFIER booleano NUMBER (RPAREN)+ //HGS ) and here (
| (LPAREN)+ NUMBER booleano IDENTIFIER (RPAREN)+
| (LPAREN)+ NUMBER booleano NUMBER (RPAREN)+ //HGS )
=09 | (LPAREN)+ instruction (RPAREN)+
=09 | IDENTIFIER booleano (LPAREN)+ instruction (RPAREN)+;
=20
instruction : NUMBER operator NUMBER
=09 | IDENTIFIER operator NUMBER
=09 | NUMBER operator IDENTIFIER;
=20
// operatori che si possono trovare nelle condizioni
// operators which find said:
booleano : LT
| LE
| GE
=09 | GT
=09 | EGUAL
=09 | MOD
=09 | AND
=09 | OR
=09 | LPAREN
=09 | RPAREN
=09 | DIV;
=20
// ci=F2 che pu=F2 essere scritto dopo il "return"
// These are the ways to write "return"
ret : (LPAREN)+ IDENTIFIER (RPAREN)+
| (LPAREN)+ NUMBER (RPAREN)+
| (LPAREN)+ TRUE (RPAREN)+
| (LPAREN)+ FALSE (RPAREN)+;
//HGS and in ruby you can just have a return with no params
=20
operator : DIV
| MUL
=09 | PLUS
=09 | SUB
=09 | MOD;
=20
=20
//-----------------------------------------------------------------------= -------
// LEXER
//-----------------------------------------------------------------------=
-------
[...]

The rest looks fairly normal.
But then, I don't use ANTLR
=20
=20
If you can help me for me it's very important.
Thank you very much!
bye,
puellula
=20 Hugh
=20
=20

---559023410-641078858-1131550976=:13445--
---559023410-641078858-1131550976=:13445--
 
R

Rob .

Can you help me? I have to write a Ruby Editor + Parser
with Java

I'm curious why do you *have* to write a Ruby Editor + Parser with Java?

If you wish to parse Ruby code in Java, I recommend using the JRuby
parser (http://jruby.sourceforge.net/).

If you want to write a Ruby Editor in Java, there are existing editor
efforts underway that would welcome enthusiastic developers helping
out. I'm writing the jEdit Ruby Editor Plugin
(http://jedit.org/ruby/). There are also Eclipse based plugins
underway, like Ruby Development Tools and RadRails.

If you want to use JRuby, here is some example parsing code:

DefaultRubyParser parser =3D new DefaultRubyParser();
parser.init(new RubyParserConfiguration());
LexerSource source =3D LexerSource.getSource(file, content);

RubyParserResult result =3D parser.parse(source);
Node node =3D result.getAST();

NodeVisitor visitor =3D new RubyNodeVisitor();

if (node !=3D null) {
node.accept(visitor);
}

List members =3D visitor.getMembers();

You implement your own version of the JRuby NodeVisitor interface,
which looks something like this:

class RubyNodeVisitor extends AbstractVisitor { ...
protected void visitNode(Node node) { ...
public void visitBlockNode(BlockNode node) { ...
public void visitNewlineNode(NewlineNode node) { ...
public void visitModuleNode(ModuleNode node) { ...
public void visitClassNode(ClassNode node) { ...
public void visitDefnNode(DefnNode node) { ...
public void visitDefsNode(DefsNode node) { ...
public void visitScopeNode(ScopeNode node) { ...
// etc
}

cheers,
Rob
 
P

puellula

Eric,
I'm posting this to ruby talk because I think only persons knows Ruby
can help me with Ruby Grammar. I haven't problem with ANTLR, but with
Ruby Grammar.

Thanks you
puellula
 
P

puellula

Hugh,
I'm Italian and I had write comment ( // ) in Italian, pardon me.

Thank you,
puellula
 
P

puellula

Rob,
I have to write this for a University Examination. The examination is
for Java Language.

I know jRuby, and now I will see better this, thank you!

Oh, thanks for jEdit, now I will see this, it's very interesting for
me! :)
Thank you for your help! Now I will see these and I will try!

If I have problem I can find you here and I will call you!
Thank you very much.

puellula
 
H

Hugh Sasse

Hugh,
I'm Italian and I had write comment ( // ) in Italian, pardon me.

:), yes, that's fine, just wanted to be sure I had the gist of it
about right.
 
E

Eric Mahurin

------=_Part_33183_13540997.1131557580800
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Eric,
I'm posting this to ruby talk because I think only persons knows Ruby
can help me with Ruby Grammar. I haven't problem with ANTLR, but with
Ruby Grammar.

Thanks you
puellula
The first thing you need to do is pick the subset of ruby you want to parse=
 
N

Nikolai Weibull

I have to write this for a University Examination. The examination is
for Java Language.

This is what I suspected. Shouldn=E2=80=99t you be solving this by yours=
elf?
Or perhaps ask your teacher, course assitants, or fellow students for
help? See http://www.faqs.org/faqs/usenet/primer/part1/. If you must
have answers to questions you should be figuring out for yourself but
don=E2=80=99t want to and you could consider _paying_ someone to do your =
work
for you, then there=E2=80=99s always http://answers.google.com/answers/.
I know jRuby, and now I will see better this, thank you!
=20
Oh, thanks for jEdit, now I will see this, it's very interesting for
me! :)
Thank you for your help! Now I will see these and I will try!
=20
If I have problem I can find you here and I will call you!

If you must, could you at least limit it to jRuby? JEdit isn=E2=80=99t
ruby-talk material (instead, see
http://www.jedit.org/index.php?page=3Dfeedback). Furthermore, JRuby has
its own mailing lists, see http://jruby.sourceforge.net/contact.shtml.
And finally, for questions pertaining to ANTLR, you might want to browse
the mailing list archives at
http://www.antlr.org:8080/mailman/listinfo/antlr-interest and perhaps
even subscribe to that list.

nikolai

--=20
Nikolai Weibull: now available free of charge at http://bitwi.se/!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}
 
P

Phil Tomson

This is what I suspected. Shouldn=E2=80=99t you be solving this by yours=
elf?
Or perhaps ask your teacher, course assitants, or fellow students for
help? See http://www.faqs.org/faqs/usenet/primer/part1/. If you must
have answers to questions you should be figuring out for yourself but
don=E2=80=99t want to and you could consider _paying_ someone to do your =
work
for you, then there=E2=80=99s always http://answers.google.com/answers/.

Well, this is a pretty tough exam question, so while in general I would
agree with you Nikolai, in this case I'd be willing to cut some slack.
And who knows, with an exam question like this maybe we can end up with a new
parser for Ruby. I hope the professor publishes the best result ;-)


Phil
 
S

Stefan Kaes

Phil said:
Well, this is a pretty tough exam question, so while in general I would
agree with you Nikolai, in this case I'd be willing to cut some slack.
And who knows, with an exam question like this maybe we can end up with a new
parser for Ruby. I hope the professor publishes the best result ;-)


Phil
Turning to the internet to get your exam problems solved by others is
completely unethical. If you can't do it on your own, you deserve to
flunk. Nothing else is appropriate here.
 
P

puellula

Eric,
for this there aren't problem. I think the problem is in the grammar I
had write.

Thanks
puellula
 
P

puellula

Nikolai,
you haven't to help me if you don't want.
I post here one of my problems, ok? If there are someone would help me
he is wellcome, therefore don't worry.
You don't know my teacher and my situation!

Don't worry for me, please.

thank you
puellula
 
P

puellula

Nikolai,
you haven't to help me if you don't want.
I post here one of my problems, ok? If there are someone would help me
he is wellcome, therefore don't worry.
You don't know my teacher and my situation!

Don't worry for me, please.

thank you
puellula
 
P

puellula

Nikolai,
you haven't to help me if you don't want.
I post here one of my problems, ok? If there are someone would help me
he is wellcome, therefore don't worry.
You don't know my teacher and my situation!

Don't worry for me, please.

thank you
puellula
 
B

Benedikt Heinen

Turning to the internet to get your exam problems solved by others is
completely unethical. If you can't do it on your own, you deserve to flunk.
Nothing else is appropriate here.

I am not quite sure how to put this - turning to the Internet for help is
not in itself unethical, not even for a uni course. Some courses might
well be hard enough in themselves to warrant researching / asking for
help.

It will become unethical, though, the moment you turn in your result
without attributing any help you got.

I think one of the signs of a good engineer would be to also go out and
ask others for help on something, rather than inventing the wheel for the
gazillionth time.

Whether the ruby grammar here is an example complex enough to warrant
asking for help, I can't quite say - I haven't seen the course the
original poster is taking, nor do I really know where the parser fits into
the task at hand.

I sure as heck did not *really* enjoy my javacc exercises - though, I
pulled through them without resorting to too much help... ;-)




Benedikt

ALLIANCE, n. In international politics, the union of two thieves who
have their hands so deeply inserted in each other's pockets that
they cannot separately plunder a third.
(Ambrose Bierce, The Devil's Dictionary)
 
P

puellula

Please,
I don't want obligate nobody to help me.
I thinked that google group could help me, but I mistook!
I know that it's my examination. I don't want to find someone write me
a parser, don't worry.
I haven't to pay nobody to create my parser, I'm able to create this.
I thinked here there are someone can help me to understand where I
mistake.

thank you to persons had write in this post for help me!
bye
puellula
 
G

gabriele renzi

Benedikt Heinen ha scritto:

I sure as heck did not *really* enjoy my javacc exercises - though, I
pulled through them without resorting to too much help... ;-)

I enjoyed my bison exercies, but I actually needed help from the bison
help mailing list in some things :)
 
N

Nikolai Weibull

Well, this is a pretty tough exam question, so while in general I
would agree with you Nikolai, in this case I'd be willing to cut some
slack. =20

Writing a grammar for a small subset of an existing language isn=E2=80=99=
t what
I would call a =E2=80=9Cpretty tough exam question=E2=80=9D. It=E2=80=99=
s what I would call the
first step in a three-step laboration in creating a 1)
grammar, lexer, and parser, 2) interpreter, 3) compiler/translator.
I=E2=80=99ve assisted on an introductory course in programming languages =
that
has this setup, and while the results haven=E2=80=99t always been great o=
n the
final written examination, 85-90% of the students have been able to hand
in all three laborations and gotten a passing grade on them.

Coming up with an idea for a language and a grammar isn=E2=80=99t easy, b=
ut
that=E2=80=99s precisely why these things are on the syllabus. =E2=80=9C=
We don=E2=80=99t do it
because it=E2=80=99s easy, we do it because it=E2=80=99s hard.=E2=80=9D

Finally, people should certainly be able to ask questions and certainly
on things relating to their homework. However, asking questions like
=E2=80=9Ccan you take my partial solution and rewrite/finish it so that i=
t
works=E2=80=9D isn=E2=80=99t what I would call a valid question. An hone=
st question
like =E2=80=9CI have this homework exercise that requires me to write a g=
rammar
for a small subset of Ruby and I was wondering if anyone has a
suggestion on how to parse parameter lists. I have this partial
solution that seems to be able to handle the simple cases but am having
trouble with the more esoteric ones. Here=E2=80=99s what I=E2=80=99ve go=
t and a
beginning on the rest. Anyone have any suggestions? I=E2=80=99ve tried =
the
following things, but it fails for this and this, and I don=E2=80=99t qui=
te
understand why.=E2=80=9D I=E2=80=99d consider answering such a question,=
provided I had
something to contribute.
And who knows, with an exam question like this maybe we can end up
with a new parser for Ruby. I hope the professor publishes the best
result ;-)

I=E2=80=99m not going to hold my breath for this one. Ruby=E2=80=99s gra=
mmar is very
complex and it=E2=80=99ll take a lot more skill than one acquires during =
a
semester writing grammars/lexers/parsers to write a parser for it. I
know I=E2=80=99d have a hard time writing one.

nikolai

--=20
Nikolai Weibull: now available free of charge at http://bitwi.se/!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,470
Messages
2,571,807
Members
48,797
Latest member
PeterSimpson
Top