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

Token object creation

Expand Messages
  • Pedro Joao Assis
    Hi, I need to get the qualifierName (parser rule) text in the qualifierDeclaration parser rule. To do this, I have followed the Antlr manual for element
    Message 1 of 5 , Mar 7, 2001
    • 0 Attachment
      Hi,

      I need to get the qualifierName (parser rule) text in the
      qualifierDeclaration parser rule. To do this, I have followed the Antlr
      manual for element labeling, but it not working since it does not compile
      the antlr generated parser java file.

      To make the compilation possible I have included in the init section of
      the qualifierDeclaration the following: Token qlfName = new Token
      (); (which I think it is not required, since the antlr should create the
      appropriated code). But still it does not work, the contents of the
      qlfName.getText() is "<notext>"!

      Probably I'm missing some important issue here... :( Please send me some
      hints.

      qualifierDeclaration [boolean trace]
      { boolean skipQlf = false;
      }
      : "qualifier"
      ( "association"
      | "indication"
      | qlfName: qualifierName
      { skipQlf = cim2x.qlfLookup (qlfName.getText());}
      )
      { if (trace)
      System.out.println ("Qualifier Dcl found");
      ;
      ...
      qualifierName
      : ( IDENTIFIER
      | "schema"
      | "class"
      | "property"
      | "reference"
      | "method"
      | "parameter"
      | "any"
      | "source"
      | "sourcetype"
      | "nonlocal"
      | "nonlocaltype"
      )
      ;

      Best regards

      --
      Pedro
    • Pedro Joao Assis
      Hi, This appears to work if the labeling is on lexer rules used in the parser, like this Parser ... qualifierDeclaration { boolean skipQlf = false; String
      Message 2 of 5 , Mar 8, 2001
      • 0 Attachment
        Hi,

        This appears to work if the labeling is on lexer rules used in the parser,
        like this

        Parser
        ...
        qualifierDeclaration
        { boolean skipQlf = false;
        String qlfName = null; }
        : "qualifier"
        ( "association"
        | "indication"
        | (qlfName = qualifierName
        { skipQlf = cim2x.qlfLookup (qlfName);
        cim2x.mofOut.println ( qlfName + " " +
        skipQlf); }
        )
        )
        qualifierType
        scope
        (defaultFlavor)?
        SEMI
        { if (cim2x.trace)
        cim2x.mofOut.println
        ("qualifierDeclaration"); }
        ;
        ...
        qualifierName returns [String qlfID]
        { qlfID = null; }
        : ( v:IDENTIFIER { qlfID = v.getText (); }
        | "schema" { qlfID = new String ("Schema"); }
        | "class" { qlfID = new String ("Class"); }
        | "property" { qlfID = new String ("Property"); }
        | "reference" { qlfID = new String ("Reference"); }
        | "method" { qlfID = new String ("Method"); }
        | "parameter" { qlfID = new String ("Parameter"); }
        | "any" { qlfID = new String ("Any"); }
        | "source" { qlfID = new String ("Source"); }
        | "sourcetype" { qlfID = new String ("Sourcetype"); }
        | "nonlocal" { qlfID = new String ("Nonlocal"); }
        | "nonlocaltype" { qlfID = new String ("Nonlocaltype"); }
        )
        { if (cim2x.trace)
        cim2x.mofOut.print ("qualifierName / "); }
        ;
        ...
        Lexer
        ...
        IDENTIFIER
        options {
        testLiterals = true;
        }
        : ('a'..'z'|'_') ('a'..'z'|'_'|'0'..'9')*
        { // cim2x.mofOut.print ("ident: " + $getText + " / ");
        }
        ;

        So, it is true that only lexer rules, and yes this are the ones that grab
        the tokens..., can be labeled? like this,
        v:IDENTIFIER { qlfID = v.getText (); }

        and it is not possible to make the following?

        qualifierName
        : v:( IDENTIFIER
        | "schema"
        | "class"
        | "property"
        | "reference"
        | "method"
        | "parameter"
        | "any"
        | "source"
        | "sourcetype"
        | "nonlocal"
        | "nonlocaltype"
        ){ System.out.println (v.getText()); }
        ;

        Best regards

        --
        Pedro

        On Wed, 7 Mar 2001, Pedro Joao Assis wrote:

        > Hi,
        >
        > I need to get the qualifierName (parser rule) text in the
        > qualifierDeclaration parser rule. To do this, I have followed the Antlr
        > manual for element labeling, but it not working since it does not compile
        > the antlr generated parser java file.
        >
        > To make the compilation possible I have included in the init section of
        > the qualifierDeclaration the following: Token qlfName = new Token
        > (); (which I think it is not required, since the antlr should create the
        > appropriated code). But still it does not work, the contents of the
        > qlfName.getText() is "<notext>"!
        >
        > Probably I'm missing some important issue here... :( Please send me some
        > hints.
        >
        > qualifierDeclaration [boolean trace]
        > { boolean skipQlf = false;
        > }
        > : "qualifier"
        > ( "association"
        > | "indication"
        > | qlfName: qualifierName
        > { skipQlf = cim2x.qlfLookup (qlfName.getText());}
        > )
        > { if (trace)
        > System.out.println ("Qualifier Dcl found");
        > ;
        > ...
        > qualifierName
        > : ( IDENTIFIER
        > | "schema"
        > | "class"
        > | "property"
        > | "reference"
        > | "method"
        > | "parameter"
        > | "any"
        > | "source"
        > | "sourcetype"
        > | "nonlocal"
        > | "nonlocaltype"
        > )
        > ;
      • mzukowski@bco.com
        ... The following does not work in ANTLR, because you are labeling a subrule. Do you have AST building turned on or off? If you could post a small, complete
        Message 3 of 5 , Mar 8, 2001
        • 0 Attachment
          RE: [antlr-interest] Token object creation

          > So, it is true that only lexer rules, and yes this are the
          > ones that grab
          > the tokens..., can be labeled? like this,
          >               v:IDENTIFIER { qlfID = v.getText (); }
          >
          > and it is not possible to make the following?

          The following does not work in ANTLR, because you are labeling a subrule.  Do you have AST building turned on or off?  If you could post a small, complete grammar which demonstrates your problem I could try and figure out what is going wrong.

           
          > qualifierName
          >       :       v:( IDENTIFIER
          >               | "schema"
          >               | "class"
          >               | "property"
          >               | "reference"
          >               | "method"
          >               | "parameter"
          >               | "any"
          >               | "source"
          >               | "sourcetype"
          >               | "nonlocal"
          >               | "nonlocaltype"
          >               ){ System.out.println (v.getText()); }
          >       ;
          >

          Monty

        • Pedro Joao Assis
          Hi Monty,On Thu, 8 Mar 2001 mzukowski@bco.com wrote: The following does not work in ANTLR, because you are labeling a subrule. Do you have AST
          Message 4 of 5 , Mar 8, 2001
          • 0 Attachment
            Hi Monty,

            On Thu, 8 Mar 2001 mzukowski@... wrote:

            >
            > The following does not work in ANTLR, because you are labeling a subrule.
            > Do you have AST building turned on or off? If you could post a small,
            > complete grammar which demonstrates your problem I could try and figure out
            > what is going wrong.
            >
            >

            Ok, here goes: grammar - pass2.g - and its main - cim2x.java. This version
            has very little actions, I was starting to do that, but all the rules -
            parser and lexer - are there. The grammar aims to develop a MOF (IDL
            based) to Java translation, enabling the generation of CIM 2.x (described
            by MOF) Java objects. I need to get the text match by the parser sub-rules
            in order to create and initialize the correct Java objects (this is based
            in a java package that it is not included in the attached files).

            Thanks

            Best regards
            --
            Pedro
          • mzukowski@bco.com
            ... I took a look at your grammar, and basically the answer is yes. If you are not building trees, and you are not, then you can only label token references
            Message 5 of 5 , Mar 9, 2001
            • 0 Attachment
              RE: [antlr-interest] Token object creation

              > -----Original Message-----
              > From: Pedro Joao Assis [mailto:passis@...]
              > Sent: Thursday, March 08, 2001 2:59 AM
              > To: antlr-interest@yahoogroups.com
              > Subject: Re: [antlr-interest] Token object creation
              >
              > So, it is true that only lexer rules, and yes this are the
              > ones that grab
              > the tokens..., can be labeled? like this,
              >               v:IDENTIFIER { qlfID = v.getText (); }

              I took a look at your grammar, and basically the answer is yes.  If you are not building trees, and you are not, then you can only label token references and not rule references.  The reason being that ANTLR isn't smart enough to know if a rule will always only match one token or not.  ANTLR should generate a warning to this effect, but it currently does not.

              Labels can be used with rules, however, if you are building trees.  If you happen to want to do that then you can label the rule and grab the text out of the node returned, since you know only one node will be returned for the rules which are like qualifierName.

              Monty

            Your message has been successfully submitted and would be delivered to recipients shortly.