Hi, I'm a newbie about parsing and antlr, I started to write a grammar
in order to parse an imput like this:

; comment
* comment
HEADER A=5 *MM /"NAME"


I cannot define a lexer rule for '*' beginning  comment lines because
'*MM' and '/"NAME"' have specific meaning inside 'HEADER' line, if I
define a lexer rule, the generated token is the whole string: '
*MM /"NAME"'

I've got this parser and lexer rules:
----------------------------------------------------------
grammar nuova1;

options {
        output=AST;
        ASTLabelType=CommonTree;
        backtrack=false;
        }

tokens {

        SLASH='/';
        ASTERISK='*';

        SEMICOLON=';';
        }

prog    :       (LINECOMMENT| starComment)* header  EOF;

header  :       HEADER headerItem+ LINECOMMENT? ;

headerItem
        :
                A'='? (REAL | NUMBER | digit)
        |       ASTERISK ID
        |       SLASH QSTRING
        ;

//why without this rule are single digit numbers not recognized at
all?
digit   :       '0'
        |       '1'
        |       '2'
        |       '3'
        |       '4'
        |       '5'
        |       '6'
        |       '7'
        |       '8'
        |       '9'
        ;

starComment     :       '*' ~('\r' | '\n')* '\r'? '\n' ;

NEWLINE :( '\r'? '\n')   | EOF;

REAL    :  NUMBER '.' NUMBER    ;

NUMBER  :  ('0'..'9') ('0'..'9')* ;

//generic ID
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ;
//quoted string
QSTRING :       '"' STRING '"';

//generic string
STRING  :('a'..'z'|'A'..'Z'|'_'|'%'|'?')
('a'..'z'|'A'..'Z'|'0'..'9'|'_'|'%'|'?')* ;

WS  :   (' '|'\t')+ { skip();} ;

LINECOMMENT     :       ';' ~('\r' | '\n')* '\r'? '\n';
----------------------------------------------------------

All imput lines are terminating with CR LF codes

when I try to parse an input like this:

; comment
HEADER A=5 *MM /"NAME"

it works fine

But when I add a "star" comment line

; comment
* comment
HEADER A=5 *MM /"NAME"

it fails without recognizing newline and it tries to include also
header line into starcomment.

What's wrong on my approach?

I use antlrworks1.2.2 on antlr 3.1.1
Thank's in advance.

Giovanni Frattini


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to