 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:\antlr2.7.1\pascal> java classpath C:\antlr2.7.1;
C:\antlr2.7.1\pascal Main showtree Test_neg_number.pas
Parsing...
C:\antlr2.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.
________________________________________________________  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: antlrinterest@yahoogroups.com
> Subject: [antlrinterest] 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:\antlr2.7.1\pascal> java classpath C:\antlr2.7.1;
> C:\antlr2.7.1\pascal Main showtree Test_neg_number.pas
> Parsing...
> C:\antlr2.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/
>
>
>  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 antlrinterest@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: antlrinterest@y...
> > Subject: [antlrinterest] 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:\antlr2.7.1\pascal> java classpath C:\antlr2.7.1;
> > C:\antlr2.7.1\pascal Main showtree Test_neg_number.pas
> > Parsing...
> > C:\antlr2.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/
> >
> >
> >  Monty Zukowski wrote:
>
This would work for the number... and give you a huge headache since now
> 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
>
31 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