Ruby grammar

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

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.
Asking about whether a complete grammar for a language you created (you
picked a certain subset of ruby and modified it from the looks of it) seems
a little silly. Although I feel like I'm doing your homework, here are a few
problems I see:

- what's the + after the LPAREN and RPAREN for? You don't want an arbitrary
and unbalanced number of them do you?

- you have a strange definition for an IDENTIFIER

- I haven't used antlr in a while, but I doubt "loop do" will work since it
has a space in it. I think keywords are determined when an identifier is
matched (looks up the identifier in a hash to see if it is a keyword
instead).

- in LL parsers, you typically need separate rules for each level of
precedence. I don't see that above.

···

On 11/9/05, puellula@gmail.com <puellula@gmail.com> wrote:

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

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.

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 {
  k = 2; // pone a 2 il lookahead di token

                                      // Can look ahead 2 tokens

  exportVocab=Ruby; // chiama il suo vocabolario "Ruby"

                                      // Now it uses the Ruby
                                      // vocabulary

  defaultErrorHandler = false; // non genera gestori di errori di
parser

                                      // don't do anything with
                                      // grammatical errors
        //HGS I think part of the problem is turning this off, but
        //HGS still.....

  buildAST=true; // il parser costruisce un AST (Abstract Syntax
Tree)

                          // The parser constructs an AST

}

// l'intera classe

  // Internal? class

program : (CLASS IDENTIFIER body_class)*;

// il corpo della classe

  // body class

body_class : (DEF (IDENTIFIER)* body)*;

// il corpo

  // the body

body : (statement)+;

// istruzioni che si possono trovare nel corpo del programma

  // instructins here find the body of the program

statement : (IF | WHILE) condition
    > LOOP_DO
    > YIELD LPAREN IDENTIFIER RPAREN

            //HGS you don't need the parens in ruby...

    > RETURN ret

            //HGS ret would be optional.

    > END

            //HGS (

    > IDENTIFIER ASSIGN (LPAREN)+ (IDENTIFIER | NUMBER | STRING)
(RPAREN)+
    > IDENTIFIER ASSIGN (LPAREN)+ IDENTIFIER (RPAREN)+
    > 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'?

// 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 )

    > (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

  // operators which find <something> conditioanls -- relational
  // operators

booleano : LT
         > LE
         > GE
   > GT
   > EGUAL
   > MOD
   > AND
   > OR
   > LPAREN
   > RPAREN
   > DIV;

// ciò che può 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

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

//------------------------------------------------------------------------------
// LEXER
//------------------------------------------------------------------------------

        [...]

The rest looks fairly normal.
But then, I don't use ANTLR

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

        Hugh

···

On Thu, 10 Nov 2005, puellula@gmail.com wrote:

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 = new DefaultRubyParser();
parser.init(new RubyParserConfiguration());
LexerSource source = LexerSource.getSource(file, content);

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

NodeVisitor visitor = new RubyNodeVisitor();

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

List members = 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

···

On 11/9/05, puellula@gmail.com <puellula@gmail.com> wrote:

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

Quoting Eric Mahurin <eric.mahurin@gmail.com>:

Why are you posting this to ruby-talk? Wouldn't antlr-interest be
better?

I don't know, it seems quite on-topic to me, especially after the
discussions we'd just had about the need to develop a non-YACC Ruby
grammar.

-mental

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

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

Thank you,
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! :slight_smile:
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

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.

Thank you,
puellula

        Hugh

···

On Thu, 10 Nov 2005, puellula@gmail.com wrote:

Quoting puellula@gmail.com:

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.

It's worth noting that there is no authoritative grammar for Ruby
except the YACC grammar in Ruby's source code.

-mental

The first thing you need to do is pick the subset of ruby you want to parse.
If you want to know the syntax of a particular part of ruby, I think
ruby-talk can help. But, please be specific instead of saying "I can't parse
ruby with the antlr grammar I made". Give a piece of code you are trying to
parse, how it is failing, applicable BNF rules for that code fragment, etc.

I suspect the main problem you are having is how to take an LR grammar (i.e.
yacc) to LL grammar (i.e. ANTLR). From the grammar you posted, it looks like
you may have some misunderstandings about how antlr works.

···

On 11/9/05, puellula@gmail.com <puellula@gmail.com> wrote:

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

puellula@gmail.com wrote:

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

This is what I suspected. Shouldn’t you be solving this by yourself?
Or perhaps ask your teacher, course assitants, or fellow students for
help? See Internet FAQ Archives - Online Education - faqs.org. If you must
have answers to questions you should be figuring out for yourself but
don’t want to and you could consider _paying_ someone to do your work
for you, then there’s always http://answers.google.com/answers/\.

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! :slight_smile:
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!

If you must, could you at least limit it to jRuby? JEdit isn’t
ruby-talk material (instead, see
jEdit - Programmer's Text Editor -). 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

···

--
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);}

Mental I know,
but I had to write Ruby Grammar from me :cry:
I don't know what I have to do, now!

thank you,
puellula

In article <20051109184727.GB9100@puritan.petwork>,

···

Nikolai Weibull <mailing-lists.ruby-talk@rawuncut.elitemail.org> wrote:

puellula@gmail.com wrote:

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 Internet FAQ Archives - Online Education - faqs.org. 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 :wink:

Phil

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

Thanks
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

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

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

Phil Tomson wrote:

In article <20051109184727.GB9100@puritan.petwork>,

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 Internet FAQ Archives - Online Education - faqs.org. 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 :wink:

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.

···

Nikolai Weibull <mailing-lists.ruby-talk@rawuncut.elitemail.org> wrote:

puellula@gmail.com wrote:

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