Loading ...
Sorry, an error occurred while loading the content.

Expand Messages
  • javadesigner
    I am trying to extend the introduction to antlr example by lexing hello world (note the space) ... class P extends Parser; startRule ...
    Message 1 of 2 , Apr 30, 2004
    • 0 Attachment
      I am trying to extend the introduction to antlr example
      by lexing 'hello world' (note the space)

      -------- from the antlr docs-----------
      class P extends Parser;
      startRule
      : n:NAME
      {System.out.println("Hi there, "+n.getText());}
      ;

      class L extends Lexer;
      // one-or-more letters followed by a newline
      NAME: ('a'..'z' | 'A'..'Z' | ' ')+ NEWLINE
      ;
      NEWLINE
      : '\r' '\n' // DOS
      | '\n' // UNIX
      ;
      -----------------------------------------

      The only change I made was added the ' ' after 'A' ..'Z'
      After compiling the above and running the test
      program (the test program is also from the antlr docs and
      is shown at the end for completeness), I get:

      ---------------------------------------------
      root@turing:~/parsing/test# java test
      foo
      Hi there, foo

      NOTE THE ERROR BELOW>>>>>

      root@turing:~/parsing/test# java test
      foo bar
      exception: line 1:4: unexpected char: ' '
      -------------------------------------------

      Can anyone explain what is going on and why
      I get an unexpected char message ? I even tried:

      NAME: ('a'..'z' | 'A'..'Z' | '9')+ NEWLINE

      and after recompiling:
      root@turing:~/parsing/test# java test
      foo9bar
      exception: line 1:4: unexpected char: '9'

      Help ! This shouldn't be that hard should it ?

      Best regards,

      --j

      ------------ test program -------------------
      import java.io.*;
      class test {
      public static void main(String[] args) {
      try {
      L lexer = new L(new DataInputStream(System.in));
      P parser = new P(lexer);
      parser.startRule();
      } catch(Exception e) {
      System.err.println("exception: "+e);
      }
      }
      }
    • Terence Parr
      Hi. Check out charVocabulary. Terence ... -- Professor Comp. Sci., University of San Francisco Creator, ANTLR Parser Generator, http://www.antlr.org
      Message 2 of 2 , May 1, 2004
      • 0 Attachment
        Hi. Check out charVocabulary.
        Terence
        On Apr 30, 2004, at 10:50 PM, javadesigner wrote:

        > I am trying to extend the introduction to antlr example
        > by lexing 'hello world' (note the space)
        >
        > -------- from the antlr docs-----------
        > class P extends Parser;
        > startRule
        > : n:NAME
        > {System.out.println("Hi there, "+n.getText());}
        > ;
        >
        > class L extends Lexer;
        > // one-or-more letters followed by a newline
        > NAME: ('a'..'z' | 'A'..'Z' | ' ')+ NEWLINE
        > ;
        > NEWLINE
        > : '\r' '\n' // DOS
        > | '\n' // UNIX
        > ;
        > -----------------------------------------
        >
        > The only change I made was added the ' ' after 'A' ..'Z'
        > After compiling the above and running the test
        > program (the test program is also from the antlr docs and
        > is shown at the end for completeness), I get:
        >
        > ---------------------------------------------
        > root@turing:~/parsing/test# java test
        > foo
        > Hi there, foo
        >
        > NOTE THE ERROR BELOW>>>>>
        >
        > root@turing:~/parsing/test# java test
        > foo bar
        > exception: line 1:4: unexpected char: ' '
        > -------------------------------------------
        >
        > Can anyone explain what is going on and why
        > I get an unexpected char message ? I even tried:
        >
        > NAME: ('a'..'z' | 'A'..'Z' | '9')+ NEWLINE
        >
        > and after recompiling:
        > root@turing:~/parsing/test# java test
        > foo9bar
        > exception: line 1:4: unexpected char: '9'
        >
        > Help ! This shouldn't be that hard should it ?
        >
        > Best regards,
        >
        > --j
        >
        > ------------ test program -------------------
        > import java.io.*;
        > class test {
        > public static void main(String[] args) {
        > try {
        > L lexer = new L(new DataInputStream(System.in));
        > P parser = new P(lexer);
        > parser.startRule();
        > } catch(Exception e) {
        > System.err.println("exception: "+e);
        > }
        > }
        > }
        >
        >
        >
        >
        >
        > Yahoo! Groups Links
        >
        >
        >
        >
        >
        >
        --
        Professor Comp. Sci., 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!
        Cofounder, http://www.peerscope.com pure link sharing
      Your message has been successfully submitted and would be delivered to recipients shortly.