[antlr-interest] how to get the value of an identifier

Gordon Tyler Gordon.Tyler at quest.com
Tue Sep 29 08:30:16 PDT 2009


Try changing "ConstDef" to "constDef". You're declaring it as a lexer token (by using an uppercase first letter), when really it should be a parser rule.

-----Original Message-----
From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Thierry USO
Sent: September 29, 2009 10:54 AM
To: antlr-interest
Subject: Re: [antlr-interest] how to get the value of an identifier

A complete example which shows my problem.

--- ADL.g ---

grammar ADL; // Rally Application Development Language

constant_definition
: ConstDef
;

ConstDef
: {System.out.println("");} CONST 
(
(' '|'\t')* Identifier {System.out.print($Identifier.text + " ");} 
(' '|'\t')* EQUAL 
(' '|'\t')* CharString {System.out.print($CharString.text + " ");}
(' '|'\t')* SEMICOLON
)+
;

CharString
: '"' (LETTER)+ '"'
;

CONST
: 'CONST' {System.out.print("CONST ");}
;

LETTER
: ('a'..'z'|'A'..'Z')
;

DIGIT
: ('0'..'9')
;

SEMICOLON
: ';' {System.out.println(";");}
;

EQUAL
: '=' {System.out.print("= ");}
;

Identifier
: LETTER (LETTER|'_'|DIGIT)+
;

----

--- ADL.java ---

import java.io.*;
import org.antlr.runtime.*;
import org.antlr.runtime.debug.DebugEventSocketProxy;

public class ADL {

    public static void main(String args[]) throws Exception {
        ADLLexer lex = new ADLLexer(new ANTLRFileStream("/home/thierry/antlr/output/tid.txt"));
        CommonTokenStream tokens = new CommonTokenStream(lex);
        
        ADLParser g = new ADLParser(tokens);
        try {
          g.constant_definition();
        } catch (RecognitionException e)  {
            e.printStackTrace();
        }       
    }
}

----

--- tid.txt ---

CONST  str1 = "ok" ;
	str2="ko";
       str3 = "end";

----

$ java ADL

CONST str1 = "ok" ;
= ;
= ;

Only the first values (Identifier, CharString) are printed and not the following ;-(
I used antlrworks-1.2.3.

Thierry.




List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address


More information about the antlr-interest mailing list