From: Hugh Sasse Date: 2005-11-10T00:44:30+09:00 Subject: Re: RUBY GRAMMAR ---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 On Thu, 10 Nov 2005, 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. >=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 conditioanls -- relational // operators > 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--