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

Thierry USO thierry.uso at wanadoo.fr
Tue Sep 29 07:54:06 PDT 2009


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.





More information about the antlr-interest mailing list