[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Getting verbose output from the parser
One more not, probably the best documentation you could find is
looking how others implemented it. For example looking at the
interpreter for MiniBasic written by Etienne Gagnon, was very useful
to me.
By the way, I think it would be a good idea to have
http://sablecc.org/grammars.html page mention that by clicking on
Download link you may get much more than just a grammar specification
file, you may find full implementation for parsers, interpreters,
compilers, etc.
Greg
On 6/10/06, Greg Borota <depaulcti@xxxxxxxxx> wrote:
I am not an expert in the subject but anyway I think you would better
let the lexer "decide" what's a float literal (that's it, define it in
the Helpers/Tokens section).
You can find already tested ways of defining float_literals on SableCC
site, grammars section
http://sablecc.org/grammars.html
http://sablecc.org/grammars/ISOC.grammar.html
http://sablecc.org/grammars/j14.sablecc.html
Please not that many of those grammars have an interpreter/parser,etc.
associated that can be obtained by clicking on the Download link (did
not notice this at first):
http://sourceforge.net/project/showfiles.php?group_id=5704
etc.
Greg
Helpers
floating_constant =
decimal_floating_constant |
hex_floating_constant;
Tokens
On 6/10/06, Stephen Torri <storri@xxxxxxxxx> wrote:
> Is there a way to get more verbose output from the parser than just a
> ParserException? I need to develop a parser which can recognize the C++
> programming language. For starters I am working on the recognizing two
> basic types, floats and integers. The integers are being recognized
> correctly but the floats are stopping at the second character with an
> exception.
>
> Why will this grammar not recognize "2.52e+8" or "7E5"?
>
> Stephen
>
> --------------------
>
> Package CPP;
>
> Helpers
>
>
> blank = ' ';
> // char = [['a'..'z']+['A'..'Z']];
> digit = ['0'..'9'];
> eol = 10 | 13;
> hexidecimal_digit= ['a'..'f'] | ['A'..'F'] | ['0'..'9'];
> non_zero_digit = ['1'..'9'];
> octal_digit = ['0'..'7'];
> sign = '+' | '-';
>
> Tokens
>
> // ******************************
> // Characters
> // ******************************
> tk_blank = 9 | 10 | 13 | blank;
> tk_float_suffix = 'f' | 'F';
> tk_long_suffix = 'l' | 'L';
> tk_exponent = 'e' | 'E';
> tk_sign = sign;
>
> // Basic numbers
> tk_decimal_literal = non_zero_digit digit*;
> tk_digit = digit;
> tk_digit_sequence = digit+;
> tk_hexidecimal_literal = ( '0x' | '0X' ) hexidecimal_digit*;
>
> tk_octal_literal = '0' octal_digit*;
> tk_unsigned_suffix = 'u' | 'U';
>
> // Basic characters
> tk_left_paren = '(';
> tk_period = '.';
> tk_right_paren = ')';
>
> Ignored Tokens
>
> tk_blank;
>
> Productions
>
> // literal ::= integer-literal |
> // character-literal |
> // floating-literal |
> // string-literal |
> // boolean-literal |
> // null-literal // __null
>
> literal =
> {float} floating_literal |
> {int} integer_literal;
>
> //string_literal |
> //boolean_literal |
> //null_literal;
>
> // Floating point numbers
> floating_literal =
> {frac} fractional_constant
> exponent_part?
> float_suffix? |
> {seq} tk_digit+
> exponent_part
> float_suffix?;
>
> fractional_constant =
> {fraction} [num]:tk_digit_sequence? tk_period
> [exp]:tk_digit_sequence |
> {whole} tk_digit_sequence tk_period;
>
> float_suffix = {f_char} tk_float_suffix |
> {l_char} tk_long_suffix;
>
> exponent_part = tk_exponent tk_sign? tk_digit_sequence;
>
> // Integer numbers
> integer_literal =
> {dec} tk_decimal_literal integer_suffix? |
> {oct} tk_octal_literal integer_suffix? |
> {hex} tk_hexidecimal_literal integer_suffix?;
>
> integer_suffix =
> {ul} tk_unsigned_suffix tk_long_suffix? |
> {lu} tk_long_suffix tk_unsigned_suffix?;
>
>
>
>
> _______________________________________________
> SableCC-user mailing list
> SableCC-user@xxxxxxxxxxx
> http://sablecc.org/lists/control/listinfo/sablecc-user
>