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

Need help with a negetive number

Expand Messages
  • danreo1952@yahoo.com
    Hello, I am having a problem. I want -3 to be parsed out as [-3, ] rather than [-, ] [3, ]. Am I stuck with what I have ? I have tried
    Message 1 of 4 , Jul 13, 2001
      Hello, I am having a problem.
      I want "-3" to be parsed out as [-3,<NUM_INT>]
      rather than [-,<MINUS>] [3,<NUM_INT>].
      Am I stuck with what I have ?

      I have tried changing the Lexer:
      from:
      | ( '1'..'9') ('0'..'9')* { _ttype = NUM_INT; }
      to:
      | ('+'|'-'|)? ( '1'..'9') ('0'..'9')* { _ttype = NUM_INT; }

      No help.

      I tried changing the Parser:

      constant : unsignedNumber
      | signedNumber
      | constantIdentifier
      | sign constantIdentifier
      | string
      | factorial
      ;

      signedNumber : signedInteger
      ;

      signedInteger : ( sign NUM_INT )
      ;

      No help.

      Please advise. I have looked at TinyBasic and CGRAM,
      no help and/or over my head.

      Thank You
      Bill B.
      ___________________________________________________

      Section 1. My Pascal code for testing
      Section 2. Output of:
      System.out.println(" 2. after dotreeAction:" +
      parser.getAST().toStringList() + ", the end.");

      -------------------------------------------------------
      Section 1.

      /* Testing negetive number */
      program test(test);
      var
      factting : factorial;
      begin
      factting := -3;
      end.
      --------------------------------------------------------
      Section 2.

      [Win95]C:\antlr-2.7.1\pascal> java -classpath C:\antlr-2.7.1;
      C:\antlr-2.7.1\pascal Main -showtree Test_neg_number.pas
      Parsing...
      C:\antlr-2.7.1\pascal\Test_neg_number.pas
      2. after dotreeAction: program [test,<IDENT>] [(,<LPAREN>]
      [test,<IDENT>] [),<RPAREN>] [;,<SEMI>] var [factting,<IDENT>]
      [:,<COLON>] factorial [;,<SEMI>] begin [factting,<IDENT>]
      [:=,<ASSIGN>] [-,<MINUS>] [3,<NUM_INT>] [;,<SEMI>] end
      [.,<DOT>], the end.
      ________________________________________________________
    • Monty Zukowski
      Antlr lexers combine all unprotected rules into one big rule, and that makes it impossible to have two different rules that start with the same prefix. This
      Message 2 of 4 , Jul 13, 2001
        Antlr lexers combine all unprotected rules into one big rule, and that makes it
        impossible to have two different rules that start with the same prefix. This is
        why you see the rule for PERIOD inside the rule for NUMBER in the java grammar,
        etc.

        So you need to remove your MINUS and PLUS rules and add something like this into
        your NUMBER rule:

        | ('+'|'-'|)? ( '1'..'9') ('0'..'9')* { _ttype = NUM_INT; }
        | '+' { _ttype = PLUS; }
        | '-' { _ttype = MINUS; }

        Monty

        > -----Original Message-----
        > From: danreo1952@... [mailto:danreo1952@...]
        > Sent: Friday, July 13, 2001 6:09 AM
        > To: antlr-interest@yahoogroups.com
        > Subject: [antlr-interest] Need help with a negetive number
        >
        >
        > Hello, I am having a problem.
        > I want "-3" to be parsed out as [-3,<NUM_INT>]
        > rather than [-,<MINUS>] [3,<NUM_INT>].
        > Am I stuck with what I have ?
        >
        > I have tried changing the Lexer:
        > from:
        > | ( '1'..'9') ('0'..'9')* { _ttype = NUM_INT; }
        > to:
        > | ('+'|'-'|)? ( '1'..'9') ('0'..'9')* { _ttype = NUM_INT; }
        >
        > No help.
        >
        > I tried changing the Parser:
        >
        > constant : unsignedNumber
        > | signedNumber
        > | constantIdentifier
        > | sign constantIdentifier
        > | string
        > | factorial
        > ;
        >
        > signedNumber : signedInteger
        > ;
        >
        > signedInteger : ( sign NUM_INT )
        > ;
        >
        > No help.
        >
        > Please advise. I have looked at TinyBasic and CGRAM,
        > no help and/or over my head.
        >
        > Thank You
        > Bill B.
        > ___________________________________________________
        >
        > Section 1. My Pascal code for testing
        > Section 2. Output of:
        > System.out.println(" 2. after dotreeAction:" +
        > parser.getAST().toStringList() + ", the end.");
        >
        > -------------------------------------------------------
        > Section 1.
        >
        > /* Testing negetive number */
        > program test(test);
        > var
        > factting : factorial;
        > begin
        > factting := -3;
        > end.
        > --------------------------------------------------------
        > Section 2.
        >
        > [Win95]C:\antlr-2.7.1\pascal> java -classpath C:\antlr-2.7.1;
        > C:\antlr-2.7.1\pascal Main -showtree Test_neg_number.pas
        > Parsing...
        > C:\antlr-2.7.1\pascal\Test_neg_number.pas
        > 2. after dotreeAction: program [test,<IDENT>] [(,<LPAREN>]
        > [test,<IDENT>] [),<RPAREN>] [;,<SEMI>] var [factting,<IDENT>]
        > [:,<COLON>] factorial [;,<SEMI>] begin [factting,<IDENT>]
        > [:=,<ASSIGN>] [-,<MINUS>] [3,<NUM_INT>] [;,<SEMI>] end
        > [.,<DOT>], the end.
        > ________________________________________________________
        >
        >
        >
        >
        > Your use of Yahoo! Groups is subject to
        > http://docs.yahoo.com/info/terms/
        >
        >
        >
      • danreo1952@yahoo.com
        Hello Monty: Now, ... Works just fine. Now -3 comes in fine. Thank You Very Much Bill B. ... that makes it ... prefix. This is ... java grammar, ... like this
        Message 3 of 4 , Jul 16, 2001
          Hello Monty:
          Now,
          | ('+'|'-'|)? ( '1'..'9') ('0'..'9')* { _ttype = NUM_INT; }
          | '+' { _ttype = PLUS; }
          | '-' { _ttype = MINUS; }
          Works just fine. Now -3 comes in fine.

          Thank You Very Much
          Bill B.

          --- In antlr-interest@y..., "Monty Zukowski" <mzukowski@b...> wrote:
          > Antlr lexers combine all unprotected rules into one big rule, and
          that makes it
          > impossible to have two different rules that start with the same
          prefix. This is
          > why you see the rule for PERIOD inside the rule for NUMBER in the
          java grammar,
          > etc.
          >
          > So you need to remove your MINUS and PLUS rules and add something
          like this into
          > your NUMBER rule:
          >
          > | ('+'|'-'|)? ( '1'..'9') ('0'..'9')* { _ttype = NUM_INT; }
          > | '+' { _ttype = PLUS; }
          > | '-' { _ttype = MINUS; }
          >
          > Monty
          >
          > > -----Original Message-----
          > > From: danreo1952@y... [mailto:danreo1952@y...]
          > > Sent: Friday, July 13, 2001 6:09 AM
          > > To: antlr-interest@y...
          > > Subject: [antlr-interest] Need help with a negetive number
          > >
          > >
          > > Hello, I am having a problem.
          > > I want "-3" to be parsed out as [-3,<NUM_INT>]
          > > rather than [-,<MINUS>] [3,<NUM_INT>].
          > > Am I stuck with what I have ?
          > >
          > > I have tried changing the Lexer:
          > > from:
          > > | ( '1'..'9') ('0'..'9')* { _ttype = NUM_INT;
          }
          > > to:
          > > | ('+'|'-'|)? ( '1'..'9') ('0'..'9')* { _ttype = NUM_INT;
          }
          > >
          > > No help.
          > >
          > > I tried changing the Parser:
          > >
          > > constant : unsignedNumber
          > > | signedNumber
          > > | constantIdentifier
          > > | sign constantIdentifier
          > > | string
          > > | factorial
          > > ;
          > >
          > > signedNumber : signedInteger
          > > ;
          > >
          > > signedInteger : ( sign NUM_INT )
          > > ;
          > >
          > > No help.
          > >
          > > Please advise. I have looked at TinyBasic and CGRAM,
          > > no help and/or over my head.
          > >
          > > Thank You
          > > Bill B.
          > > ___________________________________________________
          > >
          > > Section 1. My Pascal code for testing
          > > Section 2. Output of:
          > > System.out.println(" 2. after dotreeAction:" +
          > > parser.getAST().toStringList() + ", the end.");
          > >
          > > -------------------------------------------------------
          > > Section 1.
          > >
          > > /* Testing negetive number */
          > > program test(test);
          > > var
          > > factting : factorial;
          > > begin
          > > factting := -3;
          > > end.
          > > --------------------------------------------------------
          > > Section 2.
          > >
          > > [Win95]C:\antlr-2.7.1\pascal> java -classpath C:\antlr-2.7.1;
          > > C:\antlr-2.7.1\pascal Main -showtree Test_neg_number.pas
          > > Parsing...
          > > C:\antlr-2.7.1\pascal\Test_neg_number.pas
          > > 2. after dotreeAction: program [test,<IDENT>] [(,<LPAREN>]
          > > [test,<IDENT>] [),<RPAREN>] [;,<SEMI>] var [factting,<IDENT>]
          > > [:,<COLON>] factorial [;,<SEMI>] begin [factting,<IDENT>]
          > > [:=,<ASSIGN>] [-,<MINUS>] [3,<NUM_INT>] [;,<SEMI>] end
          > > [.,<DOT>], the end.
          > > ________________________________________________________
          > >
          > >
          > >
          > >
          > > Your use of Yahoo! Groups is subject to
          > > http://docs.yahoo.com/info/terms/
          > >
          > >
          > >
        • Sinan
          ... This would work for the number... and give you a huge headache since now 3-1 will be parsed as [3, ] --- [-1, ] Instead of
          Message 4 of 4 , Jul 18, 2001
            Monty Zukowski wrote:
            >
            > Antlr lexers combine all unprotected rules into one big rule, and that makes it
            > impossible to have two different rules that start with the same prefix. This is
            > why you see the rule for PERIOD inside the rule for NUMBER in the java grammar,
            > etc.
            >
            > So you need to remove your MINUS and PLUS rules and add something like this into
            > your NUMBER rule:
            >
            > | ('+'|'-'|)? ( '1'..'9') ('0'..'9')* { _ttype = NUM_INT; }
            > | '+' { _ttype = PLUS; }
            > | '-' { _ttype = MINUS; }
            >
            > Monty
            >

            This would work for the number... and give you a huge headache since now

            3-1 will be parsed as [3,<NUM_INT>] --- [-1,<NUM_INT>]

            Instead of
            ['-',<SUBTRACT]
            |
            |
            [3,<NUM_INT>] --- [-1,<NUM_INT>]

            Best to leave it as is and append a '-' to the beginning of token text
            in the parser.


            Rule of all thumbs: Never lex a signed integer.

            Sinan
          Your message has been successfully submitted and would be delivered to recipients shortly.