[antlr-interest] Re: Tutorial Seems to Have Improper Java

kozchris csnyder at alumni.ncsu.edu
Thu Nov 18 15:55:02 PST 2004



looked at the example.
Create 2 files Main.java and LP.g as defined below.
Run LP.g file through antlr and get ExprLexer.java and ExprParser.java.
Make sure your class path has the antlr.jar and directory with your 3
java files.
Build your 3 java files "javac *.java" and run "java Main".

Chris

//first file:
//Main.java
import antlr.*;
public class Main {
  public static void main(String[] args) throws Exception {
    ExprLexer lexer = new ExprLexer(System.in);
    ExprParser parser = new ExprParser(lexer);
    parser.expr();
  }
} 
//////////////////////
//end first file
//////////////////////

//second file
//LP.g
// Lexer
class ExprLexer extends Lexer;

options {
    k=2; // needed for newline junk
    charVocabulary='\u0000'..'\u007F'; // allow ascii
}

LPAREN: '(' ;
RPAREN: ')' ;
PLUS  : '+' ;
MINUS : '-' ;
STAR  : '*' ;
INT   : ('0'..'9')+ ;
WS    : ( ' '
        | '\r' '\n'
        | '\n'
        | '\t'
        )
        {$setType(Token.SKIP);}
      ;


// Parser
class ExprParser extends Parser;

expr:   mexpr ((PLUS|MINUS) mexpr)*
    ;

mexpr
    :   atom (STAR atom)*
    ;

atom:   INT
    |   LPAREN expr RPAREN
    ;
//////////////////////
//end second file
//////////////////////



--- In antlr-interest at yahoogroups.com, Matthew Tedder <teddemc at y...>
wrote:
> 
> Separating them into separate files doesn't seem to
> make any difference... The following line gives an
> error... it's expecting a '{'
> 
> class ExprLexer extends Lexer;
> 
> I am learning Java, but I know C++.. I presume Lexer
> is defined in antlr.* but I do not understand what is
> supposed to be happening here...
> 
> Matthew
> 
> 
> 
> --- Terence Parr <parrt at c...> wrote:
> 
> > 
> > 
> > On Nov 18, 2004, at 1:45 PM, Matthew Tedder wrote:
> > > While I code in C/C++ and flex/bison, I am new to
> > Java
> > > and trying to learn antlr, too.  The following
> > link is
> > > to code I found in an antlr tutorial:
> > >
> > > http://pastebin.com/121239
> > >
> > > But it doesn't work.. javac says it's expecting
> > '{'
> > > after:
> > >
> > > class ExrLexer extends lever;
> > >
> > > Can anyone tell me what is wrong here?  Once I get
> > > just one simple thing working, I think I could be
> > > happy playing with it for a while..
> > 
> > Somebody has the main and .g file looking like one. 
> > Make this a 
> > separate file:
> > 
> > import antlr.*;
> > public class Main {
> >   public static void main(String[] args) throws
> > Exception {
> >     ExprLexer lexer = new ExprLexer(System.in);
> >     ExprParser parser = new ExprParser(lexer);
> >     parser.expr();
> >   }
> > }
> > 
> > Ter
> > --
> > CS Professor & Grad Director, University of San
> > Francisco
> > Creator, ANTLR Parser Generator,
> > http://www.antlr.org
> > Cofounder, http://www.jguru.com
> > Cofounder, http://www.knowspam.net enjoy email
> > again!
> > 
> > 
> > 
> > 
> > 
> >  
> > Yahoo! Groups Links
> > 
> > 
> >     antlr-interest-unsubscribe at yahoogroups.com
> > 
> >  
> > 
> > 
> > 
> >





 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 





More information about the antlr-interest mailing list