[antlr-interest] Simple grammar help needed

Kamal Verma kverma at yahoo.com
Wed Aug 1 14:21:51 PDT 2007


thanks again; I have ordered the book I think that will help, meanwhile it arrives, I started working to first recognize following constructs,

1) Name1="Value1" 
2) Name2=/Value2/

Once this works I will weave them into the conditional expression. Using following grammar, but now It recognizes (1) but has MismatchToken in the (2). Attached is the grammar created. Now the question is,

1) Any better way to do this ?
2) In the grammar below can I do like regex:  SLASH NOT_SLASH SLASH ?? if yes, can you please point me out on that ?

----------
grammar ExParser;

regexCompare
    : propName '=' REG_EXP_CHARS 
    ;

fragment REG_EXP_CHARS
    :    '/'!   
        (    REG_EXP_CHAR
        )*   
        '/'!
        ;

propCompare
    : propName '=' STRING_LITERAL 
    ;

propName
    :    IDENTIFIER
    ;

fragment REG_EXP_CHAR
    : (IDENTIFIER_PART_CHARACTER|'^'|'+'|'*'|'.') 
    ;
    

IDENTIFIER
    :    IDENTIFIER_START_CHARACTER (IDENTIFIER_PART_CHARACTER)*
    ;

IDENTIFIER_START_CHARACTER
    :    ('a'..'z'|'A'..'Z'|'_'|'$') 
    ;

IDENTIFIER_PART_CHARACTER
    :    ('a'..'z'|'A'..'Z'|'_'|'0'..'9'|'$') 
    ;


STRING_LITERAL
    :    REGULAR_STRING_LITERAL
    |    VERBATIM_STRING_LITERAL
    ;
    
fragment REGULAR_STRING_LITERAL
    :    '\"'!   
        (    REGULAR_STRING_LITERAL_CHARACTER
        )*   
        '\"'!
    ;

fragment REGULAR_STRING_LITERAL_CHARACTER
    :    SINGLE_REGULAR_STRING_LITERAL_CHARACTER
    |    SIMPLE_ESCAPE_SEQUENCE
    ;
    
fragment SINGLE_REGULAR_STRING_LITERAL_CHARACTER
    :     ~( '\"' | '\\' | '\u000D' | '\u000A' | '\u2028' | '\u2029')
    ;

fragment VERBATIM_STRING_LITERAL
    :
    ~('\"' | '\\')
    ;

fragment SIMPLE_ESCAPE_SEQUENCE
    :    '\'' | '\\"' | '\\\\' | '\\0' | '\\a'  
    |    '\\b' | '\\f'  | '\\n'  | '\\r' | '\\t' | '\\v'
    ;
-----------

Any ideas ?

Thanks in advance,
_K



----- Original Message ----
From: "Diehl, Matthew J" <matthew.j.diehl at intel.com>
To: Kamal Verma <kverma at yahoo.com>
Sent: Wednesday, August 1, 2007 1:05:48 PM
Subject: RE: [antlr-interest] Simple grammar help needed



 
DIV {
MARGIN:0px;}



Kamal,

 

sorry, for the lack of time, I'll just write out real quick 
how I'd approach this...and you'll have to correct my code yourself 
:)

 

It looks like you can use any number of ANDs and ORs, 
so:

 

grammar test;

options {

  output=AST;

  ASTLabelType=CommonTree;

}

 

tokens {

  OR='OR';

  AND='AND';

  NOT='NOT';

}

 

input

  : expression* //(actually, I don't know how 
you're separating your individual lines of expressions, so you'd have to do 
something different here maybe)

  ;

 

expression

  : NOT? simpleExpression //not sure if this is legal, 
but this allows parens or no parens for: NOT (Name1=/regex/) or NOT 
Name1=/regex/

  ;

 

simpleExpression

  : assignment ( (OR | AND) 
simpleExpression)

  ;

 

assignment

  : '(' ID '=' REGEX ')'

  | ID '=' REGEX

  ;

 

And then you've just got to define the lexer, which with all of the sample grammars online, shouldn't be too hard

Best of luck!

Matt





  
  
  From: Kamal Verma [mailto:kverma at yahoo.com] 
  
Sent: Tuesday, July 31, 2007 7:17 PM
To: Diehl, Matthew 
  J
Subject: Re: [antlr-interest] Simple grammar help 
  needed



  

  
  thanks 
  Matt,
I was wondering if you an example on how to approach it? Anyone with 
  an starting grammer can be of help!
Thanks a tons in advance.
-K


  ----- 
  Original Message ----
From: "Diehl, Matthew J" 
  <matthew.j.diehl at intel.com>
To: Kamal Verma 
  <kverma at yahoo.com>
Sent: Tuesday, July 31, 2007 6:25:19 
  PM
Subject: RE: [antlr-interest] Simple grammar help needed


  DIV {
MARGIN:0px;}


  Kamal

   

  too simple as in: too simple due to the learning curve of 
  ANTLR?  -> maybe

  too simple as in: too simple to 
  do -> definitely not :)  I'm fully converted to using ANTLR on 
  things that are a little too complex to do with grep or perl 
  'easily'.

   

  If it was me, I would do it in ANTLR, especially since 
  I've used it for over a couple months, and am very comfortable with it, and 
  new projects use a lot of similar code (so a lot of cutting and pasting 
  between different projects makes it easy).  If I hadn't used ANTLR 
  before, maybe I wouldn't, and would just try to hand-write it in java or 
  something like that...

   

  Matt


  
    
    
    From: antlr-interest-bounces at antlr.org 
    [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Kamal 
    Verma
Sent: Tuesday, July 31, 2007 2:55 PM
To: 
    antlr-interest at antlr.org
Subject: [antlr-interest] Simple grammar 
    help needed



    

    
    I am new to the ANTLR, I have a requirement to parse following kind of 
    input,


(Name1=/regex/) AND (Name2='value')
(Name1=/regex/) 
    OR  (Name2='value')
NOT (Name1=/regex/) 
((Name1=/regex/) 
    OR  (Name2='value')) AND Name2='value'


Do you think it is 
    too simple to try to write a lexer and parser for this ?

Thanks in 
    the advance,
-K





    
    Fussy? Opinionated? Impossible to please? Perfect. Join Yahoo!'s user panel and lay it on us. 
  





  
  Be a better Globetrotter. Get 
  better travel answers from someone who knows.
Yahoo! Answers - Check it 
  out. 





       
____________________________________________________________________________________
Moody friends. Drama queens. Your life? Nope! - their life, your story. Play Sims Stories at Yahoo! Games.
http://sims.yahoo.com/  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070801/f945dc1b/attachment-0001.html 


More information about the antlr-interest mailing list