[antlr-interest] Determining a finite number of characters and
getting tokens
Loring Craymer
lgcraymer at yahoo.com
Wed Jul 19 16:44:04 PDT 2006
Jayne--
1.) Look at the section entitled "Element Labels" in the documentation. If I understand your question correctly, you want to label a token and assign it to a variable from a parser rule.
2.) This is handled via a definition like
NUMBER
:
{ int count = 0; }
( DIGIT { count++} )+
{ if (count == 3)
$setType(NUM3)
else if (count == 4)
$setType(NUM4)
}
;
Also, DIGIT should be a protected rule (does not return a token, just adds characters to the character buffer).
Good luck!
--Loring
"Ochoa, Jayne" <Jayne.Ochoa at absg.com> wrote: Good afternoon! (Atleast from Dallas, TX. :) )
I am very new to ANTLR. Just heard about it a month ago and am still
studying the docs and trying to understand it. I am familiar with EBNF
and have been playing around with ANTLR. Still having trouble, however.
My development environment is Visual Studio 2005 and C#.
Two questions:
1) I need to be able to simply grab a token from the parser. I was
trying to do something very simple based on the cut and paste example,
but have not been able to just "grab" a token. How does one go about
doing that? Or does it depend on the grammar?
2) I created an incredibly simple grammar for a phone number just to get
my feet wet with ANTLR. Is there a way to determine a finite number of
digits? I'll paste my grammar file. I realize it's a bit ammateurish,
but I was simply playing around with this to understand how to create a
grammar file for ANTLR. I have pasted my grammar file below. Reading it,
it is obvious as to why I get a non-determinism error for NUM3 and NUM4.
This is why I have that question as to whether it is possible to dermine
a finite number of digits. For example, in my grammar below, NUM3 would
be defined as 3 digits.
-- Jayne
---- Begin Phonenumber.g file ----
// Parser
header
{
using System;
using System.Collections.Generic;
using System.Text;
}
options
{
language = "CSharp";
namespace = "PhoneNumber";
}
class PhoneNoParser extends Parser;
options
{
defaultErrorHandler = false;
k=2;
}
phonenumber
: NUM3 DOT NUM3 DOT NUM4
| areacode NUM3 DASH NUM4
;
areacode
: LPAREN NUM3 RPAREN
| NUM3 (SLASH | DASH)
;
// Lexer
class PhoneNoLexer extends Lexer;
options
{
k=2;
}
NUM3
: DIGIT DIGIT DIGIT
;
NUM4
: NUM3 DIGIT
;
DASH
: '-'
;
DOT
: '.'
;
SLASH
: '/'
;
LPAREN
: '('
;
RPAREN
: ')'
;
DIGIT
: '0'..'9'
;
WS
: (
' '
| '\t'
| ( '\n'
| '\r'
| "\r\n")
{newline();}
)
{ $setType(Token.SKIP);}
;
---- END Phonenumber.g file ----
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060719/303c5369/attachment-0001.html
More information about the antlr-interest
mailing list