[antlr-interest] Determining a finite number of characters and getting tokens

Ochoa, Jayne Jayne.Ochoa at absg.com
Wed Jul 19 16:09:21 PDT 2006


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 ----


More information about the antlr-interest mailing list