Loading ...
Sorry, an error occurred while loading the content.
 

BNF to ANTLR grammar conversion

Expand Messages
  • Alex J. Rodriguez
    How do I go from BNF to ANTLR grammar programmatically? I am looking for a way to generate an XPath parser/evaluator from the following BNF (the syntax is
    Message 1 of 2 , Dec 10, 2004
      How do I go from BNF to ANTLR grammar programmatically? I am looking
      for a way to generate an XPath parser/evaluator from the following BNF
      (the syntax is pretty small, and is really a stripped down XPath 1.0
      syntax defined by the SDO spec):

      path ::= '/'? (step '/')* step
      step ::= '@'? property
      | property '[' index_from_1 ']'
      | property '.' index_from_0
      | reference '[' attribute '=' value ']'
      | ".."
      property ::= NCName ;; may be simple or complex type
      attribute ::= NCName ;; must be simple type
      reference :: NCName ;; must be DataObject type
      index_from_0 ::= Digits
      index_from_1 ::= NotZero (Digits)?
      value ::= Literal
      | Number
      | Boolean
      Literal ::= '"' [^"]* '"'
      | "'" [^']* "'"
      Number ::= Digits ('.' Digits?)?
      | '.' Digits
      Boolean ::= true
      | false
      NotZero ::= [1-9]
      Digits ::= [0-9]+

      ;; leading '/' begins at the root
      ;; ".." is the containing DataObject
      ;; Only the last step have an attribute as the property


      - Alex Rodriguez
    • Alexey Demakov
      Hi Alex, I don t know solution. Parser generated from BNF can t use custom actions as ANTLR lets. But you can convert BNF to ANTLR manually and use it for
      Message 2 of 2 , Dec 14, 2004
        Hi Alex,

        I don't know solution. Parser generated from BNF can't use
        custom actions as ANTLR lets. But you can convert BNF to ANTLR
        manually and use it for parser generation.
        And you can generate html doc with BNF used by ANTLR using -html
        command line switch. I convert syntax of your grammar to ANTLR notation,
        but some conflicts should be resolved.

        Regards,
        Alexey

        -----
        Alexey Demakov
        TreeDL: Tree Description Language: http://treedl.sourceforge.net
        RedVerst Group: http://www.unitesk.com


        class myParser extends Parser;

        path : (SLASH)? (step SLASH)* step;
        step : (AT)? property
        | property LB index_from_1 RB
        | property DOT index_from_0
        | reference LB attribute EQUAL value RB
        | DOTDOT;
        property : NCName; // may be simple or complex type
        attribute : NCName; // must be simple type
        reference : NCName; // must be DataObject type
        index_from_0 : Digits;
        index_from_1 : NotZero (Digits)?;
        value : Literal
        | Number
        | Boolean;

        class myLexer extends Lexer;

        SLASH : '/';

        AT : '@';

        LB : '[';

        RB : ']';

        DOT : '.';

        DOTDOT : "..";

        EQUAL : '=';

        Literal : '"' (~'"')* '"'
        | "'" (~'\'')* "'";
        Number : Digits ('.' (Digits)?)?
        | '.' Digits;
        Boolean : "true"
        | "false";
        NotZero : '1'..'9';
        Digits : ('0'..'9')+;

        // leading '/' begins at the root
        // ".." is the containing DataObject
        // Only the last step have an attribute as the property

        ----- Original Message -----
        From: "Alex J. Rodriguez" <alexjrodriguez@...>
        To: <antlr-interest@yahoogroups.com>
        Sent: Saturday, December 11, 2004 3:12 AM
        Subject: [antlr-interest] BNF to ANTLR grammar conversion


        >
        >
        >
        > How do I go from BNF to ANTLR grammar programmatically? I am looking
        > for a way to generate an XPath parser/evaluator from the following BNF
        > (the syntax is pretty small, and is really a stripped down XPath 1.0
        > syntax defined by the SDO spec):
        >
        > path ::= '/'? (step '/')* step
        > step ::= '@'? property
        > | property '[' index_from_1 ']'
        > | property '.' index_from_0
        > | reference '[' attribute '=' value ']'
        > | ".."
        > property ::= NCName ;; may be simple or complex type
        > attribute ::= NCName ;; must be simple type
        > reference :: NCName ;; must be DataObject type
        > index_from_0 ::= Digits
        > index_from_1 ::= NotZero (Digits)?
        > value ::= Literal
        > | Number
        > | Boolean
        > Literal ::= '"' [^"]* '"'
        > | "'" [^']* "'"
        > Number ::= Digits ('.' Digits?)?
        > | '.' Digits
        > Boolean ::= true
        > | false
        > NotZero ::= [1-9]
        > Digits ::= [0-9]+
        >
        > ;; leading '/' begins at the root
        > ;; ".." is the containing DataObject
        > ;; Only the last step have an attribute as the property
        >
        >
        > - Alex Rodriguez
        >
        >
        >
        >
        >
        >
        >
        >
        >
        >
        >
        > Yahoo! Groups Links
        >
        >
        >
        >
        >
        >
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.